summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/PrivateLibrary/RedfishLib/RedfishLib.c
blob: cca0ea774893fb368088a84a053f01a4caeaebe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
/** @file
  Provides a set of utility APIs that allow to create/read/update/delete
  (CRUD) Redfish resources and provide basic query.

  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "RedfishMisc.h"

/**
  This function uses REST EX protocol provided in RedfishConfigServiceInfo.
  The service enumerator will also handle the authentication flow automatically
  if HTTP basic auth or Redfish session login is configured to use.

  Callers are responsible for freeing the returned service by RedfishCleanupService().

  @param[in]  RedfishConfigServiceInfo Redfish service information the EFI Redfish
                                       feature driver communicates with.

  @return     New created Redfish Service, or NULL if error happens.

**/
REDFISH_SERVICE
EFIAPI
RedfishCreateService (
  IN  REDFISH_CONFIG_SERVICE_INFORMATION  *RedfishConfigServiceInfo
  )
{
  REDFISH_SERVICE            RedfishService;
  EDKII_REDFISH_AUTH_METHOD  AuthMethod;
  CHAR8                      *UserId;
  CHAR8                      *Password;
  EFI_STATUS                 Status;

  RedfishService = NULL;
  UserId         = NULL;
  Password       = NULL;

  //
  // Check Input Parameters.
  //
  if (RedfishConfigServiceInfo == NULL) {
    return NULL;
  }

  //
  // Get Authentication Configuration.
  //
  Status = RedfishGetAuthInfo (&AuthMethod, &UserId, &Password);
  if (EFI_ERROR (Status)) {
    goto ON_EXIT;
  }

  //
  // Create a redfish service node based on Redfish network host interface.
  //
  RedfishService = RedfishCreateLibredfishService (
                     RedfishConfigServiceInfo,
                     AuthMethod,
                     UserId,
                     Password
                     );

ON_EXIT:
  if (UserId != NULL) {
    FreePool (UserId);
  }

  if (Password != NULL) {
    FreePool (Password);
  }

  return RedfishService;
}

/**
  Free the Service and all its related resources.

  @param[in]    RedfishService     The Service to access the Redfish resources.

**/
VOID
EFIAPI
RedfishCleanupService (
  IN REDFISH_SERVICE  RedfishService
  )
{
  if (RedfishService == NULL) {
    return;
  }

  cleanupServiceEnumerator (RedfishService);
}

/**
  Create REDFISH_PAYLOAD instance in local with JSON represented resource value and
  the Redfish Service.

  The returned REDFISH_PAYLOAD can be used to create or update Redfish resource in
  server side.

  Callers are responsible for freeing the returned payload by RedfishCleanupPayload().

  @param[in]    Value                 JSON Value of the redfish resource.
  @param[in]    RedfishService        The Service to access the Redfish resources.

  @return     REDFISH_PAYLOAD instance of the resource, or NULL if error happens.

**/
REDFISH_PAYLOAD
EFIAPI
RedfishCreatePayload (
  IN EDKII_JSON_VALUE  Value,
  IN REDFISH_SERVICE   RedfishService
  )
{
  EDKII_JSON_VALUE  CopyValue;

  CopyValue = JsonValueClone (Value);
  return createRedfishPayload (CopyValue, RedfishService);
}

/**
  Free the RedfishPayload and all its related resources.

  @param[in]    Payload        Payload to be freed.

**/
VOID
EFIAPI
RedfishCleanupPayload (
  IN REDFISH_PAYLOAD  Payload
  )
{
  if (Payload == NULL) {
    return;
  }

  cleanupPayload ((redfishPayload *)Payload);
}

/**
  This function returns the decoded JSON value of a REDFISH_PAYLOAD.

  Caller doesn't need to free the returned JSON value because it will be released
  in corresponding RedfishCleanupPayload() function.

  @param[in]    Payload     A REDFISH_PAYLOAD instance.

  @return     Decoded JSON value of the payload.

**/
EDKII_JSON_VALUE
EFIAPI
RedfishJsonInPayload (
  IN REDFISH_PAYLOAD  Payload
  )
{
  if (Payload == NULL) {
    return NULL;
  }

  return ((redfishPayload *)Payload)->json;
}

