[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / client / SdcCatalogServices.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import com.fasterxml.jackson.databind.node.TextNode;
33 import org.onap.clamp.clds.client.req.SdcReq;
34 import org.onap.clamp.clds.model.*;
35 import org.onap.clamp.clds.model.prop.ModelProperties;
36 import org.onap.clamp.clds.model.refprop.RefProp;
37 import org.apache.commons.csv.CSVFormat;
38 import org.apache.commons.csv.CSVRecord;
39 import org.apache.commons.lang3.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43
44 import java.io.*;
45 import java.net.HttpURLConnection;
46 import java.net.URL;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import java.util.List;
51
52 public class SdcCatalogServices {
53     private static final Logger logger = LoggerFactory.getLogger(SdcSendReqDelegate.class);
54
55     @Autowired
56     private RefProp refProp;
57
58     public String getAsdcServicesInformation(String uuid) throws Exception {
59         String baseUrl = refProp.getStringValue("asdc.serviceUrl");
60         String basicAuth = SdcReq.getAsdcBasicAuth(refProp);
61         try {
62             String url = baseUrl;
63             if (uuid != null) {
64                 url = baseUrl + "/" + uuid + "/metadata";
65             }
66             URL urlObj = new URL(url);
67
68             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
69
70             conn.setRequestProperty("X-ONAP-InstanceID", "CLAMP-Tool");
71             conn.setRequestProperty("Authorization", basicAuth);
72             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
73             conn.setRequestMethod("GET");
74
75             String resp = getResponse(conn);
76             if (resp != null) {
77                 logger.info(resp.toString());
78                 return resp;
79             }
80         } catch (Exception e) {
81             logger.error("not able to ger any service information from asdc for uuid:" + uuid);
82         }
83         return "";
84     }
85
86     /**
87      * To remove duplicate serviceUUIDs from asdc services List
88      *
89      * @param rawCldsAsdcServiceList
90      * @return
91      */
92     public List<CldsAsdcServiceInfo> removeDuplicateServices(List<CldsAsdcServiceInfo> rawCldsAsdcServiceList) {
93         List<CldsAsdcServiceInfo> cldsAsdcServiceInfoList = null;
94         if (rawCldsAsdcServiceList != null && rawCldsAsdcServiceList.size() > 0) {
95             // sort list
96             Collections.sort(rawCldsAsdcServiceList);
97             // and then take only the services with the max version (last in the list with the same name)
98             cldsAsdcServiceInfoList = new ArrayList<>();
99             for (int i = 1; i < rawCldsAsdcServiceList.size(); i++) {
100                 // compare name with previous - if not equal, then keep the previous (it's the last with that name)
101                 CldsAsdcServiceInfo prev = rawCldsAsdcServiceList.get(i - 1);
102                 if (!rawCldsAsdcServiceList.get(i).getName().equals(prev.getName())) {
103                     cldsAsdcServiceInfoList.add(prev);
104                 }
105             }
106             // add the last in the list
107             cldsAsdcServiceInfoList.add(rawCldsAsdcServiceList.get(rawCldsAsdcServiceList.size() - 1));
108         }
109         return cldsAsdcServiceInfoList;
110     }
111
112     /**
113      * To remove duplicate serviceUUIDs from asdc resources List
114      *
115      * @param rawCldsAsdcResourceList
116      * @return
117      */
118     public List<CldsAsdcResource> removeDuplicateAsdcResourceInstances(List<CldsAsdcResource> rawCldsAsdcResourceList) {
119         List<CldsAsdcResource> cldsAsdcResourceList = null;
120         if (rawCldsAsdcResourceList != null && rawCldsAsdcResourceList.size() > 0) {
121             // sort list
122             Collections.sort(rawCldsAsdcResourceList);
123             // and then take only the resources with the max version (last in the list with the same name)
124             cldsAsdcResourceList = new ArrayList<>();
125             for (int i = 1; i < rawCldsAsdcResourceList.size(); i++) {
126                 // compare name with previous - if not equal, then keep the previous (it's the last with that name)
127                 CldsAsdcResource prev = rawCldsAsdcResourceList.get(i - 1);
128                 if (!rawCldsAsdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
129                     cldsAsdcResourceList.add(prev);
130                 }
131             }
132             // add the last in the list
133             cldsAsdcResourceList.add(rawCldsAsdcResourceList.get(rawCldsAsdcResourceList.size() - 1));
134         }
135         return cldsAsdcResourceList;
136     }
137
138
139     /**
140      * To remove duplicate basic resources with same resourceUUIDs
141      *
142      * @param rawCldsAsdcResourceListBasicList
143      * @return
144      */
145     public List<CldsAsdcResourceBasicInfo> removeDuplicateAsdcResourceBasicInfo(List<CldsAsdcResourceBasicInfo> rawCldsAsdcResourceListBasicList) {
146         List<CldsAsdcResourceBasicInfo> cldsAsdcResourceBasicInfoList = null;
147         if (rawCldsAsdcResourceListBasicList != null && rawCldsAsdcResourceListBasicList.size() > 0) {
148             // sort list
149             Collections.sort(rawCldsAsdcResourceListBasicList);
150             // and then take only the resources with the max version (last in the list with the same name)
151             cldsAsdcResourceBasicInfoList = new ArrayList<>();
152             for (int i = 1; i < rawCldsAsdcResourceListBasicList.size(); i++) {
153                 // compare name with previous - if not equal, then keep the previous (it's the last with that name)
154                 CldsAsdcResourceBasicInfo prev = rawCldsAsdcResourceListBasicList.get(i - 1);
155                 if (!rawCldsAsdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
156                     cldsAsdcResourceBasicInfoList.add(prev);
157                 }
158             }
159             // add the last in the list
160             cldsAsdcResourceBasicInfoList.add(rawCldsAsdcResourceListBasicList.get(rawCldsAsdcResourceListBasicList.size() - 1));
161         }
162         return cldsAsdcResourceBasicInfoList;
163     }
164
165     /**
166      * To get ServiceUUID by using serviceInvariantUUID
167      *
168      * @param invariantID
169      * @return
170      * @throws Exception
171      */
172     public String getServiceUUIDFromServiceInvariantID(String invariantID) throws Exception {
173         String serviceUUID = "";
174         String responseStr = getAsdcServicesInformation(null);
175         List<CldsAsdcServiceInfo> rawCldsAsdcServicesList = getCldsAsdcServicesListFromJson(responseStr);
176         List<CldsAsdcServiceInfo> cldsAsdcServicesList = removeDuplicateServices(rawCldsAsdcServicesList);
177         if (cldsAsdcServicesList != null && cldsAsdcServicesList.size() > 0) {
178             for (CldsAsdcServiceInfo currCldsAsdcServiceInfo : cldsAsdcServicesList) {
179                 if (currCldsAsdcServiceInfo != null && currCldsAsdcServiceInfo.getInvariantUUID() != null
180                         && currCldsAsdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantID)) {
181                     serviceUUID = currCldsAsdcServiceInfo.getUuid();
182                     break;
183                 }
184             }
185         }
186         return serviceUUID;
187     }
188
189     /**
190      * To get CldsAsdsServiceInfo class by parsing json string
191      *
192      * @param jsonStr
193      * @return
194      * @throws JsonParseException
195      * @throws JsonMappingException
196      * @throws IOException
197      */
198     public List<CldsAsdcServiceInfo> getCldsAsdcServicesListFromJson(String jsonStr) throws IOException {
199         ObjectMapper objectMapper = new ObjectMapper();
200         if (StringUtils.isBlank(jsonStr)) {
201             return null;
202         }
203         return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, CldsAsdcServiceInfo.class));
204     }
205
206     /**
207      * To get List<CldsAsdcResourceBasicInfo> class by parsing json string
208      *
209      * @param jsonStr
210      * @return
211      * @throws JsonParseException
212      * @throws JsonMappingException
213      * @throws IOException
214      */
215     public List<CldsAsdcResourceBasicInfo> getAllAsdcResourcesListFromJson(String jsonStr) throws IOException {
216         ObjectMapper objectMapper = new ObjectMapper();
217         if (StringUtils.isBlank(jsonStr)) {
218             return null;
219         }
220         return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, CldsAsdcResourceBasicInfo.class));
221     }
222
223     /**
224      * To get CldsAsdsResource class by parsing json string
225      *
226      * @param jsonStr
227      * @return
228      * @throws JsonParseException
229      * @throws JsonMappingException
230      * @throws IOException
231      */
232     public CldsAsdcResource getCldsAsdcResourceFromJson(String jsonStr) throws IOException {
233         ObjectMapper objectMapper = new ObjectMapper();
234         return objectMapper.readValue(jsonStr, CldsAsdcResource.class);
235     }
236
237     /**
238      * To get CldsAsdcServiceDetail by parsing json string
239      *
240      * @param jsonStr
241      * @return
242      * @throws JsonParseException
243      * @throws JsonMappingException
244      * @throws IOException
245      */
246     public CldsAsdcServiceDetail getCldsAsdcServiceDetailFromJson(String jsonStr) throws IOException {
247         ObjectMapper objectMapper = new ObjectMapper();
248         return objectMapper.readValue(jsonStr, CldsAsdcServiceDetail.class);
249     }
250
251     /**
252      * To upload artifact to asdc based on serviceUUID and resourcename on url
253      * @param prop
254      * @param userid
255      * @param url
256      * @param formatttedAsdcReq
257      * @return
258      * @throws Exception
259      */
260     public String uploadArtifactToAsdc(ModelProperties prop, String userid, String url, String formatttedAsdcReq) throws Exception {
261         logger.info("userid=" + userid);
262         String md5Text = SdcReq.calculateMD5ByString(formatttedAsdcReq);
263         byte[] postData = SdcReq.stringToByteArray(formatttedAsdcReq);
264         int postDataLength = postData.length;
265         HttpURLConnection conn = getAsdcHttpUrlConnection(userid, postDataLength, url, md5Text);
266         try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
267             wr.write(postData);
268         }
269         boolean requestFailed = true;
270         int responseCode = conn.getResponseCode();
271         logger.info("responseCode=" + responseCode);
272         if (responseCode == 200) {
273             requestFailed = false;
274         }
275
276         String responseStr = getResponse(conn);
277         if (responseStr != null) {
278             if (requestFailed) {
279                 logger.error("requestFailed - responseStr=" + responseStr);
280                 throw new Exception(responseStr);
281             }
282         }
283         return responseStr;
284     }
285
286     private HttpURLConnection getAsdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) throws IOException {
287         logger.info("userid=" + userid);
288         String basicAuth = SdcReq.getAsdcBasicAuth(refProp);
289         String asdcXONAPInstanceID = refProp.getStringValue("asdc.asdcX-ONAP-InstanceID");
290         URL urlObj = new URL(url);
291         HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
292         conn.setDoOutput(true);
293         conn.setRequestProperty("X-ONAP-InstanceID", asdcXONAPInstanceID);
294         conn.setRequestProperty("Authorization", basicAuth);
295         conn.setRequestProperty("Content-Type", "application/json");
296         conn.setRequestProperty("Content-MD5", md5Text);
297         conn.setRequestProperty("HTTP_CSP_USERID", userid);
298         conn.setRequestMethod("POST");
299         conn.setRequestProperty("charset", "utf-8");
300         conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
301         conn.setUseCaches(false);
302         return conn;
303     }
304
305     private String getResponse(HttpURLConnection conn) throws IOException {
306         try (InputStream is = getInputStream(conn)) {
307             if (is != null) {
308                 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
309                     StringBuffer response = new StringBuffer();
310                     String inputLine;
311                     while ((inputLine = in.readLine()) != null) {
312                         response.append(inputLine);
313                     }
314                     return response.toString();
315                 }
316             }
317         }
318         return null;
319     }
320
321     private InputStream getInputStream(HttpURLConnection conn) throws IOException {
322         InputStream inStream = conn.getErrorStream();
323         if (inStream == null) {
324             inStream = conn.getInputStream();
325         }
326         return inStream;
327     }
328
329
330     public CldsDBServiceCache getCldsDBServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) throws IOException {
331         CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache();
332         cldsDbServiceCache.setCldsDataInstream(cldsServiceData);
333         cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID());
334         cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID());
335         return cldsDbServiceCache;
336     }
337
338     public boolean isCldsAsdcCacheDataExpired(CldsServiceData cldsServiceData) throws Exception {
339         boolean expired = false;
340         if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
341             String cachedServiceUUID = cldsServiceData.getServiceUUID();
342             String latestServiceUUID = getServiceUUIDFromServiceInvariantID(cldsServiceData.getServiceInvariantUUID());
343             String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS");
344             if ((!cachedServiceUUID.equalsIgnoreCase(latestServiceUUID)) ||
345                     (cldsServiceData.getAgeOfRecord() != null && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) {
346                 expired = true;
347             }
348         } else {
349             expired = true;
350         }
351         return expired;
352     }
353
354     public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUUID) throws Exception {
355         String url = refProp.getStringValue("asdc.serviceUrl");
356         String catalogUrl = refProp.getStringValue("asdc.catalog.url");
357         String serviceUUID = getServiceUUIDFromServiceInvariantID(invariantServiceUUID);
358         String serviceDetailUrl = url + "/" + serviceUUID + "/metadata";
359         String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false);
360         ObjectMapper objectMapper = new ObjectMapper();
361         CldsServiceData cldsServiceData = new CldsServiceData();
362         if (responseStr != null) {
363             CldsAsdcServiceDetail cldsAsdcServiceDetail = objectMapper.readValue(responseStr, CldsAsdcServiceDetail.class);
364             cldsServiceData.setServiceUUID(cldsAsdcServiceDetail.getUuid());
365             cldsServiceData.setServiceInvariantUUID(cldsAsdcServiceDetail.getInvariantUUID());
366
367             // To remove  duplicate  resources from serviceDetail and add valid vfs to service
368             if (cldsAsdcServiceDetail != null && cldsAsdcServiceDetail.getResources() != null) {
369                 List<CldsAsdcResource> cldsAsdcResourceList = removeDuplicateAsdcResourceInstances(cldsAsdcServiceDetail.getResources());
370                 if (cldsAsdcResourceList != null && cldsAsdcResourceList.size() > 0) {
371                     List<CldsVfData> cldsVfDataList = new ArrayList<>();
372                     for (CldsAsdcResource currCldsAsdcResource : cldsAsdcResourceList) {
373                         if (currCldsAsdcResource != null && currCldsAsdcResource.getResoucreType() != null && currCldsAsdcResource.getResoucreType().equalsIgnoreCase("VF")) {
374                             CldsVfData currCldsVfData = new CldsVfData();
375                             currCldsVfData.setVfName(currCldsAsdcResource.getResourceInstanceName());
376                             currCldsVfData.setVfInvariantResourceUUID(currCldsAsdcResource.getResourceInvariantUUID());
377                             cldsVfDataList.add(currCldsVfData);
378                         }
379                     }
380                     cldsServiceData.setCldsVfs(cldsVfDataList);
381                     // For each vf in the list , add all vfc's
382                     getAllVfcForVfList(cldsVfDataList, catalogUrl);
383                     logger.info("value of cldsServiceData:" + cldsServiceData);
384                     logger.info("value of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
385                 }
386             }
387         }
388         return cldsServiceData;
389     }
390
391     /**
392      * @param cldsVfDataList
393      * @throws IOException
394      */
395     private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl) throws IOException {
396         // todo : refact this..
397         if (cldsVfDataList != null && cldsVfDataList.size() > 0) {
398             List<CldsAsdcResourceBasicInfo> allAsdcResources = getAllAsdcResources();
399             String resourceVFType = "VF";
400             List<CldsAsdcResourceBasicInfo> allVfResources = getAllAsdcVForVFCResourcesBasedOnResourceType(resourceVFType, allAsdcResources);
401             String resourceVFCType = "VFC";
402             List<CldsAsdcResourceBasicInfo> allVfcResources = getAllAsdcVForVFCResourcesBasedOnResourceType(resourceVFCType, allAsdcResources);
403             for (CldsVfData currCldsVfData : cldsVfDataList) {
404                 if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
405                     String resourceUUID = getResourceUUIDFromResourceInvariantUUID(currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
406                     if (resourceUUID != null) {
407                         String vfResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata";
408                         String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUUIDUrl, false);
409                         if (vfResponse != null) {
410                             List<CldsVfcData> vfcDataListFromVfResponse = getVFCDataListFromVfResponse(vfResponse);
411                             if (vfcDataListFromVfResponse != null) {
412                                 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
413                                 if (vfcDataListFromVfResponse.size() > 0) {
414                                     // To get artifacts for every VFC and get alarm conditions from artifact
415                                     for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
416                                         if (currCldsVfcData != null && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
417                                             String resourceVFCUUID = getResourceUUIDFromResourceInvariantUUID(currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
418                                             if (resourceVFCUUID != null) {
419                                                 String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceVFCUUID + "/metadata";
420                                                 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl, false);
421                                                 if (vfcResponse != null) {
422                                                     List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(vfcResponse);
423                                                     currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
424                                                 }
425                                             } else {
426                                                 logger.info("No resourceVFC UUID found for given invariantID:" + currCldsVfcData.getVfcInvariantResourceUUID());
427                                             }
428                                         }
429                                     }
430                                 }
431                             }
432                         }
433                     } else {
434                         logger.info("No resourceUUID found for given invariantREsourceUUID:" + currCldsVfData.getVfInvariantResourceUUID());
435                     }
436                 }
437             }
438         }
439     }
440
441     private List<CldsVfcData> getVFCDataListFromVfResponse(String vfResponse) throws IOException {
442         ObjectMapper mapper = new ObjectMapper();
443         ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
444         ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
445         List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
446         if (vfcArrayNode != null && vfcArrayNode.size() > 0) {
447             for (int index = 0; index < vfcArrayNode.size(); index++) {
448                 CldsVfcData currCldsVfcData = new CldsVfcData();
449                 ObjectNode currVfcNode = (ObjectNode) vfcArrayNode.get(index);
450                 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
451                 if (resourceTypeNode != null && resourceTypeNode.textValue().equalsIgnoreCase("VFC")) {
452                     TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
453                     TextNode vfcInvariantResourceUUID = (TextNode) currVfcNode.get("resourceInvariantUUID");
454                     currCldsVfcData.setVfcName(vfcResourceName.textValue());
455                     currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUUID.textValue());
456                     cldsVfcDataList.add(currCldsVfcData);
457                 }
458             }
459         }
460         return cldsVfcDataList;
461     }
462
463     private String removeUnwantedBracesFromString(String id) {
464         if (id != null && id.contains("\"")) {
465             id = id.replaceAll("\"", "");
466         }
467         return id;
468     }
469
470     private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) throws IOException {
471         List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
472         ObjectMapper mapper = new ObjectMapper();
473         ObjectNode vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
474         ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
475
476         if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
477             for (int index = 0; index < artifactsArrayNode.size(); index++) {
478                 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
479                 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
480                 if (artifactUrlNode != null) {
481                     String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
482                     cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
483                     logger.info(responsesFromArtifactUrl);
484                 }
485             }
486         }
487         return cldsAlarmConditionList;
488     }
489
490     private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) throws IOException {
491         List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
492         Reader alarmReader = new StringReader(allAlarmCondsValues);
493         Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
494         if (records != null) {
495             Iterator<CSVRecord> it = records.iterator();
496             if (it.hasNext()) {
497                 it.next();
498             }
499             it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
500         }
501         return cldsAlarmConditionList;
502     }
503
504     private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
505         if (record == null) {
506             return;
507         }
508         if (record.size() < 5) {
509             logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
510             return;
511         }
512         if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3)) || StringUtils.isBlank(record.get(4))) {
513             logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
514             return;
515         }
516         CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
517         cldsAlarmCondition.setEventSourceType(record.get(1));
518         cldsAlarmCondition.setAlarmConditionKey(record.get(3));
519         cldsAlarmCondition.setSeverity(record.get(4));
520         cldsAlarmConditionList.add(cldsAlarmCondition);
521     }
522
523     private String getResponsesFromArtifactUrl(String artifactsUrl) throws IOException {
524         String hostUrl = refProp.getStringValue("asdc.hostUrl");
525         artifactsUrl = artifactsUrl.replaceAll("\"", "");
526         String artifactUrl = hostUrl + artifactsUrl;
527         logger.info("value of artifactURl:" + artifactUrl);
528         String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true);
529         logger.info("value of artifactResponse:" + currArtifactResponse);
530         return currArtifactResponse;
531     }
532
533     /**
534      * Service to services/resources/artifacts from asdc.Pass alarmConditions as true to get alarmconditons from artifact url and else it is false
535      *
536      * @param url
537      * @param alarmConditions
538      * @return
539      * @throws IOException
540      */
541     private String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) throws IOException {
542         String responseStr;
543         try {
544             url = removeUnwantedBracesFromString(url);
545             URL urlObj = new URL(url);
546
547             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
548             String basicAuth = SdcReq.getAsdcBasicAuth(refProp);
549             conn.setRequestProperty("X-ONAP-InstanceID", "CLAMP-Tool");
550             conn.setRequestProperty("Authorization", basicAuth);
551             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
552             conn.setRequestMethod("GET");
553
554             int responseCode = conn.getResponseCode();
555             logger.info("responseCode=" + responseCode);
556
557             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
558             StringBuffer response = new StringBuffer();
559             String inputLine;
560             while ((inputLine = in.readLine()) != null) {
561                 response.append(inputLine);
562                 if (alarmConditions) {
563                     response.append("\n");
564                 }
565             }
566             responseStr = response.toString();
567             in.close();
568         } catch (Exception e) {
569             logger.error("Exception occured :" + e.getMessage());
570             throw e;
571         }
572         return responseStr;
573     }
574
575     /**
576      * To create properties object by using cldsServicedata
577      *
578      * @param globalProps
579      * @param cldsServiceData
580      * @return
581      * @throws IOException
582      */
583     public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) throws IOException {
584         String totalPropsStr;
585         ObjectMapper mapper = new ObjectMapper();
586         ObjectNode globalPropsJson;
587         if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
588
589             /**
590              *  Objectnode to save all byservice, byvf , byvfc and byalarm nodes
591              */
592             ObjectNode byIdObjectNode = mapper.createObjectNode();
593             /**
594              * To create vf ResourceUUID node with serviceInvariantUUID
595              *
596              */
597             ObjectNode invariantUUIDObjectNodeWithVF = createVFObjectNodeByServiceInvariantUUID(mapper, cldsServiceData);
598             byIdObjectNode.putPOJO("byService", invariantUUIDObjectNodeWithVF);
599
600             /**
601              *  To create byVf and vfcResourceNode with vfResourceUUID
602              */
603             ObjectNode vfcObjectNodeByVfUUID = createVFCObjectNodeByVfUUID(mapper, cldsServiceData.getCldsVfs());
604             byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUUID);
605
606
607             /**
608              *  To create byVfc and alarmCondition with vfcResourceUUID
609              */
610             ObjectNode vfcResourceUUIDObjectNode = mapper.createObjectNode();
611             if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {
612                 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
613                     if (currCldsVfData != null) {
614                         createAlarmCondObjectNodeByVfcUUID(mapper, vfcResourceUUIDObjectNode, currCldsVfData.getCldsVfcs());
615                     }
616                 }
617             }
618             byIdObjectNode.putPOJO("byVfc", vfcResourceUUIDObjectNode);
619
620             /**
621              *  To create byAlarmCondition  with alarmConditionKey
622              */
623             List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData);
624             ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
625
626             byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
627
628             globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
629
630             globalPropsJson.putPOJO("shared", byIdObjectNode);
631             logger.info("valuie of objNode:" + globalPropsJson);
632         } else {
633             /**
634              *  to create json with total properties when no serviceUUID passed
635              */
636             globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
637         }
638         totalPropsStr = globalPropsJson.toString();
639         return totalPropsStr;
640     }
641
642     private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData) {
643         List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
644         if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {
645             for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
646                 if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) {
647                     for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
648                         if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null && currCldsVfcData.getCldsAlarmConditions().size() > 0) {
649                             for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
650                                 if (currCldsAlarmCondition != null) {
651                                     alarmCondList.add(currCldsAlarmCondition);
652                                 }
653                             }
654                         }
655                     }
656                 }
657             }
658         }
659         return alarmCondList;
660     }
661
662     private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, List<CldsAlarmCondition> cldsAlarmCondList) {
663         ObjectNode alarmCondKeyNode = mapper.createObjectNode();
664
665         if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) {
666             for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
667                 if (currCldsAlarmCondition != null) {
668                     ObjectNode alarmCondNode = mapper.createObjectNode();
669                     alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
670                     alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
671                     alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
672                 }
673             }
674         } else {
675             ObjectNode alarmCondNode = mapper.createObjectNode();
676             alarmCondNode.put("eventSourceType", "");
677             alarmCondNode.put("eventSeverity", "");
678             alarmCondKeyNode.putPOJO("", alarmCondNode);
679         }
680         return alarmCondKeyNode;
681     }
682
683     private ObjectNode createVFObjectNodeByServiceInvariantUUID(ObjectMapper mapper, CldsServiceData cldsServiceData) {
684         ObjectNode invariantUUIDObjectNode = mapper.createObjectNode();
685         ObjectNode vfObjectNode = mapper.createObjectNode();
686         ObjectNode vfUUIDNode = mapper.createObjectNode();
687         List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
688         if (cldsVfsList != null && cldsVfsList.size() > 0) {
689             for (CldsVfData currCldsVfData : cldsVfsList) {
690                 if (currCldsVfData != null) {
691                     vfUUIDNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
692                 }
693             }
694         } else {
695             vfUUIDNode.put("", "");
696         }
697         vfObjectNode.putPOJO("vf", vfUUIDNode);
698         invariantUUIDObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
699         return invariantUUIDObjectNode;
700     }
701
702     private void createAlarmCondObjectNodeByVfcUUID(ObjectMapper mapper, ObjectNode vfcResourceUUIDObjectNode, List<CldsVfcData> cldsVfcDataList) {
703         ObjectNode alarmCondContsObjectNode = mapper.createObjectNode();
704         ObjectNode alarmCondNode = mapper.createObjectNode();
705         //      alarmCondNode.put("", "");
706         if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) {
707             for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
708                 if (currCldsVfcData != null) {
709                     if (currCldsVfcData.getCldsAlarmConditions() != null && currCldsVfcData.getCldsAlarmConditions().size() > 0) {
710                         for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
711                             alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), currCldsAlarmCondition.getAlarmConditionKey());
712                         }
713                         alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
714                     }
715                     alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
716                     vfcResourceUUIDObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), alarmCondContsObjectNode);
717                 }
718             }
719         } else {
720             alarmCondNode.put("", "");
721             alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
722             vfcResourceUUIDObjectNode.putPOJO("", alarmCondContsObjectNode);
723         }
724     }
725
726     private ObjectNode createVFCObjectNodeByVfUUID(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
727         ObjectNode vfUUIDObjectNode = mapper.createObjectNode();
728
729         if (cldsVfDataList != null && cldsVfDataList.size() > 0) {
730             for (CldsVfData currCldsVfData : cldsVfDataList) {
731                 if (currCldsVfData != null) {
732                     ObjectNode vfcObjectNode = mapper.createObjectNode();
733                     ObjectNode vfcUUIDNode = mapper.createObjectNode();
734                     if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) {
735                         for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
736                             vfcUUIDNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), currCldsVfcData.getVfcName());
737                         }
738                     } else {
739                         vfcUUIDNode.put("", "");
740                     }
741                     vfcObjectNode.putPOJO("vfc", vfcUUIDNode);
742                     vfUUIDObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode);
743                 }
744             }
745         } else {
746             ObjectNode vfcUUIDNode = mapper.createObjectNode();
747             vfcUUIDNode.put("", "");
748             ObjectNode vfcObjectNode = mapper.createObjectNode();
749             vfcObjectNode.putPOJO("vfc", vfcUUIDNode);
750             vfUUIDObjectNode.putPOJO("", vfcObjectNode);
751         }
752         return vfUUIDObjectNode;
753     }
754
755     public String getArtifactIdIfArtifactAlreadyExists(CldsAsdcServiceDetail cldsAsdcServiceDetail, String artifactName) {
756         String artifactUUId = null;
757         boolean artifactxists = false;
758         if (cldsAsdcServiceDetail != null && cldsAsdcServiceDetail.getResources() != null && cldsAsdcServiceDetail.getResources().size() > 0) {
759             for (CldsAsdcResource currCldsAsdcResource : cldsAsdcServiceDetail.getResources()) {
760                 if (artifactxists) {
761                     break;
762                 }
763                 if (currCldsAsdcResource != null && currCldsAsdcResource.getArtifacts() != null && currCldsAsdcResource.getArtifacts().size() > 0) {
764                     for (CldsAsdcArtifact currCldsAsdcArtifact : currCldsAsdcResource.getArtifacts()) {
765                         if (currCldsAsdcArtifact != null && currCldsAsdcArtifact.getArtifactName() != null) {
766                             if (currCldsAsdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
767                                 artifactUUId = currCldsAsdcArtifact.getArtifactUUID();
768                                 artifactxists = true;
769                                 break;
770                             }
771                         }
772                     }
773                 }
774             }
775         }
776         return artifactUUId;
777     }
778
779     public String updateControlLoopStatusToDCAE(String dcaeUrl, String invariantResourceUUID, String invariantServiceUUID, String artifactName) {
780         String baseUrl = refProp.getStringValue("asdc.serviceUrl");
781         String basicAuth = SdcReq.getAsdcBasicAuth(refProp);
782         String postStatusData = "{ \n" +
783                 "\"event\" : \"" + "Created" + "\",\n" +
784                 "\"serviceUUID\" : \"" + invariantServiceUUID + "\",\n" +
785                 "\"resourceUUID\" :\"" + invariantResourceUUID + "\",\n" +
786                 "\"artifactName\" : \"" + artifactName + "\",\n" +
787                 "} \n";
788         try {
789             String url = baseUrl;
790             if (invariantServiceUUID != null) {
791                 url = dcaeUrl + "/closed-loops";
792             }
793             URL urlObj = new URL(url);
794
795             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
796             conn.setRequestProperty("X-ONAP-InstanceID", "CLAMP-Tool");
797             conn.setRequestProperty("Authorization", basicAuth);
798             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
799             conn.setRequestMethod("POST");
800
801             byte[] postData = SdcReq.stringToByteArray(postStatusData);
802             try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
803                 wr.write(postData);
804             }
805
806             int responseCode = conn.getResponseCode();
807             logger.info("responseCode=" + responseCode);
808
809             String resp = getResponse(conn);
810             if (resp != null) {
811                 return resp;
812             }
813         } catch (Exception e) {
814             logger.error("not able to ger any service information from asdc for uuid:" + invariantServiceUUID);
815         }
816         return "";
817     }
818
819     /**
820      * To get all asdc VF/VFC Resources basic info
821      *
822      * @return
823      * @throws IOException
824      */
825     private List<CldsAsdcResourceBasicInfo> getAllAsdcVForVFCResourcesBasedOnResourceType(String resourceType, List<CldsAsdcResourceBasicInfo> allAsdcResources) throws IOException {
826         List<CldsAsdcResourceBasicInfo> allAsdcVFResources = new ArrayList<>();
827         if (allAsdcResources != null && allAsdcResources.size() > 0) {
828             for (CldsAsdcResourceBasicInfo currResource : allAsdcResources) {
829                 if (currResource != null && currResource.getResourceType() != null && currResource.getResourceType().equalsIgnoreCase(resourceType)) {
830                     allAsdcVFResources.add(currResource);
831                 }
832             }
833         }
834         return allAsdcVFResources;
835     }
836
837     private String getResourceUUIDFromResourceInvariantUUID(String resourceInvariantUUID, List<CldsAsdcResourceBasicInfo> resourceInfoList) throws IOException {
838         String resourceUUID = null;
839         if (resourceInfoList != null && resourceInfoList.size() > 0) {
840             for (CldsAsdcResourceBasicInfo currResource : resourceInfoList) {
841                 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
842                         && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUUID)) {
843                     resourceUUID = currResource.getUuid();
844                     break;
845                 }
846             }
847         }
848         return resourceUUID;
849     }
850
851     /**
852      * To get all asdc Resources basic info
853      *
854      * @return
855      * @throws IOException
856      */
857     private List<CldsAsdcResourceBasicInfo> getAllAsdcResources() throws IOException {
858         String catalogUrl = refProp.getStringValue("asdc.catalog.url");
859         String resourceUrl = catalogUrl + "resources";
860         String allAsdcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false);
861         List<CldsAsdcResourceBasicInfo> allAsdcResourceBasicInfo = getAllAsdcResourcesListFromJson(allAsdcResources);
862         return removeDuplicateAsdcResourceBasicInfo(allAsdcResourceBasicInfo);
863     }
864 }