]> git.proxmox.com Git - mirror_edk2.git/blob - RedfishPkg/PrivateInclude/Library/RedfishLib.h
b2488abbd4b513fbfdc80bd4fc133a832af101dd
[mirror_edk2.git] / RedfishPkg / PrivateInclude / Library / RedfishLib.h
1 /** @file
2 This library provides a set of utility APIs that allow to create/read/update/delete
3 (CRUD) Redfish resources and provide basic query abilities by using [URI/RedPath]
4 (https://github.com/DMTF/libredfish).
5
6 The query language is based on XPath (https://www.w3.org/TR/1999/REC-xpath-19991116/).
7 This library and query language essentially treat the entire Redfish Service like it
8 was a single JSON document. In other words whenever it encounters an odata.id in JSON
9 document, it will retrieve the new JSON document (if needed). We name the path as
10 RedPath:
11
12 Expression Description
13
14 nodename Selects the JSON entity with the name "nodename".
15 If the value of nodename is an object with "@odata.id",
16 it will continue get the value from "@odata.id".
17
18 / Selects from the root node
19
20 [index] Selects the index number JSON entity from an array or
21 object. If the JSON entity is one collection (has
22 Members & Members@odata.count), means to get the index
23 member in "Members". Index number >=1, 1 means to return
24 the first instance.
25
26 [XXX] Operation on JSON entity.
27 If the JSON entity is one collection (has Members &
28 Members@odata.count), means to get all elements in
29 "Members". If the JSON entity is one array, means to
30 get all elements in array. Others will match the nodename
31 directly (e.g. JSON_OBJECT, JSON_STRING, JSON_TRUE,
32 JSON_FALSE, JSON_INTEGER).
33
34 [nodename] Selects all the elements from an JSON entity that
35 contain a property named "nodename"
36
37 [name=value] Selects all the elements from an JSON entity where
38 the property "name" is equal to "value"
39
40 [name~value] Selects all the elements from an JSON entity where
41 the string property "name" is equal to "value" using
42 case insensitive comparison.
43
44 [name<value] Selects all the elements from an JSON entity where
45 the property "name" is less than "value"
46
47 [name<=value] Selects all the elements from an JSON entity where
48 the property "name" is less than or equal to "value"
49
50 [name>value] Selects all the elements from an JSON entity where
51 the property "name" is greater than "value"
52
53 [name>=value] Selects all the elements from an JSON entity where
54 the property "name" is greater than or equal to "value"
55
56 Some examples:
57
58 /v1/Chassis[1] - Will return the first Chassis instance.
59 /v1/Chassis[SKU=1234] - Will return all Chassis instances with a SKU field equal to 1234.
60 /v1/Systems[Storage] - Will return all the System instances that have Storage field populated.
61
62 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
63 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
64
65 SPDX-License-Identifier: BSD-2-Clause-Patent
66
67 **/
68
69 #ifndef REDFISH_LIB_H_
70 #define REDFISH_LIB_H_
71
72 #include <Library/JsonLib.h>
73
74 #include <Protocol/Http.h>
75 #include <Protocol/EdkIIRedfishConfigHandler.h>
76
77 #define ODATA_TYPE_NAME_MAX_SIZE 128
78 #define ODATA_TYPE_MAX_SIZE 128
79
80 ///
81 /// Library class public defines
82 ///
83 typedef VOID *REDFISH_SERVICE;
84 typedef VOID *REDFISH_PAYLOAD;
85
86 ///
87 /// Library class public structures/unions
88 ///
89 typedef struct {
90 EFI_HTTP_STATUS_CODE *StatusCode;
91 UINTN HeaderCount;
92 EFI_HTTP_HEADER *Headers;
93 REDFISH_PAYLOAD Payload;
94 } REDFISH_RESPONSE;
95
96 ///
97 /// Odata type-name mapping structure.
98 ///
99 typedef struct {
100 CONST CHAR8 OdataTypeName[ODATA_TYPE_NAME_MAX_SIZE];
101 CONST CHAR8 OdataType[ODATA_TYPE_MAX_SIZE];
102 } REDFISH_ODATA_TYPE_MAPPING;
103
104 /**
105 This function uses REST EX protocol provided in RedfishConfigServiceInfo.
106 The service enumerator will also handle the authentication flow automatically
107 if HTTP basic auth or Redfish session login is configured to use.
108
109 Callers are responsible for freeing the returned service by RedfishCleanupService().
110
111 @param[in] RedfishConfigServiceInfo Redfish service information the EFI Redfish
112 feature driver communicates with.
113
114 @return New created Redfish Service, or NULL if error happens.
115
116 **/
117 REDFISH_SERVICE
118 EFIAPI
119 RedfishCreateService (
120 IN REDFISH_CONFIG_SERVICE_INFORMATION *RedfishConfigServiceInfo
121 );
122
123 /**
124 Free the Service and all its related resources.
125
126 @param[in] RedfishService The Service to access the Redfish resources.
127
128 **/
129 VOID
130 EFIAPI
131 RedfishCleanupService (
132 IN REDFISH_SERVICE RedfishService
133 );
134
135 /**
136 Create REDFISH_PAYLOAD instance in local with JSON represented resource value and
137 the Redfish Service.
138
139 The returned REDFISH_PAYLOAD can be used to create or update Redfish resource in
140 server side.
141
142 Callers are responsible for freeing the returned payload by RedfishCleanupPayload().
143
144 @param[in] Value JSON Value of the redfish resource.
145 @param[in] RedfishService The Service to access the Redfish resources.
146
147 @return REDFISH_PAYLOAD instance of the resource, or NULL if error happens.
148
149 **/
150 REDFISH_PAYLOAD
151 EFIAPI
152 RedfishCreatePayload (
153 IN EDKII_JSON_VALUE Value,
154 IN REDFISH_SERVICE RedfishService
155 );
156
157 /**
158 Free the RedfishPayload and all its related resources.
159
160 @param[in] Payload Payload to be freed.
161
162 **/
163 VOID
164 EFIAPI
165 RedfishCleanupPayload (
166 IN REDFISH_PAYLOAD Payload
167 );
168
169 /**
170 This function returns the decoded JSON value of a REDFISH_PAYLOAD.
171
172 Caller doesn't need to free the returned JSON value because it will be released
173 in corresponding RedfishCleanupPayload() function.
174
175 @param[in] Payload A REDFISH_PAYLOAD instance.
176
177 @return Decoded JSON value of the payload.
178
179 **/
180 EDKII_JSON_VALUE
181 EFIAPI
182 RedfishJsonInPayload (
183 IN REDFISH_PAYLOAD Payload
184 );
185
186 /**
187 Fill the input RedPath string with system UUID from SMBIOS table or use the customized
188 ID if FromSmbios == FALSE.
189
190 This is a helper function to build a RedPath string which can be used to address
191 a Redfish resource for this computer system. The input PathString must have a Systems
192 note in format of "Systems[UUID=%g]" or "Systems[UUID~%g]" to fill the UUID value.
193
194 Example:
195 Use "/v1/Systems[UUID=%g]/Bios" to build a RedPath to address the "Bios" resource
196 for this computer system.
197
198 @param[in] RedPath RedPath format to be build.
199 @param[in] FromSmbios Get system UUID from SMBIOS as computer system instance ID.
200 @param[in] IdString The computer system instance ID.
201
202 @return Full RedPath with system UUID inside, or NULL if error happens.
203
204 **/
205 CHAR8 *
206 EFIAPI
207 RedfishBuildPathWithSystemUuid (
208 IN CONST CHAR8 *RedPath,
209 IN BOOLEAN FromSmbios,
210 IN CHAR8 *IdString OPTIONAL
211 );
212
213 /**
214 Get a redfish response addressed by a RedPath string, including HTTP StatusCode, Headers
215 and Payload which record any HTTP response messages.
216
217 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
218 redfish response data.
219
220 @param[in] RedfishService The Service to access the Redfish resources.
221 @param[in] RedPath RedPath string to address a resource, must start
222 from the root node.
223 @param[out] RedResponse Pointer to the Redfish response data.
224
225 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
226 NULL and the value is 2XX. The corresponding redfish resource has
227 been returned in Payload within RedResponse.
228 @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.
229 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
230 more error info from returned HTTP StatusCode, Headers and Payload
231 within RedResponse:
232 1. If the returned Payload is NULL, indicates any error happen.
233 2. If the returned StatusCode is NULL, indicates any error happen.
234 3. If the returned StatusCode is not 2XX, indicates any error happen.
235 **/
236 EFI_STATUS
237 EFIAPI
238 RedfishGetByService (
239 IN REDFISH_SERVICE RedfishService,
240 IN CONST CHAR8 *RedPath,
241 OUT REDFISH_RESPONSE *RedResponse
242 );
243
244 /**
245 Get a redfish response addressed by URI, including HTTP StatusCode, Headers
246 and Payload which record any HTTP response messages.
247
248 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
249 redfish response data.
250
251 @param[in] RedfishService The Service to access the URI resources.
252 @param[in] URI String to address a resource.
253 @param[out] RedResponse Pointer to the Redfish response data.
254
255 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
256 NULL and the value is 2XX. The corresponding redfish resource has
257 been returned in Payload within RedResponse.
258 @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.
259 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
260 more error info from returned HTTP StatusCode, Headers and Payload
261 within RedResponse:
262 1. If the returned Payload is NULL, indicates any error happen.
263 2. If the returned StatusCode is NULL, indicates any error happen.
264 3. If the returned StatusCode is not 2XX, indicates any error happen.
265 **/
266 EFI_STATUS
267 EFIAPI
268 RedfishGetByUri (
269 IN REDFISH_SERVICE RedfishService,
270 IN CONST CHAR8 *Uri,
271 OUT REDFISH_RESPONSE *RedResponse
272 );
273
274 /**
275 Get a redfish response addressed by the input Payload and relative RedPath string,
276 including HTTP StatusCode, Headers and Payload which record any HTTP response messages.
277
278 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
279 redfish response data.
280
281 @param[in] Payload A existing REDFISH_PAYLOAD instance.
282 @param[in] RedPath Relative RedPath string to address a resource inside Payload.
283 @param[out] RedResponse Pointer to the Redfish response data.
284
285 @retval EFI_SUCCESS The opeartion is successful:
286 1. The HTTP StatusCode is NULL and the returned Payload in
287 RedResponse is not NULL, indicates the Redfish resource has
288 been parsed from the input payload directly.
289 2. The HTTP StatusCode is not NULL and the value is 2XX,
290 indicates the corresponding redfish resource has been returned
291 in Payload within RedResponse.
292 @retval EFI_INVALID_PARAMETER Payload, RedPath, or RedResponse is NULL.
293 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
294 more error info from returned HTTP StatusCode, Headers and Payload
295 within RedResponse:
296 1. If the returned Payload is NULL, indicates any error happen.
297 2. If StatusCode is not NULL and the returned value of StatusCode
298 is not 2XX, indicates any error happen.
299 **/
300 EFI_STATUS
301 EFIAPI
302 RedfishGetByPayload (
303 IN REDFISH_PAYLOAD Payload,
304 IN CONST CHAR8 *RedPath,
305 OUT REDFISH_RESPONSE *RedResponse
306 );
307
308 /**
309 Use HTTP PATCH to perform updates on pre-existing Redfish resource.
310
311 This function uses the RedfishService to patch a Redfish resource addressed by
312 Uri (only the relative path is required). Changes to one or more properties within
313 the target resource are represented in the input Content, properties not specified
314 in Content won't be changed by this request. The corresponding redfish response will
315 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
316 messages.
317
318 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
319 redfish response data.
320
321 @param[in] RedfishService The Service to access the Redfish resources.
322 @param[in] Uri Relative path to address the resource.
323 @param[in] Content JSON represented properties to be update.
324 @param[out] RedResponse Pointer to the Redfish response data.
325
326 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
327 NULL and the value is 2XX. The Redfish resource will be returned
328 in Payload within RedResponse if server send it back in the HTTP
329 response message body.
330 @retval EFI_INVALID_PARAMETER RedfishService, Uri, Content, or RedResponse is NULL.
331 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
332 more error info from returned HTTP StatusCode, Headers and Payload
333 within RedResponse:
334 1. If the returned StatusCode is NULL, indicates any error happen.
335 2. If the returned StatusCode is not NULL and the value is not 2XX,
336 indicates any error happen.
337 **/
338 EFI_STATUS
339 EFIAPI
340 RedfishPatchToUri (
341 IN REDFISH_SERVICE RedfishService,
342 IN CONST CHAR8 *Uri,
343 IN CONST CHAR8 *Content,
344 OUT REDFISH_RESPONSE *RedResponse
345 );
346
347 /**
348 Use HTTP PATCH to perform updates on target payload. Patch to odata.id in Payload directly.
349
350 This function uses the Payload to patch the Target. Changes to one or more properties
351 within the target resource are represented in the input Payload, properties not specified
352 in Payload won't be changed by this request. The corresponding redfish response will
353 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
354 messages.
355
356 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
357 redfish response data.
358
359 @param[in] Target The target payload to be updated.
360 @param[in] Payload Palyoad with properties to be changed.
361 @param[out] RedResponse Pointer to the Redfish response data.
362
363 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
364 NULL and the value is 2XX. The Redfish resource will be returned
365 in Payload within RedResponse if server send it back in the HTTP
366 response message body.
367 @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.
368 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
369 more error info from returned HTTP StatusCode, Headers and Payload
370 within RedResponse:
371 1. If the returned StatusCode is NULL, indicates any error happen.
372 2. If the returned StatusCode is not NULL and the value is not 2XX,
373 indicates any error happen.
374 **/
375 EFI_STATUS
376 EFIAPI
377 RedfishPatchToPayload (
378 IN REDFISH_PAYLOAD Target,
379 IN REDFISH_PAYLOAD Payload,
380 OUT REDFISH_RESPONSE *RedResponse
381 );
382
383 /**
384 Use HTTP POST to create a new resource in target payload.
385
386 The POST request should be submitted to the Resource Collection in which the new resource
387 is to belong. The Resource Collection is addressed by Target payload. The Redfish may
388 ignore any service controlled properties. The corresponding redfish response will returned,
389 including HTTP StatusCode, Headers and Payload which record any HTTP response messages.
390
391 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
392 redfish response data.
393
394 @param[in] Target Target payload of the Resource Collection.
395 @param[in] Payload The new resource to be created.
396 @param[out] RedResponse Pointer to the Redfish response data.
397
398 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
399 NULL and the value is 2XX. The Redfish resource will be returned
400 in Payload within RedResponse if server send it back in the HTTP
401 response message body.
402 @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.
403 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
404 more error info from returned HTTP StatusCode, Headers and Payload
405 within RedResponse:
406 1. If the returned StatusCode is NULL, indicates any error happen.
407 2. If the returned StatusCode is not NULL and the value is not 2XX,
408 indicates any error happen.
409 **/
410 EFI_STATUS
411 EFIAPI
412 RedfishPostToPayload (
413 IN REDFISH_PAYLOAD Target,
414 IN REDFISH_PAYLOAD Payload,
415 OUT REDFISH_RESPONSE *RedResponse
416 );
417
418 /**
419 Use HTTP DELETE to remove a resource.
420
421 This function uses the RedfishService to remove a Redfish resource which is addressed
422 by input Uri (only the relative path is required). The corresponding redfish response will
423 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
424 messages.
425
426 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
427 redfish response data.
428
429 @param[in] RedfishService The Service to access the Redfish resources.
430 @param[in] Uri Relative path to address the resource.
431 @param[out] RedResponse Pointer to the Redfish response data.
432
433 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not
434 NULL and the value is 2XX, the Redfish resource has been removed.
435 If there is any message returned from server, it will be returned
436 in Payload within RedResponse.
437 @retval EFI_INVALID_PARAMETER RedfishService, Uri, or RedResponse is NULL.
438 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get
439 more error info from returned HTTP StatusCode, Headers and Payload
440 within RedResponse:
441 1. If the returned StatusCode is NULL, indicates any error happen.
442 2. If the returned StatusCode is not NULL and the value is not 2XX,
443 indicates any error happen.
444 **/
445 EFI_STATUS
446 EFIAPI
447 RedfishDeleteByUri (
448 IN REDFISH_SERVICE RedfishService,
449 IN CONST CHAR8 *Uri,
450 OUT REDFISH_RESPONSE *RedResponse
451 );
452
453 /**
454 Dump text in fractions.
455
456 @param[in] String ASCII string to dump.
457
458 **/
459 VOID
460 RedfishDumpJsonStringFractions (
461 IN CHAR8 *String
462 );
463
464 /**
465 Extract the JSON text content from REDFISH_PAYLOAD and dump to debug console.
466
467 @param[in] Payload The Redfish payload to dump.
468
469 **/
470 VOID
471 RedfishDumpPayload (
472 IN REDFISH_PAYLOAD Payload
473 );
474
475 /**
476 Dump text in JSON value.
477
478 @param[in] JsonValue The Redfish JSON value to dump.
479
480 **/
481 VOID
482 RedfishDumpJson (
483 IN EDKII_JSON_VALUE JsonValue
484 );
485
486 /**
487 This function will cleanup the HTTP header and Redfish payload resources.
488
489 @param[in] StatusCode The status code in HTTP response message.
490 @param[in] HeaderCount Number of HTTP header structures in Headers list.
491 @param[in] Headers Array containing list of HTTP headers.
492 @param[in] Payload The Redfish payload to dump.
493
494 **/
495 VOID
496 RedfishFreeResponse (
497 IN EFI_HTTP_STATUS_CODE *StatusCode,
498 IN UINTN HeaderCount,
499 IN EFI_HTTP_HEADER *Headers,
500 IN REDFISH_PAYLOAD Payload
501 );
502
503 /**
504 Check if the "@odata.type" in Payload is valid or not.
505
506 @param[in] Payload The Redfish payload to be checked.
507 @param[in] OdataTypeName OdataType will be retrived from mapping list.
508 @param[in] OdataTypeMappingList The list of OdataType.
509 @param[in] OdataTypeMappingListSize The number of mapping list
510
511 @return TRUE if the "@odata.type" in Payload is valid, otherwise FALSE.
512
513 **/
514 BOOLEAN
515 RedfishIsValidOdataType (
516 IN REDFISH_PAYLOAD Payload,
517 IN CONST CHAR8 *OdataTypeName,
518 IN REDFISH_ODATA_TYPE_MAPPING *OdataTypeMappingList,
519 IN UINTN OdataTypeMappingListSize
520 );
521
522 /**
523 Check if the payload is collection
524
525 @param[in] Payload The Redfish payload to be checked.
526
527 @return TRUE if the payload is collection.
528
529 **/
530 BOOLEAN
531 RedfishIsPayloadCollection (
532 IN REDFISH_PAYLOAD Payload
533 );
534
535 /**
536 Get collection size.
537
538 @param[in] Payload The Redfish collection payload
539 @param[in] CollectionSize Size of this collection
540
541 @return EFI_SUCCESS Coolection size is returned in CollectionSize
542 @return EFI_INVALID_PARAMETER The payload is not a collection.
543 **/
544 EFI_STATUS
545 RedfishGetCollectionSize (
546 IN REDFISH_PAYLOAD Payload,
547 IN UINTN *CollectionSize
548 );
549
550 /**
551 Get Redfish payload of collection member
552
553 @param[in] Payload The Redfish collection payload
554 @param[in] Index Index of collection member
555
556 @return NULL Fail to get collection member.
557 @return Non NULL Payload is returned.
558 **/
559 REDFISH_PAYLOAD
560 RedfishGetPayloadByIndex (
561 IN REDFISH_PAYLOAD Payload,
562 IN UINTN Index
563 );
564
565 /**
566 Check and return Redfish resource of the given Redpath.
567
568 @param[in] RedfishService Pointer to REDFISH_SERVICE
569 @param[in] Redpath Redpath of the resource.
570 @param[in] Response Optional return the resource.
571
572 @return EFI_STATUS
573 **/
574 EFI_STATUS
575 RedfishCheckIfRedpathExist (
576 IN REDFISH_SERVICE RedfishService,
577 IN CHAR8 *Redpath,
578 IN REDFISH_RESPONSE *Response OPTIONAL
579 );
580
581 /**
582 This function returns the string of Redfish service version.
583
584 @param[in] RedfishService Redfish service instance.
585 @param[out] ServiceVersionStr Redfish service string.
586
587 @return EFI_STATUS
588
589 **/
590 EFI_STATUS
591 RedfishGetServiceVersion (
592 IN REDFISH_SERVICE RedfishService,
593 OUT CHAR8 **ServiceVersionStr
594 );
595
596 /**
597 This function returns the string of Redfish service version.
598
599 @param[in] ServiceVerisonStr The string of Redfish service version.
600 @param[in] Url The URL to build Redpath with ID.
601 Start with "/", for example "/Registries"
602 @param[in] Id ID string
603 @param[out] Redpath Pointer to retrive Redpath, caller has to free
604 the memory allocated for this string.
605 @return EFI_STATUS
606
607 **/
608 EFI_STATUS
609 RedfishBuildRedpathUseId (
610 IN CHAR8 *ServiceVerisonStr,
611 IN CHAR8 *Url,
612 IN CHAR8 *Id,
613 OUT CHAR8 **Redpath
614 );
615
616 #endif