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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
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;
34 import java.io.BufferedReader;
35 import java.io.DataOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.Reader;
40 import java.io.StringReader;
41 import java.net.HttpURLConnection;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Iterator;
47 import java.util.List;
49 import javax.ws.rs.BadRequestException;
51 import org.apache.commons.csv.CSVFormat;
52 import org.apache.commons.csv.CSVRecord;
53 import org.apache.commons.lang3.StringUtils;
54 import org.onap.clamp.clds.client.req.SdcReq;
55 import org.onap.clamp.clds.exception.SdcCommunicationException;
56 import org.onap.clamp.clds.model.CldsAlarmCondition;
57 import org.onap.clamp.clds.model.CldsDBServiceCache;
58 import org.onap.clamp.clds.model.CldsSdcArtifact;
59 import org.onap.clamp.clds.model.CldsSdcResource;
60 import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo;
61 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
62 import org.onap.clamp.clds.model.CldsSdcServiceInfo;
63 import org.onap.clamp.clds.model.CldsServiceData;
64 import org.onap.clamp.clds.model.CldsVfData;
65 import org.onap.clamp.clds.model.CldsVfKPIData;
66 import org.onap.clamp.clds.model.CldsVfcData;
67 import org.onap.clamp.clds.model.prop.Global;
68 import org.onap.clamp.clds.model.prop.ModelProperties;
69 import org.onap.clamp.clds.model.refprop.RefProp;
70 import org.onap.clamp.clds.util.LoggingUtils;
71 import org.springframework.beans.factory.annotation.Autowired;
73 public class SdcCatalogServices {
74 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class);
75 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
77 private static final String RESOURCE_VF_TYPE = "VF";
78 private static final String RESOURCE_VFC_TYPE = "VFC";
81 private RefProp refProp;
84 * This method get the SDC services Information with the corresponding
89 * @return A Json String with all the service list
91 public String getSdcServicesInformation(String uuid) {
92 Date startTime = new Date();
93 String baseUrl = refProp.getStringValue("sdc.serviceUrl");
94 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
95 LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation");
100 url = baseUrl + "/" + uuid + "/metadata";
102 URL urlObj = new URL(url);
104 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
106 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
107 conn.setRequestProperty("Authorization", basicAuth);
108 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
109 conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
110 conn.setRequestMethod("GET");
112 String resp = getResponse(conn);
116 LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName());
119 } catch (IOException e) {
120 LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName());
121 LoggingUtils.setErrorContext("900", "Get sdc services error");
122 logger.error("not able to get any service information from sdc for uuid:" + uuid, e);
124 LoggingUtils.setTimeContext(startTime, new Date());
125 metricsLogger.info("getSdcServicesInformation complete");
132 * To remove duplicate serviceUUIDs from sdc services List.
134 * @param rawCldsSdcServiceList
135 * A list of CldsSdcServiceInfo
136 * @return A list of CldsSdcServiceInfo without duplicate service UUID
138 public List<CldsSdcServiceInfo> removeDuplicateServices(List<CldsSdcServiceInfo> rawCldsSdcServiceList) {
139 List<CldsSdcServiceInfo> cldsSdcServiceInfoList = null;
140 if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) {
142 Collections.sort(rawCldsSdcServiceList);
143 // and then take only the services with the max version (last in the
144 // list with the same name)
145 cldsSdcServiceInfoList = new ArrayList<>();
146 for (int i = 1; i < rawCldsSdcServiceList.size(); i++) {
147 // compare name with previous - if not equal, then keep the
148 // previous (it's the last with that name)
149 CldsSdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1);
150 if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) {
151 cldsSdcServiceInfoList.add(prev);
154 // add the last in the list
155 cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1));
157 return cldsSdcServiceInfoList;
161 * To remove duplicate serviceUUIDs from sdc resources List.
163 * @param rawCldsSdcResourceList
166 public List<CldsSdcResource> removeDuplicateSdcResourceInstances(List<CldsSdcResource> rawCldsSdcResourceList) {
167 List<CldsSdcResource> cldsSdcResourceList = null;
168 if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) {
170 Collections.sort(rawCldsSdcResourceList);
171 // and then take only the resources with the max version (last in
172 // the list with the same name)
173 cldsSdcResourceList = new ArrayList<>();
174 for (int i = 1; i < rawCldsSdcResourceList.size(); i++) {
175 // compare name with previous - if not equal, then keep the
176 // previous (it's the last with that name)
177 CldsSdcResource prev = rawCldsSdcResourceList.get(i - 1);
178 if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
179 cldsSdcResourceList.add(prev);
182 // add the last in the list
183 cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1));
185 return cldsSdcResourceList;
189 * To remove duplicate basic resources with same resourceUUIDs.
191 * @param rawCldsSdcResourceListBasicList
194 public List<CldsSdcResourceBasicInfo> removeDuplicateSdcResourceBasicInfo(
195 List<CldsSdcResourceBasicInfo> rawCldsSdcResourceListBasicList) {
196 List<CldsSdcResourceBasicInfo> cldsSdcResourceBasicInfoList = null;
197 if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) {
199 Collections.sort(rawCldsSdcResourceListBasicList);
200 // and then take only the resources with the max version (last in
201 // the list with the same name)
202 cldsSdcResourceBasicInfoList = new ArrayList<>();
203 for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) {
204 // compare name with previous - if not equal, then keep the
205 // previous (it's the last with that name)
206 CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1);
207 if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
208 cldsSdcResourceBasicInfoList.add(prev);
211 // add the last in the list
212 cldsSdcResourceBasicInfoList
213 .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1));
215 return cldsSdcResourceBasicInfoList;
219 * To get ServiceUUID by using serviceInvariantUUID.
223 * @return The service UUID
225 public String getServiceUuidFromServiceInvariantId(String invariantId) {
226 String serviceUuid = "";
227 String responseStr = getSdcServicesInformation(null);
228 List<CldsSdcServiceInfo> rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr);
229 List<CldsSdcServiceInfo> cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList);
230 if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) {
231 for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) {
232 if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null
233 && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) {
234 serviceUuid = currCldsSdcServiceInfo.getUuid();
243 * To get CldsAsdsServiceInfo class by parsing json string.
246 * The Json string that must be decoded
247 * @return The list of CldsSdcServiceInfo, if there is a failure it return
250 public List<CldsSdcServiceInfo> getCldsSdcServicesListFromJson(String jsonStr) {
251 ObjectMapper objectMapper = new ObjectMapper();
252 if (StringUtils.isBlank(jsonStr)) {
253 return new ArrayList<>();
256 return objectMapper.readValue(jsonStr,
257 objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class));
258 } catch (IOException e) {
259 logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e);
260 return new ArrayList<>();
265 * To get List of CldsSdcResourceBasicInfo class by parsing json string.
268 * The JSOn string that must be decoded
269 * @return The list of CldsSdcResourceBasicInfo, an empty list in case of
272 public List<CldsSdcResourceBasicInfo> getAllSdcResourcesListFromJson(String jsonStr) {
273 ObjectMapper objectMapper = new ObjectMapper();
274 if (StringUtils.isBlank(jsonStr)) {
275 return new ArrayList<>();
279 return objectMapper.readValue(jsonStr,
280 objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class));
281 } catch (IOException e) {
282 logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e);
283 return new ArrayList<>();
288 * To get CldsAsdsResource class by parsing json string.
292 * @throws IOException
294 public CldsSdcResource getCldsSdcResourceFromJson(String jsonStr) throws IOException {
295 ObjectMapper objectMapper = new ObjectMapper();
296 return objectMapper.readValue(jsonStr, CldsSdcResource.class);
300 * To get CldsSdcServiceDetail by parsing json string.
305 public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) {
306 ObjectMapper objectMapper = new ObjectMapper();
308 return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class);
309 } catch (IOException e) {
310 logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e);
316 * To upload artifact to sdc based on serviceUUID and resource name on url.
321 * @param formatedSdcReq
324 public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatedSdcReq) {
325 // Verify whether it is triggered by Validation Test button from UI
327 return "sdc artifact upload not executed for test action";
330 logger.info("userid=" + userid);
331 String md5Text = SdcReq.calculateMD5ByString(formatedSdcReq);
332 byte[] postData = SdcReq.stringToByteArray(formatedSdcReq);
333 int postDataLength = postData.length;
334 HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text);
335 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
338 boolean requestFailed = true;
339 int responseCode = conn.getResponseCode();
340 logger.info("responseCode=" + responseCode);
341 if (responseCode == 200) {
342 requestFailed = false;
345 String responseStr = getResponse(conn);
346 if (responseStr != null && requestFailed) {
347 logger.error("requestFailed - responseStr=" + responseStr);
348 throw new BadRequestException(responseStr);
351 } catch (IOException e) {
352 logger.error("Exception when attempting to communicate with SDC", e);
353 throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e);
358 private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) {
360 logger.info("userid=" + userid);
361 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
362 String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
363 URL urlObj = new URL(url);
364 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
365 conn.setDoOutput(true);
366 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId);
367 conn.setRequestProperty("Authorization", basicAuth);
368 conn.setRequestProperty("Content-Type", "application/json");
369 conn.setRequestProperty("Content-MD5", md5Text);
370 conn.setRequestProperty("USER_ID", userid);
371 conn.setRequestMethod("POST");
372 conn.setRequestProperty("charset", "utf-8");
373 conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
374 conn.setUseCaches(false);
375 conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
377 } catch (IOException e) {
378 logger.error("Exception when attempting to open connection with SDC", e);
379 throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e);
383 private String getResponse(HttpURLConnection conn) {
384 try (InputStream is = getInputStream(conn)) {
386 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
387 StringBuilder response = new StringBuilder();
389 while ((inputLine = in.readLine()) != null) {
390 response.append(inputLine);
392 return response.toString();
397 } catch (IOException e) {
398 logger.error("Exception when attempting to open SDC response", e);
399 throw new SdcCommunicationException("Exception when attempting to open SDC response", e);
403 private InputStream getInputStream(HttpURLConnection conn) {
405 InputStream inStream = conn.getErrorStream();
406 if (inStream == null) {
407 inStream = conn.getInputStream();
410 } catch (IOException e) {
411 logger.error("Exception when attempting to open SDC error stream", e);
412 throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e);
416 public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) {
418 CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache();
419 cldsDbServiceCache.setCldsDataInstream(cldsServiceData);
420 cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID());
421 cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID());
422 return cldsDbServiceCache;
423 } catch (IOException e) {
424 logger.error("Exception when getting service in cache", e);
425 throw new SdcCommunicationException("Exception when getting service in cache", e);
429 public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) {
430 boolean expired = false;
431 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
432 String cachedServiceUuid = cldsServiceData.getServiceUUID();
433 String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID());
434 String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS");
435 if ((!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null
436 && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) {
445 public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) {
446 String url = refProp.getStringValue("sdc.serviceUrl");
447 String catalogUrl = refProp.getStringValue("sdc.catalog.url");
448 String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid);
449 String serviceDetailUrl = url + "/" + serviceUuid + "/metadata";
450 String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false);
451 ObjectMapper objectMapper = new ObjectMapper();
452 CldsServiceData cldsServiceData = new CldsServiceData();
453 if (responseStr != null) {
454 CldsSdcServiceDetail cldsSdcServiceDetail;
456 cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class);
457 } catch (IOException e) {
458 logger.error("Exception when decoding the CldsServiceData JSON from SDC", e);
459 throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e);
461 cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid());
462 cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID());
464 // To remove duplicate resources from serviceDetail and add valid
466 if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {
467 List<CldsSdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(
468 cldsSdcServiceDetail.getResources());
469 if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) {
470 List<CldsVfData> cldsVfDataList = new ArrayList<>();
471 for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) {
472 if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null
473 && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) {
474 CldsVfData currCldsVfData = new CldsVfData();
475 currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());
476 currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());
477 cldsVfDataList.add(currCldsVfData);
480 cldsServiceData.setCldsVfs(cldsVfDataList);
481 // For each vf in the list , add all vfc's
482 getAllVfcForVfList(cldsVfDataList, catalogUrl);
483 logger.info("value of cldsServiceData:" + cldsServiceData);
484 logger.info("value of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
488 return cldsServiceData;
491 private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl) {
492 // todo : refact this..
493 if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
494 List<CldsSdcResourceBasicInfo> allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(
496 List<CldsSdcResourceBasicInfo> allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType(
498 for (CldsVfData currCldsVfData : cldsVfDataList) {
499 if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
500 String resourceUuid = getResourceUuidFromResourceInvariantUuid(
501 currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
502 if (resourceUuid != null) {
503 String vfResourceUuidUrl = catalogUrl + "resources" + "/" + resourceUuid + "/metadata";
504 String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl, false);
505 if (vfResponse != null) {
506 // Below 2 line are to get the KPI(field path) data
507 // associated with the VF's
508 List<CldsVfKPIData> cldsVfKPIDataList = getFieldPathFromVF(vfResponse);
509 currCldsVfData.setCldsKPIList(cldsVfKPIDataList);
511 List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);
512 if (vfcDataListFromVfResponse != null) {
513 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
514 if (!vfcDataListFromVfResponse.isEmpty()) {
515 // To get artifacts for every VFC and get
516 // alarm conditions from artifact
517 for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
518 if (currCldsVfcData != null
519 && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
520 String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid(
521 currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
522 if (resourceVfcUuid != null) {
523 String vfcResourceUuidUrl = catalogUrl + "resources" + "/"
524 + resourceVfcUuid + "/metadata";
525 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(
526 vfcResourceUuidUrl, false);
527 if (vfcResponse != null) {
528 List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(
530 currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
533 logger.info("No resourceVFC UUID found for given invariantID:"
534 + currCldsVfcData.getVfcInvariantResourceUUID());
542 logger.info("No resourceUUID found for given invariantREsourceUUID:"
543 + currCldsVfData.getVfInvariantResourceUUID());
550 private List<CldsVfcData> getVfcDataListFromVfResponse(String vfResponse) {
551 ObjectMapper mapper = new ObjectMapper();
552 ObjectNode vfResponseNode;
554 vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
555 } catch (IOException e) {
556 logger.error("Exception when decoding the JSON list of CldsVfcData", e);
557 return new ArrayList<>();
559 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
560 List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
561 if (vfcArrayNode != null) {
562 for (JsonNode vfcjsonNode : vfcArrayNode) {
563 CldsVfcData currCldsVfcData = new CldsVfcData();
564 ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
565 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
566 if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
567 TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
568 TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID");
569 currCldsVfcData.setVfcName(vfcResourceName.textValue());
570 currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue());
571 cldsVfcDataList.add(currCldsVfcData);
572 } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
573 cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue()));
577 return cldsVfcDataList;
580 private List<CldsVfcData> getVFCfromCVFC(String resourceUUID) {
581 String catalogUrl = refProp.getStringValue("sdc.catalog.url");
582 List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
584 if (resourceUUID != null) {
585 String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata";
587 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl, false);
588 ObjectMapper mapper = new ObjectMapper();
589 ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
590 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
592 if (vfcArrayNode != null) {
593 for (JsonNode vfcjsonNode : vfcArrayNode) {
594 CldsVfcData currCldsVfcData = new CldsVfcData();
595 ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
596 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
597 if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
598 TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
599 TextNode vfcInvariantResourceUUID = (TextNode) currVfcNode.get("resourceInvariantUUID");
600 currCldsVfcData.setVfcName(vfcResourceName.textValue());
601 currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUUID.textValue());
602 cldsVfcDataList.add(currCldsVfcData);
606 } catch (IOException e) {
607 logger.error("Exception during JSON analyzis", e);
610 return cldsVfcDataList;
613 private String removeUnwantedBracesFromString(String id) {
614 return (id != null) ? id.replaceAll("\"", "") : "";
617 private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) {
618 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
619 ObjectMapper mapper = new ObjectMapper();
620 ObjectNode vfcResponseNode;
622 vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
623 } catch (IOException e) {
624 logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e);
625 return cldsAlarmConditionList;
627 ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
629 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
630 for (int index = 0; index < artifactsArrayNode.size(); index++) {
631 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
632 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
633 if (artifactUrlNode != null) {
634 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
635 cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
636 logger.info(responsesFromArtifactUrl);
640 return cldsAlarmConditionList;
643 private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) {
645 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
646 Reader alarmReader = new StringReader(allAlarmCondsValues);
647 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
648 if (records != null) {
649 Iterator<CSVRecord> it = records.iterator();
653 it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
655 return cldsAlarmConditionList;
656 } catch (IOException e) {
657 logger.error("Exception when attempting to parse the CSV containing the alarm", e);
658 return new ArrayList<>();
662 // Method to get the artifact for any particular VF
663 private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) {
664 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
665 ObjectMapper mapper = new ObjectMapper();
666 ObjectNode vfResponseNode;
668 vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
669 } catch (IOException e) {
670 logger.error("Exception when decoding the JSON list of CldsVfKPIData", e);
671 return cldsVfKPIDataList;
673 ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");
675 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
676 for (int index = 0; index < artifactsArrayNode.size(); index++) {
677 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
678 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
679 TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName");
680 String artifactName = "";
681 if (artifactNameNode != null) {
682 artifactName = artifactNameNode.textValue();
683 artifactName = artifactName.substring(artifactName.lastIndexOf('.') + 1);
685 if (artifactUrlNode != null && "csv".equalsIgnoreCase(artifactName)) {
686 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
687 cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl));
688 logger.info(responsesFromArtifactUrl);
692 return cldsVfKPIDataList;
695 private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) {
696 if (record.size() < 6) {
697 logger.debug("invalid csv field path Record,total columns less than 6: " + record);
701 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
702 || StringUtils.isBlank(record.get(5))) {
703 logger.debug("Invalid csv field path Record,one of column is having blank value : " + record);
707 CldsVfKPIData cldsVfKPIData = new CldsVfKPIData();
708 cldsVfKPIData.setNfNamingCode(record.get(0).trim());
709 cldsVfKPIData.setNfNamingValue(record.get(1).trim());
711 cldsVfKPIData.setFieldPath(record.get(2).trim());
712 cldsVfKPIData.setFieldPathValue(record.get(3).trim());
714 cldsVfKPIData.setThresholdName(record.get(4).trim());
715 cldsVfKPIData.setThresholdValue(record.get(5).trim());
716 return cldsVfKPIData;
720 // Method to get the artifactURL Data and set the CldsVfKPIData node
721 private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) {
723 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
724 Reader alarmReader = new StringReader(allFieldPathValues);
725 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
726 if (records != null) {
727 for (CSVRecord record : records) {
728 CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record);
729 if (kpiData != null) {
730 cldsVfKPIDataList.add(kpiData);
734 return cldsVfKPIDataList;
735 } catch (IOException e) {
736 logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e);
737 return new ArrayList<>();
741 private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
742 if (record == null) {
745 if (record.size() < 5) {
746 logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
749 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
750 || StringUtils.isBlank(record.get(4))) {
751 logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
754 CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
755 cldsAlarmCondition.setEventSourceType(record.get(1));
756 cldsAlarmCondition.setEventName(record.get(2));
757 cldsAlarmCondition.setAlarmConditionKey(record.get(3));
758 cldsAlarmCondition.setSeverity(record.get(4));
759 cldsAlarmConditionList.add(cldsAlarmCondition);
762 public String getResponsesFromArtifactUrl(String artifactsUrl) {
763 String hostUrl = refProp.getStringValue("sdc.hostUrl");
764 String artifactsUrlReworked = artifactsUrl.replaceAll("\"", "");
765 String artifactUrl = hostUrl + artifactsUrlReworked;
766 logger.info("value of artifactURl:" + artifactUrl);
767 String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true);
768 logger.info("value of artifactResponse:" + currArtifactResponse);
769 return currArtifactResponse;
773 * Service to services/resources/artifacts from sdc.Pass alarmConditions as
774 * true to get alarmconditons from artifact url and else it is false
777 * @param alarmConditions
780 public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) {
781 Date startTime = new Date();
783 LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL");
784 String urlReworked = removeUnwantedBracesFromString(url);
785 URL urlObj = new URL(urlReworked);
787 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
788 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
789 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
790 conn.setRequestProperty("Authorization", basicAuth);
791 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
792 conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
793 conn.setRequestMethod("GET");
795 int responseCode = conn.getResponseCode();
796 logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode);
797 StringBuilder response;
798 try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
799 response = new StringBuilder();
801 while ((inputLine = in.readLine()) != null) {
802 if (!inputLine.isEmpty()) {
803 response.append(inputLine);
805 if (alarmConditions) {
806 response.append("\n");
810 LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName());
811 return response.toString();
812 } catch (IOException e) {
813 LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
814 LoggingUtils.setErrorContext("900", "Get sdc resources error");
815 logger.error("Exception occurred during query to SDC", e);
818 LoggingUtils.setTimeContext(startTime, new Date());
819 metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed");
825 * To create properties object by using cldsServicedata.
828 * @param cldsServiceData
831 public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) {
832 String totalPropsStr;
833 ObjectMapper mapper = new ObjectMapper();
834 ObjectNode globalPropsJson;
835 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
837 // Objectnode to save all byservice, byvf , byvfc and byalarm nodes
838 ObjectNode byIdObjectNode = mapper.createObjectNode();
840 // To create vf ResourceUUID node with serviceInvariantUUID
841 ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper,
843 byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf);
845 // To create byVf and vfcResourceNode with vfResourceUUID
846 ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs());
847 byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid);
850 ObjectNode kpiObjectNode = mapper.createObjectNode();
851 if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
852 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
853 if (currCldsVfData != null) {
854 createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList());
858 byIdObjectNode.putPOJO("byKpi", kpiObjectNode);
860 // To create byVfc and alarmCondition with vfcResourceUUID
861 ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();
862 if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
863 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
864 if (currCldsVfData != null) {
865 createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode,
866 currCldsVfData.getCldsVfcs());
870 byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode);
872 // To create byAlarmCondition with alarmConditionKey
873 List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
875 ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
877 byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
879 // To create byAlertDescription with AlertDescription
880 List<CldsAlarmCondition> allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
882 ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(mapper, allAlertDescriptions);
884 byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert);
886 globalPropsJson = decodeGlobalProp(globalProps, mapper);
888 globalPropsJson.putPOJO("shared", byIdObjectNode);
889 logger.info("value of objNode:" + globalPropsJson);
892 * to create json with total properties when no serviceUUID passed
894 globalPropsJson = decodeGlobalProp(globalProps, mapper);
896 totalPropsStr = globalPropsJson.toString();
897 return totalPropsStr;
900 private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) {
902 return (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
903 } catch (IOException e) {
904 logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e);
905 return mapper.createObjectNode();
910 * Method to get alarm conditions/alert description from Service Data.
912 * @param cldsServiceData
913 * CldsServiceData the Service Data to analyze
915 * The String event name that will be used to filter the alarm
917 * @return The list of CldsAlarmCondition for the event name specified
919 public List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData,
921 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
922 if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null
923 && !cldsServiceData.getCldsVfs().isEmpty()) {
924 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
925 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfData(currCldsVfData, eventName));
928 return alarmCondList;
932 * Method to get alarm conditions/alert description from VF Data.
934 * @param currCldsVfData
935 * The Vf Data to analyze
937 * The String event name that will be used to filter the alarm
939 * @return The list of CldsAlarmCondition for the event name specified
941 private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfData(CldsVfData currCldsVfData, String eventName) {
942 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
944 if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
945 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
946 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfcData(currCldsVfcData, eventName));
949 return alarmCondList;
953 * Method to get alarm conditions/alert description from VFC Data.
955 * @param currCldsVfcData
956 * The VfC Data to analyze
958 * The String event name that will be used to filter the alarm
960 * @return The list of CldsAlarmCondition for the event name specified
962 private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfcData(CldsVfcData currCldsVfcData,
964 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
966 if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null
967 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
968 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
969 if (currCldsAlarmCondition != null
970 && currCldsAlarmCondition.getEventName().equalsIgnoreCase(eventName)) {
971 alarmCondList.add(currCldsAlarmCondition);
975 return alarmCondList;
978 private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper,
979 List<CldsAlarmCondition> cldsAlarmCondList) {
980 ObjectNode alarmCondKeyNode = mapper.createObjectNode();
982 if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) {
983 for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
984 if (currCldsAlarmCondition != null) {
985 ObjectNode alarmCondNode = mapper.createObjectNode();
986 alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
987 alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
988 alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
992 ObjectNode alarmCondNode = mapper.createObjectNode();
993 alarmCondNode.put("eventSourceType", "");
994 alarmCondNode.put("eventSeverity", "");
995 alarmCondKeyNode.putPOJO("", alarmCondNode);
997 return alarmCondKeyNode;
1000 private ObjectNode createVfObjectNodeByServiceInvariantUuid(ObjectMapper mapper, CldsServiceData cldsServiceData) {
1001 ObjectNode invariantUuidObjectNode = mapper.createObjectNode();
1002 ObjectNode vfObjectNode = mapper.createObjectNode();
1003 ObjectNode vfUuidNode = mapper.createObjectNode();
1004 List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
1005 if (cldsVfsList != null && !cldsVfsList.isEmpty()) {
1006 for (CldsVfData currCldsVfData : cldsVfsList) {
1007 if (currCldsVfData != null) {
1008 vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
1012 vfUuidNode.put("", "");
1014 vfObjectNode.putPOJO("vf", vfUuidNode);
1015 invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
1016 return invariantUuidObjectNode;
1019 private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode,
1020 List<CldsVfKPIData> cldsVfKpiDataList) {
1021 if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) {
1022 for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) {
1023 if (currCldsVfKpiData != null) {
1024 ObjectNode thresholdNameObjectNode = mapper.createObjectNode();
1026 ObjectNode fieldPathObjectNode = mapper.createObjectNode();
1027 ObjectNode nfNamingCodeNode = mapper.createObjectNode();
1029 fieldPathObjectNode.put(currCldsVfKpiData.getFieldPathValue(),
1030 currCldsVfKpiData.getFieldPathValue());
1031 nfNamingCodeNode.put(currCldsVfKpiData.getNfNamingValue(), currCldsVfKpiData.getNfNamingValue());
1033 thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode);
1034 thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode);
1036 vfResourceUuidObjectNode.putPOJO(currCldsVfKpiData.getThresholdValue(), thresholdNameObjectNode);
1042 private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUuidObjectNode,
1043 List<CldsVfcData> cldsVfcDataList) {
1044 ObjectNode vfcObjectNode = mapper.createObjectNode();
1045 ObjectNode alarmCondNode = mapper.createObjectNode();
1046 ObjectNode alertDescNode = mapper.createObjectNode();
1047 if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) {
1048 for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
1049 if (currCldsVfcData != null) {
1050 if (currCldsVfcData.getCldsAlarmConditions() != null
1051 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
1052 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
1053 alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1054 currCldsAlarmCondition.getAlarmConditionKey());
1055 if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) {
1056 alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1057 currCldsAlarmCondition.getAlarmConditionKey());
1059 alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
1060 currCldsAlarmCondition.getAlarmConditionKey());
1065 vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
1066 vfcObjectNode.putPOJO("alertDescription", alertDescNode);
1067 vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode);
1071 alarmCondNode.put("", "");
1072 vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
1073 alertDescNode.put("", "");
1074 vfcObjectNode.putPOJO("alertDescription", alarmCondNode);
1075 vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode);
1080 * Method to create vfc and kpi nodes inside vf node
1083 * @param cldsVfDataList
1086 private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
1087 ObjectNode vfUuidObjectNode = mapper.createObjectNode();
1089 if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
1090 for (CldsVfData currCldsVfData : cldsVfDataList) {
1091 if (currCldsVfData != null) {
1092 ObjectNode vfObjectNode = mapper.createObjectNode();
1093 ObjectNode vfcUuidNode = mapper.createObjectNode();
1094 ObjectNode kpiObjectNode = mapper.createObjectNode();
1095 if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
1096 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
1097 vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),
1098 currCldsVfcData.getVfcName());
1101 vfcUuidNode.put("", "");
1103 if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) {
1104 for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) {
1105 kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(),
1106 currCldsVfKPIData.getThresholdValue());
1109 kpiObjectNode.put("", "");
1111 vfObjectNode.putPOJO("vfc", vfcUuidNode);
1112 vfObjectNode.putPOJO("kpi", kpiObjectNode);
1113 vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode);
1117 ObjectNode vfcUuidNode = mapper.createObjectNode();
1118 vfcUuidNode.put("", "");
1119 ObjectNode vfcObjectNode = mapper.createObjectNode();
1120 vfcObjectNode.putPOJO("vfc", vfcUuidNode);
1121 vfUuidObjectNode.putPOJO("", vfcObjectNode);
1123 return vfUuidObjectNode;
1127 * This method searches the equivalent artifact UUID for a specific
1128 * artifactName in a SdcServiceDetail.
1130 * @param cldsSdcServiceDetail
1131 * The SdcServiceDetail that will be analyzed
1132 * @param artifactName
1133 * The artifact name that will be searched
1134 * @return The artifact UUID found
1136 public String getArtifactIdIfArtifactAlreadyExists(CldsSdcServiceDetail cldsSdcServiceDetail, String artifactName) {
1137 String artifactUuid = null;
1138 boolean artifactExists = false;
1139 if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null
1140 && !cldsSdcServiceDetail.getResources().isEmpty()) {
1141 for (CldsSdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) {
1142 if (artifactExists) {
1145 if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null
1146 && !currCldsSdcResource.getArtifacts().isEmpty()) {
1147 for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) {
1148 if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null
1149 && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
1150 artifactUuid = currCldsSdcArtifact.getArtifactUUID();
1151 artifactExists = true;
1158 return artifactUuid;
1161 public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid,
1162 String invariantServiceUuid, String artifactName) {
1163 String baseUrl = refProp.getStringValue("sdc.serviceUrl");
1164 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
1165 String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \""
1166 + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n"
1167 + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n";
1169 String url = baseUrl;
1170 if (invariantServiceUuid != null) {
1171 url = dcaeUrl + "/closed-loops";
1173 URL urlObj = new URL(url);
1175 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
1176 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
1177 conn.setRequestProperty("Authorization", basicAuth);
1178 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
1179 conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId());
1180 conn.setRequestMethod("POST");
1182 byte[] postData = SdcReq.stringToByteArray(postStatusData);
1183 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
1187 int responseCode = conn.getResponseCode();
1188 logger.info("responseCode=" + responseCode);
1190 String resp = getResponse(conn);
1194 } catch (IOException e) {
1195 logger.error("Not able to get any service information from sdc for uuid:" + invariantServiceUuid, e);
1201 * To get all sdc VF/VFC Resources basic info.
1203 * @param resourceType
1205 * @return The list of CldsSdcResourceBasicInfo
1207 private List<CldsSdcResourceBasicInfo> getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) {
1208 String catalogUrl = refProp.getStringValue("sdc.catalog.url");
1209 String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType;
1210 String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false);
1211 return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources));
1214 private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid,
1215 List<CldsSdcResourceBasicInfo> resourceInfoList) {
1216 String resourceUuid = null;
1217 if (resourceInfoList != null && !resourceInfoList.isEmpty()) {
1218 for (CldsSdcResourceBasicInfo currResource : resourceInfoList) {
1219 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
1220 && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) {
1221 resourceUuid = currResource.getUuid();
1226 return resourceUuid;
1230 * Method to get service invariant uuid from model properties.
1233 * The Clds model properties
1234 * @return The Service Id
1236 private String getServiceInvariantUuidFromProps(ModelProperties props) {
1237 String invariantUuid = "";
1238 Global globalProps = props.getGlobal();
1239 if (globalProps != null && globalProps.getService() != null) {
1240 invariantUuid = globalProps.getService();
1242 return invariantUuid;
1246 * This method upload the BluePrint to SDC.
1249 * The Clds model Properties
1251 * The user id for SDC
1252 * @param sdcReqUrlsList
1253 * The list of SDC URL to try
1254 * @param formattedSdcReq
1255 * The blueprint to upload
1256 * @param formattedSdcLocationReq
1257 * THe location Blueprint to upload
1258 * @param artifactName
1259 * The artifact name from where we can get the Artifact UUID
1260 * @param locationArtifactName
1261 * The location artifact name from where we can get the Artifact
1265 public void uploadToSdc(ModelProperties prop, String userid, List<String> sdcReqUrlsList, String formattedSdcReq,
1266 String formattedSdcLocationReq, String artifactName, String locationArtifactName) {
1267 logger.info("userid=" + userid);
1268 String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop);
1269 if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {
1270 for (String url : sdcReqUrlsList) {
1272 String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
1273 logger.info("ServiceUUID used before upload in url:" + originalServiceUuid);
1274 String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid);
1275 CldsSdcServiceDetail cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation);
1276 String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
1278 // Upload artifacts to sdc
1279 String updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
1280 String responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcReq);
1281 logger.info("value of sdc Response of uploading to sdc :" + responseStr);
1282 String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
1283 if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) {
1284 url = url.replace(originalServiceUuid, updatedServiceUuid);
1286 logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid);
1287 sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid);
1288 cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation);
1289 uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
1290 locationArtifactName);
1291 // To send location information also to sdc
1292 updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
1293 responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcLocationReq);
1294 logger.info("value of sdc Response of uploading location to sdc :" + responseStr);