2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.client;
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;
45 import java.net.HttpURLConnection;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import java.util.List;
52 public class SdcCatalogServices {
53 private static final Logger logger = LoggerFactory.getLogger(SdcSendReqDelegate.class);
56 private RefProp refProp;
58 public String getAsdcServicesInformation(String uuid) throws Exception {
59 String baseUrl = refProp.getStringValue("asdc.serviceUrl");
60 String basicAuth = SdcReq.getAsdcBasicAuth(refProp);
64 url = baseUrl + "/" + uuid + "/metadata";
66 URL urlObj = new URL(url);
68 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
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");
75 String resp = getResponse(conn);
77 logger.info(resp.toString());
80 } catch (Exception e) {
81 logger.error("not able to ger any service information from asdc for uuid:" + uuid);
87 * To remove duplicate serviceUUIDs from asdc services List
89 * @param rawCldsAsdcServiceList
92 public List<CldsAsdcServiceInfo> removeDuplicateServices(List<CldsAsdcServiceInfo> rawCldsAsdcServiceList) {
93 List<CldsAsdcServiceInfo> cldsAsdcServiceInfoList = null;
94 if (rawCldsAsdcServiceList != null && rawCldsAsdcServiceList.size() > 0) {
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);
106 // add the last in the list
107 cldsAsdcServiceInfoList.add(rawCldsAsdcServiceList.get(rawCldsAsdcServiceList.size() - 1));
109 return cldsAsdcServiceInfoList;
113 * To remove duplicate serviceUUIDs from asdc resources List
115 * @param rawCldsAsdcResourceList
118 public List<CldsAsdcResource> removeDuplicateAsdcResourceInstances(List<CldsAsdcResource> rawCldsAsdcResourceList) {
119 List<CldsAsdcResource> cldsAsdcResourceList = null;
120 if (rawCldsAsdcResourceList != null && rawCldsAsdcResourceList.size() > 0) {
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);
132 // add the last in the list
133 cldsAsdcResourceList.add(rawCldsAsdcResourceList.get(rawCldsAsdcResourceList.size() - 1));
135 return cldsAsdcResourceList;
140 * To remove duplicate basic resources with same resourceUUIDs
142 * @param rawCldsAsdcResourceListBasicList
145 public List<CldsAsdcResourceBasicInfo> removeDuplicateAsdcResourceBasicInfo(List<CldsAsdcResourceBasicInfo> rawCldsAsdcResourceListBasicList) {
146 List<CldsAsdcResourceBasicInfo> cldsAsdcResourceBasicInfoList = null;
147 if (rawCldsAsdcResourceListBasicList != null && rawCldsAsdcResourceListBasicList.size() > 0) {
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);
159 // add the last in the list
160 cldsAsdcResourceBasicInfoList.add(rawCldsAsdcResourceListBasicList.get(rawCldsAsdcResourceListBasicList.size() - 1));
162 return cldsAsdcResourceBasicInfoList;
166 * To get ServiceUUID by using serviceInvariantUUID
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();
190 * To get CldsAsdsServiceInfo class by parsing json string
194 * @throws JsonParseException
195 * @throws JsonMappingException
196 * @throws IOException
198 public List<CldsAsdcServiceInfo> getCldsAsdcServicesListFromJson(String jsonStr) throws IOException {
199 ObjectMapper objectMapper = new ObjectMapper();
200 if (StringUtils.isBlank(jsonStr)) {
203 return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, CldsAsdcServiceInfo.class));
207 * To get List<CldsAsdcResourceBasicInfo> class by parsing json string
211 * @throws JsonParseException
212 * @throws JsonMappingException
213 * @throws IOException
215 public List<CldsAsdcResourceBasicInfo> getAllAsdcResourcesListFromJson(String jsonStr) throws IOException {
216 ObjectMapper objectMapper = new ObjectMapper();
217 if (StringUtils.isBlank(jsonStr)) {
220 return objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructCollectionType(List.class, CldsAsdcResourceBasicInfo.class));
224 * To get CldsAsdsResource class by parsing json string
228 * @throws JsonParseException
229 * @throws JsonMappingException
230 * @throws IOException
232 public CldsAsdcResource getCldsAsdcResourceFromJson(String jsonStr) throws IOException {
233 ObjectMapper objectMapper = new ObjectMapper();
234 return objectMapper.readValue(jsonStr, CldsAsdcResource.class);
238 * To get CldsAsdcServiceDetail by parsing json string
242 * @throws JsonParseException
243 * @throws JsonMappingException
244 * @throws IOException
246 public CldsAsdcServiceDetail getCldsAsdcServiceDetailFromJson(String jsonStr) throws IOException {
247 ObjectMapper objectMapper = new ObjectMapper();
248 return objectMapper.readValue(jsonStr, CldsAsdcServiceDetail.class);
252 * To upload artifact to asdc based on serviceUUID and resourcename on url
256 * @param formatttedAsdcReq
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())) {
269 boolean requestFailed = true;
270 int responseCode = conn.getResponseCode();
271 logger.info("responseCode=" + responseCode);
272 if (responseCode == 200) {
273 requestFailed = false;
276 String responseStr = getResponse(conn);
277 if (responseStr != null) {
279 logger.error("requestFailed - responseStr=" + responseStr);
280 throw new Exception(responseStr);
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);
305 private String getResponse(HttpURLConnection conn) throws IOException {
306 try (InputStream is = getInputStream(conn)) {
308 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
309 StringBuffer response = new StringBuffer();
311 while ((inputLine = in.readLine()) != null) {
312 response.append(inputLine);
314 return response.toString();
321 private InputStream getInputStream(HttpURLConnection conn) throws IOException {
322 InputStream inStream = conn.getErrorStream();
323 if (inStream == null) {
324 inStream = conn.getInputStream();
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;
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))) {
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());
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);
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());
388 return cldsServiceData;
392 * @param cldsVfDataList
393 * @throws IOException
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);
426 logger.info("No resourceVFC UUID found for given invariantID:" + currCldsVfcData.getVfcInvariantResourceUUID());
434 logger.info("No resourceUUID found for given invariantREsourceUUID:" + currCldsVfData.getVfInvariantResourceUUID());
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);
460 return cldsVfcDataList;
463 private String removeUnwantedBracesFromString(String id) {
464 if (id != null && id.contains("\"")) {
465 id = id.replaceAll("\"", "");
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");
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);
487 return cldsAlarmConditionList;
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();
499 it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
501 return cldsAlarmConditionList;
504 private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
505 if (record == null) {
508 if (record.size() < 5) {
509 logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
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);
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);
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;
534 * Service to services/resources/artifacts from asdc.Pass alarmConditions as true to get alarmconditons from artifact url and else it is false
537 * @param alarmConditions
539 * @throws IOException
541 private String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) throws IOException {
544 url = removeUnwantedBracesFromString(url);
545 URL urlObj = new URL(url);
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");
554 int responseCode = conn.getResponseCode();
555 logger.info("responseCode=" + responseCode);
557 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
558 StringBuffer response = new StringBuffer();
560 while ((inputLine = in.readLine()) != null) {
561 response.append(inputLine);
562 if (alarmConditions) {
563 response.append("\n");
566 responseStr = response.toString();
568 } catch (Exception e) {
569 logger.error("Exception occured :" + e.getMessage());
576 * To create properties object by using cldsServicedata
579 * @param cldsServiceData
581 * @throws IOException
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) {
590 * Objectnode to save all byservice, byvf , byvfc and byalarm nodes
592 ObjectNode byIdObjectNode = mapper.createObjectNode();
594 * To create vf ResourceUUID node with serviceInvariantUUID
597 ObjectNode invariantUUIDObjectNodeWithVF = createVFObjectNodeByServiceInvariantUUID(mapper, cldsServiceData);
598 byIdObjectNode.putPOJO("byService", invariantUUIDObjectNodeWithVF);
601 * To create byVf and vfcResourceNode with vfResourceUUID
603 ObjectNode vfcObjectNodeByVfUUID = createVFCObjectNodeByVfUUID(mapper, cldsServiceData.getCldsVfs());
604 byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUUID);
608 * To create byVfc and alarmCondition with vfcResourceUUID
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());
618 byIdObjectNode.putPOJO("byVfc", vfcResourceUUIDObjectNode);
621 * To create byAlarmCondition with alarmConditionKey
623 List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData);
624 ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
626 byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
628 globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
630 globalPropsJson.putPOJO("shared", byIdObjectNode);
631 logger.info("valuie of objNode:" + globalPropsJson);
634 * to create json with total properties when no serviceUUID passed
636 globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
638 totalPropsStr = globalPropsJson.toString();
639 return totalPropsStr;
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);
659 return alarmCondList;
662 private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, List<CldsAlarmCondition> cldsAlarmCondList) {
663 ObjectNode alarmCondKeyNode = mapper.createObjectNode();
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);
675 ObjectNode alarmCondNode = mapper.createObjectNode();
676 alarmCondNode.put("eventSourceType", "");
677 alarmCondNode.put("eventSeverity", "");
678 alarmCondKeyNode.putPOJO("", alarmCondNode);
680 return alarmCondKeyNode;
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());
695 vfUUIDNode.put("", "");
697 vfObjectNode.putPOJO("vf", vfUUIDNode);
698 invariantUUIDObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
699 return invariantUUIDObjectNode;
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());
713 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
715 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
716 vfcResourceUUIDObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), alarmCondContsObjectNode);
720 alarmCondNode.put("", "");
721 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
722 vfcResourceUUIDObjectNode.putPOJO("", alarmCondContsObjectNode);
726 private ObjectNode createVFCObjectNodeByVfUUID(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
727 ObjectNode vfUUIDObjectNode = mapper.createObjectNode();
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());
739 vfcUUIDNode.put("", "");
741 vfcObjectNode.putPOJO("vfc", vfcUUIDNode);
742 vfUUIDObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode);
746 ObjectNode vfcUUIDNode = mapper.createObjectNode();
747 vfcUUIDNode.put("", "");
748 ObjectNode vfcObjectNode = mapper.createObjectNode();
749 vfcObjectNode.putPOJO("vfc", vfcUUIDNode);
750 vfUUIDObjectNode.putPOJO("", vfcObjectNode);
752 return vfUUIDObjectNode;
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()) {
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;
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" +
789 String url = baseUrl;
790 if (invariantServiceUUID != null) {
791 url = dcaeUrl + "/closed-loops";
793 URL urlObj = new URL(url);
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");
801 byte[] postData = SdcReq.stringToByteArray(postStatusData);
802 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
806 int responseCode = conn.getResponseCode();
807 logger.info("responseCode=" + responseCode);
809 String resp = getResponse(conn);
813 } catch (Exception e) {
814 logger.error("not able to ger any service information from asdc for uuid:" + invariantServiceUUID);
820 * To get all asdc VF/VFC Resources basic info
823 * @throws IOException
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);
834 return allAsdcVFResources;
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();
852 * To get all asdc Resources basic info
855 * @throws IOException
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);