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