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