Removed redundant code
[clamp.git] / src / main / java / org / onap / clamp / clds / client / SdcCatalogServices.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import com.fasterxml.jackson.databind.node.TextNode;
33
34 import java.io.BufferedReader;
35 import java.io.DataOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.Reader;
40 import java.io.StringReader;
41 import java.net.HttpURLConnection;
42 import java.net.URL;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Iterator;
47 import java.util.List;
48
49 import javax.ws.rs.BadRequestException;
50
51 import org.apache.commons.csv.CSVFormat;
52 import org.apache.commons.csv.CSVRecord;
53 import org.apache.commons.lang3.StringUtils;
54 import org.onap.clamp.clds.client.req.SdcReq;
55 import org.onap.clamp.clds.exception.SdcCommunicationException;
56 import org.onap.clamp.clds.model.CldsAlarmCondition;
57 import org.onap.clamp.clds.model.CldsDBServiceCache;
58 import org.onap.clamp.clds.model.CldsSdcArtifact;
59 import org.onap.clamp.clds.model.CldsSdcResource;
60 import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo;
61 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
62 import org.onap.clamp.clds.model.CldsSdcServiceInfo;
63 import org.onap.clamp.clds.model.CldsServiceData;
64 import org.onap.clamp.clds.model.CldsVfData;
65 import org.onap.clamp.clds.model.CldsVfKPIData;
66 import org.onap.clamp.clds.model.CldsVfcData;
67 import org.onap.clamp.clds.model.prop.Global;
68 import org.onap.clamp.clds.model.prop.ModelProperties;
69 import org.onap.clamp.clds.model.refprop.RefProp;
70 import org.onap.clamp.clds.util.LoggingUtils;
71 import org.springframework.beans.factory.annotation.Autowired;
72
73 public class SdcCatalogServices {
74     protected static final EELFLogger logger            = EELFManager.getInstance().getLogger(SdcCatalogServices.class);
75     protected static final EELFLogger metricsLogger     = EELFManager.getInstance().getMetricsLogger();
76
77     private static final String       RESOURCE_VF_TYPE  = "VF";
78     private static final String       RESOURCE_VFC_TYPE = "VFC";
79
80     @Autowired
81     private RefProp                   refProp;
82
83     /**
84      * This method get the SDC services Information with the corresponding
85      * Service UUID.
86      * 
87      * @param uuid
88      *            The service UUID
89      * @return A Json String with all the service list
90      */
91     public String getSdcServicesInformation(String uuid) {
92         Date startTime = new Date();
93         String baseUrl = refProp.getStringValue("sdc.serviceUrl");
94         String basicAuth = SdcReq.getSdcBasicAuth(refProp);
95         LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation");
96
97         try {
98             String url = baseUrl;
99             if (uuid != null) {
100                 url = baseUrl + "/" + uuid + "/metadata";
101             }
102             URL urlObj = new URL(url);
103
104             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
105
106             conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
107             conn.setRequestProperty("Authorization", basicAuth);
108             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
109             conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
110             conn.setRequestMethod("GET");
111
112             String resp = getResponse(conn);
113             if (resp != null) {
114                 logger.info(resp);
115                 // metrics log
116                 LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName());
117                 return resp;
118             }
119         } catch (IOException e) {
120             LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName());
121             LoggingUtils.setErrorContext("900", "Get sdc services error");
122             logger.error("not able to get any service information from sdc for uuid:" + uuid, e);
123         } finally {
124             LoggingUtils.setTimeContext(startTime, new Date());
125             metricsLogger.info("getSdcServicesInformation complete");
126         }
127
128         return "";
129     }
130
131     /**
132      * To remove duplicate serviceUUIDs from sdc services List.
133      *
134      * @param rawCldsSdcServiceList
135      *            A list of CldsSdcServiceInfo
136      * @return A list of CldsSdcServiceInfo without duplicate service UUID
137      */
138     public List<CldsSdcServiceInfo> removeDuplicateServices(List<CldsSdcServiceInfo> rawCldsSdcServiceList) {
139         List<CldsSdcServiceInfo> cldsSdcServiceInfoList = null;
140         if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) {
141             // sort list
142             Collections.sort(rawCldsSdcServiceList);
143             // and then take only the services with the max version (last in the
144             // list with the same name)
145             cldsSdcServiceInfoList = new ArrayList<>();
146             for (int i = 1; i < rawCldsSdcServiceList.size(); i++) {
147                 // compare name with previous - if not equal, then keep the
148                 // previous (it's the last with that name)
149                 CldsSdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1);
150                 if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) {
151                     cldsSdcServiceInfoList.add(prev);
152                 }
153             }
154             // add the last in the list
155             cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1));
156         }
157         return cldsSdcServiceInfoList;
158     }
159
160     /**
161      * To remove duplicate serviceUUIDs from sdc resources List.
162      *
163      * @param rawCldsSdcResourceList
164      * @return
165      */
166     public List<CldsSdcResource> removeDuplicateSdcResourceInstances(List<CldsSdcResource> rawCldsSdcResourceList) {
167         List<CldsSdcResource> cldsSdcResourceList = null;
168         if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) {
169             // sort list
170             Collections.sort(rawCldsSdcResourceList);
171             // and then take only the resources with the max version (last in
172             // the list with the same name)
173             cldsSdcResourceList = new ArrayList<>();
174             for (int i = 1; i < rawCldsSdcResourceList.size(); i++) {
175                 // compare name with previous - if not equal, then keep the
176                 // previous (it's the last with that name)
177                 CldsSdcResource prev = rawCldsSdcResourceList.get(i - 1);
178                 if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
179                     cldsSdcResourceList.add(prev);
180                 }
181             }
182             // add the last in the list
183             cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1));
184         }
185         return cldsSdcResourceList;
186     }
187
188     /**
189      * To remove duplicate basic resources with same resourceUUIDs.
190      *
191      * @param rawCldsSdcResourceListBasicList
192      * @return
193      */
194     public List<CldsSdcResourceBasicInfo> removeDuplicateSdcResourceBasicInfo(
195             List<CldsSdcResourceBasicInfo> rawCldsSdcResourceListBasicList) {
196         List<CldsSdcResourceBasicInfo> cldsSdcResourceBasicInfoList = null;
197         if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) {
198             // sort list
199             Collections.sort(rawCldsSdcResourceListBasicList);
200             // and then take only the resources with the max version (last in
201             // the list with the same name)
202             cldsSdcResourceBasicInfoList = new ArrayList<>();
203             for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) {
204                 // compare name with previous - if not equal, then keep the
205                 // previous (it's the last with that name)
206                 CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1);
207                 if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
208                     cldsSdcResourceBasicInfoList.add(prev);
209                 }
210             }
211             // add the last in the list
212             cldsSdcResourceBasicInfoList
213                     .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1));
214         }
215         return cldsSdcResourceBasicInfoList;
216     }
217
218     /**
219      * To get ServiceUUID by using serviceInvariantUUID.
220      *
221      * @param invariantId
222      *            The invariant ID
223      * @return The service UUID
224      */
225     public String getServiceUuidFromServiceInvariantId(String invariantId) {
226         String serviceUuid = "";
227         String responseStr = getSdcServicesInformation(null);
228         List<CldsSdcServiceInfo> rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr);
229         List<CldsSdcServiceInfo> cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList);
230         if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) {
231             for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) {
232                 if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null
233                         && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) {
234                     serviceUuid = currCldsSdcServiceInfo.getUuid();
235                     break;
236                 }
237             }
238         }
239         return serviceUuid;
240     }
241
242     /**
243      * To get CldsAsdsServiceInfo class by parsing json string.
244      *
245      * @param jsonStr
246      *            The Json string that must be decoded
247      * @return The list of CldsSdcServiceInfo, if there is a failure it return
248      *         an empty list
249      */
250     public List<CldsSdcServiceInfo> getCldsSdcServicesListFromJson(String jsonStr) {
251         ObjectMapper objectMapper = new ObjectMapper();
252         if (StringUtils.isBlank(jsonStr)) {
253             return new ArrayList<>();
254         }
255         try {
256             return objectMapper.readValue(jsonStr,
257                     objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class));
258         } catch (IOException e) {
259             logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e);
260             return new ArrayList<>();
261         }
262     }
263
264     /**
265      * To get List of CldsSdcResourceBasicInfo class by parsing json string.
266      *
267      * @param jsonStr
268      *            The JSOn string that must be decoded
269      * @return The list of CldsSdcResourceBasicInfo, an empty list in case of
270      *         issues
271      */
272     public List<CldsSdcResourceBasicInfo> getAllSdcResourcesListFromJson(String jsonStr) {
273         ObjectMapper objectMapper = new ObjectMapper();
274         if (StringUtils.isBlank(jsonStr)) {
275             return new ArrayList<>();
276         }
277
278         try {
279             return objectMapper.readValue(jsonStr,
280                     objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class));
281         } catch (IOException e) {
282             logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e);
283             return new ArrayList<>();
284         }
285     }
286
287     /**
288      * To get CldsAsdsResource class by parsing json string.
289      *
290      * @param jsonStr
291      * @return
292      * @throws IOException
293      */
294     public CldsSdcResource getCldsSdcResourceFromJson(String jsonStr) throws IOException {
295         ObjectMapper objectMapper = new ObjectMapper();
296         return objectMapper.readValue(jsonStr, CldsSdcResource.class);
297     }
298
299     /**
300      * To get CldsSdcServiceDetail by parsing json string.
301      *
302      * @param jsonStr
303      * @return
304      */
305     public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) {
306         ObjectMapper objectMapper = new ObjectMapper();
307         try {
308             return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class);
309         } catch (IOException e) {
310             logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e);
311             return null;
312         }
313     }
314
315     /**
316      * To upload artifact to sdc based on serviceUUID and resource name on url.
317      *
318      * @param prop
319      * @param userid
320      * @param url
321      * @param formatedSdcReq
322      * @return
323      */
324     public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatedSdcReq) {
325         // Verify whether it is triggered by Validation Test button from UI
326         if (prop.isTest()) {
327             return "sdc artifact upload not executed for test action";
328         }
329         try {
330             logger.info("userid=" + userid);
331             String md5Text = SdcReq.calculateMD5ByString(formatedSdcReq);
332             byte[] postData = SdcReq.stringToByteArray(formatedSdcReq);
333             int postDataLength = postData.length;
334             HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text);
335             try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
336                 wr.write(postData);
337             }
338             boolean requestFailed = true;
339             int responseCode = conn.getResponseCode();
340             logger.info("responseCode=" + responseCode);
341             if (responseCode == 200) {
342                 requestFailed = false;
343             }
344
345             String responseStr = getResponse(conn);
346             if (responseStr != null && requestFailed) {
347                 logger.error("requestFailed - responseStr=" + responseStr);
348                 throw new BadRequestException(responseStr);
349             }
350             return responseStr;
351         } catch (IOException e) {
352             logger.error("Exception when attempting to communicate with SDC", e);
353             throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e);
354         }
355
356     }
357
358     private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) {
359         try {
360             logger.info("userid=" + userid);
361             String basicAuth = SdcReq.getSdcBasicAuth(refProp);
362             String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
363             URL urlObj = new URL(url);
364             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
365             conn.setDoOutput(true);
366             conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId);
367             conn.setRequestProperty("Authorization", basicAuth);
368             conn.setRequestProperty("Content-Type", "application/json");
369             conn.setRequestProperty("Content-MD5", md5Text);
370             conn.setRequestProperty("USER_ID", userid);
371             conn.setRequestMethod("POST");
372             conn.setRequestProperty("charset", "utf-8");
373             conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
374             conn.setUseCaches(false);
375             conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
376             return conn;
377         } catch (IOException e) {
378             logger.error("Exception when attempting to open connection with SDC", e);
379             throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e);
380         }
381     }
382
383     private String getResponse(HttpURLConnection conn) {
384         try (InputStream is = getInputStream(conn)) {
385             if (is != null) {
386                 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
387                     StringBuilder response = new StringBuilder();
388                     String inputLine;
389                     while ((inputLine = in.readLine()) != null) {
390                         response.append(inputLine);
391                     }
392                     return response.toString();
393                 }
394             } else {
395                 return null;
396             }
397         } catch (IOException e) {
398             logger.error("Exception when attempting to open SDC response", e);
399             throw new SdcCommunicationException("Exception when attempting to open SDC response", e);
400         }
401     }
402
403     private InputStream getInputStream(HttpURLConnection conn) {
404         try {
405             InputStream inStream = conn.getErrorStream();
406             if (inStream == null) {
407                 inStream = conn.getInputStream();
408             }
409             return inStream;
410         } catch (IOException e) {
411             logger.error("Exception when attempting to open SDC error stream", e);
412             throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e);
413         }
414     }
415
416     public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) {
417         try {
418             CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache();
419             cldsDbServiceCache.setCldsDataInstream(cldsServiceData);
420             cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID());
421             cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID());
422             return cldsDbServiceCache;
423         } catch (IOException e) {
424             logger.error("Exception when getting service in cache", e);
425             throw new SdcCommunicationException("Exception when getting service in cache", e);
426         }
427     }
428
429     public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) {
430         boolean expired = false;
431         if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
432             String cachedServiceUuid = cldsServiceData.getServiceUUID();
433             String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID());
434             String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS");
435             if ((!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null
436                     && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) {
437                 expired = true;
438             }
439         } else {
440             expired = true;
441         }
442         return expired;
443     }
444
445     public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) {
446         String url = refProp.getStringValue("sdc.serviceUrl");
447         String catalogUrl = refProp.getStringValue("sdc.catalog.url");
448         String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid);
449         String serviceDetailUrl = url + "/" + serviceUuid + "/metadata";
450         String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false);
451         ObjectMapper objectMapper = new ObjectMapper();
452         CldsServiceData cldsServiceData = new CldsServiceData();
453         if (responseStr != null) {
454             CldsSdcServiceDetail cldsSdcServiceDetail;
455             try {
456                 cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class);
457             } catch (IOException e) {
458                 logger.error("Exception when decoding the CldsServiceData JSON from SDC", e);
459                 throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e);
460             }
461             cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid());
462             cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID());
463
464             // To remove duplicate resources from serviceDetail and add valid
465             // vfs to service
466             if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {
467                 List<CldsSdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(
468                         cldsSdcServiceDetail.getResources());
469                 if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) {
470                     List<CldsVfData> cldsVfDataList = new ArrayList<>();
471                     for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) {
472                         if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null
473                                 && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) {
474                             CldsVfData currCldsVfData = new CldsVfData();
475                             currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());
476                             currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());
477                             cldsVfDataList.add(currCldsVfData);
478                         }
479                     }
480                     cldsServiceData.setCldsVfs(cldsVfDataList);
481                     // For each vf in the list , add all vfc's
482                     getAllVfcForVfList(cldsVfDataList, catalogUrl);
483                     logger.info("value of cldsServiceData:" + cldsServiceData);
484                     logger.info("value of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
485                 }
486             }
487         }
488         return cldsServiceData;
489     }
490
491     private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl) {
492         // todo : refact this..
493         if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
494             List<CldsSdcResourceBasicInfo> allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(
495                     RESOURCE_VF_TYPE);
496             List<CldsSdcResourceBasicInfo> allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType(
497                     RESOURCE_VFC_TYPE);
498             for (CldsVfData currCldsVfData : cldsVfDataList) {
499                 if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
500                     String resourceUuid = getResourceUuidFromResourceInvariantUuid(
501                             currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
502                     if (resourceUuid != null) {
503                         String vfResourceUuidUrl = catalogUrl + "resources" + "/" + resourceUuid + "/metadata";
504                         String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl, false);
505                         if (vfResponse != null) {
506                             // Below 2 line are to get the KPI(field path) data
507                             // associated with the VF's
508                             List<CldsVfKPIData> cldsVfKPIDataList = getFieldPathFromVF(vfResponse);
509                             currCldsVfData.setCldsKPIList(cldsVfKPIDataList);
510
511                             List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);
512                             if (vfcDataListFromVfResponse != null) {
513                                 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
514                                 if (!vfcDataListFromVfResponse.isEmpty()) {
515                                     // To get artifacts for every VFC and get
516                                     // alarm conditions from artifact
517                                     for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
518                                         if (currCldsVfcData != null
519                                                 && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
520                                             String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid(
521                                                     currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
522                                             if (resourceVfcUuid != null) {
523                                                 String vfcResourceUuidUrl = catalogUrl + "resources" + "/"
524                                                         + resourceVfcUuid + "/metadata";
525                                                 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(
526                                                         vfcResourceUuidUrl, false);
527                                                 if (vfcResponse != null) {
528                                                     List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(
529                                                             vfcResponse);
530                                                     currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
531                                                 }
532                                             } else {
533                                                 logger.info("No resourceVFC UUID found for given invariantID:"
534                                                         + currCldsVfcData.getVfcInvariantResourceUUID());
535                                             }
536                                         }
537                                     }
538                                 }
539                             }
540                         }
541                     } else {
542                         logger.info("No resourceUUID found for given invariantREsourceUUID:"
543                                 + currCldsVfData.getVfInvariantResourceUUID());
544                     }
545                 }
546             }
547         }
548     }
549
550     private List<CldsVfcData> getVfcDataListFromVfResponse(String vfResponse) {
551         ObjectMapper mapper = new ObjectMapper();
552         ObjectNode vfResponseNode;
553         try {
554             vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
555         } catch (IOException e) {
556             logger.error("Exception when decoding the JSON list of CldsVfcData", e);
557             return new ArrayList<>();
558         }
559         ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
560         List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
561         if (vfcArrayNode != null) {
562             for (JsonNode vfcjsonNode : vfcArrayNode) {
563                 CldsVfcData currCldsVfcData = new CldsVfcData();
564                 ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
565                 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
566                 if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
567                     TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
568                     TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID");
569                     currCldsVfcData.setVfcName(vfcResourceName.textValue());
570                     currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue());
571                     cldsVfcDataList.add(currCldsVfcData);
572                 } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
573                     cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue()));
574                 }
575             }
576         }
577         return cldsVfcDataList;
578     }
579
580     private List<CldsVfcData> getVFCfromCVFC(String resourceUUID) {
581         String catalogUrl = refProp.getStringValue("sdc.catalog.url");
582         List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
583
584         if (resourceUUID != null) {
585             String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata";
586             try {
587                 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl, false);
588                 ObjectMapper mapper = new ObjectMapper();
589                 ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
590                 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
591
592                 if (vfcArrayNode != null) {
593                     for (JsonNode vfcjsonNode : vfcArrayNode) {
594                         CldsVfcData currCldsVfcData = new CldsVfcData();
595                         ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
596                         TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
597                         if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
598                             TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
599                             TextNode vfcInvariantResourceUUID = (TextNode) currVfcNode.get("resourceInvariantUUID");
600                             currCldsVfcData.setVfcName(vfcResourceName.textValue());
601                             currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUUID.textValue());
602                             cldsVfcDataList.add(currCldsVfcData);
603                         }
604                     }
605                 }
606             } catch (IOException e) {
607                 logger.error("Exception during JSON analyzis", e);
608             }
609         }
610         return cldsVfcDataList;
611     }
612
613     private String removeUnwantedBracesFromString(String id) {
614         return (id != null) ? id.replaceAll("\"", "") : "";
615     }
616
617     private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) {
618         List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
619         ObjectMapper mapper = new ObjectMapper();
620         ObjectNode vfcResponseNode;
621         try {
622             vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
623         } catch (IOException e) {
624             logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e);
625             return cldsAlarmConditionList;
626         }
627         ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
628
629         if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
630             for (int index = 0; index < artifactsArrayNode.size(); index++) {
631                 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
632                 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
633                 if (artifactUrlNode != null) {
634                     String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
635                     cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
636                     logger.info(responsesFromArtifactUrl);
637                 }
638             }
639         }
640         return cldsAlarmConditionList;
641     }
642
643     private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) {
644         try {
645             List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
646             Reader alarmReader = new StringReader(allAlarmCondsValues);
647             Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
648             if (records != null) {
649                 Iterator<CSVRecord> it = records.iterator();
650                 if (it.hasNext()) {
651                     it.next();
652                 }
653                 it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
654             }
655             return cldsAlarmConditionList;
656         } catch (IOException e) {
657             logger.error("Exception when attempting to parse the CSV containing the alarm", e);
658             return new ArrayList<>();
659         }
660     }
661
662     // Method to get the artifact for any particular VF
663     private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) {
664         List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
665         ObjectMapper mapper = new ObjectMapper();
666         ObjectNode vfResponseNode;
667         try {
668             vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
669         } catch (IOException e) {
670             logger.error("Exception when decoding the JSON list of CldsVfKPIData", e);
671             return cldsVfKPIDataList;
672         }
673         ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");
674
675         if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
676             for (int index = 0; index < artifactsArrayNode.size(); index++) {
677                 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
678                 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
679                 TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName");
680                 String artifactName = "";
681                 if (artifactNameNode != null) {
682                     artifactName = artifactNameNode.textValue();
683                     artifactName = artifactName.substring(artifactName.lastIndexOf('.') + 1);
684                 }
685                 if (artifactUrlNode != null && "csv".equalsIgnoreCase(artifactName)) {
686                     String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
687                     cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl));
688                     logger.info(responsesFromArtifactUrl);
689                 }
690             }
691         }
692         return cldsVfKPIDataList;
693     }
694
695     private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) {
696         if (record.size() < 6) {
697             logger.debug("invalid csv field path Record,total columns less than 6: " + record);
698             return null;
699         }
700
701         if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
702                 || StringUtils.isBlank(record.get(5))) {
703             logger.debug("Invalid csv field path Record,one of column is having blank value : " + record);
704             return null;
705         }
706
707         CldsVfKPIData cldsVfKPIData = new CldsVfKPIData();
708         cldsVfKPIData.setNfNamingCode(record.get(0).trim());
709         cldsVfKPIData.setNfNamingValue(record.get(1).trim());
710
711         cldsVfKPIData.setFieldPath(record.get(2).trim());
712         cldsVfKPIData.setFieldPathValue(record.get(3).trim());
713
714         cldsVfKPIData.setThresholdName(record.get(4).trim());
715         cldsVfKPIData.setThresholdValue(record.get(5).trim());
716         return cldsVfKPIData;
717
718     }
719
720     // Method to get the artifactURL Data and set the CldsVfKPIData node
721     private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) {
722         try {
723             List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
724             Reader alarmReader = new StringReader(allFieldPathValues);
725             Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
726             if (records != null) {
727                 for (CSVRecord record : records) {
728                     CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record);
729                     if (kpiData != null) {
730                         cldsVfKPIDataList.add(kpiData);
731                     }
732                 }
733             }
734             return cldsVfKPIDataList;
735         } catch (IOException e) {
736             logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e);
737             return new ArrayList<>();
738         }
739     }
740
741     private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
742         if (record == null) {
743             return;
744         }
745         if (record.size() < 5) {
746             logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
747             return;
748         }
749         if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
750                 || StringUtils.isBlank(record.get(4))) {
751             logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
752             return;
753         }
754         CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
755         cldsAlarmCondition.setEventSourceType(record.get(1));
756         cldsAlarmCondition.setEventName(record.get(2));
757         cldsAlarmCondition.setAlarmConditionKey(record.get(3));
758         cldsAlarmCondition.setSeverity(record.get(4));
759         cldsAlarmConditionList.add(cldsAlarmCondition);
760     }
761
762     public String getResponsesFromArtifactUrl(String artifactsUrl) {
763         String hostUrl = refProp.getStringValue("sdc.hostUrl");
764         String artifactsUrlReworked = artifactsUrl.replaceAll("\"", "");
765         String artifactUrl = hostUrl + artifactsUrlReworked;
766         logger.info("value of artifactURl:" + artifactUrl);
767         String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true);
768         logger.info("value of artifactResponse:" + currArtifactResponse);
769         return currArtifactResponse;
770     }
771
772     /**
773      * Service to services/resources/artifacts from sdc.Pass alarmConditions as
774      * true to get alarmconditons from artifact url and else it is false
775      *
776      * @param url
777      * @param alarmConditions
778      * @return
779      */
780     public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) {
781         Date startTime = new Date();
782         try {
783             LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL");
784             String urlReworked = removeUnwantedBracesFromString(url);
785             URL urlObj = new URL(urlReworked);
786
787             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
788             String basicAuth = SdcReq.getSdcBasicAuth(refProp);
789             conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
790             conn.setRequestProperty("Authorization", basicAuth);
791             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
792             conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
793             conn.setRequestMethod("GET");
794
795             int responseCode = conn.getResponseCode();
796             logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode);
797             StringBuilder response;
798             try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
799                 response = new StringBuilder();
800                 String inputLine;
801                 while ((inputLine = in.readLine()) != null) {
802                     if (!inputLine.isEmpty()) {
803                         response.append(inputLine);
804                     }
805                     if (alarmConditions) {
806                         response.append("\n");
807                     }
808                 }
809             }
810             LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName());
811             return response.toString();
812         } catch (IOException e) {
813             LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
814             LoggingUtils.setErrorContext("900", "Get sdc resources error");
815             logger.error("Exception occurred during query to SDC", e);
816             return "";
817         } finally {
818             LoggingUtils.setTimeContext(startTime, new Date());
819             metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed");
820         }
821
822     }
823
824     /**
825      * To create properties object by using cldsServicedata.
826      *
827      * @param globalProps
828      * @param cldsServiceData
829      * @return
830      */
831     public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) {
832         String totalPropsStr;
833         ObjectMapper mapper = new ObjectMapper();
834         ObjectNode globalPropsJson;
835         if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
836
837             // Objectnode to save all byservice, byvf , byvfc and byalarm nodes
838             ObjectNode byIdObjectNode = mapper.createObjectNode();
839
840             // To create vf ResourceUUID node with serviceInvariantUUID
841             ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper,
842                     cldsServiceData);
843             byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf);
844
845             // To create byVf and vfcResourceNode with vfResourceUUID
846             ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs());
847             byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid);
848
849             // To create byKpi
850             ObjectNode kpiObjectNode = mapper.createObjectNode();
851             if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
852                 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
853                     if (currCldsVfData != null) {
854                         createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList());
855                     }
856                 }
857             }
858             byIdObjectNode.putPOJO("byKpi", kpiObjectNode);
859
860             // To create byVfc and alarmCondition with vfcResourceUUID
861             ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();
862             if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
863                 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
864                     if (currCldsVfData != null) {
865                         createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode,
866                                 currCldsVfData.getCldsVfcs());
867                     }
868                 }
869             }
870             byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode);
871
872             // To create byAlarmCondition with alarmConditionKey
873             List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
874                     "alarmCondition");
875             ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
876
877             byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
878
879             // To create byAlertDescription with AlertDescription
880             List<CldsAlarmCondition> allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
881                     "alertDescription");
882             ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(mapper, allAlertDescriptions);
883
884             byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert);
885
886             globalPropsJson = decodeGlobalProp(globalProps, mapper);
887
888             globalPropsJson.putPOJO("shared", byIdObjectNode);
889             logger.info("value of objNode:" + globalPropsJson);
890         } else {
891             /**
892              * to create json with total properties when no serviceUUID passed
893              */
894             globalPropsJson = decodeGlobalProp(globalProps, mapper);
895         }
896         totalPropsStr = globalPropsJson.toString();
897         return totalPropsStr;
898     }
899
900     private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) {
901         try {
902             return (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
903         } catch (IOException e) {
904             logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e);
905             return mapper.createObjectNode();
906         }
907     }
908
909     /**
910      * Method to get alarm conditions/alert description from Service Data.
911      * 
912      * @param cldsServiceData
913      *            CldsServiceData the Service Data to analyze
914      * @param eventName
915      *            The String event name that will be used to filter the alarm
916      *            list
917      * @return The list of CldsAlarmCondition for the event name specified
918      */
919     public List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData,
920             String eventName) {
921         List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
922         if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null
923                 && !cldsServiceData.getCldsVfs().isEmpty()) {
924             for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
925                 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfData(currCldsVfData, eventName));
926             }
927         }
928         return alarmCondList;
929     }
930
931     /**
932      * Method to get alarm conditions/alert description from VF Data.
933      * 
934      * @param currCldsVfData
935      *            The Vf Data to analyze
936      * @param eventName
937      *            The String event name that will be used to filter the alarm
938      *            list
939      * @return The list of CldsAlarmCondition for the event name specified
940      */
941     private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfData(CldsVfData currCldsVfData, String eventName) {
942         List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
943
944         if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
945             for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
946                 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfcData(currCldsVfcData, eventName));
947             }
948         }
949         return alarmCondList;
950     }
951
952     /**
953      * Method to get alarm conditions/alert description from VFC Data.
954      * 
955      * @param currCldsVfcData
956      *            The VfC Data to analyze
957      * @param eventName
958      *            The String event name that will be used to filter the alarm
959      *            list
960      * @return The list of CldsAlarmCondition for the event name specified
961      */
962     private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfcData(CldsVfcData currCldsVfcData,
963             String eventName) {
964         List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
965
966         if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null
967                 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
968             for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
969                 if (currCldsAlarmCondition != null
970                         && currCldsAlarmCondition.getEventName().equalsIgnoreCase(eventName)) {
971                     alarmCondList.add(currCldsAlarmCondition);
972                 }
973             }
974         }
975         return alarmCondList;
976     }
977
978     private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper,
979             List<CldsAlarmCondition> cldsAlarmCondList) {
980         ObjectNode alarmCondKeyNode = mapper.createObjectNode();
981
982         if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) {
983             for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
984                 if (currCldsAlarmCondition != null) {
985                     ObjectNode alarmCondNode = mapper.createObjectNode();
986                     alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
987                     alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
988                     alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
989                 }
990             }
991         } else {
992             ObjectNode alarmCondNode = mapper.createObjectNode();
993             alarmCondNode.put("eventSourceType", "");
994             alarmCondNode.put("eventSeverity", "");
995             alarmCondKeyNode.putPOJO("", alarmCondNode);
996         }
997         return alarmCondKeyNode;
998     }
999
1000     private ObjectNode createVfObjectNodeByServiceInvariantUuid(ObjectMapper mapper, CldsServiceData cldsServiceData) {
1001         ObjectNode invariantUuidObjectNode = mapper.createObjectNode();
1002         ObjectNode vfObjectNode = mapper.createObjectNode();
1003         ObjectNode vfUuidNode = mapper.createObjectNode();
1004         List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
1005         if (cldsVfsList != null && !cldsVfsList.isEmpty()) {
1006             for (CldsVfData currCldsVfData : cldsVfsList) {
1007                 if (currCldsVfData != null) {
1008                     vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
1009                 }
1010             }
1011         } else {
1012             vfUuidNode.put("", "");
1013         }
1014         vfObjectNode.putPOJO("vf", vfUuidNode);
1015         invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
1016         return invariantUuidObjectNode;
1017     }
1018
1019     private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode,
1020             List<CldsVfKPIData> cldsVfKpiDataList) {
1021         if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) {
1022             for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) {
1023                 if (currCldsVfKpiData != null) {
1024                     ObjectNode thresholdNameObjectNode = mapper.createObjectNode();
1025
1026                     ObjectNode fieldPathObjectNode = mapper.createObjectNode();
1027                     ObjectNode nfNamingCodeNode = mapper.createObjectNode();
1028
1029                     fieldPathObjectNode.put(currCldsVfKpiData.getFieldPathValue(),
1030                             currCldsVfKpiData.getFieldPathValue());
1031                     nfNamingCodeNode.put(currCldsVfKpiData.getNfNamingValue(), currCldsVfKpiData.getNfNamingValue());
1032
1033                     thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode);
1034                     thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode);
1035
1036                     vfResourceUuidObjectNode.putPOJO(currCldsVfKpiData.getThresholdValue(), thresholdNameObjectNode);
1037                 }
1038             }
1039         }
1040     }
1041
1042     private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUuidObjectNode,
1043             List<CldsVfcData> cldsVfcDataList) {
1044         ObjectNode vfcObjectNode = mapper.createObjectNode();
1045         ObjectNode alarmCondNode = mapper.createObjectNode();
1046         ObjectNode alertDescNode = mapper.createObjectNode();
1047         if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) {
1048             for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
1049                 if (currCldsVfcData != null) {
1050                     if (currCldsVfcData.getCldsAlarmConditions() != null
1051                             && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
1052                         for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
1053                             alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1054                                     currCldsAlarmCondition.getAlarmConditionKey());
1055                             if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) {
1056                                 alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1057                                         currCldsAlarmCondition.getAlarmConditionKey());
1058                             } else {
1059                                 alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1060                                         currCldsAlarmCondition.getAlarmConditionKey());
1061                             }
1062                         }
1063                     }
1064
1065                     vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
1066                     vfcObjectNode.putPOJO("alertDescription", alertDescNode);
1067                     vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode);
1068                 }
1069             }
1070         } else {
1071             alarmCondNode.put("", "");
1072             vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
1073             alertDescNode.put("", "");
1074             vfcObjectNode.putPOJO("alertDescription", alarmCondNode);
1075             vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode);
1076         }
1077     }
1078
1079     /**
1080      * Method to create vfc and kpi nodes inside vf node
1081      * 
1082      * @param mapper
1083      * @param cldsVfDataList
1084      * @return
1085      */
1086     private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
1087         ObjectNode vfUuidObjectNode = mapper.createObjectNode();
1088
1089         if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
1090             for (CldsVfData currCldsVfData : cldsVfDataList) {
1091                 if (currCldsVfData != null) {
1092                     ObjectNode vfObjectNode = mapper.createObjectNode();
1093                     ObjectNode vfcUuidNode = mapper.createObjectNode();
1094                     ObjectNode kpiObjectNode = mapper.createObjectNode();
1095                     if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
1096                         for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
1097                             vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),
1098                                     currCldsVfcData.getVfcName());
1099                         }
1100                     } else {
1101                         vfcUuidNode.put("", "");
1102                     }
1103                     if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) {
1104                         for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) {
1105                             kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(),
1106                                     currCldsVfKPIData.getThresholdValue());
1107                         }
1108                     } else {
1109                         kpiObjectNode.put("", "");
1110                     }
1111                     vfObjectNode.putPOJO("vfc", vfcUuidNode);
1112                     vfObjectNode.putPOJO("kpi", kpiObjectNode);
1113                     vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode);
1114                 }
1115             }
1116         } else {
1117             ObjectNode vfcUuidNode = mapper.createObjectNode();
1118             vfcUuidNode.put("", "");
1119             ObjectNode vfcObjectNode = mapper.createObjectNode();
1120             vfcObjectNode.putPOJO("vfc", vfcUuidNode);
1121             vfUuidObjectNode.putPOJO("", vfcObjectNode);
1122         }
1123         return vfUuidObjectNode;
1124     }
1125
1126     /**
1127      * This method searches the equivalent artifact UUID for a specific
1128      * artifactName in a SdcServiceDetail.
1129      * 
1130      * @param cldsSdcServiceDetail
1131      *            The SdcServiceDetail that will be analyzed
1132      * @param artifactName
1133      *            The artifact name that will be searched
1134      * @return The artifact UUID found
1135      */
1136     public String getArtifactIdIfArtifactAlreadyExists(CldsSdcServiceDetail cldsSdcServiceDetail, String artifactName) {
1137         String artifactUuid = null;
1138         boolean artifactExists = false;
1139         if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null
1140                 && !cldsSdcServiceDetail.getResources().isEmpty()) {
1141             for (CldsSdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) {
1142                 if (artifactExists) {
1143                     break;
1144                 }
1145                 if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null
1146                         && !currCldsSdcResource.getArtifacts().isEmpty()) {
1147                     for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) {
1148                         if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null
1149                                 && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
1150                             artifactUuid = currCldsSdcArtifact.getArtifactUUID();
1151                             artifactExists = true;
1152                             break;
1153                         }
1154                     }
1155                 }
1156             }
1157         }
1158         return artifactUuid;
1159     }
1160
1161     public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid,
1162             String invariantServiceUuid, String artifactName) {
1163         String baseUrl = refProp.getStringValue("sdc.serviceUrl");
1164         String basicAuth = SdcReq.getSdcBasicAuth(refProp);
1165         String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \""
1166                 + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n"
1167                 + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n";
1168         try {
1169             String url = baseUrl;
1170             if (invariantServiceUuid != null) {
1171                 url = dcaeUrl + "/closed-loops";
1172             }
1173             URL urlObj = new URL(url);
1174
1175             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
1176             conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
1177             conn.setRequestProperty("Authorization", basicAuth);
1178             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
1179             conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
1180             conn.setRequestMethod("POST");
1181
1182             byte[] postData = SdcReq.stringToByteArray(postStatusData);
1183             try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
1184                 wr.write(postData);
1185             }
1186
1187             int responseCode = conn.getResponseCode();
1188             logger.info("responseCode=" + responseCode);
1189
1190             String resp = getResponse(conn);
1191             if (resp != null) {
1192                 return resp;
1193             }
1194         } catch (IOException e) {
1195             logger.error("Not able to get any service information from sdc for uuid:" + invariantServiceUuid, e);
1196         }
1197         return "";
1198     }
1199
1200     /**
1201      * To get all sdc VF/VFC Resources basic info.
1202      * 
1203      * @param resourceType
1204      *            The resourceType
1205      * @return The list of CldsSdcResourceBasicInfo
1206      */
1207     private List<CldsSdcResourceBasicInfo> getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) {
1208         String catalogUrl = refProp.getStringValue("sdc.catalog.url");
1209         String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType;
1210         String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false);
1211         return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources));
1212     }
1213
1214     private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid,
1215             List<CldsSdcResourceBasicInfo> resourceInfoList) {
1216         String resourceUuid = null;
1217         if (resourceInfoList != null && !resourceInfoList.isEmpty()) {
1218             for (CldsSdcResourceBasicInfo currResource : resourceInfoList) {
1219                 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
1220                         && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) {
1221                     resourceUuid = currResource.getUuid();
1222                     break;
1223                 }
1224             }
1225         }
1226         return resourceUuid;
1227     }
1228
1229     /**
1230      * Method to get service invariant uuid from model properties.
1231      * 
1232      * @param props
1233      *            The Clds model properties
1234      * @return The Service Id
1235      */
1236     private String getServiceInvariantUuidFromProps(ModelProperties props) {
1237         String invariantUuid = "";
1238         Global globalProps = props.getGlobal();
1239         if (globalProps != null && globalProps.getService() != null) {
1240             invariantUuid = globalProps.getService();
1241         }
1242         return invariantUuid;
1243     }
1244
1245     /**
1246      * This method upload the BluePrint to SDC.
1247      * 
1248      * @param prop
1249      *            The Clds model Properties
1250      * @param userid
1251      *            The user id for SDC
1252      * @param sdcReqUrlsList
1253      *            The list of SDC URL to try
1254      * @param formattedSdcReq
1255      *            The blueprint to upload
1256      * @param formattedSdcLocationReq
1257      *            THe location Blueprint to upload
1258      * @param artifactName
1259      *            The artifact name from where we can get the Artifact UUID
1260      * @param locationArtifactName
1261      *            The location artifact name from where we can get the Artifact
1262      *            UUID
1263      * 
1264      */
1265     public void uploadToSdc(ModelProperties prop, String userid, List<String> sdcReqUrlsList, String formattedSdcReq,
1266             String formattedSdcLocationReq, String artifactName, String locationArtifactName) {
1267         logger.info("userid=" + userid);
1268         String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop);
1269         if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {
1270             for (String url : sdcReqUrlsList) {
1271                 if (url != null) {
1272                     String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
1273                     logger.info("ServiceUUID used before upload in url:" + originalServiceUuid);
1274                     String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid);
1275                     CldsSdcServiceDetail cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation);
1276                     String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
1277                             artifactName);
1278                     // Upload artifacts to sdc
1279                     String updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
1280                     String responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcReq);
1281                     logger.info("value of sdc Response of uploading to sdc :" + responseStr);
1282                     String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
1283                     if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) {
1284                         url = url.replace(originalServiceUuid, updatedServiceUuid);
1285                     }
1286                     logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid);
1287                     sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid);
1288                     cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation);
1289                     uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
1290                             locationArtifactName);
1291                     // To send location information also to sdc
1292                     updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
1293                     responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcLocationReq);
1294                     logger.info("value of sdc Response of uploading location to sdc :" + responseStr);
1295                 }
1296             }
1297         }
1298     }
1299 }