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