2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 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 * Modifications copyright (c) 2018 Nokia
\r
21 * ===================================================================
\r
25 package org.onap.clamp.clds.client.req.sdc;
\r
27 import com.att.eelf.configuration.EELFLogger;
\r
28 import com.att.eelf.configuration.EELFManager;
\r
29 import com.fasterxml.jackson.databind.JsonNode;
\r
30 import com.fasterxml.jackson.databind.ObjectMapper;
\r
31 import com.fasterxml.jackson.databind.node.ArrayNode;
\r
32 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
33 import com.fasterxml.jackson.databind.node.TextNode;
\r
35 import java.io.BufferedReader;
\r
36 import java.io.DataOutputStream;
\r
37 import java.io.IOException;
\r
38 import java.io.InputStream;
\r
39 import java.io.InputStreamReader;
\r
40 import java.io.Reader;
\r
41 import java.io.StringReader;
\r
42 import java.net.HttpURLConnection;
\r
43 import java.net.URL;
\r
44 import java.nio.charset.StandardCharsets;
\r
45 import java.security.GeneralSecurityException;
\r
46 import java.util.ArrayList;
\r
47 import java.util.Base64;
\r
48 import java.util.Collections;
\r
49 import java.util.Date;
\r
50 import java.util.Iterator;
\r
51 import java.util.List;
\r
53 import javax.ws.rs.BadRequestException;
\r
55 import org.apache.commons.codec.DecoderException;
\r
56 import org.apache.commons.codec.digest.DigestUtils;
\r
57 import org.apache.commons.csv.CSVFormat;
\r
58 import org.apache.commons.csv.CSVRecord;
\r
59 import org.apache.commons.io.IOUtils;
\r
60 import org.apache.commons.lang3.StringUtils;
\r
61 import org.apache.http.HttpHeaders;
\r
62 import org.onap.clamp.clds.config.ClampProperties;
\r
63 import org.onap.clamp.clds.exception.sdc.SdcCommunicationException;
\r
64 import org.onap.clamp.clds.model.CldsAlarmCondition;
\r
65 import org.onap.clamp.clds.model.CldsServiceData;
\r
66 import org.onap.clamp.clds.model.CldsVfData;
\r
67 import org.onap.clamp.clds.model.CldsVfKPIData;
\r
68 import org.onap.clamp.clds.model.CldsVfcData;
\r
69 import org.onap.clamp.clds.model.properties.Global;
\r
70 import org.onap.clamp.clds.model.properties.ModelProperties;
\r
71 import org.onap.clamp.clds.model.sdc.SdcArtifact;
\r
72 import org.onap.clamp.clds.model.sdc.SdcResource;
\r
73 import org.onap.clamp.clds.model.sdc.SdcResourceBasicInfo;
\r
74 import org.onap.clamp.clds.model.sdc.SdcServiceDetail;
\r
75 import org.onap.clamp.clds.model.sdc.SdcServiceInfo;
\r
76 import org.onap.clamp.clds.service.CldsService;
\r
77 import org.onap.clamp.clds.util.CryptoUtils;
\r
78 import org.onap.clamp.clds.util.JacksonUtils;
\r
79 import org.onap.clamp.clds.util.LoggingUtils;
\r
80 import org.springframework.beans.factory.annotation.Autowired;
\r
81 import org.springframework.context.annotation.Primary;
\r
82 import org.springframework.stereotype.Component;
\r
86 public class SdcCatalogServices {
\r
88 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class);
\r
89 private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
90 private static final String RESOURCE_VF_TYPE = "VF";
\r
91 private static final String RESOURCE_VFC_TYPE = "VFC";
\r
92 private static final String RESOURCE_CVFC_TYPE = "CVFC";
\r
93 private static final String SDC_REQUESTID_PROPERTY_NAME = "sdc.header.requestId";
\r
94 private static final String SDC_METADATA_URL_PREFIX = "/metadata";
\r
95 private static final String SDC_INSTANCE_ID_PROPERTY_NAME = "sdc.InstanceID";
\r
96 private static final String SDC_CATALOG_URL_PROPERTY_NAME = "sdc.catalog.url";
\r
97 private static final String SDC_SERVICE_URL_PROPERTY_NAME = "sdc.serviceUrl";
\r
98 private static final String SDC_INSTANCE_ID_CLAMP = "CLAMP-Tool";
\r
99 private static final String RESOURCE_URL_PREFIX = "resources";
\r
101 private ClampProperties refProp;
\r
104 * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic
\r
105 * dGVzdDoxMjM0NTY=).
\r
107 * @return The String with Basic Auth and password
\r
108 * @throws GeneralSecurityException
\r
109 * In case of issue when decryting the SDC password
\r
110 * @throws DecoderException
\r
111 * In case of issues with the decoding of the HexString message
\r
113 private String getSdcBasicAuth() throws GeneralSecurityException, DecoderException {
\r
114 String sdcId = refProp.getStringValue("sdc.serviceUsername");
\r
115 String sdcPw = refProp.getStringValue("sdc.servicePassword");
\r
116 String password = CryptoUtils.decrypt(sdcPw);
\r
117 String idPw = Base64.getEncoder().encodeToString((sdcId + ":" + password).getBytes(StandardCharsets.UTF_8));
\r
118 return "Basic " + idPw;
\r
122 * This method get the SDC services Information with the corresponding
\r
127 * @return A Json String with all the service list
\r
128 * @throws GeneralSecurityException
\r
129 * In case of issue when decryting the SDC password
\r
130 * @throws DecoderException
\r
131 * In case of issues with the decoding of the Hex String
\r
133 public String getSdcServicesInformation(String uuid) throws GeneralSecurityException, DecoderException {
\r
134 Date startTime = new Date();
\r
135 String baseUrl = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME);
\r
136 String basicAuth = getSdcBasicAuth();
\r
137 LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation");
\r
139 String url = baseUrl;
\r
140 if (uuid != null && !uuid.isEmpty()) {
\r
141 url = baseUrl + "/" + uuid + SDC_METADATA_URL_PREFIX;
\r
143 URL urlObj = new URL(url);
\r
144 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
\r
145 conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP);
\r
146 conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
\r
147 conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
\r
148 conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
\r
149 conn.setRequestMethod("GET");
\r
150 String resp = getResponse(conn);
\r
151 logger.debug("Services list received from SDC:" + resp);
\r
153 LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName());
\r
155 } catch (IOException e) {
\r
156 LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName());
\r
157 LoggingUtils.setErrorContext("900", "Get sdc services error");
\r
158 logger.error("not able to get any service information from sdc for uuid:" + uuid, e);
\r
160 LoggingUtils.setTimeContext(startTime, new Date());
\r
161 metricsLogger.info("getSdcServicesInformation complete");
\r
167 * To remove duplicate serviceUUIDs from sdc services List.
\r
169 * @param rawCldsSdcServiceList
\r
170 * A list of CldsSdcServiceInfo
\r
171 * @return A list of CldsSdcServiceInfo without duplicate service UUID
\r
173 public List<SdcServiceInfo> removeDuplicateServices(List<SdcServiceInfo> rawCldsSdcServiceList) {
\r
174 List<SdcServiceInfo> cldsSdcServiceInfoList = null;
\r
175 if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) {
\r
177 Collections.sort(rawCldsSdcServiceList);
\r
178 // and then take only the services with the max version (last in the
\r
179 // list with the same name)
\r
180 cldsSdcServiceInfoList = new ArrayList<>();
\r
181 for (int i = 1; i < rawCldsSdcServiceList.size(); i++) {
\r
182 // compare name with previous - if not equal, then keep the
\r
183 // previous (it's the last with that name)
\r
184 SdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1);
\r
185 if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) {
\r
186 cldsSdcServiceInfoList.add(prev);
\r
189 // add the last in the list
\r
190 cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1));
\r
192 return cldsSdcServiceInfoList;
\r
196 * To remove duplicate serviceUUIDs from sdc resources List.
\r
198 * @param rawCldsSdcResourceList
\r
199 * @return List of CldsSdcResource
\r
201 public List<SdcResource> removeDuplicateSdcResourceInstances(List<SdcResource> rawCldsSdcResourceList) {
\r
202 List<SdcResource> cldsSdcResourceList = null;
\r
203 if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) {
\r
205 Collections.sort(rawCldsSdcResourceList);
\r
206 // and then take only the resources with the max version (last in
\r
207 // the list with the same name)
\r
208 cldsSdcResourceList = new ArrayList<>();
\r
209 for (int i = 1; i < rawCldsSdcResourceList.size(); i++) {
\r
210 // compare name with previous - if not equal, then keep the
\r
211 // previous (it's the last with that name)
\r
212 SdcResource prev = rawCldsSdcResourceList.get(i - 1);
\r
213 if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) {
\r
214 cldsSdcResourceList.add(prev);
\r
217 // add the last in the list
\r
218 cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1));
\r
220 return cldsSdcResourceList;
\r
224 * To remove duplicate basic resources with same resourceUUIDs.
\r
226 * @param rawCldsSdcResourceListBasicList
\r
227 * @return List of CldsSdcResourceBasicInfo
\r
229 public List<SdcResourceBasicInfo> removeDuplicateSdcResourceBasicInfo(
\r
230 List<SdcResourceBasicInfo> rawCldsSdcResourceListBasicList) {
\r
231 List<SdcResourceBasicInfo> cldsSdcResourceBasicInfoList = new ArrayList<>();
\r
232 if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) {
\r
234 Collections.sort(rawCldsSdcResourceListBasicList);
\r
235 // and then take only the resources with the max version (last in
\r
236 // the list with the same name)
\r
237 for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) {
\r
238 // compare name with previous - if not equal, then keep the
\r
239 // previous (it's the last with that name)
\r
240 SdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1);
\r
241 if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) {
\r
242 cldsSdcResourceBasicInfoList.add(prev);
\r
245 // add the last in the list
\r
246 cldsSdcResourceBasicInfoList
\r
247 .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1));
\r
249 return cldsSdcResourceBasicInfoList;
\r
253 * To get ServiceUUID by using serviceInvariantUUID.
\r
255 * @param invariantId
\r
257 * @return The service UUID
\r
258 * @throws GeneralSecurityException
\r
259 * In case of issue when decryting the SDC password
\r
260 * @throws DecoderException
\r
261 * In case of issues with the decoding of the Hex String
\r
263 public String getServiceUuidFromServiceInvariantId(String invariantId)
\r
264 throws GeneralSecurityException, DecoderException {
\r
265 String serviceUuid = "";
\r
266 String responseStr = getSdcServicesInformation(null);
\r
267 List<SdcServiceInfo> rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr);
\r
268 List<SdcServiceInfo> cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList);
\r
269 if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) {
\r
270 for (SdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) {
\r
271 if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null
\r
272 && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) {
\r
273 serviceUuid = currCldsSdcServiceInfo.getUuid();
\r
278 return serviceUuid;
\r
282 * To get CldsAsdsServiceInfo class by parsing json string.
\r
285 * The Json string that must be decoded
\r
286 * @return The list of CldsSdcServiceInfo, if there is a failure it return
\r
289 private List<SdcServiceInfo> getCldsSdcServicesListFromJson(String jsonStr) {
\r
290 if (StringUtils.isBlank(jsonStr)) {
\r
291 return new ArrayList<>();
\r
294 return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, JacksonUtils.getObjectMapperInstance()
\r
295 .getTypeFactory().constructCollectionType(List.class, SdcServiceInfo.class));
\r
296 } catch (IOException e) {
\r
297 logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e);
\r
298 return new ArrayList<>();
\r
303 * To get List of CldsSdcResourceBasicInfo class by parsing json string.
\r
306 * The JSOn string that must be decoded
\r
307 * @return The list of CldsSdcResourceBasicInfo, an empty list in case of
\r
310 private List<SdcResourceBasicInfo> getAllSdcResourcesListFromJson(String jsonStr) {
\r
311 if (StringUtils.isBlank(jsonStr)) {
\r
312 return new ArrayList<>();
\r
315 return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, JacksonUtils.getObjectMapperInstance()
\r
316 .getTypeFactory().constructCollectionType(List.class, SdcResourceBasicInfo.class));
\r
317 } catch (IOException e) {
\r
318 logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e);
\r
319 return new ArrayList<>();
\r
324 * To get CldsSdcServiceDetail by parsing json string.
\r
329 public SdcServiceDetail decodeCldsSdcServiceDetailFromJson(String jsonStr) {
\r
331 return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, SdcServiceDetail.class);
\r
332 } catch (IOException e) {
\r
333 logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e);
\r
338 // upload artifact to sdc based on serviceUUID and resource name on url
\r
339 private String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) {
\r
340 // Verify whether it is triggered by Validation Test button from UI
\r
341 if (prop.isTestOnly()) {
\r
342 return "sdc artifact upload not executed for test action";
\r
345 logger.info("userid=" + userid);
\r
346 byte[] postData = formattedSdcReq.getBytes(StandardCharsets.UTF_8);
\r
347 int postDataLength = postData.length;
\r
348 HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, formattedSdcReq);
\r
349 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
\r
350 wr.write(postData);
\r
352 boolean requestFailed = true;
\r
353 int responseCode = conn.getResponseCode();
\r
354 logger.info("responseCode=" + responseCode);
\r
355 if (responseCode == 200) {
\r
356 requestFailed = false;
\r
358 String responseStr = getResponse(conn);
\r
359 if (responseStr != null && requestFailed) {
\r
360 logger.error("requestFailed - responseStr=" + responseStr);
\r
361 throw new BadRequestException(responseStr);
\r
363 return responseStr;
\r
364 } catch (IOException e) {
\r
365 logger.error("Exception when attempting to communicate with SDC", e);
\r
366 throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e);
\r
370 private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String content) {
\r
372 logger.info("userid=" + userid);
\r
373 String basicAuth = getSdcBasicAuth();
\r
374 String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
\r
375 URL urlObj = new URL(url);
\r
376 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
\r
377 conn.setDoOutput(true);
\r
378 conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), sdcXonapInstanceId);
\r
379 conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
\r
380 conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json");
\r
381 conn.setRequestProperty(HttpHeaders.CONTENT_MD5,
\r
382 Base64.getEncoder().encodeToString(DigestUtils.md5Hex(content).getBytes("UTF-8")));
\r
383 conn.setRequestProperty("USER_ID", userid);
\r
384 conn.setRequestMethod("POST");
\r
385 conn.setRequestProperty("charset", "utf-8");
\r
386 conn.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Integer.toString(postDataLength));
\r
387 conn.setUseCaches(false);
\r
388 conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
\r
390 } catch (IOException e) {
\r
391 logger.error("Exception when attempting to open connection with SDC", e);
\r
392 throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e);
\r
393 } catch (DecoderException e) {
\r
394 logger.error("Exception when attempting to decode the Hex string", e);
\r
395 throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e);
\r
396 } catch (GeneralSecurityException e) {
\r
397 logger.error("Exception when attempting to decrypt the encrypted password", e);
\r
398 throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e);
\r
402 private String getResponse(HttpURLConnection conn) {
\r
403 try (InputStream is = getInputStream(conn)) {
\r
404 try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
\r
405 return IOUtils.toString(in);
\r
407 } catch (IOException e) {
\r
408 logger.error("Exception when attempting to open SDC response", e);
\r
409 throw new SdcCommunicationException("Exception when attempting to open SDC response", e);
\r
413 private InputStream getInputStream(HttpURLConnection conn) {
\r
415 InputStream inStream = conn.getErrorStream();
\r
416 if (inStream == null) {
\r
417 inStream = conn.getInputStream();
\r
420 } catch (IOException e) {
\r
421 logger.error("Exception when attempting to open SDC error stream", e);
\r
422 throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e);
\r
427 * Check if the SDC Info in cache has expired.
\r
429 * @param cldsServiceData
\r
430 * The object representing the service data
\r
431 * @return boolean flag
\r
432 * @throws GeneralSecurityException
\r
433 * In case of issues with the decryting the encrypted password
\r
434 * @throws DecoderException
\r
435 * In case of issues with the decoding of the Hex String
\r
437 public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData)
\r
438 throws GeneralSecurityException, DecoderException {
\r
439 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
\r
440 String cachedServiceUuid = cldsServiceData.getServiceUUID();
\r
441 String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID());
\r
442 String configuredMaxAge = refProp.getStringValue("clds.service.cache.invalidate.after.seconds");
\r
443 if (configuredMaxAge == null) {
\r
445 "clds.service.cache.invalidate.after.seconds NOT set in clds-reference.properties file, taking 60s as default");
\r
446 configuredMaxAge = "60";
\r
448 return (!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null
\r
449 && cldsServiceData.getAgeOfRecord() > Long.parseLong(configuredMaxAge));
\r
456 * Get the Service Data with Alarm Conditions for a given
\r
457 * invariantServiceUuid.
\r
459 * @param invariantServiceUuid
\r
460 * @return The CldsServiceData
\r
461 * @throws GeneralSecurityException
\r
462 * In case of issues with the decryting the encrypted password
\r
463 * @throws DecoderException
\r
464 * In case of issues with the decoding of the Hex String
\r
466 public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid)
\r
467 throws GeneralSecurityException, DecoderException {
\r
468 String url = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME);
\r
469 String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
\r
470 String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid);
\r
471 String serviceDetailUrl = url + "/" + serviceUuid + SDC_METADATA_URL_PREFIX;
\r
472 String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl);
\r
473 CldsServiceData cldsServiceData = new CldsServiceData();
\r
474 if (responseStr != null) {
\r
475 SdcServiceDetail cldsSdcServiceDetail;
\r
477 cldsSdcServiceDetail = JacksonUtils.getObjectMapperInstance().readValue(responseStr,
\r
478 SdcServiceDetail.class);
\r
479 } catch (IOException e) {
\r
480 logger.error("Exception when decoding the CldsServiceData JSON from SDC", e);
\r
481 throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e);
\r
483 // To remove duplicate resources from serviceDetail and add valid
\r
485 if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {
\r
486 cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid());
\r
487 cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID());
\r
488 List<SdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(
\r
489 cldsSdcServiceDetail.getResources());
\r
490 if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) {
\r
491 List<CldsVfData> cldsVfDataList = new ArrayList<>();
\r
492 for (SdcResource currCldsSdcResource : cldsSdcResourceList) {
\r
493 if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null
\r
494 && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) {
\r
495 CldsVfData currCldsVfData = new CldsVfData();
\r
496 currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());
\r
497 currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());
\r
498 cldsVfDataList.add(currCldsVfData);
\r
501 cldsServiceData.setCldsVfs(cldsVfDataList);
\r
502 // For each vf in the list , add all vfc's
\r
503 getAllVfcForVfList(cldsVfDataList, catalogUrl);
\r
504 logger.info("Invariant Service ID of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID());
\r
508 return cldsServiceData;
\r
511 private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl)
\r
512 throws GeneralSecurityException {
\r
513 // todo : refact this..
\r
514 if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
\r
515 List<SdcResourceBasicInfo> allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_VF_TYPE);
\r
516 List<SdcResourceBasicInfo> allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType(
\r
517 RESOURCE_VFC_TYPE);
\r
518 allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE));
\r
519 for (CldsVfData currCldsVfData : cldsVfDataList) {
\r
520 if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) {
\r
521 String resourceUuid = getResourceUuidFromResourceInvariantUuid(
\r
522 currCldsVfData.getVfInvariantResourceUUID(), allVfResources);
\r
523 if (resourceUuid != null) {
\r
524 String vfResourceUuidUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUuid
\r
525 + SDC_METADATA_URL_PREFIX;
\r
526 String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl);
\r
527 if (vfResponse != null) {
\r
528 // Below 2 line are to get the KPI(field path) data
\r
529 // associated with the VF's
\r
530 List<CldsVfKPIData> cldsVfKPIDataList = getFieldPathFromVF(vfResponse);
\r
531 currCldsVfData.setCldsKPIList(cldsVfKPIDataList);
\r
532 List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);
\r
533 if (vfcDataListFromVfResponse != null) {
\r
534 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);
\r
535 if (!vfcDataListFromVfResponse.isEmpty()) {
\r
536 // To get artifacts for every VFC and get
\r
537 // alarm conditions from artifact
\r
538 for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {
\r
539 if (currCldsVfcData != null
\r
540 && currCldsVfcData.getVfcInvariantResourceUUID() != null) {
\r
541 String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid(
\r
542 currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources);
\r
543 if (resourceVfcUuid != null) {
\r
544 String vfcResourceUuidUrl = catalogUrl + RESOURCE_URL_PREFIX + "/"
\r
545 + resourceVfcUuid + SDC_METADATA_URL_PREFIX;
\r
546 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(
\r
547 vfcResourceUuidUrl);
\r
548 if (vfcResponse != null) {
\r
549 List<CldsAlarmCondition> alarmCondtionsFromVfc = getAlarmCondtionsFromVfc(
\r
551 currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc);
\r
554 logger.info("No resourceVFC UUID found for given invariantID:"
\r
555 + currCldsVfcData.getVfcInvariantResourceUUID());
\r
563 logger.info("No resourceUUID found for given invariantREsourceUUID:"
\r
564 + currCldsVfData.getVfInvariantResourceUUID());
\r
571 private List<CldsVfcData> getVfcDataListFromVfResponse(String vfResponse) {
\r
572 ObjectNode vfResponseNode;
\r
574 vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfResponse);
\r
575 } catch (IOException e) {
\r
576 logger.error("Exception when decoding the JSON list of CldsVfcData", e);
\r
577 return new ArrayList<>();
\r
579 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
\r
580 List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
\r
581 if (vfcArrayNode != null) {
\r
582 for (JsonNode vfcjsonNode : vfcArrayNode) {
\r
583 ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
\r
584 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
\r
585 if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
\r
586 handleVFCtypeNode(currVfcNode, cldsVfcDataList);
\r
587 } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
\r
588 handleCVFCtypeNode(currVfcNode, cldsVfcDataList);
\r
592 return cldsVfcDataList;
\r
595 private void handleVFCtypeNode(ObjectNode currVfcNode, List<CldsVfcData> cldsVfcDataList) {
\r
596 CldsVfcData currCldsVfcData = new CldsVfcData();
\r
597 TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName");
\r
598 TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID");
\r
599 currCldsVfcData.setVfcName(vfcResourceName.textValue());
\r
600 currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue());
\r
601 cldsVfcDataList.add(currCldsVfcData);
\r
604 private void handleCVFCtypeNode(ObjectNode currVfcNode, List<CldsVfcData> cldsVfcDataList) {
\r
605 handleVFCtypeNode(currVfcNode, cldsVfcDataList);
\r
606 cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue()));
\r
609 private List<CldsVfcData> getVFCfromCVFC(String resourceUUID) {
\r
610 String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
\r
611 List<CldsVfcData> cldsVfcDataList = new ArrayList<>();
\r
612 if (resourceUUID != null) {
\r
613 String vfcResourceUUIDUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUUID + SDC_METADATA_URL_PREFIX;
\r
615 String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl);
\r
616 ObjectNode vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfcResponse);
\r
617 ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources");
\r
618 if (vfcArrayNode != null) {
\r
619 for (JsonNode vfcjsonNode : vfcArrayNode) {
\r
620 ObjectNode currVfcNode = (ObjectNode) vfcjsonNode;
\r
621 TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType");
\r
622 if (resourceTypeNode != null && "VFC".equalsIgnoreCase(resourceTypeNode.textValue())) {
\r
623 handleVFCtypeNode(currVfcNode, cldsVfcDataList);
\r
627 } catch (IOException e) {
\r
628 logger.error("Exception during JSON analyzis", e);
\r
631 return cldsVfcDataList;
\r
634 private String removeUnwantedBracesFromString(String id) {
\r
635 return (id != null) ? id.replaceAll("\"", "") : "";
\r
638 private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException {
\r
639 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
\r
640 ObjectNode vfcResponseNode;
\r
642 vfcResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfcResponse);
\r
643 } catch (IOException e) {
\r
644 logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e);
\r
645 return cldsAlarmConditionList;
\r
647 ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts");
\r
648 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
\r
649 for (int index = 0; index < artifactsArrayNode.size(); index++) {
\r
650 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
\r
651 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
\r
652 if (artifactUrlNode != null) {
\r
653 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
\r
654 cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl));
\r
655 logger.info(responsesFromArtifactUrl);
\r
659 return cldsAlarmConditionList;
\r
662 private List<CldsAlarmCondition> parseCsvToGetAlarmConditions(String allAlarmCondsValues) {
\r
664 List<CldsAlarmCondition> cldsAlarmConditionList = new ArrayList<>();
\r
665 Reader alarmReader = new StringReader(allAlarmCondsValues);
\r
666 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
\r
667 if (records != null) {
\r
668 Iterator<CSVRecord> it = records.iterator();
\r
669 if (it.hasNext()) {
\r
672 it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record));
\r
674 return cldsAlarmConditionList;
\r
675 } catch (IOException e) {
\r
676 logger.error("Exception when attempting to parse the CSV containing the alarm", e);
\r
677 return new ArrayList<>();
\r
681 // Method to get the artifact for any particular VF
\r
682 private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) throws GeneralSecurityException {
\r
683 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
\r
684 ObjectNode vfResponseNode;
\r
686 vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfResponse);
\r
687 } catch (IOException e) {
\r
688 logger.error("Exception when decoding the JSON list of CldsVfKPIData", e);
\r
689 return cldsVfKPIDataList;
\r
691 ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");
\r
692 if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) {
\r
693 for (int index = 0; index < artifactsArrayNode.size(); index++) {
\r
694 ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index);
\r
695 TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL");
\r
696 TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName");
\r
697 String artifactName = "";
\r
698 if (artifactNameNode != null) {
\r
699 artifactName = artifactNameNode.textValue();
\r
700 artifactName = artifactName.substring(artifactName.lastIndexOf('.') + 1);
\r
702 if (artifactUrlNode != null && "csv".equalsIgnoreCase(artifactName)) {
\r
703 String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue());
\r
704 cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl));
\r
705 logger.info(responsesFromArtifactUrl);
\r
709 return cldsVfKPIDataList;
\r
712 private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) {
\r
713 if (record.size() < 6) {
\r
714 logger.debug("invalid csv field path Record,total columns less than 6: " + record);
\r
717 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
\r
718 || StringUtils.isBlank(record.get(5))) {
\r
719 logger.debug("Invalid csv field path Record,one of column is having blank value : " + record);
\r
722 CldsVfKPIData cldsVfKPIData = new CldsVfKPIData();
\r
723 cldsVfKPIData.setNfNamingCode(record.get(0).trim());
\r
724 cldsVfKPIData.setNfNamingValue(record.get(1).trim());
\r
725 cldsVfKPIData.setFieldPath(record.get(2).trim());
\r
726 cldsVfKPIData.setFieldPathValue(record.get(3).trim());
\r
727 cldsVfKPIData.setThresholdName(record.get(4).trim());
\r
728 cldsVfKPIData.setThresholdValue(record.get(5).trim());
\r
729 return cldsVfKPIData;
\r
732 // Method to get the artifactURL Data and set the CldsVfKPIData node
\r
733 private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) {
\r
735 List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();
\r
736 Reader alarmReader = new StringReader(allFieldPathValues);
\r
737 Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);
\r
738 if (records != null) {
\r
739 for (CSVRecord record : records) {
\r
740 CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record);
\r
741 if (kpiData != null) {
\r
742 cldsVfKPIDataList.add(kpiData);
\r
746 return cldsVfKPIDataList;
\r
747 } catch (IOException e) {
\r
748 logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e);
\r
749 return new ArrayList<>();
\r
753 private void processRecord(List<CldsAlarmCondition> cldsAlarmConditionList, CSVRecord record) {
\r
754 if (record == null) {
\r
757 if (record.size() < 5) {
\r
758 logger.debug("invalid csv alarm Record,total columns less than 5: " + record);
\r
761 if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3))
\r
762 || StringUtils.isBlank(record.get(4))) {
\r
763 logger.debug("invalid csv alarm Record,one of column is having blank value : " + record);
\r
766 CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition();
\r
767 cldsAlarmCondition.setEventSourceType(record.get(1));
\r
768 cldsAlarmCondition.setEventName(record.get(2));
\r
769 cldsAlarmCondition.setAlarmConditionKey(record.get(3));
\r
770 cldsAlarmCondition.setSeverity(record.get(4));
\r
771 cldsAlarmConditionList.add(cldsAlarmCondition);
\r
774 // Get the responses for the current artifact from the artifacts URL.
\r
775 private String getResponsesFromArtifactUrl(String artifactsUrl) {
\r
776 String hostUrl = refProp.getStringValue("sdc.hostUrl");
\r
777 String artifactsUrlReworked = artifactsUrl.replaceAll("\"", "");
\r
778 String artifactUrl = hostUrl + artifactsUrlReworked;
\r
779 logger.info("value of artifactURl:" + artifactUrl);
\r
780 String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl);
\r
781 logger.info("value of artifactResponse:" + currArtifactResponse);
\r
782 return currArtifactResponse;
\r
786 * Service to services/resources/artifacts from sdc.Pass alarmConditions as
\r
787 * true to get alarm conditons from artifact url and else it is false
\r
790 * The URL to trigger
\r
791 * @return The String containing the payload
\r
793 public String getCldsServicesOrResourcesBasedOnURL(String url) {
\r
794 Date startTime = new Date();
\r
796 LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL");
\r
797 String urlReworked = removeUnwantedBracesFromString(url);
\r
798 URL urlObj = new URL(urlReworked);
\r
799 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
\r
800 String basicAuth = getSdcBasicAuth();
\r
801 conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP);
\r
802 conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
\r
803 conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
\r
804 conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId());
\r
805 conn.setRequestMethod("GET");
\r
806 int responseCode = conn.getResponseCode();
\r
807 logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode);
\r
808 try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
\r
809 String response = IOUtils.toString(in);
\r
810 LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName());
\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
818 } catch (DecoderException e) {
\r
819 LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
\r
820 LoggingUtils.setErrorContext("900", "Get sdc resources error");
\r
821 logger.error("Exception when attempting to decode the Hex string", e);
\r
822 throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e);
\r
823 } catch (GeneralSecurityException e) {
\r
824 LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName());
\r
825 LoggingUtils.setErrorContext("900", "Get sdc resources error");
\r
826 logger.error("Exception when attempting to decrypt the encrypted password", e);
\r
827 throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e);
\r
829 LoggingUtils.setTimeContext(startTime, new Date());
\r
830 metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed");
\r
835 * To create properties object by using cldsServicedata.
\r
837 * @param globalProps
\r
838 * @param cldsServiceData
\r
840 * @throws IOException
\r
841 * In case of issues during the parsing of the Global Properties
\r
843 public String createPropertiesObjectByUUID(CldsServiceData cldsServiceData) throws IOException {
\r
844 String totalPropsStr;
\r
845 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
846 ObjectNode globalPropsJson = (ObjectNode) refProp.getJsonTemplate(CldsService.GLOBAL_PROPERTIES_KEY);
\r
847 if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) {
\r
848 // Objectnode to save all byservice, byvf , byvfc and byalarm nodes
\r
849 ObjectNode byIdObjectNode = mapper.createObjectNode();
\r
850 // To create vf ResourceUUID node with serviceInvariantUUID
\r
851 ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(cldsServiceData);
\r
852 byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf);
\r
853 // To create byVf and vfcResourceNode with vfResourceUUID
\r
854 ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(cldsServiceData.getCldsVfs());
\r
855 byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid);
\r
857 ObjectNode kpiObjectNode = mapper.createObjectNode();
\r
858 if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
\r
859 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
\r
860 if (currCldsVfData != null) {
\r
861 createKpiObjectNodeByVfUuid(kpiObjectNode, currCldsVfData.getCldsKPIList());
\r
865 byIdObjectNode.putPOJO("byKpi", kpiObjectNode);
\r
866 // To create byVfc and alarmCondition with vfcResourceUUID
\r
867 ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();
\r
868 if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {
\r
869 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
\r
870 if (currCldsVfData != null) {
\r
871 createAlarmCondObjectNodeByVfcUuid(vfcResourceUuidObjectNode, currCldsVfData.getCldsVfcs());
\r
875 byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode);
\r
876 // To create byAlarmCondition with alarmConditionKey
\r
877 List<CldsAlarmCondition> allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
\r
879 ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(allAlarmConditions);
\r
880 byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey);
\r
881 // To create byAlertDescription with AlertDescription
\r
882 List<CldsAlarmCondition> allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
\r
883 "alertDescription");
\r
884 ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(allAlertDescriptions);
\r
885 byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert);
\r
886 globalPropsJson.putPOJO("shared", byIdObjectNode);
\r
887 logger.info("Global properties JSON created with SDC info:" + globalPropsJson);
\r
889 totalPropsStr = globalPropsJson.toString();
\r
890 return totalPropsStr;
\r
894 * Method to get alarm conditions/alert description from Service Data.
\r
896 * @param cldsServiceData
\r
897 * CldsServiceData the Service Data to analyze
\r
899 * The String event name that will be used to filter the alarm
\r
901 * @return The list of CldsAlarmCondition for the event name specified
\r
903 public List<CldsAlarmCondition> getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData,
\r
904 String eventName) {
\r
905 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
\r
906 if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null
\r
907 && !cldsServiceData.getCldsVfs().isEmpty()) {
\r
908 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {
\r
909 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfData(currCldsVfData, eventName));
\r
912 return alarmCondList;
\r
916 * Method to get alarm conditions/alert description from VF Data.
\r
918 * @param currCldsVfData
\r
919 * The Vf Data to analyze
\r
921 * The String event name that will be used to filter the alarm
\r
923 * @return The list of CldsAlarmCondition for the event name specified
\r
925 private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfData(CldsVfData currCldsVfData, String eventName) {
\r
926 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
\r
927 if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
\r
928 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
\r
929 alarmCondList.addAll(getAllAlarmConditionsFromCldsVfcData(currCldsVfcData, eventName));
\r
932 return alarmCondList;
\r
936 * Method to get alarm conditions/alert description from VFC Data.
\r
938 * @param currCldsVfcData
\r
939 * The VfC Data to analyze
\r
941 * The String event name that will be used to filter the alarm
\r
943 * @return The list of CldsAlarmCondition for the event name specified
\r
945 private List<CldsAlarmCondition> getAllAlarmConditionsFromCldsVfcData(CldsVfcData currCldsVfcData,
\r
946 String eventName) {
\r
947 List<CldsAlarmCondition> alarmCondList = new ArrayList<>();
\r
948 if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null
\r
949 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
\r
950 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
\r
951 if (currCldsAlarmCondition != null
\r
952 && currCldsAlarmCondition.getEventName().equalsIgnoreCase(eventName)) {
\r
953 alarmCondList.add(currCldsAlarmCondition);
\r
957 return alarmCondList;
\r
960 private ObjectNode createAlarmCondObjectNodeByAlarmKey(List<CldsAlarmCondition> cldsAlarmCondList) {
\r
961 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
962 ObjectNode alarmCondKeyNode = mapper.createObjectNode();
\r
963 if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) {
\r
964 for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {
\r
965 if (currCldsAlarmCondition != null) {
\r
966 ObjectNode alarmCondNode = mapper.createObjectNode();
\r
967 alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType());
\r
968 alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity());
\r
969 alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode);
\r
973 ObjectNode alarmCondNode = mapper.createObjectNode();
\r
974 alarmCondNode.put("eventSourceType", "");
\r
975 alarmCondNode.put("eventSeverity", "");
\r
976 alarmCondKeyNode.putPOJO("", alarmCondNode);
\r
978 return alarmCondKeyNode;
\r
981 private ObjectNode createVfObjectNodeByServiceInvariantUuid(CldsServiceData cldsServiceData) {
\r
982 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
983 ObjectNode invariantUuidObjectNode = mapper.createObjectNode();
\r
984 ObjectNode vfObjectNode = mapper.createObjectNode();
\r
985 ObjectNode vfUuidNode = mapper.createObjectNode();
\r
986 List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();
\r
987 if (cldsVfsList != null && !cldsVfsList.isEmpty()) {
\r
988 for (CldsVfData currCldsVfData : cldsVfsList) {
\r
989 if (currCldsVfData != null) {
\r
990 vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());
\r
994 vfUuidNode.put("", "");
\r
996 vfObjectNode.putPOJO("vf", vfUuidNode);
\r
997 invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode);
\r
998 return invariantUuidObjectNode;
\r
1001 private void createKpiObjectNodeByVfUuid(ObjectNode vfResourceUuidObjectNode,
\r
1002 List<CldsVfKPIData> cldsVfKpiDataList) {
\r
1003 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
1004 if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) {
\r
1005 for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) {
\r
1006 if (currCldsVfKpiData != null) {
\r
1007 ObjectNode thresholdNameObjectNode = mapper.createObjectNode();
\r
1008 ObjectNode fieldPathObjectNode = mapper.createObjectNode();
\r
1009 ObjectNode nfNamingCodeNode = mapper.createObjectNode();
\r
1010 fieldPathObjectNode.put(currCldsVfKpiData.getFieldPathValue(),
\r
1011 currCldsVfKpiData.getFieldPathValue());
\r
1012 nfNamingCodeNode.put(currCldsVfKpiData.getNfNamingValue(), currCldsVfKpiData.getNfNamingValue());
\r
1013 thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode);
\r
1014 thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode);
\r
1015 vfResourceUuidObjectNode.putPOJO(currCldsVfKpiData.getThresholdValue(), thresholdNameObjectNode);
\r
1021 private void createAlarmCondObjectNodeByVfcUuid(ObjectNode vfcResourceUuidObjectNode,
\r
1022 List<CldsVfcData> cldsVfcDataList) {
\r
1023 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
1024 ObjectNode vfcObjectNode = mapper.createObjectNode();
\r
1025 ObjectNode alarmCondNode = mapper.createObjectNode();
\r
1026 ObjectNode alertDescNode = mapper.createObjectNode();
\r
1027 if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) {
\r
1028 for (CldsVfcData currCldsVfcData : cldsVfcDataList) {
\r
1029 if (currCldsVfcData != null) {
\r
1030 if (currCldsVfcData.getCldsAlarmConditions() != null
\r
1031 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
\r
1032 for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {
\r
1033 if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) {
\r
1034 alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
\r
1035 currCldsAlarmCondition.getAlarmConditionKey());
\r
1037 alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(),
\r
1038 currCldsAlarmCondition.getAlarmConditionKey());
\r
1042 vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
\r
1043 vfcObjectNode.putPOJO("alertDescription", alertDescNode);
\r
1044 vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode);
\r
1048 alarmCondNode.put("", "");
\r
1049 vfcObjectNode.putPOJO("alarmCondition", alarmCondNode);
\r
1050 alertDescNode.put("", "");
\r
1051 vfcObjectNode.putPOJO("alertDescription", alarmCondNode);
\r
1052 vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode);
\r
1057 * Method to create vfc and kpi nodes inside vf node
\r
1060 * @param cldsVfDataList
\r
1063 private ObjectNode createVfcObjectNodeByVfUuid(List<CldsVfData> cldsVfDataList) {
\r
1064 ObjectMapper mapper = JacksonUtils.getObjectMapperInstance();
\r
1065 ObjectNode vfUuidObjectNode = mapper.createObjectNode();
\r
1066 if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {
\r
1067 for (CldsVfData currCldsVfData : cldsVfDataList) {
\r
1068 if (currCldsVfData != null) {
\r
1069 ObjectNode vfObjectNode = mapper.createObjectNode();
\r
1070 ObjectNode vfcUuidNode = mapper.createObjectNode();
\r
1071 ObjectNode kpiObjectNode = mapper.createObjectNode();
\r
1072 if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {
\r
1073 for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {
\r
1074 if (currCldsVfcData.getCldsAlarmConditions() != null
\r
1075 && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {
\r
1076 vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),
\r
1077 currCldsVfcData.getVfcName());
\r
1081 vfcUuidNode.put("", "");
\r
1083 if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) {
\r
1084 for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) {
\r
1085 kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(),
\r
1086 currCldsVfKPIData.getThresholdValue());
\r
1089 kpiObjectNode.put("", "");
\r
1091 vfObjectNode.putPOJO("vfc", vfcUuidNode);
\r
1092 vfObjectNode.putPOJO("kpi", kpiObjectNode);
\r
1093 vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode);
\r
1097 ObjectNode vfcUuidNode = mapper.createObjectNode();
\r
1098 vfcUuidNode.put("", "");
\r
1099 ObjectNode vfcObjectNode = mapper.createObjectNode();
\r
1100 vfcObjectNode.putPOJO("vfc", vfcUuidNode);
\r
1101 vfUuidObjectNode.putPOJO("", vfcObjectNode);
\r
1103 return vfUuidObjectNode;
\r
1107 * This method searches the equivalent artifact UUID for a specific
\r
1108 * artifactName in a SdcServiceDetail.
\r
1110 * @param cldsSdcServiceDetail
\r
1111 * The SdcServiceDetail that will be analyzed
\r
1112 * @param artifactName
\r
1113 * The artifact name that will be searched
\r
1114 * @return The artifact UUID found
\r
1116 public String getArtifactIdIfArtifactAlreadyExists(SdcServiceDetail cldsSdcServiceDetail, String artifactName) {
\r
1117 String artifactUuid = null;
\r
1118 boolean artifactExists = false;
\r
1119 if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null
\r
1120 && !cldsSdcServiceDetail.getResources().isEmpty()) {
\r
1121 for (SdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) {
\r
1122 if (artifactExists) {
\r
1125 if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null
\r
1126 && !currCldsSdcResource.getArtifacts().isEmpty()) {
\r
1127 for (SdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) {
\r
1128 if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null
\r
1129 && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) {
\r
1130 artifactUuid = currCldsSdcArtifact.getArtifactUUID();
\r
1131 artifactExists = true;
\r
1138 return artifactUuid;
\r
1141 // To get all sdc VF/VFC Resources basic info.
\r
1142 private List<SdcResourceBasicInfo> getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) {
\r
1143 String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME);
\r
1144 String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType;
\r
1145 String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl);
\r
1146 return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources));
\r
1149 private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid,
\r
1150 List<SdcResourceBasicInfo> resourceInfoList) {
\r
1151 String resourceUuid = null;
\r
1152 if (resourceInfoList != null && !resourceInfoList.isEmpty()) {
\r
1153 for (SdcResourceBasicInfo currResource : resourceInfoList) {
\r
1154 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null
\r
1155 && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) {
\r
1156 resourceUuid = currResource.getUuid();
\r
1161 return resourceUuid;
\r
1164 // Method to get service invariant uuid from model properties.
\r
1165 private String getServiceInvariantUuidFromProps(ModelProperties props) {
\r
1166 String invariantUuid = "";
\r
1167 Global globalProps = props.getGlobal();
\r
1168 if (globalProps != null && globalProps.getService() != null) {
\r
1169 invariantUuid = globalProps.getService();
\r
1171 return invariantUuid;
\r
1175 * This method upload the BluePrint to SDC.
\r
1178 * The Clds model Properties
\r
1180 * The user id for SDC
\r
1181 * @param sdcReqUrlsList
\r
1182 * The list of SDC URL to try
\r
1183 * @param formattedSdcReq
\r
1184 * The blueprint to upload
\r
1185 * @param formattedSdcLocationReq
\r
1186 * THe location Blueprint to upload
\r
1187 * @param artifactName
\r
1188 * The artifact name from where we can get the Artifact UUID
\r
1189 * @param locationArtifactName
\r
1190 * The location artifact name from where we can get the Artifact
\r
1192 * @throws GeneralSecurityException
\r
1193 * In case of issues with the decryting the encrypted password
\r
1194 * @throws DecoderException
\r
1195 * In case of issues with the decoding of the Hex String
\r
1197 public void uploadToSdc(ModelProperties prop, String userid, List<String> sdcReqUrlsList, String formattedSdcReq,
\r
1198 String formattedSdcLocationReq, String artifactName, String locationArtifactName)
\r
1199 throws GeneralSecurityException, DecoderException {
\r
1200 logger.info("userid=" + userid);
\r
1201 String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop);
\r
1202 if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {
\r
1203 for (String url : sdcReqUrlsList) {
\r
1204 if (url != null) {
\r
1205 String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
\r
1206 logger.info("ServiceUUID used before upload in url:" + originalServiceUuid);
\r
1207 String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid);
\r
1208 SdcServiceDetail cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
\r
1209 String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
\r
1211 // Upload artifacts to sdc
\r
1212 String updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
\r
1213 String responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcReq);
\r
1214 logger.info("value of sdc Response of uploading to sdc :" + responseStr);
\r
1215 String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
\r
1216 if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) {
\r
1217 url = url.replace(originalServiceUuid, updatedServiceUuid);
\r
1219 logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid);
\r
1220 sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid);
\r
1221 cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
\r
1222 uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail,
\r
1223 locationArtifactName);
\r
1224 // To send location information also to sdc
\r
1225 updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url;
\r
1226 responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcLocationReq);
\r
1227 logger.info("value of sdc Response of uploading location to sdc :" + responseStr);
\r
1234 * Method to delete blueprint and location json artifacts from sdc
\r
1238 * @param sdcReqUrlsList
\r
1239 * @param artifactName
\r
1240 * @param locationArtifactName
\r
1241 * @throws GeneralSecurityException
\r
1242 * @throws DecoderException
\r
1244 public void deleteArtifactsFromSdc(ModelProperties prop, String userid, List<String> sdcReqUrlsList,
\r
1245 String artifactName, String locationArtifactName) throws GeneralSecurityException, DecoderException {
\r
1246 String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop);
\r
1247 for (String url : sdcReqUrlsList) {
\r
1248 String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
\r
1249 logger.info("ServiceUUID used before deleting in url:" + originalServiceUuid);
\r
1250 String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid);
\r
1251 SdcServiceDetail cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
\r
1252 String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, artifactName);
\r
1253 String responseStr = deleteArtifact(userid, url, uploadedArtifactUuid);
\r
1254 logger.info("value of sdc Response of deleting blueprint from sdc :" + responseStr);
\r
1255 String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid);
\r
1256 if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) {
\r
1257 url = url.replace(originalServiceUuid, updatedServiceUuid);
\r
1259 logger.info("ServiceUUID used after delete in ulr:" + updatedServiceUuid);
\r
1260 sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid);
\r
1261 cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation);
\r
1262 uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, locationArtifactName);
\r
1263 responseStr = deleteArtifact(userid, url, uploadedArtifactUuid);
\r
1264 logger.info("value of sdc Response of deleting location json from sdc :" + responseStr);
\r
1268 private String deleteArtifact(String userid, String url, String uploadedArtifactUuid) {
\r
1270 String responseStr = "";
\r
1271 if (uploadedArtifactUuid != null && !uploadedArtifactUuid.isEmpty()) {
\r
1272 logger.info("userid=" + userid);
\r
1273 String basicAuth = getSdcBasicAuth();
\r
1274 String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID");
\r
1275 url = url + "/" + uploadedArtifactUuid;
\r
1276 URL urlObj = new URL(url);
\r
1277 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
\r
1278 conn.setDoOutput(true);
\r
1279 conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), sdcXonapInstanceId);
\r
1280 conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth);
\r
1281 conn.setRequestProperty("USER_ID", userid);
\r
1282 conn.setRequestMethod("DELETE");
\r
1283 conn.setRequestProperty("charset", "utf-8");
\r
1284 conn.setUseCaches(false);
\r
1285 conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME),
\r
1286 LoggingUtils.getRequestId());
\r
1287 boolean requestFailed = true;
\r
1288 int responseCode = conn.getResponseCode();
\r
1289 logger.info("responseCode=" + responseCode);
\r
1290 if (responseCode == 200) {
\r
1291 requestFailed = false;
\r
1293 responseStr = getResponse(conn);
\r
1294 if (responseStr != null && requestFailed) {
\r
1295 logger.error("requestFailed - responseStr=" + responseStr);
\r
1296 throw new BadRequestException(responseStr);
\r
1299 return responseStr;
\r
1300 } catch (IOException | DecoderException | GeneralSecurityException e) {
\r
1301 logger.error("Exception when attempting to communicate with SDC", e);
\r
1302 throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e);
\r