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 java.io.BufferedReader;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32 import java.io.StringReader;
33 import java.net.HttpURLConnection;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Date;
38 import java.util.Iterator;
39 import java.util.List;
41 import org.apache.commons.csv.CSVFormat;
42 import org.apache.commons.csv.CSVRecord;
43 import org.apache.commons.lang3.StringUtils;
44 import org.onap.clamp.clds.client.req.SdcReq;
45 import org.onap.clamp.clds.model.CldsAlarmCondition;
46 import org.onap.clamp.clds.model.CldsDBServiceCache;
47 import org.onap.clamp.clds.model.CldsSdcArtifact;
48 import org.onap.clamp.clds.model.CldsSdcResource;
49 import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo;
50 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
51 import org.onap.clamp.clds.model.CldsSdcServiceInfo;
52 import org.onap.clamp.clds.model.CldsServiceData;
53 import org.onap.clamp.clds.model.CldsVfData;
54 import org.onap.clamp.clds.model.CldsVfKPIData;
55 import org.onap.clamp.clds.model.CldsVfcData;
56 import org.onap.clamp.clds.model.prop.ModelProperties;
57 import org.onap.clamp.clds.model.refprop.RefProp;
58 import org.onap.clamp.clds.util.LoggingUtils;
59 import org.springframework.beans.factory.annotation.Autowired;
61 import com.att.eelf.configuration.EELFLogger;
62 import com.att.eelf.configuration.EELFManager;
63 import com.fasterxml.jackson.core.JsonParseException;
64 import com.fasterxml.jackson.core.JsonProcessingException;
65 import com.fasterxml.jackson.databind.JsonMappingException;
66 import com.fasterxml.jackson.databind.JsonNode;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68 import com.fasterxml.jackson.databind.node.ArrayNode;
69 import com.fasterxml.jackson.databind.node.ObjectNode;
70 import com.fasterxml.jackson.databind.node.TextNode;
72 public class SdcCatalogServices {
73 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class);
74 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
76 private static final String RESOURCE_VF_TYPE = "VF";
77 private static final String RESOURCE_VFC_TYPE = "VFC";
80 private RefProp refProp;
82 public String getSdcServicesInformation(String uuid) throws Exception {
83 Date startTime = new Date();
84 String baseUrl = refProp.getStringValue("sdc.serviceUrl");
85 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
89 url = baseUrl + "/" + uuid + "/metadata";
91 URL urlObj = new URL(url);
93 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
95 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
96 conn.setRequestProperty("Authorization", basicAuth);
97 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
98 conn.setRequestMethod("GET");
100 String resp = getResponse(conn);
102 logger.info(resp.toString());
106 LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName());
108 } catch (Exception e) {
109 LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName());
110 LoggingUtils.setErrorContext("900", "Get sdc services error");
111 logger.error("not able to get any service information from sdc for uuid:" + uuid + " , exception is - " + e.getMessage());
113 LoggingUtils.setTimeContext(startTime, new Date());
114 LoggingUtils.setTargetContext("SDC", "Get Services");
115 metricsLogger.info("Get sdc services information");
121 * To remove duplicate serviceUUIDs from sdc services List
123 * @param rawCldsSdcServiceList
126 public List<CldsSdcServiceInfo> removeDuplicateServices(List<CldsSdcServiceInfo> rawCldsSdcServiceList) {
127 List<CldsSdcServiceInfo> cldsSdcServiceInfoList = null;
128 if (rawCldsSdcServiceList != null && rawCldsSdcServiceList.size() > 0) {
130 Collections.sort(rawCldsSdcServiceList);
131 // and then take only the services with the max version (last in the
132 // list with the same name)
133 cldsSdcServiceInfoList = new ArrayList<>();
134 for (int i = 1; i < rawCldsSdcServiceList.size(); i++) {
135 // compare name with previous - if not equal, then keep the
136 // previous (it's the last with that name)
137 CldsSdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1);
138 if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) {
139 cldsSdcServiceInfoList.add(prev);
142 // add the last in the list
143 cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1));
145 return cldsSdcServiceInfoList;
149 * To remove duplicate serviceUUIDs from sdc resources List
151 * @param rawCldsSdcResourceList
154 public List<CldsSdcResource> removeDuplicateSdcResourceInstances(List<CldsSdcResource> rawCldsSdcResourceList) {
155 List<CldsSdcResource> cldsSdcResourceList = null;
156 if (rawCldsSdcResourceList != null && rawCldsSdcResourceList.size() > 0) {
158 Collections.sort(rawCldsSdcResourceList);
159 // and then take only the resources with the max version (last in
160 // the list with the same name)
161 cldsSdcResourceList = new ArrayList<>();
162 for (int i = 1; i < rawCldsSdcResourceList.size(); i++) {
163 // compare name with previous - if not equal, then keep the
164 // previous (it's the last with that name)
165 CldsSdcResource prev = rawCldsSdcResourceList.get(i - 1);
166 if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
167 cldsSdcResourceList.add(prev);
170 // add the last in the list
171 cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1));
173 return cldsSdcResourceList;
177 * To remove duplicate basic resources with same resourceUUIDs
179 * @param rawCldsSdcResourceListBasicList
182 public List<CldsSdcResourceBasicInfo> removeDuplicateSdcResourceBasicInfo(
183 List<CldsSdcResourceBasicInfo> rawCldsSdcResourceListBasicList) {
184 List<CldsSdcResourceBasicInfo> cldsSdcResourceBasicInfoList = null;
185 if (rawCldsSdcResourceListBasicList != null && rawCldsSdcResourceListBasicList.size() > 0) {
187 Collections.sort(rawCldsSdcResourceListBasicList);
188 // and then take only the resources with the max version (last in
189 // the list with the same name)
190 cldsSdcResourceBasicInfoList = new ArrayList<>();
191 for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) {
192 // compare name with previous - if not equal, then keep the
193 // previous (it's the last with that name)
194 CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1);
195 if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
196 cldsSdcResourceBasicInfoList.add(prev);
199 // add the last in the list
200 cldsSdcResourceBasicInfoList
201 .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1));
203 return cldsSdcResourceBasicInfoList;
207 * To get ServiceUUID by using serviceInvariantUUID
213 public String getServiceUuidFromServiceInvariantId(String invariantId) throws Exception {
214 String serviceUuid = "";
215 String responseStr = getSdcServicesInformation(null);
216 List<CldsSdcServiceInfo> rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr);
217 List<CldsSdcServiceInfo> cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList);
218 if (cldsSdcServicesList != null && cldsSdcServicesList.size() > 0) {
219 for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) {
220 if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null
221 && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) {
222 serviceUuid = currCldsSdcServiceInfo.getUuid();
231 * To get CldsAsdsServiceInfo class by parsing json string
235 * @throws JsonParseException
236 * @throws JsonMappingException
237 * @throws IOException
239 public List<CldsSdcServiceInfo> getCldsSdcServicesListFromJson(String jsonStr) throws IOException {
240 ObjectMapper objectMapper = new ObjectMapper();
241 if (StringUtils.isBlank(jsonStr)) {
244 return objectMapper.readValue(jsonStr,
245 objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class));
249 * To get List<CldsSdcResourceBasicInfo> class by parsing json string
253 * @throws JsonParseException
254 * @throws JsonMappingException
255 * @throws IOException
257 public List<CldsSdcResourceBasicInfo> getAllSdcResourcesListFromJson(String jsonStr) throws IOException {
258 ObjectMapper objectMapper = new ObjectMapper();
259 if (StringUtils.isBlank(jsonStr)) {
262 return objectMapper.readValue(jsonStr,
263 objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class));
267 * To get CldsAsdsResource class by parsing json string
271 * @throws JsonParseException
272 * @throws JsonMappingException
273 * @throws IOException
275 public CldsSdcResource getCldsSdcResourceFromJson(String jsonStr) throws IOException {
276 ObjectMapper objectMapper = new ObjectMapper();
277 return objectMapper.readValue(jsonStr, CldsSdcResource.class);
281 * To get CldsSdcServiceDetail by parsing json string
285 * @throws JsonParseException
286 * @throws JsonMappingException
287 * @throws IOException
289 public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) throws IOException {
290 ObjectMapper objectMapper = new ObjectMapper();
291 return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class);
295 * To upload artifact to sdc based on serviceUUID and resourcename on url
300 * @param formatttedSdcReq
304 public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq)
306 // Verify whether it is triggered by Validation Test button from UI
308 return "sdc artifact upload not executed for test action";
310 logger.info("userid=" + userid);
311 String md5Text = SdcReq.calculateMD5ByString(formatttedSdcReq);
312 byte[] postData = SdcReq.stringToByteArray(formatttedSdcReq);
313 int postDataLength = postData.length;
314 HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text);
315 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
318 boolean requestFailed = true;
319 int responseCode = conn.getResponseCode();
320 logger.info("responseCode=" + responseCode);
321 if (responseCode == 200) {
322 requestFailed = false;
325 String responseStr = getResponse(conn);
326 if (responseStr != null) {
328 logger.error("requestFailed - responseStr=" + responseStr);
329 throw new Exception(responseStr);
335 private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text)
337 logger.info("userid=" + userid);
338 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
339 String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
340 URL urlObj = new URL(url);
341 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
342 conn.setDoOutput(true);
343 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId);
344 conn.setRequestProperty("Authorization", basicAuth);
345 conn.setRequestProperty("Content-Type", "application/json");
346 conn.setRequestProperty("Content-MD5", md5Text);
347 conn.setRequestProperty("USER_ID", userid);
348 conn.setRequestMethod("POST");
349 conn.setRequestProperty("charset", "utf-8");
350 conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
351 conn.setUseCaches(false);
355 private String getResponse(HttpURLConnection conn) throws IOException {
356 try (InputStream is = getInputStream(conn)) {
358 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
359 StringBuffer response = new StringBuffer();
361 while ((inputLine = in.readLine()) != null) {
362 response.append(inputLine);
364 return response.toString();
371 private InputStream getInputStream(HttpURLConnection conn) throws IOException {
372 InputStream inStream = conn.getErrorStream();
373 if (inStream == null) {
374 inStream = conn.getInputStream();
379 public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData)
381 CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache();
382 cldsDbServiceCache.setCldsDataInstream(cldsServiceData);
383 cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID());
384 cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID());
385 return cldsDbServiceCache;
388 public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws Exception {
389 boolean expired = false;
390 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
391 String cachedServiceUuid = cldsServiceData.getServiceUUID();
392 String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID());
393 String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS");
394 if ((!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null
395 && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) {
404 public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) throws Exception {
405 String url = refProp.getStringValue("sdc.serviceUrl");
406 String catalogUrl = refProp.getStringValue("sdc.catalog.url");
407 String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid);
408 String serviceDetailUrl = url + "/" + serviceUuid + "/metadata";
409 String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false);
410 ObjectMapper objectMapper = new ObjectMapper();
411 CldsServiceData cldsServiceData = new CldsServiceData();
412 if (responseStr != null) {
413 CldsSdcServiceDetail cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class);
414 cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid());
415 cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID());
417 // To remove duplicate resources from serviceDetail and add valid
419 if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {
420 List<CldsSdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(
421 cldsSdcServiceDetail.getResources());
422 if (cldsSdcResourceList != null && cldsSdcResourceList.size() > 0) {
423 List<CldsVfData> cldsVfDataList = new ArrayList<>();
424 for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) {
425 if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null
426 && currCldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) {
427 CldsVfData currCldsVfData = new CldsVfData();
428 currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());
429 currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());
430 cldsVfDataList.add(currCldsVfData);
433 cldsServiceData.setCldsVfs(cldsVfDataList);
434 // For each vf in the list , add all vfc's
435 getAllVfcForVfList(cldsVfDataList, catalogUrl);
436 logger.info("value of cldsServiceData:" + cldsServiceData);
437 logger.info("value of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
441 return cldsServiceData;
445 * @param cldsVfDataList
446 * @throws IOException
448 private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl) throws IOException {
449 // todo : refact this..
450 if (cldsVfDataList != null && cldsVfDataList.size() > 0) {
451 List<CldsSdcResourceBasicInfo> allVfResources = getAllSdcVForVFCResourcesBasedOnResourceType(
453 List<CldsSdcResourceBasicInfo> allVfcResources = getAllSdcVForVFCResourcesBasedOnResourceType(
455 for (CldsVfData currCldsVfData : cldsVfDataList) {
456 if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
457 String resourceUuid = getResourceUuidFromResourceInvariantUuid(
458 currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
459 if (resourceUuid != null) {
460 String vfResourceUuidUrl = catalogUrl + "resources" + "/" + resourceUuid + "/metadata";
461 String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl, false);
462 if (vfResponse != null) {
463 // Below 2 line are to get the KPI(field path) data
464 // associated with the VF's
465 List<CldsVfKPIData> cldsVfKPIDataList = getFieldPathFromVF(vfResponse);
466 currCldsVfData.setCldsKPIList(cldsVfKPIDataList);
468 List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);
469 if (vfcDataListFromVfResponse != null) {
470 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
471 if (vfcDataListFromVfResponse.size() > 0) {
472 // To get artifacts for every VFC and get
473 // alarm conditions from artifact
474 for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
475 if (currCldsVfcData != null
476 && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
477 String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid(
478 currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
479 if (resourceVfcUuid != null) {
480 String vfcResourceUuidUrl = catalogUrl + "resources" + "/"
481 + resourceVfcUuid + "/metadata";
482 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(
483 vfcResourceUuidUrl, false);
484 if (vfcResponse != null) {
485 List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(
487 currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
490 logger.info("No resourceVFC UUID found for given invariantID:"
491 + currCldsVfcData.getVfcInvariantResourceUUID());
499 logger.info("No resourceUUID found for given invariantREsourceUUID:"
500 + currCldsVfData.getVfInvariantResourceUUID());
507 private List<CldsVfcData> getVfcDataListFromVfResponse(String vfResponse) throws IOException {
508 ObjectMapper mapper = new ObjectMapper();
509 ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
510 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
511 List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
512 if (vfcArrayNode != null && vfcArrayNode.size() > 0) {
513 for (int index = 0; index < vfcArrayNode.size(); index++) {
514 CldsVfcData currCldsVfcData = new CldsVfcData();
515 ObjectNode currVfcNode = (ObjectNode) vfcArrayNode.get(index);
516 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
517 if (resourceTypeNode != null && resourceTypeNode.textValue().equalsIgnoreCase("VFC")) {
518 TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
519 TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID");
520 currCldsVfcData.setVfcName(vfcResourceName.textValue());
521 currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue());
522 cldsVfcDataList.add(currCldsVfcData);
526 return cldsVfcDataList;
529 private String removeUnwantedBracesFromString(String id) {
530 if (id != null && id.contains("\"")) {
531 id = id.replaceAll("\"", "");
536 private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) throws IOException {
537 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
538 ObjectMapper mapper = new ObjectMapper();
539 ObjectNode vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse);
540 ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
542 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
543 for (int index = 0; index < artifactsArrayNode.size(); index++) {
544 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
545 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
546 if (artifactUrlNode != null) {
547 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
548 cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
549 logger.info(responsesFromArtifactUrl);
553 return cldsAlarmConditionList;
556 private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) throws IOException {
557 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
558 Reader alarmReader = new StringReader(allAlarmCondsValues);
559 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
560 if (records != null) {
561 Iterator<CSVRecord> it = records.iterator();
565 it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
567 return cldsAlarmConditionList;
570 // Method to get the artifact for any particular VF
571 private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) throws JsonProcessingException, IOException {
572 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<CldsVfKPIData>();
573 ObjectMapper mapper = new ObjectMapper();
574 ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);
575 ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");
577 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
578 for (int index = 0; index < artifactsArrayNode.size(); index++) {
579 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
580 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
581 TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName");
582 String artifactName = "";
583 if (artifactNameNode != null) {
584 artifactName = artifactNameNode.textValue();
585 artifactName = artifactName.substring(artifactName.lastIndexOf(".") + 1);
587 if (artifactUrlNode != null && artifactName != null && !artifactName.isEmpty()
588 && artifactName.equalsIgnoreCase("csv")) {
589 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
590 cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl));
591 logger.info(responsesFromArtifactUrl);
595 return cldsVfKPIDataList;
598 private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) {
599 if (record.size() < 6) {
600 logger.debug("invalid csv field path Record,total columns less than 6: " + record);
604 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
605 || StringUtils.isBlank(record.get(5))) {
606 logger.debug("Invalid csv field path Record,one of column is having blank value : " + record);
610 CldsVfKPIData cldsVfKPIData = new CldsVfKPIData();
611 cldsVfKPIData.setNfNamingCode(record.get(0).trim());
612 cldsVfKPIData.setNfNamingValue(record.get(1).trim());
614 cldsVfKPIData.setFieldPath(record.get(2).trim());
615 cldsVfKPIData.setFieldPathValue(record.get(3).trim());
617 cldsVfKPIData.setThresholdName(record.get(4).trim());
618 cldsVfKPIData.setThresholdValue(record.get(5).trim());
619 return cldsVfKPIData;
623 // Method to get the artifactURL Data and set the CldsVfKPIData node
624 private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) throws IOException {
625 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<CldsVfKPIData>();
626 Reader alarmReader = new StringReader(allFieldPathValues);
627 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
628 if (records != null) {
629 for (CSVRecord record : records) {
630 CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record);
631 if (kpiData != null) {
632 cldsVfKPIDataList.add(kpiData);
636 return cldsVfKPIDataList;
639 private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
640 if (record == null) {
643 if (record.size() < 5) {
644 logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
647 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
648 || StringUtils.isBlank(record.get(4))) {
649 logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
652 CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
653 cldsAlarmCondition.setEventSourceType(record.get(1));
654 cldsAlarmCondition.setAlarmConditionKey(record.get(3));
655 cldsAlarmCondition.setSeverity(record.get(4));
656 cldsAlarmConditionList.add(cldsAlarmCondition);
659 public String getResponsesFromArtifactUrl(String artifactsUrl) throws IOException {
660 String hostUrl = refProp.getStringValue("sdc.hostUrl");
661 artifactsUrl = artifactsUrl.replaceAll("\"", "");
662 String artifactUrl = hostUrl + artifactsUrl;
663 logger.info("value of artifactURl:" + artifactUrl);
664 String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true);
665 logger.info("value of artifactResponse:" + currArtifactResponse);
666 return currArtifactResponse;
670 * Service to services/resources/artifacts from sdc.Pass alarmConditions as
671 * true to get alarmconditons from artifact url and else it is false
674 * @param alarmConditions
676 * @throws IOException
678 public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) {
681 url = removeUnwantedBracesFromString(url);
682 URL urlObj = new URL(url);
684 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
685 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
686 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
687 conn.setRequestProperty("Authorization", basicAuth);
688 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
689 conn.setRequestMethod("GET");
691 int responseCode = conn.getResponseCode();
692 logger.info("responseCode=" + responseCode);
694 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
695 StringBuffer response = new StringBuffer();
697 while ((inputLine = in.readLine()) != null) {
698 response.append(inputLine);
699 if (alarmConditions) {
700 response.append("\n");
703 responseStr = response.toString();
706 } catch (Exception e) {
707 logger.error("Exception occurred :", e);
714 * To create properties object by using cldsServicedata
717 * @param cldsServiceData
719 * @throws IOException
721 public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) throws IOException {
722 String totalPropsStr;
723 ObjectMapper mapper = new ObjectMapper();
724 ObjectNode globalPropsJson;
725 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
727 // Objectnode to save all byservice, byvf , byvfc and byalarm nodes
728 ObjectNode byIdObjectNode = mapper.createObjectNode();
730 // To create vf ResourceUUID node with serviceInvariantUUID
731 ObjectNode invariantUuidObjectNodeWithVF = createVFObjectNodeByServiceInvariantUUID(mapper,
733 byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVF);
735 // To create byVf and vfcResourceNode with vfResourceUUID
736 ObjectNode vfcObjectNodeByVfUuid = createVFCObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs());
737 byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid);
740 ObjectNode kpiObjectNode = mapper.createObjectNode();
741 if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {
742 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
743 if (currCldsVfData != null) {
744 createKPIObjectNodeByVfUUID(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList());
748 byIdObjectNode.putPOJO("byKpi", kpiObjectNode);
750 // To create byVfc and alarmCondition with vfcResourceUUID
751 ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();
752 if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {
753 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
754 if (currCldsVfData != null) {
755 createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode,
756 currCldsVfData.getCldsVfcs());
760 byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode);
762 // To create byAlarmCondition with alarmConditionKey
763 List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData);
764 ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions);
766 byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
768 globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
770 globalPropsJson.putPOJO("shared", byIdObjectNode);
771 logger.info("valuie of objNode:" + globalPropsJson);
774 * to create json with total properties when no serviceUUID passed
776 globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class);
778 totalPropsStr = globalPropsJson.toString();
779 return totalPropsStr;
782 public List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData) {
783 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
784 if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null
785 && cldsServiceData.getCldsVfs().size() > 0) {
786 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
787 if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null
788 && currCldsVfData.getCldsVfcs().size() > 0) {
789 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
790 if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null
791 && currCldsVfcData.getCldsAlarmConditions().size() > 0) {
792 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
793 if (currCldsAlarmCondition != null) {
794 alarmCondList.add(currCldsAlarmCondition);
802 return alarmCondList;
805 private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper,
806 List<CldsAlarmCondition> cldsAlarmCondList) {
807 ObjectNode alarmCondKeyNode = mapper.createObjectNode();
809 if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) {
810 for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
811 if (currCldsAlarmCondition != null) {
812 ObjectNode alarmCondNode = mapper.createObjectNode();
813 alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
814 alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
815 alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
819 ObjectNode alarmCondNode = mapper.createObjectNode();
820 alarmCondNode.put("eventSourceType", "");
821 alarmCondNode.put("eventSeverity", "");
822 alarmCondKeyNode.putPOJO("", alarmCondNode);
824 return alarmCondKeyNode;
827 private ObjectNode createVFObjectNodeByServiceInvariantUUID(ObjectMapper mapper, CldsServiceData cldsServiceData) {
828 ObjectNode invariantUuidObjectNode = mapper.createObjectNode();
829 ObjectNode vfObjectNode = mapper.createObjectNode();
830 ObjectNode vfUuidNode = mapper.createObjectNode();
831 List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
832 if (cldsVfsList != null && cldsVfsList.size() > 0) {
833 for (CldsVfData currCldsVfData : cldsVfsList) {
834 if (currCldsVfData != null) {
835 vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
839 vfUuidNode.put("", "");
841 vfObjectNode.putPOJO("vf", vfUuidNode);
842 invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
843 return invariantUuidObjectNode;
846 private void createKPIObjectNodeByVfUUID(ObjectMapper mapper, ObjectNode vfResourceUUIDObjectNode,
847 List<CldsVfKPIData> cldsVfKPIDataList) {
848 if (cldsVfKPIDataList != null && cldsVfKPIDataList.size() > 0) {
849 for (CldsVfKPIData currCldsVfKPIData : cldsVfKPIDataList) {
850 if (currCldsVfKPIData != null) {
851 ObjectNode thresholdNameObjectNode = mapper.createObjectNode();
853 ObjectNode fieldPathObjectNode = mapper.createObjectNode();
854 ObjectNode nfNamingCodeNode = mapper.createObjectNode();
856 fieldPathObjectNode.put(currCldsVfKPIData.getFieldPathValue(),
857 currCldsVfKPIData.getFieldPathValue());
858 nfNamingCodeNode.put(currCldsVfKPIData.getNfNamingValue(), currCldsVfKPIData.getNfNamingValue());
860 thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode);
861 thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode);
863 vfResourceUUIDObjectNode.putPOJO(currCldsVfKPIData.getThresholdValue(), thresholdNameObjectNode);
869 private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUUIDObjectNode,
870 List<CldsVfcData> cldsVfcDataList) {
871 ObjectNode alarmCondContsObjectNode = mapper.createObjectNode();
872 ObjectNode alarmCondNode = mapper.createObjectNode();
873 // alarmCondNode.put("", "");
874 if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) {
875 for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
876 if (currCldsVfcData != null) {
877 if (currCldsVfcData.getCldsAlarmConditions() != null
878 && currCldsVfcData.getCldsAlarmConditions().size() > 0) {
879 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
880 alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
881 currCldsAlarmCondition.getAlarmConditionKey());
883 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
885 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
886 vfcResourceUUIDObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(),
887 alarmCondContsObjectNode);
891 alarmCondNode.put("", "");
892 alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode);
893 vfcResourceUUIDObjectNode.putPOJO("", alarmCondContsObjectNode);
897 private ObjectNode createVFCObjectNodeByVfUuid(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {
898 ObjectNode vfUUIDObjectNode = mapper.createObjectNode();
900 if (cldsVfDataList != null && cldsVfDataList.size() > 0) {
901 for (CldsVfData currCldsVfData : cldsVfDataList) {
902 if (currCldsVfData != null) {
903 ObjectNode vfcObjectNode = mapper.createObjectNode();
904 ObjectNode vfcUuidNode = mapper.createObjectNode();
905 if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) {
906 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
907 vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),
908 currCldsVfcData.getVfcName());
911 vfcUuidNode.put("", "");
913 vfcObjectNode.putPOJO("vfc", vfcUuidNode);
914 vfUUIDObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode);
918 ObjectNode vfcUuidNode = mapper.createObjectNode();
919 vfcUuidNode.put("", "");
920 ObjectNode vfcObjectNode = mapper.createObjectNode();
921 vfcObjectNode.putPOJO("vfc", vfcUuidNode);
922 vfUUIDObjectNode.putPOJO("", vfcObjectNode);
924 return vfUUIDObjectNode;
927 public String getArtifactIdIfArtifactAlreadyExists(CldsSdcServiceDetail CldsSdcServiceDetail, String artifactName) {
928 String artifactUuid = null;
929 boolean artifactxists = false;
930 if (CldsSdcServiceDetail != null && CldsSdcServiceDetail.getResources() != null
931 && CldsSdcServiceDetail.getResources().size() > 0) {
932 for (CldsSdcResource currCldsSdcResource : CldsSdcServiceDetail.getResources()) {
936 if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null
937 && currCldsSdcResource.getArtifacts().size() > 0) {
938 for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) {
939 if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null) {
940 if (currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
941 artifactUuid = currCldsSdcArtifact.getArtifactUUID();
942 artifactxists = true;
953 public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid,
954 String invariantServiceUuid, String artifactName) {
955 String baseUrl = refProp.getStringValue("sdc.serviceUrl");
956 String basicAuth = SdcReq.getSdcBasicAuth(refProp);
957 String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \""
958 + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n"
959 + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n";
961 String url = baseUrl;
962 if (invariantServiceUuid != null) {
963 url = dcaeUrl + "/closed-loops";
965 URL urlObj = new URL(url);
967 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
968 conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool");
969 conn.setRequestProperty("Authorization", basicAuth);
970 conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
971 conn.setRequestMethod("POST");
973 byte[] postData = SdcReq.stringToByteArray(postStatusData);
974 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
978 int responseCode = conn.getResponseCode();
979 logger.info("responseCode=" + responseCode);
981 String resp = getResponse(conn);
985 } catch (Exception e) {
986 logger.error("not able to ger any service information from sdc for uuid:" + invariantServiceUuid);
992 * To get all sdc VF/VFC Resources basic info
995 * @throws IOException
997 private List<CldsSdcResourceBasicInfo> getAllSdcVForVFCResourcesBasedOnResourceType(String resourceType)
999 List<CldsSdcResourceBasicInfo> allSdcResourceVFCBasicInfo = new ArrayList<CldsSdcResourceBasicInfo>();
1000 String catalogUrl = refProp.getStringValue("sdc.catalog.url");
1001 String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType;
1002 String allSdcVFCResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false);
1004 allSdcResourceVFCBasicInfo = getAllSdcResourcesListFromJson(allSdcVFCResources);
1005 return removeDuplicateSdcResourceBasicInfo(allSdcResourceVFCBasicInfo);
1008 private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUUID,
1009 List<CldsSdcResourceBasicInfo> resourceInfoList) throws IOException {
1010 String resourceUuid = null;
1011 if (resourceInfoList != null && resourceInfoList.size() > 0) {
1012 for (CldsSdcResourceBasicInfo currResource : resourceInfoList) {
1013 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
1014 && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUUID)) {
1015 resourceUuid = currResource.getUuid();
1020 return resourceUuid;