/**
  Fill the input RedPath string with system UUID from SMBIOS table or use the customized
  ID if  FromSmbios == FALSE.

  This is a helper function to build a RedPath string which can be used to address
  a Redfish resource for this computer system. The input PathString must have a Systems
  note in format of "Systems[UUID=%g]" or "Systems[UUID~%g]" to fill the UUID value.

  Example:
    Use "/v1/Systems[UUID=%g]/Bios" to build a RedPath to address the "Bios" resource
    for this computer system.

  @param[in]    RedPath        RedPath format to be build.
  @param[in]    FromSmbios     Get system UUID from SMBIOS as computer system instance ID.
  @param[in]    IdString       The computer system instance ID.

  @return     Full RedPath with system UUID inside, or NULL if error happens.

**/
CHAR8 *
EFIAPI
RedfishBuildPathWithSystemUuid (
  IN CONST CHAR8  *RedPath,
  IN BOOLEAN      FromSmbios,
  IN CHAR8        *IdString OPTIONAL
  )
{
  UINTN       BufSize;
  CHAR8       *RetRedPath;
  EFI_GUID    SystemUuid;
  EFI_STATUS  Status;

  if (RedPath == NULL) {
    return NULL;
  }

  //
  // Find system UUID from SMBIOS table.
  //
  if (FromSmbios) {
    Status = NetLibGetSystemGuid (&SystemUuid);
    if (EFI_ERROR (Status)) {
      return NULL;
    }

    // AsciiStrLen ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") = 36
    BufSize = AsciiStrSize (RedPath) + AsciiStrLen ("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
  } else {
    BufSize = AsciiStrSize (RedPath) + AsciiStrLen (IdString);
  }

  RetRedPath = AllocateZeroPool (BufSize);
  if (RetRedPath == NULL) {
    return NULL;
  }

  if (FromSmbios) {
    AsciiSPrint (RetRedPath, BufSize, RedPath, &SystemUuid);
  } else {
    AsciiSPrint (RetRedPath, BufSize, RedPath, IdString);
  }

  return RetRedPath;
}

/**
  Get a redfish response addressed by a RedPath string, including HTTP StatusCode, Headers
  and Payload which record any HTTP response messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService        The Service to access the Redfish resources.
  @param[in]    RedPath               RedPath string to address a resource, must start
                                      from the root node.
  @param[out]   RedResponse           Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The corresponding redfish resource has
                                  been returned in Payload within RedResponse.
  @retval EFI_INVALID_PARAMETER   RedfishService, RedPath, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned Payload is NULL, indicates any error happen.
                                  2. If the returned StatusCode is NULL, indicates any error happen.
                                  3. If the returned StatusCode is not 2XX, indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishGetByService (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *RedPath,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  if ((RedfishService == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadByPath (RedfishService, RedPath, &(RedResponse->StatusCode));

  //
  // 1. If the returned Payload is NULL, indicates any error happen.
  // 2. If the returned StatusCode is NULL, indicates any error happen.
  //
  if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {
    return EFI_DEVICE_ERROR;
  }

  //
  // 3. If the returned StatusCode is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}

/**
  Get a redfish response addressed by URI, including HTTP StatusCode, Headers
  and Payload which record any HTTP response messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService    The Service to access the URI resources.
  @param[in]    Uri               String to address a resource.
  @param[out]   RedResponse       Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The corresponding redfish resource has
                                  been returned in Payload within RedResponse.
  @retval EFI_INVALID_PARAMETER   RedfishService, RedPath, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned Payload is NULL, indicates any error happen.
                                  2. If the returned StatusCode is NULL, indicates any error happen.
                                  3. If the returned StatusCode is not 2XX, indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishGetByUri (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *Uri,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  EDKII_JSON_VALUE  JsonValue;

  if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  JsonValue            = getUriFromService (RedfishService, Uri, &RedResponse->StatusCode);
  RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);

  //
  // 1. If the returned Payload is NULL, indicates any error happen.
  // 2. If the returned StatusCode is NULL, indicates any error happen.
  //
  if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {
    return EFI_DEVICE_ERROR;
  }

  //
  // 3. If the returned StatusCode is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}

/**
  Get a redfish response addressed by the input Payload and relative RedPath string,
  including HTTP StatusCode, Headers and Payload which record any HTTP response messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    Payload           A existing REDFISH_PAYLOAD instance.
  @param[in]    RedPath           Relative RedPath string to address a resource inside Payload.
  @param[out]   RedResponse       Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful:
                                  1. The HTTP StatusCode is NULL and the returned Payload in
                                  RedResponse is not NULL, indicates the Redfish resource has
                                  been parsed from the input payload directly.
                                  2. The HTTP StatusCode is not NULL and the value is 2XX,
                                  indicates the corresponding redfish resource has been returned
                                  in Payload within RedResponse.
  @retval EFI_INVALID_PARAMETER   Payload, RedPath, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned Payload is NULL, indicates any error happen.
                                  2. If StatusCode is not NULL and the returned value of StatusCode
                                     is not 2XX, indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishGetByPayload (
  IN     REDFISH_PAYLOAD   Payload,
  IN     CONST CHAR8       *RedPath,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  if ((Payload == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadForPathString (Payload, RedPath, &(RedResponse->StatusCode));

  //
  // 1. If the returned Payload is NULL, indicates any error happen.
  //
  if (RedResponse->Payload == NULL) {
    return EFI_DEVICE_ERROR;
  }

  //
  // 2. If StatusCode is not NULL and the returned value of StatusCode is not 2XX, indicates any
  //    error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((RedResponse->StatusCode != NULL) && \
      ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
       (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)
      ))
  {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}

/**
  Use HTTP PATCH to perform updates on pre-existing Redfish resource.

  This function uses the RedfishService to patch a Redfish resource addressed by
  Uri (only the relative path is required). Changes to one or more properties within
  the target resource are represented in the input Content, properties not specified
  in Content won't be changed by this request. The corresponding redfish response will
  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService        The Service to access the Redfish resources.
  @param[in]    Uri                   Relative path to address the resource.
  @param[in]    Content               JSON represented properties to be update.
  @param[out]   RedResponse           Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The Redfish resource will be returned
                                  in Payload within RedResponse if server send it back in the HTTP
                                  response message body.
  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, Content, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishPatchToUri (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *Uri,
  IN     CONST CHAR8       *Content,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  EFI_STATUS        Status;
  EDKII_JSON_VALUE  JsonValue;

  Status    = EFI_SUCCESS;
  JsonValue = NULL;

  if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  JsonValue = (EDKII_JSON_VALUE)patchUriFromService (
                                  RedfishService,
                                  Uri,
                                  Content,
                                  &(RedResponse->StatusCode)
                                  );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    Status = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    Status = EFI_DEVICE_ERROR;
  }

ON_EXIT:
  if (JsonValue != NULL) {
    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
    if (RedResponse->Payload == NULL) {
      //
      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
      // we care about if the returned StatusCode is 2XX.
      //
      JsonValueFree (JsonValue);
    }
  }

  return Status;
}

/**
  Use HTTP PATCH to perform updates on target payload. Patch to odata.id in Payload directly.

  This function uses the Payload to patch the Target. Changes to one or more properties
  within the target resource are represented in the input Payload, properties not specified
  in Payload won't be changed by this request. The corresponding redfish response will
  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    Target           The target payload to be updated.
  @param[in]    Payload          Palyoad with properties to be changed.
  @param[out]   RedResponse      Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The Redfish resource will be returned
                                  in Payload within RedResponse if server send it back in the HTTP
                                  response message body.
  @retval EFI_INVALID_PARAMETER   Target, Payload, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishPatchToPayload (
  IN     REDFISH_PAYLOAD   Target,
  IN     REDFISH_PAYLOAD   Payload,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  RedResponse->Payload = (REDFISH_PAYLOAD)patchPayload (
                                            Target,
                                            Payload,
                                            &(RedResponse->StatusCode)
                                            );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    return EFI_DEVICE_ERROR;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}

/**
  Use HTTP POST to create new Redfish resource in the Resource Collection.

  The POST request should be submitted to the Resource Collection in which the new resource
  is to belong. The Resource Collection is addressed by URI. The Redfish may
  ignore any service controlled properties. The corresponding redfish response will returned,
  including HTTP StatusCode, Headers and Payload which record any HTTP response messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService        The Service to access the Redfish resources.
  @param[in]    Uri                   Relative path to address the resource.
  @param[in]    Content               JSON represented properties to be update.
  @param[in]    ContentSize           Size of the Content to be send to Redfish service
  @param[in]    ContentType           Type of the Content to be send to Redfish service
  @param[out]   RedResponse           Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The Redfish resource will be returned
                                  in Payload within RedResponse if server send it back in the HTTP
                                  response message body.
  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, Content, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishPostToUri (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *Uri,
  IN     CONST CHAR8       *Content,
  IN     UINTN             ContentSize,
  IN     CONST CHAR8       *ContentType,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  EFI_STATUS        Status;
  EDKII_JSON_VALUE  JsonValue;

  Status    = EFI_SUCCESS;
  JsonValue = NULL;

  if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  JsonValue = (EDKII_JSON_VALUE)postUriFromService (
                                  RedfishService,
                                  Uri,
                                  Content,
                                  ContentSize,
                                  ContentType,
                                  &(RedResponse->StatusCode)
                                  );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    Status = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    Status = EFI_DEVICE_ERROR;
  }

ON_EXIT:
  if (JsonValue != NULL) {
    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
    if (RedResponse->Payload == NULL) {
      //
      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
      // we care about if the returned StatusCode is 2XX.
      //
      JsonValueFree (JsonValue);
    }
  }

  return Status;
}

/**
  Use HTTP POST to create a new resource in target payload.

  The POST request should be submitted to the Resource Collection in which the new resource
  is to belong. The Resource Collection is addressed by Target payload. The Redfish may
  ignore any service controlled properties. The corresponding redfish response will returned,
  including HTTP StatusCode, Headers and Payload which record any HTTP response messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    Target          Target payload of the Resource Collection.
  @param[in]    Payload         The new resource to be created.
  @param[out]   RedResponse     Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX. The Redfish resource will be returned
                                  in Payload within RedResponse if server send it back in the HTTP
                                  response message body.
  @retval EFI_INVALID_PARAMETER   Target, Payload, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishPostToPayload (
  IN     REDFISH_PAYLOAD   Target,
  IN     REDFISH_PAYLOAD   Payload,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  RedResponse->Payload = (REDFISH_PAYLOAD)postPayload (
                                            Target,
                                            Payload,
                                            &(RedResponse->StatusCode)
                                            );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    return EFI_DEVICE_ERROR;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    return EFI_DEVICE_ERROR;
  }

  return EFI_SUCCESS;
}

/**
  Use HTTP DELETE to remove a resource.

  This function uses the RedfishService to remove a Redfish resource which is addressed
  by input Uri (only the relative path is required). The corresponding redfish response will
  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService        The Service to access the Redfish resources.
  @param[in]    Uri                   Relative path to address the resource.
  @param[out]   RedResponse           Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX, the Redfish resource has been removed.
                                  If there is any message returned from server, it will be returned
                                  in Payload within RedResponse.
  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishDeleteByUri (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *Uri,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  EFI_STATUS        Status;
  EDKII_JSON_VALUE  JsonValue;

  Status    = EFI_SUCCESS;
  JsonValue = NULL;

  if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  JsonValue = (EDKII_JSON_VALUE)deleteUriFromService (
                                  RedfishService,
                                  Uri,
                                  &(RedResponse->StatusCode)
                                  );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    Status = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    Status = EFI_DEVICE_ERROR;
  }

ON_EXIT:
  if (JsonValue != NULL) {
    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
    if (RedResponse->Payload == NULL) {
      //
      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
      // we care about if the returned StatusCode is 2XX.
      //
      JsonValueFree (JsonValue);
    }
  }

  return Status;
}

/**
  Use HTTP DELETE to remove a resource.

  This function uses the RedfishService to remove a Redfish resource which is addressed
  by input Uri (only the relative path is required). The corresponding redfish response will
  returned, including HTTP StatusCode, Headers and Payload which record any HTTP response
  messages.

  Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in
  redfish response data.

  @param[in]    RedfishService        The Service to access the Redfish resources.
  @param[in]    Uri                   Relative path to address the resource.
  @param[in]    Content               JSON represented properties to be deleted.
  @param[out]   RedResponse           Pointer to the Redfish response data.

  @retval EFI_SUCCESS             The opeartion is successful, indicates the HTTP StatusCode is not
                                  NULL and the value is 2XX, the Redfish resource has been removed.
                                  If there is any message returned from server, it will be returned
                                  in Payload within RedResponse.
  @retval EFI_INVALID_PARAMETER   RedfishService, Uri, or RedResponse is NULL.
  @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred. Callers can get
                                  more error info from returned HTTP StatusCode, Headers and Payload
                                  within RedResponse:
                                  1. If the returned StatusCode is NULL, indicates any error happen.
                                  2. If the returned StatusCode is not NULL and the value is not 2XX,
                                     indicates any error happen.
**/
EFI_STATUS
EFIAPI
RedfishDeleteByUriEx (
  IN     REDFISH_SERVICE   RedfishService,
  IN     CONST CHAR8       *Uri,
  IN     CONST CHAR8       *Content,
  OUT    REDFISH_RESPONSE  *RedResponse
  )
{
  EFI_STATUS        Status;
  EDKII_JSON_VALUE  JsonValue;

  Status    = EFI_SUCCESS;
  JsonValue = NULL;

  if ((RedfishService == NULL) || (Content == NULL) || (Uri == NULL) || (RedResponse == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));

  JsonValue = (EDKII_JSON_VALUE)deleteUriFromServiceEx (
                                  RedfishService,
                                  Uri,
                                  Content,
                                  &(RedResponse->StatusCode)
                                  );

  //
  // 1. If the returned StatusCode is NULL, indicates any error happen.
  //
  if (RedResponse->StatusCode == NULL) {
    Status = EFI_DEVICE_ERROR;
    goto ON_EXIT;
  }

  //
  // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.
  //    NOTE: If there is any error message returned from server, it will be returned in
  //          Payload within RedResponse.
  //
  if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \
      (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))
  {
    Status = EFI_DEVICE_ERROR;
  }

ON_EXIT:
  if (JsonValue != NULL) {
    RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);
    if (RedResponse->Payload == NULL) {
      //
      // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what
      // we care about if the returned StatusCode is 2XX.
      //
      JsonValueFree (JsonValue);
    }
  }

  return Status;
}

