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