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