summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java
blob: 5664653bf08bd20a64dd6fe47b0d363966f36999 (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
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

package org.onap.clamp.clds.client.req.sdc;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.BadRequestException;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;
import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.exception.sdc.SdcCommunicationException;
import org.onap.clamp.clds.model.CldsAlarmCondition;
import org.onap.clamp.clds.model.CldsServiceData;
import org.onap.clamp.clds.model.CldsVfData;
import org.onap.clamp.clds.model.CldsVfKPIData;
import org.onap.clamp.clds.model.CldsVfcData;
import org.onap.clamp.clds.model.properties.Global;
import org.onap.clamp.clds.model.properties.ModelProperties;
import org.onap.clamp.clds.model.sdc.SdcArtifact;
import org.onap.clamp.clds.model.sdc.SdcResource;
import org.onap.clamp.clds.model.sdc.SdcResourceBasicInfo;
import org.onap.clamp.clds.model.sdc.SdcServiceDetail;
import org.onap.clamp.clds.model.sdc.SdcServiceInfo;
import org.onap.clamp.clds.util.CryptoUtils;
import org.onap.clamp.clds.util.LoggingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SdcCatalogServices {

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class);
    private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
    private static final String RESOURCE_VF_TYPE = "VF";
    private static final String RESOURCE_VFC_TYPE = "VFC";
    private static final String RESOURCE_CVFC_TYPE = "CVFC";
    private static final String SDC_REQUESTID_PROPERTY_NAME = "sdc.header.requestId";
    private static final String SDC_METADATA_URL_PREFIX = "/metadata";
    private static final String SDC_INSTANCE_ID_PROPERTY_NAME = "sdc.InstanceID";
    private static final String SDC_CATALOG_URL_PROPERTY_NAME = "sdc.catalog.url";
    private static final String SDC_SERVICE_URL_PROPERTY_NAME = "sdc.serviceUrl";
    private static final String SDC_INSTANCE_ID_CLAMP = "CLAMP-Tool";
    private static final String RESOURCE_URL_PREFIX = "resources";
    @Autowired
    private ClampProperties refProp;

    /**
     * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic
     * dGVzdDoxMjM0NTY=).
     *
     * @return The String with Basic Auth and password
     * @throws GeneralSecurityException
     *             In case of issue when decryting the SDC password
     * @throws DecoderException
     *             In case of issues with the decoding of the HexString message
     */
    private String getSdcBasicAuth() throws GeneralSecurityException, DecoderException {
        String sdcId = refProp.getStringValue("sdc.serviceUsername");
        String sdcPw = refProp.getStringValue("sdc.servicePassword");
        String password = CryptoUtils.decrypt(sdcPw);
        String idPw = Base64.getEncoder().encodeToString((sdcId + ":" + password).getBytes(StandardCharsets.UTF_8));
        return "Basic " + idPw;
    }

    /**
     * This method get the SDC services Information with the corresponding
     * Service UUID.
     * 
     * @param uuid
     *            The service UUID
     * @return A Json String with all the service list
     * @throws GeneralSecurityException
     *             In case of issue when decryting the SDC password
     * @throws DecoderException
     *             In case of issues with the decoding of the Hex String
     */
    public String getSdcServicesInformation(String uuid) throws GeneralSecurityException, DecoderException {
        Date startTime = new Date();
        String baseUrl = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME);
        String basicAuth = getSdcBasicAuth();
        LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation");
        try {
            String url = baseUrl;
            if (uuid != null && !uuid.isEmpty()) {
                url = baseUrl + "/" + uuid + SDC_METADATA_URL_PREFIX;
            }
            URL urlObj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
            conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP);
            conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
            conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
            conn.setRequestMethod("GET");
            String resp = getResponse(conn);
            logger.debug("Services list received from SDC:" + resp);
            // metrics log
            LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName());
            return resp;
        } catch (IOException e) {
            LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName());
            LoggingUtils.setErrorContext("900", "Get sdc services error");
            logger.error("not able to get any service information from sdc for uuid:" + uuid, e);
        } finally {
            LoggingUtils.setTimeContext(startTime, new Date());
            metricsLogger.info("getSdcServicesInformation complete");
        }
        return "";
    }

    /**
     * To remove duplicate serviceUUIDs from sdc services List.
     * 
     * @param rawCldsSdcServiceList
     *            A list of CldsSdcServiceInfo
     * @return A list of CldsSdcServiceInfo without duplicate service UUID
     */
    public List<SdcServiceInfo> removeDuplicateServices(List<SdcServiceInfo> rawCldsSdcServiceList) {
        List<SdcServiceInfo> cldsSdcServiceInfoList = null;
        if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) {
            // sort list
            Collections.sort(rawCldsSdcServiceList);
            // and then take only the services with the max version (last in the
            // list with the same name)
            cldsSdcServiceInfoList = new ArrayList<>();
            for (int i = 1; i < rawCldsSdcServiceList.size(); i++) {
                // compare name with previous - if not equal, then keep the
                // previous (it's the last with that name)
                SdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1);
                if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) {
                    cldsSdcServiceInfoList.add(prev);
                }
            }
            // add the last in the list
            cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1));
        }
        return cldsSdcServiceInfoList;
    }

    /**
     * To remove duplicate serviceUUIDs from sdc resources List.
     * 
     * @param rawCldsSdcResourceList
     * @return List of CldsSdcResource
     */
    public List<SdcResource> removeDuplicateSdcResourceInstances(List<SdcResource> rawCldsSdcResourceList) {
        List<SdcResource> cldsSdcResourceList = null;
        if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) {
            // sort list
            Collections.sort(rawCldsSdcResourceList);
            // and then take only the resources with the max version (last in
            // the list with the same name)
            cldsSdcResourceList = new ArrayList<>();
            for (int i = 1; i < rawCldsSdcResourceList.size(); i++) {
                // compare name with previous - if not equal, then keep the
                // previous (it's the last with that name)
                SdcResource prev = rawCldsSdcResourceList.get(i - 1);
                if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
                    cldsSdcResourceList.add(prev);
                }
            }
            // add the last in the list
            cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1));
        }
        return cldsSdcResourceList;
    }

    /**
     * To remove duplicate basic resources with same resourceUUIDs.
     * 
     * @param rawCldsSdcResourceListBasicList
     * @return List of CldsSdcResourceBasicInfo
     */
    public List<SdcResourceBasicInfo> removeDuplicateSdcResourceBasicInfo(
            List<SdcResourceBasicInfo> rawCldsSdcResourceListBasicList) {
        List<SdcResourceBasicInfo> cldsSdcResourceBasicInfoList = null;
        if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) {
            // sort list
            Collections.sort(rawCldsSdcResourceListBasicList);
            // and then take only the resources with the max version (last in
            // the list with the same name)
            cldsSdcResourceBasicInfoList = new ArrayList<>();
            for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) {
                // compare name with previous - if not equal, then keep the
                // previous (it's the last with that name)
                SdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1);
                if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
                    cldsSdcResourceBasicInfoList.add(prev);
                }
            }
            // add the last in the list
            cldsSdcResourceBasicInfoList
                    .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1));
        }
        return cldsSdcResourceBasicInfoList;
    }

    /**
     * To get ServiceUUID by using serviceInvariantUUID.
     * 
     * @param invariantId
     *            The invariant ID
     * @return The service UUID
     * @throws GeneralSecurityException
     *             In case of issue when decryting the SDC password
     * @throws DecoderException
     *             In case of issues with the decoding of the Hex String
     */
    public String getServiceUuidFromServiceInvariantId(String invariantId)
            throws GeneralSecurityException, DecoderException {
        String serviceUuid = "";
        String responseStr = getSdcServicesInformation(null);
        List<SdcServiceInfo> rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr);
        List<SdcServiceInfo> cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList);
        if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) {
            for (SdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) {
                if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null
                        && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) {
                    serviceUuid = currCldsSdcServiceInfo.getUuid();
                    break;
                }
            }
        }
        return serviceUuid;
    }

    /**
     * To get CldsAsdsServiceInfo class by parsing json string.
     * 
     * @param jsonStr
     *            The Json string that must be decoded
     * @return The list of CldsSdcServiceInfo, if there is a failure it return
     *         an empty list
     */
    private List<SdcServiceInfo> getCldsSdcServicesListFromJson(String jsonStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        if (StringUtils.isBlank(jsonStr)) {
            return new ArrayList<>();
        }
        try {
            return objectMapper.readValue(jsonStr,
                    objectMapper.getTypeFactory().constructCollectionType(List.class, SdcServiceInfo.class));
        } catch (IOException e) {
            logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e);
            return new ArrayList<>();
        }
    }

    /**
     * To get List of CldsSdcResourceBasicInfo class by parsing json string.
     *
     * @param jsonStr
     *            The JSOn string that must be decoded
     * @return The list of CldsSdcResourceBasicInfo, an empty list in case of
     *         issues
     */
    private List<SdcResourceBasicInfo> getAllSdcResourcesListFromJson(String jsonStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        if (StringUtils.isBlank(jsonStr)) {
            return new ArrayList<>();
        }
        try {
            return objectMapper.readValue(jsonStr,
                    objectMapper.getTypeFactory().constructCollectionType(List.class, SdcResourceBasicInfo.class));
        } catch (IOException e) {
            logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e);
            return new ArrayList<>();
        }
    }

    /**
     * To get CldsSdcServiceDetail by parsing json string.
     * 
     * @param jsonStr
     * @return
     */
    public SdcServiceDetail decodeCldsSdcServiceDetailFromJson(String jsonStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(jsonStr, SdcServiceDetail.class);
        } catch (IOException e) {
            logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e);
            return null;
        }
    }

    // upload artifact to sdc based on serviceUUID and resource name on url
    private String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) {
        // Verify whether it is triggered by Validation Test button from UI
        if (prop.isTestOnly()) {
            return "sdc artifact upload not executed for test action";
        }
        try {
            logger.info("userid=" + userid);
            byte[] postData = formattedSdcReq.getBytes(StandardCharsets.UTF_8);
            int postDataLength = postData.length;
            HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, formattedSdcReq);
            try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
                wr.write(postData);
            }
            boolean requestFailed = true;
            int responseCode = conn.getResponseCode();
            logger.info("responseCode=" + responseCode);
            if (responseCode == 200) {
                requestFailed = false;
            }
            String responseStr = getResponse(conn);
            if (responseStr != null && requestFailed) {
                logger.error("requestFailed - responseStr=" + responseStr);
                throw new BadRequestException(responseStr);
            }
            return responseStr;
        } catch (IOException e) {
            logger.error("Exception when attempting to communicate with SDC", e);
            throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e);
        }
    }

    private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String content) {
        try {
            logger.info("userid=" + userid);
            String basicAuth = getSdcBasicAuth();
            String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
            URL urlObj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
            conn.setDoOutput(true);
            conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), sdcXonapInstanceId);
            conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json");
            conn.setRequestProperty(HttpHeaders.CONTENT_MD5,
                    Base64.getEncoder().encodeToString(DigestUtils.md5Hex(content).getBytes("UTF-8")));
            conn.setRequestProperty("USER_ID", userid);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("charset", "utf-8");
            conn.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Integer.toString(postDataLength));
            conn.setUseCaches(false);
            conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
            return conn;
        } catch (IOException e) {
            logger.error("Exception when attempting to open connection with SDC", e);
            throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e);
        } catch (DecoderException e) {
            logger.error("Exception when attempting to decode the Hex string", e);
            throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e);
        } catch (GeneralSecurityException e) {
            logger.error("Exception when attempting to decrypt the encrypted password", e);
            throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e);
        }
    }

    private String getResponse(HttpURLConnection conn) {
        try (InputStream is = getInputStream(conn)) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
                return IOUtils.toString(in);
            }
        } catch (IOException e) {
            logger.error("Exception when attempting to open SDC response", e);
            throw new SdcCommunicationException("Exception when attempting to open SDC response", e);
        }
    }

    private InputStream getInputStream(HttpURLConnection conn) {
        try {
            InputStream inStream = conn.getErrorStream();
            if (inStream == null) {
                inStream = conn.getInputStream();
            }
            return inStream;
        } catch (IOException e) {
            logger.error("Exception when attempting to open SDC error stream", e);
            throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e);
        }
    }

    /**
     * Check if the SDC Info in cache has expired.
     * 
     * @param cldsServiceData
     *            The object representing the service data
     * @return boolean flag
     * @throws GeneralSecurityException
     *             In case of issues with the decryting the encrypted password
     * @throws DecoderException
     *             In case of issues with the decoding of the Hex String
     */
    public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData)
            throws GeneralSecurityException, DecoderException {
        if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
            String cachedServiceUuid = cldsServiceData.getServiceUUID();
            String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID());
            String configuredMaxAge = refProp.getStringValue("clds.service.cache.invalidate.after.seconds");
            if (configuredMaxAge == null) {
                logger.warn(
                        "clds.service.cache.invalidate.after.seconds NOT set in clds-reference.properties file, taking 60s as default");
                configuredMaxAge = "60";
            }
            return (!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null
                    && cldsServiceData.getAgeOfRecord() > Long.parseLong(configuredMaxAge));
        } else {
            return true;
        }
    }

    /**
     * Get the Service Data with Alarm Conditions for a given
     * invariantServiceUuid.
     * 
     * @param invariantServiceUuid
     * @return The CldsServiceData
     * @throws GeneralSecurityException
     *             In case of issues with the decryting the encrypted password
     * @throws DecoderException
     *             In case of issues with the decoding of the Hex String
     */
    public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid)
            throws GeneralSecurityException, DecoderException {
        String url = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME);
        String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
        String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid);
        String serviceDetailUrl = url + "/" + serviceUuid + SDC_METADATA_URL_PREFIX;
        String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl);
        ObjectMapper objectMapper = new ObjectMapper();
        CldsServiceData cldsServiceData = new CldsServiceData();
        if (responseStr != null) {
            SdcServiceDetail cldsSdcServiceDetail;
            try {
                cldsSdcServiceDetail = objectMapper.readValue(responseStr, SdcServiceDetail.class);
            } catch (IOException e) {
                logger.error("Exception when decoding the CldsServiceData JSON from SDC", e);
                throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e);
            }
            // To remove duplicate resources from serviceDetail and add valid
            // vfs to service
            if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {
                cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid());
                cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID());
                List<SdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(
                        cldsSdcServiceDetail.getResources());
                if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) {
                    List<CldsVfData> cldsVfDataList = new ArrayList<>();
                    for (SdcResource currCldsSdcResource : cldsSdcResourceList) {
                        if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null
                                && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) {
                            CldsVfData currCldsVfData = new CldsVfData();
                            currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());
                            currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());
                            cldsVfDataList.add(currCldsVfData);
                        }
                    }
                    cldsServiceData.setCldsVfs(cldsVfDataList);
                    // For each vf in the list , add all vfc's
                    getAllVfcForVfList(cldsVfDataList, catalogUrl);
                    logger.info("Invariant Service ID of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
                }
            }
        }
        return cldsServiceData;
    }

    private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl)
            throws GeneralSecurityException {
        // todo : refact this..
        if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
            List<SdcResourceBasicInfo> allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_VF_TYPE);
            List<SdcResourceBasicInfo> allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType(
                    RESOURCE_VFC_TYPE);
            allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE));
            for (CldsVfData currCldsVfData : cldsVfDataList) {
                if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
                    String resourceUuid = getResourceUuidFromResourceInvariantUuid(
                            currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
                    if (resourceUuid != null) {
                        String vfResourceUuidUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUuid
                                + SDC_METADATA_URL_PREFIX;
                        String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl);
                        if (vfResponse != null) {
                            // Below 2 line are to get the KPI(field path) data
                            // associated with the VF's
                            List<CldsVfKPIData> cldsVfKPIDataList = getFieldPathFromVF(vfResponse);
                            currCldsVfData.setCldsKPIList(cldsVfKPIDataList);
                            List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);
                            if (vfcDataListFromVfResponse != null) {
                                currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
                                if (!vfcDataListFromVfResponse.isEmpty()) {
                                    // To get artifacts for every VFC and get
                                    // alarm conditions from artifact
                                    for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
                                        if (currCldsVfcData != null
                                                && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
                                            String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid(
                                                    currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
                                            if (resourceVfcUuid != null) {
                                                String vfcResourceUuidUrl = catalogUrl + RESOURCE_URL_PREFIX + "/"
                                                        + resourceVfcUuid + SDC_METADATA_URL_PREFIX;
                                                String vfcResponse = getCldsServicesOrResourcesBasedOnURL(
                                                        vfcResourceUuidUrl);
                                                if (vfcResponse != null) {
                                                    List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(
                                                            vfcResponse);
                                                    currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
                                                }
                                            } else {
                                                logger.info("No resourceVFC UUID found for given invariantID:"
                                                        + currCldsVfcData.getVfcInvariantResourceUUID());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        logger.info("No resourceUUID found for given invariantREsourceUUID:"
                                + currCldsVfData.getVfInvariantResourceUUID());
                    }
                }
            }
        }
    }

    private List<CldsVfcData> getVfcDataListFromVfResponse(String vfResponse) throws GeneralSecurityException {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode vfResponseNode;
        try {
            vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
        } catch (IOException e) {
            logger.error("Exception when decoding the JSON list of CldsVfcData", e);
            return new ArrayList<>();
        }
        ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
        List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
        if (vfcArrayNode != null) {
            for (JsonNode vfcjsonNode : vfcArrayNode) {
                ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
                TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
                if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
                    handleVFCtypeNode(currVfcNode, cldsVfcDataList);
                } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
                    handleCVFCtypeNode(currVfcNode, cldsVfcDataList);
                }
            }
        }
        return cldsVfcDataList;
    }

    private void handleVFCtypeNode(ObjectNode currVfcNode, List<CldsVfcData> cldsVfcDataList) {
        CldsVfcData currCldsVfcData = new CldsVfcData();
        TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
        TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID");
        currCldsVfcData.setVfcName(vfcResourceName.textValue());
        currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue());
        cldsVfcDataList.add(currCldsVfcData);
    }

    private void handleCVFCtypeNode(ObjectNode currVfcNode, List<CldsVfcData> cldsVfcDataList) {
        handleVFCtypeNode(currVfcNode, cldsVfcDataList);
        cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue()));
    }

    private List<CldsVfcData> getVFCfromCVFC(String resourceUUID) {
        String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
        List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
        if (resourceUUID != null) {
            String vfcResourceUUIDUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUUID + SDC_METADATA_URL_PREFIX;
            try {
                String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl);
                ObjectMapper mapper = new ObjectMapper();
                ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
                ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
                if (vfcArrayNode != null) {
                    for (JsonNode vfcjsonNode : vfcArrayNode) {
                        ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
                        TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
                        if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
                            handleVFCtypeNode(currVfcNode, cldsVfcDataList);
                        }
                    }
                }
            } catch (IOException e) {
                logger.error("Exception during JSON analyzis", e);
            }
        }
        return cldsVfcDataList;
    }

    private String removeUnwantedBracesFromString(String id) {
        return (id != null) ? id.replaceAll("\"", "") : "";
    }

    private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException {
        List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode vfcResponseNode;
        try {
            vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
        } catch (IOException e) {
            logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e);
            return cldsAlarmConditionList;
        }
        ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
        if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
            for (int index = 0; index < artifactsArrayNode.size(); index++) {
                ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
                TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
                if (artifactUrlNode != null) {
                    String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
                    cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
                    logger.info(responsesFromArtifactUrl);
                }
            }
        }
        return cldsAlarmConditionList;
    }

    private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) {
        try {
            List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
            Reader alarmReader = new StringReader(allAlarmCondsValues);
            Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
            if (records != null) {
                Iterator<CSVRecord> it = records.iterator();
                if (it.hasNext()) {
                    it.next();
                }
                it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
            }
            return cldsAlarmConditionList;
        } catch (IOException e) {
            logger.error("Exception when attempting to parse the CSV containing the alarm", e);
            return new ArrayList<>();
        }
    }

    // Method to get the artifact for any particular VF
    private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) throws GeneralSecurityException {
        List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode vfResponseNode;
        try {
            vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
        } catch (IOException e) {
            logger.error("Exception when decoding the JSON list of CldsVfKPIData", e);
            return cldsVfKPIDataList;
        }
        ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");
        if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
            for (int index = 0; index < artifactsArrayNode.size(); index++) {
                ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
                TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
                TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName");
                String artifactName = "";
                if (artifactNameNode != null) {
                    artifactName = artifactNameNode.textValue();
                    artifactName = artifactName.substring(artifactName.lastIndexOf('.') + 1);
                }
                if (artifactUrlNode != null && "csv".equalsIgnoreCase(artifactName)) {
                    String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
                    cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl));
                    logger.info(responsesFromArtifactUrl);
                }
            }
        }
        return cldsVfKPIDataList;
    }

    private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) {
        if (record.size() < 6) {
            logger.debug("invalid csv field path Record,total columns less than 6: " + record);
            return null;
        }
        if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
                || StringUtils.isBlank(record.get(5))) {
            logger.debug("Invalid csv field path Record,one of column is having blank value : " + record);
            return null;
        }
        CldsVfKPIData cldsVfKPIData = new CldsVfKPIData();
        cldsVfKPIData.setNfNamingCode(record.get(0).trim());
        cldsVfKPIData.setNfNamingValue(record.get(1).trim());
        cldsVfKPIData.setFieldPath(record.get(2).trim());
        cldsVfKPIData.setFieldPathValue(record.get(3).trim());
        cldsVfKPIData.setThresholdName(record.get(4).trim());
        cldsVfKPIData.setThresholdValue(record.get(5).trim());
        return cldsVfKPIData;
    }

    // Method to get the artifactURL Data and set the CldsVfKPIData node
    private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) {
        try {
            List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
            Reader alarmReader = new StringReader(allFieldPathValues);
            Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
            if (records != null) {
                for (CSVRecord record : records) {
                    CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record);
                    if (kpiData != null) {
                        cldsVfKPIDataList.add(kpiData);
                    }
                }
            }
            return cldsVfKPIDataList;
        } catch (IOException e) {
            logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e);
            return new ArrayList<>();
        }
    }

    private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
        if (record == null) {
            return;
        }
        if (record.size() < 5) {
            logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
            return;
        }
        if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
                || StringUtils.isBlank(record.get(4))) {
            logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
            return;
        }
        CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
        cldsAlarmCondition.setEventSourceType(record.get(1));
        cldsAlarmCondition.setEventName(record.get(2));
        cldsAlarmCondition.setAlarmConditionKey(record.get(3));
        cldsAlarmCondition.setSeverity(record.get(4));
        cldsAlarmConditionList.add(cldsAlarmCondition);
    }

    // Get the responses for the current artifact from the artifacts URL.
    private String getResponsesFromArtifactUrl(String artifactsUrl) {
        String hostUrl = refProp.getStringValue("sdc.hostUrl");
        String artifactsUrlReworked = artifactsUrl.replaceAll("\"", "");
        String artifactUrl = hostUrl + artifactsUrlReworked;
        logger.info("value of artifactURl:" + artifactUrl);
        String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl);
        logger.info("value of artifactResponse:" + currArtifactResponse);
        return currArtifactResponse;
    }

    /**
     * Service to services/resources/artifacts from sdc.Pass alarmConditions as
     * true to get alarm conditons from artifact url and else it is false
     * 
     * @param url
     *            The URL to trigger
     * @return The String containing the payload
     */
    public String getCldsServicesOrResourcesBasedOnURL(String url) {
        Date startTime = new Date();
        try {
            LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL");
            String urlReworked = removeUnwantedBracesFromString(url);
            URL urlObj = new URL(urlReworked);
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
            String basicAuth = getSdcBasicAuth();
            conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP);
            conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
            conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
            conn.setRequestMethod("GET");
            int responseCode = conn.getResponseCode();
            logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode);
            try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
                String response = IOUtils.toString(in);
                LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName());
                return response;
            }
        } catch (IOException e) {
            LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
            LoggingUtils.setErrorContext("900", "Get sdc resources error");
            logger.error("Exception occurred during query to SDC", e);
            return "";
        } catch (DecoderException e) {
            LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
            LoggingUtils.setErrorContext("900", "Get sdc resources error");
            logger.error("Exception when attempting to decode the Hex string", e);
            throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e);
        } catch (GeneralSecurityException e) {
            LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
            LoggingUtils.setErrorContext("900", "Get sdc resources error");
            logger.error("Exception when attempting to decrypt the encrypted password", e);
            throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e);
        } finally {
            LoggingUtils.setTimeContext(startTime, new Date());
            metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed");
        }
    }

    /**
     * To create properties object by using cldsServicedata.
     *
     * @param globalProps
     * @param cldsServiceData
     * @return
     */
    public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) {
        String totalPropsStr;
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode globalPropsJson;
        if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
            // Objectnode to save all byservice, byvf , byvfc and byalarm nodes
            ObjectNode byIdObjectNode = mapper.createObjectNode();
            // To create vf ResourceUUID node with serviceInvariantUUID
            ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper,
                    cldsServiceData);
            byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf);
            // To create byVf and vfcResourceNode with vfResourceUUID
            ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs());
            byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid);
            // To create byKpi
            ObjectNode kpiObjectNode = mapper.createObjectNode();
            if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
                for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
                    if (currCldsVfData != null) {
                        createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList());
                    }
                }
            }
            byIdObjectNode.putPOJO("byKpi", kpiObjectNode);
            // To create byVfc and alarmCondition with vfcResourceUUID
            ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();
            if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
                for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
                    if (currCldsVfData != null) {
                        createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode,
                                currCldsVfData.getCldsVfcs());
                    }
                }
            }
            byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode);
            // To create byAlarmCondition with alarmConditionKey
            List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
                    "alarmCondition");
            ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
            byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
            // To create byAlertDescription with AlertDescription
            List<CldsAlarmCondition> allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
                    "alertDescription");
            ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(mapper, allAlertDescriptions);
            byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert);
            globalPropsJson = decodeGlobalProp(globalProps, mapper);
            globalPropsJson.putPOJO("shared", byIdObjectNode);
            logger.info("Global properties JSON created with SDC info:" + globalPropsJson);
        } else {
            /**
             * to create json with total properties when no serviceUUID passed
             */
            globalPropsJson = decodeGlobalProp(globalProps, mapper);
        }
        totalPropsStr = globalPropsJson.toString();
        return totalPropsStr;
    }

    private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) {
        try {
            return (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
        } catch (IOException e) {
            logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e);
            return mapper.createObjectNode();
        }
    }

    /**
     * Method to get alarm conditions/alert description from Service Data.
     * 
     * @param cldsServiceData
     *            CldsServiceData the Service Data to analyze
     * @param eventName
     *            The String event name that will be used to filter the alarm
     *            list
     * @return The list of CldsAlarmCondition for the event name specified
     */
    public List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData,
            String eventName) {
        List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
        if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null
                && !cldsServiceData.getCldsVfs().isEmpty()) {
            for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
                alarmCondList.addAll(getAllAlarmConditionsFromCldsVfData(currCldsVfData, eventName));
            }
        }
        return alarmCondList;
    }

    /**
     * Method to get alarm conditions/alert description from VF Data.
     * 
     * @param currCldsVfData
     *            The Vf Data to analyze
     * @param eventName
     *            The String event name that will be used to filter the alarm
     *            list
     * @return The list of CldsAlarmCondition for the event name specified
     */
    private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfData(CldsVfData currCldsVfData, String eventName) {
        List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
        if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
            for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
                alarmCondList.addAll(getAllAlarmConditionsFromCldsVfcData(currCldsVfcData, eventName));
            }
        }
        return alarmCondList;
    }

    /**
     * Method to get alarm conditions/alert description from VFC Data.
     * 
     * @param currCldsVfcData
     *            The VfC Data to analyze
     * @param eventName
     *            The String event name that will be used to filter the alarm
     *            list
     * @return The list of CldsAlarmCondition for the event name specified
     */
    private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfcData(CldsVfcData currCldsVfcData,
            String eventName) {
        List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
        if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null
                && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
            for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
                if (currCldsAlarmCondition != null
                        && currCldsAlarmCondition.getEventName().equalsIgnoreCase(eventName)) {
                    alarmCondList.add(currCldsAlarmCondition);
                }
            }
        }
        return alarmCondList;
    }

    private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper,
            List<CldsAlarmCondition> cldsAlarmCondList) {
        ObjectNode alarmCondKeyNode = mapper.createObjectNode();
        if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) {
            for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
                if (currCldsAlarmCondition != null) {
                    ObjectNode alarmCondNode = mapper.createObjectNode();
                    alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
                    alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
                    alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
                }
            }
        } else {
            ObjectNode alarmCondNode = mapper.createObjectNode();
            alarmCondNode.put("eventSourceType", "");
            alarmCondNode.put("eventSeverity", "");
            alarmCondKeyNode.putPOJO("", alarmCondNode);
        }
        return alarmCondKeyNode;
    }

    private ObjectNode createVfObjectNodeByServiceInvariantUuid(ObjectMapper mapper, CldsServiceData cldsServiceData) {
        ObjectNode invariantUuidObjectNode = mapper.createObjectNode();
        ObjectNode vfObjectNode = mapper.createObjectNode();
        ObjectNode vfUuidNode = mapper.createObjectNode();
        List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
        if (cldsVfsList != null && !cldsVfsList.isEmpty()) {
            for (CldsVfData currCldsVfData : cldsVfsList) {
                if (currCldsVfData != null) {
                    vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
                }
            }
        } else {
            vfUuidNode.put("", "");
        }
        vfObjectNode.putPOJO("vf", vfUuidNode);
        invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
        return invariantUuidObjectNode;
    }

    private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode,
            List<CldsVfKPIData> cldsVfKpiDataList) {
        if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) {
            for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) {
                if (currCldsVfKpiData != null) {
                    ObjectNode thresholdNameObjectNode = mapper.createObjectNode();
                    ObjectNode fieldPathObjectNode = mapper.createObjectNode();
                    ObjectNode nfNamingCodeNode = mapper.createObjectNode();
                    fieldPathObjectNode.put(currCldsVfKpiData.getFieldPathValue(),
                            currCldsVfKpiData.getFieldPathValue());
                    nfNamingCodeNode.put(currCldsVfKpiData.getNfNamingValue(), currCldsVfKpiData.getNfNamingValue());
                    thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode);
                    thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode);
                    vfResourceUuidObjectNode.putPOJO(currCldsVfKpiData.getThresholdValue(), thresholdNameObjectNode);
                }
            }
        }
    }

    private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUuidObjectNode,
            List<CldsVfcData> cldsVfcDataList) {
        ObjectNode vfcObjectNode = mapper.createObjectNode();
        ObjectNode alarmCondNode = mapper.createObjectNode();
        ObjectNode alertDescNode = mapper.createObjectNode();
        if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) {
            for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
                if (currCldsVfcData != null) {
                    if (currCldsVfcData.getCldsAlarmConditions() != null
                            && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
                        for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
                            if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) {
                                alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
                                        currCldsAlarmCondition.getAlarmConditionKey());
                            } else {
                                alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
                                        currCldsAlarmCondition.getAlarmConditionKey());
                            }
                        }
                    }
                    vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
                    vfcObjectNode.putPOJO("alertDescription", alertDescNode);
                    vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode);
                }
            }
        } else {
            alarmCondNode.put("", "");
            vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
            alertDescNode.put("", "");
            vfcObjectNode.putPOJO("alertDescription", alarmCondNode);
            vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode);
        }
    }

    /**
     * Method to create vfc and kpi nodes inside vf node
     * 
     * @param mapper
     * @param cldsVfDataList
     * @return
     */
    private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
        ObjectNode vfUuidObjectNode = mapper.createObjectNode();
        if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
            for (CldsVfData currCldsVfData : cldsVfDataList) {
                if (currCldsVfData != null) {
                    ObjectNode vfObjectNode = mapper.createObjectNode();
                    ObjectNode vfcUuidNode = mapper.createObjectNode();
                    ObjectNode kpiObjectNode = mapper.createObjectNode();
                    if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
                        for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
                            if (currCldsVfcData.getCldsAlarmConditions() != null
                                    && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
                                vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),
                                        currCldsVfcData.getVfcName());
                            }
                        }
                    } else {
                        vfcUuidNode.put("", "");
                    }
                    if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) {
                        for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) {
                            kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(),
                                    currCldsVfKPIData.getThresholdValue());
                        }
                    } else {
                        kpiObjectNode.put("", "");
                    }
                    vfObjectNode.putPOJO("vfc", vfcUuidNode);
                    vfObjectNode.putPOJO("kpi", kpiObjectNode);
                    vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode);
                }
            }
        } else {
            ObjectNode vfcUuidNode = mapper.createObjectNode();
            vfcUuidNode.put("", "");
            ObjectNode vfcObjectNode = mapper.createObjectNode();
            vfcObjectNode.putPOJO("vfc", vfcUuidNode);
            vfUuidObjectNode.putPOJO("", vfcObjectNode);
        }
        return vfUuidObjectNode;
    }

    /**
     * This method searches the equivalent artifact UUID for a specific
     * artifactName in a SdcServiceDetail.
     * 
     * @param cldsSdcServiceDetail
     *            The SdcServiceDetail that will be analyzed
     * @param artifactName
     *            The artifact name that will be searched
     * @return The artifact UUID found
     */
    public String getArtifactIdIfArtifactAlreadyExists(SdcServiceDetail cldsSdcServiceDetail, String artifactName) {
        String artifactUuid = null;
        boolean artifactExists = false;
        if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null
                && !cldsSdcServiceDetail.getResources().isEmpty()) {
            for (SdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) {
                if (artifactExists) {
                    break;
                }
                if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null
                        && !currCldsSdcResource.getArtifacts().isEmpty()) {
                    for (SdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) {
                        if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null
                                && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
                            artifactUuid = currCldsSdcArtifact.getArtifactUUID();
                            artifactExists = true;
                            break;
                        }
                    }
                }
            }
        }
        return artifactUuid;
    }

    // To get all sdc VF/VFC Resources basic info.
    private List<SdcResourceBasicInfo> getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) {
        String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
        String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType;
        String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl);
        return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources));
    }

    private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid,
            List<SdcResourceBasicInfo> resourceInfoList) {
        String resourceUuid = null;
        if (resourceInfoList != null && !resourceInfoList.isEmpty()) {
            for (SdcResourceBasicInfo currResource : resourceInfoList) {
                if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
                        && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) {
                    resourceUuid = currResource.getUuid();
                    break;
                }
            }
        }
        return resourceUuid;
    }

    // Method to get service invariant uuid from model properties.
    private String getServiceInvariantUuidFromProps(ModelProperties props) {
        String invariantUuid = "";
        Global globalProps = props.getGlobal();
        if (globalProps != null && globalProps.getService() != null) {
            invariantUuid = globalProps.getService();
        }
        return invariantUuid;
    }

    /**
     * This method upload the BluePrint to SDC.
     * 
     * @param prop
     *            The Clds model Properties
     * @param userid
     *            The user id for SDC
     * @param sdcReqUrlsList
     *            The list of SDC URL to try
     * @param formattedSdcReq
     *            The blueprint to upload
     * @param formattedSdcLocationReq
     *            THe location Blueprint to upload
     * @param artifactName
     *            The artifact name from where we can get the Artifact UUID
     * @param locationArtifactName
     *            The location artifact name from where we can get the Artifact
     *            UUID
     * @throws GeneralSecurityException
     *             In case of issues with the decryting the encrypted password
     * @throws DecoderException
     *             In case of issues with the decoding of the Hex String
     */
    public void uploadToSdc(ModelProperties prop, String userid, List<String> sdcReqUrlsList, String formattedSdcReq,
            String formattedSdcLocationReq, String artifactName, String locationArtifactName)
            throws GeneralSecurityException, DecoderException {
        logger.info("userid=" + userid);
        String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop);
        if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {
            for (String url : sdcReqUrlsList) {
                if (url != null) {
                    String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
                    logger.info("ServiceUUID used before upload in url:" + originalServiceUuid);
                    String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid);
                    SdcServiceDetail cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
                    String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
                            artifactName);
                    // Upload artifacts to sdc
                    String updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
                    String responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcReq);
                    logger.info("value of sdc Response of uploading to sdc :" + responseStr);
                    String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
                    if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) {
                        url = url.replace(originalServiceUuid, updatedServiceUuid);
                    }
                    logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid);
                    sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid);
                    cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
                    uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
                            locationArtifactName);
                    // To send location information also to sdc
                    updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
                    responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcLocationReq);
                    logger.info("value of sdc Response of uploading location to sdc :" + responseStr);
                }
            }
        }
    }
}