/**
  Dump text in fractions.

  @param[in]  String   ASCII string to dump.

**/
VOID
RedfishDumpJsonStringFractions (
  IN CHAR8  *String
  )
{
  CHAR8  *NextFraction;
  UINTN  StringFractionSize;
  UINTN  StrLen;
  UINTN  Count;
  CHAR8  BackupChar;

  StringFractionSize = 200;
  if (String == NULL) {
    return;
  }

  DEBUG ((DEBUG_MANAGEABILITY, "JSON text:\n"));
  NextFraction = String;
  StrLen       = AsciiStrLen (String);
  if (StrLen == 0) {
    return;
  }

  for (Count = 0; Count < (StrLen / StringFractionSize); Count++) {
    BackupChar                           = *(NextFraction + StringFractionSize);
    *(NextFraction + StringFractionSize) = 0;
    DEBUG ((DEBUG_MANAGEABILITY, "%a", NextFraction));
    *(NextFraction + StringFractionSize) = BackupChar;
    NextFraction                        += StringFractionSize;
  }

  if ((StrLen % StringFractionSize) != 0) {
    DEBUG ((DEBUG_MANAGEABILITY, "%a\n\n", NextFraction));
  }
}

/**
  Dump text in JSON value.

  @param[in]  JsonValue       The Redfish JSON value to dump.

**/
VOID
RedfishDumpJson (
  IN EDKII_JSON_VALUE  JsonValue
  )
{
  CHAR8  *String;

  String = JsonDumpString (JsonValue, 0);
  if (String == NULL) {
    return;
  }

  RedfishDumpJsonStringFractions (String);
  FreePool (String);
}

