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