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