/**
  Extract the JSON text content from REDFISH_PAYLOAD and dump to debug console.

  @param[in]  Payload       The Redfish payload to dump.

**/
VOID
RedfishDumpPayload (
  IN REDFISH_PAYLOAD  Payload
  )
{
  EDKII_JSON_VALUE  JsonValue;
  CHAR8             *String;

  JsonValue = NULL;
  String    = NULL;

  if (Payload == NULL) {
    return;
  }

  JsonValue = RedfishJsonInPayload (Payload);
  if (JsonValue == NULL) {
    return;
  }

  String = JsonDumpString (JsonValue, 0);
  if (String == NULL) {
    return;
  }

  RedfishDumpJsonStringFractions (String);
  FreePool (String);
}

/**
  This function will cleanup the HTTP header and Redfish payload resources.

  @param[in]  StatusCode        The status code in HTTP response message.
  @param[in]  HeaderCount       Number of HTTP header structures in Headers list.
  @param[in]  Headers           Array containing list of HTTP headers.
  @param[in]  Payload           The Redfish payload to dump.

**/
VOID
RedfishFreeResponse (
  IN EFI_HTTP_STATUS_CODE  *StatusCode,
  IN UINTN                 HeaderCount,
  IN EFI_HTTP_HEADER       *Headers,
  IN REDFISH_PAYLOAD       Payload
  )
{
  if (StatusCode != NULL) {
    FreePool (StatusCode);
    StatusCode = NULL;
  }

  if ((HeaderCount != 0) && (Headers != NULL)) {
    HttpFreeHeaderFields (Headers, HeaderCount);
    Headers = NULL;
  }

  if (Payload != NULL) {
    RedfishCleanupPayload (Payload);
    Payload = NULL;
  }
}

