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