/**
  Check if the "@odata.type" in Payload is valid or not.

  @param[in]  Payload                  The Redfish payload to be checked.
  @param[in]  OdataTypeName            OdataType will be retrived from mapping list.
  @param[in]  OdataTypeMappingList     The list of OdataType.
  @param[in]  OdataTypeMappingListSize The number of mapping list

  @return TRUE if the "@odata.type" in Payload is valid, otherwise FALSE.

**/
BOOLEAN
RedfishIsValidOdataType (
  IN REDFISH_PAYLOAD             Payload,
  IN CONST CHAR8                 *OdataTypeName,
  IN REDFISH_ODATA_TYPE_MAPPING  *OdataTypeMappingList,
  IN UINTN                       OdataTypeMappingListSize
  )
{
  UINTN             Index;
  EDKII_JSON_VALUE  OdataType;
  EDKII_JSON_VALUE  JsonValue;

  if ((Payload == NULL) || (OdataTypeName == NULL)) {
    return FALSE;
  }

  JsonValue = RedfishJsonInPayload (Payload);
  if (!JsonValueIsObject (JsonValue)) {
    return FALSE;
  }

  OdataType = JsonObjectGetValue (JsonValueGetObject (JsonValue), "@odata.type");
  if (!JsonValueIsString (OdataType) || (JsonValueGetAsciiString (OdataType) == NULL)) {
    return FALSE;
  }

  for (Index = 0; Index < OdataTypeMappingListSize; Index++) {
    if ((AsciiStrCmp (OdataTypeMappingList[Index].OdataTypeName, OdataTypeName) == 0) &&
        (AsciiStrCmp (OdataTypeMappingList[Index].OdataType, JsonValueGetAsciiString (OdataType)) == 0))
    {
      return TRUE;
    }
  }

  DEBUG ((DEBUG_MANAGEABILITY, "%a: This Odata type is not in the list.\n", __func__));
  return FALSE;
}

/**
  Check if the payload is collection

  @param[in]  Payload                  The Redfish payload to be checked.

  @return TRUE if the payload is  collection.

**/
BOOLEAN
RedfishIsPayloadCollection (
  IN REDFISH_PAYLOAD  Payload
  )
{
  return isPayloadCollection (Payload);
}

/**
  Get collection size.

  @param[in]  Payload         The Redfish collection payload
  @param[in]  CollectionSize  Size of this collection

  @return EFI_SUCCESS              Coolection size is returned in CollectionSize
  @return EFI_INVALID_PARAMETER    The payload is not a collection.
**/
EFI_STATUS
RedfishGetCollectionSize (
  IN REDFISH_PAYLOAD  Payload,
  IN UINTN            *CollectionSize
  )
{
  if ((Payload == NULL) || (CollectionSize == NULL)) {
    return EFI_INVALID_PARAMETER;
  }

  if (!RedfishIsPayloadCollection (Payload)) {
    return EFI_INVALID_PARAMETER;
  }

  *CollectionSize = (UINTN)getCollectionSize (Payload);
  return EFI_SUCCESS;
}

/**
  Get Redfish payload of collection member

  @param[in]  Payload    The Redfish collection payload
  @param[in]  Index      Index of collection member

  @return NULL           Fail to get collection member.
  @return Non NULL       Payload is returned.
**/
REDFISH_PAYLOAD
RedfishGetPayloadByIndex (
  IN REDFISH_PAYLOAD  Payload,
  IN UINTN            Index
  )
{
  REDFISH_RESPONSE  RedfishResponse;
  REDFISH_PAYLOAD   PayloadReturn;

  PayloadReturn = (VOID *)getPayloadByIndex (Payload, Index, &RedfishResponse.StatusCode);
  if ((PayloadReturn == NULL) ||
      ((*(RedfishResponse.StatusCode) < HTTP_STATUS_200_OK) && (*(RedfishResponse.StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)))
  {
    return NULL;
  }

  return PayloadReturn;
}

/**
  Check and return Redfish resource of the given Redpath.

  @param[in]  RedfishService  Pointer to REDFISH_SERVICE
  @param[in]  Redpath         Redpath of the resource.
  @param[in]  Response        Optional return the resource.

  @return EFI_STATUS
**/
EFI_STATUS
RedfishCheckIfRedpathExist (
  IN REDFISH_SERVICE   RedfishService,
  IN CHAR8             *Redpath,
  IN REDFISH_RESPONSE  *Response OPTIONAL
  )
{
  EFI_STATUS        Status;
  REDFISH_RESPONSE  TempResponse;

  if (Redpath == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = RedfishGetByService (RedfishService, Redpath, &TempResponse);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if (Response == NULL) {
    RedfishFreeResponse (
      TempResponse.StatusCode,
      TempResponse.HeaderCount,
      TempResponse.Headers,
      TempResponse.Payload
      );
  } else {
    CopyMem ((VOID *)Response, (VOID *)&TempResponse, sizeof (REDFISH_RESPONSE));
  }

  return EFI_SUCCESS;
}