2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai;
23 import static com.fasterxml.jackson.module.kotlin.ExtensionsKt.jacksonObjectMapper;
24 import static java.util.Collections.emptyList;
25 import static java.util.Comparator.comparing;
26 import static java.util.stream.Collectors.toMap;
27 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
28 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
29 import static org.apache.commons.lang3.StringUtils.isEmpty;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import java.io.IOException;
34 import java.io.UnsupportedEncodingException;
36 import java.net.URLEncoder;
37 import java.util.LinkedHashMap;
38 import java.util.List;
40 import java.util.UUID;
41 import java.util.function.Function;
42 import java.util.stream.Stream;
43 import javax.inject.Inject;
44 import javax.ws.rs.WebApplicationException;
45 import javax.ws.rs.core.Response;
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.http.HttpStatus;
48 import org.apache.http.client.utils.URIBuilder;
49 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
50 import org.jetbrains.annotations.NotNull;
51 import org.json.simple.JSONArray;
52 import org.json.simple.JSONObject;
53 import org.json.simple.parser.JSONParser;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.vid.aai.exceptions.InvalidAAIResponseException;
56 import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
57 import org.onap.vid.aai.model.AaiGetInstanceGroupsByCloudRegion;
58 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.AaiGetNetworkCollectionDetails;
59 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.AaiGetNetworkCollectionDetailsHelper;
60 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.AaiGetRelatedInstanceGroupsByVnfId;
61 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.CloudRegion;
62 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.InstanceGroup;
63 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Network;
64 import org.onap.vid.aai.model.AaiGetOperationalEnvironments.OperationalEnvironmentList;
65 import org.onap.vid.aai.model.AaiGetPnfResponse;
66 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
67 import org.onap.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone;
68 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
69 import org.onap.vid.aai.model.CustomQuerySimpleResult;
70 import org.onap.vid.aai.model.GetServiceModelsByDistributionStatusResponse;
71 import org.onap.vid.aai.model.LogicalLinkResponse;
72 import org.onap.vid.aai.model.ModelVer;
73 import org.onap.vid.aai.model.ModelVersions;
74 import org.onap.vid.aai.model.OwningEntityResponse;
75 import org.onap.vid.aai.model.PortDetailsTranslator;
76 import org.onap.vid.aai.model.ProjectResponse;
77 import org.onap.vid.aai.model.Properties;
78 import org.onap.vid.aai.model.ResourceType;
79 import org.onap.vid.aai.model.ServiceRelationships;
80 import org.onap.vid.aai.model.SimpleResult;
81 import org.onap.vid.aai.util.AAIRestInterface;
82 import org.onap.vid.aai.util.CacheProvider;
83 import org.onap.vid.aai.util.VidObjectMapperType;
84 import org.onap.vid.exceptions.GenericUncheckedException;
85 import org.onap.vid.model.SubscriberList;
86 import org.onap.vid.model.probes.ErrorMetadata;
87 import org.onap.vid.model.probes.ExternalComponentStatus;
88 import org.onap.vid.model.probes.HttpRequestMetadata;
89 import org.onap.vid.utils.Logging;
90 import org.onap.vid.utils.Unchecked;
91 import org.springframework.http.HttpMethod;
92 import org.springframework.web.util.UriUtils;
94 public class AaiClient implements AaiClientInterface {
97 public static final String QUERY_FORMAT_RESOURCE = "query?format=resource";
98 private static final String SERVICE_SUBSCRIPTIONS_PATH = "/service-subscriptions/service-subscription/";
99 private static final String MODEL_INVARIANT_ID = "&model-invariant-id=";
100 private static final String QUERY_FORMAT_SIMPLE = "query?format=simple";
101 private static final String BUSINESS_CUSTOMER = "/business/customers/customer/";
102 private static final String SERVICE_INSTANCE = "/service-instances/service-instance/";
103 private static final String BUSINESS_CUSTOMERS_CUSTOMER = "business/customers/customer/";
105 protected String fromAppId = "VidAaiController";
107 private PortDetailsTranslator portDetailsTranslator;
109 private final AAIRestInterface restController;
111 private final CacheProvider cacheProvider;
113 ObjectMapper objectMapper = jacksonObjectMapper();
118 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiClient.class);
120 private static final String GET_SERVICE_MODELS_REQUEST_BODY = "{\"start\" : \"service-design-and-creation/models/\", \"query\" : \"query/serviceModels-byDistributionStatus?distributionStatus=DISTRIBUTION_COMPLETE_OK\"}";
123 public AaiClient(AAIRestInterface restController, PortDetailsTranslator portDetailsTranslator, CacheProvider cacheProvider) {
124 this.restController = restController;
125 this.portDetailsTranslator = portDetailsTranslator;
126 this.cacheProvider = cacheProvider;
130 private static String checkForNull(String local) {
139 public AaiResponse getServicesByOwningEntityId(List<String> owningEntityIds){
140 Response resp = doAaiGet(getUrlFromLIst("business/owning-entities?", "owning-entity-id=", owningEntityIds), false);
141 return processAaiResponse(resp, OwningEntityResponse.class, null);
145 public AaiResponse getServicesByProjectNames(List<String> projectNames){
146 Response resp = doAaiGet(getUrlFromLIst("business/projects?", "project-name=", projectNames), false);
147 return processAaiResponse(resp, ProjectResponse.class, null);
151 public AaiResponse getServiceModelsByDistributionStatus() {
152 return getFromCache("getServiceModelsByDistributionStatus", this::getServiceModelsByDistributionStatusNonCached,
153 true, "Failed to get service models by distribution status");
156 private AaiResponse getServiceModelsByDistributionStatusNonCached(boolean propagateExceptions) {
157 GetServiceModelsByDistributionStatusResponse response = typedAaiRest(QUERY_FORMAT_RESOURCE, GetServiceModelsByDistributionStatusResponse.class,
158 GET_SERVICE_MODELS_REQUEST_BODY, HttpMethod.PUT, propagateExceptions);
159 return new AaiResponse(response, "", HttpStatus.SC_OK);
163 public AaiResponse getNetworkCollectionDetails(String serviceInstanceId) {
164 Response resp = doAaiPut(QUERY_FORMAT_RESOURCE, "{\"start\": [\"nodes/service-instances/service-instance/" + serviceInstanceId + "\"],\"query\": \"query/network-collection-ByServiceInstance\"}\n", false);
165 AaiResponse<AaiGetNetworkCollectionDetailsHelper> aaiResponse = processAaiResponse(resp, AaiGetNetworkCollectionDetailsHelper.class, null, VidObjectMapperType.FASTERXML);
166 return getNetworkCollectionDetailsResponse(aaiResponse);
170 public AaiResponse getInstanceGroupsByCloudRegion(String cloudOwner, String cloudRegionId, String networkFunction) {
171 Response resp = doAaiPut(QUERY_FORMAT_RESOURCE,
172 "{\"start\": [\"cloud-infrastructure/cloud-regions/cloud-region/" + encodePathSegment(cloudOwner) + "/" + encodePathSegment(cloudRegionId) + "\"]," +
173 "\"query\": \"query/instance-groups-byCloudRegion?type=L3-NETWORK&role=SUB-INTERFACE&function=" + encodePathSegment(networkFunction) + "\"}\n", false);
174 return processAaiResponse(resp, AaiGetInstanceGroupsByCloudRegion.class, null, VidObjectMapperType.FASTERXML);
177 private AaiResponse getNetworkCollectionDetailsResponse(AaiResponse<AaiGetNetworkCollectionDetailsHelper> aaiResponse){
178 if(aaiResponse.getHttpCode() == 200) {
179 ObjectMapper om = objectMapper;
180 AaiGetNetworkCollectionDetails aaiGetNetworkCollectionDetails = new AaiGetNetworkCollectionDetails();
182 for (int i = 0; i < aaiResponse.getT().getResults().size(); i++) {
183 LinkedHashMap<String, Object> temp = ((LinkedHashMap) aaiResponse.getT().getResults().get(i));
184 if (temp.get("service-instance") != null)
185 aaiGetNetworkCollectionDetails.getResults().setServiceInstance(om.readValue(om.writeValueAsString(temp.get("service-instance")), org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.ServiceInstance.class));
186 else if (temp.get("collection") != null)
187 aaiGetNetworkCollectionDetails.getResults().setCollection(om.readValue(om.writeValueAsString(temp.get("collection")), org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Collection.class));
188 else if (temp.get("instance-group") != null)
189 aaiGetNetworkCollectionDetails.getResults().setInstanceGroup(om.readValue(om.writeValueAsString(temp.get("instance-group")), InstanceGroup.class));
190 else if (temp.get("l3-network") != null)
191 aaiGetNetworkCollectionDetails.getResults().getNetworks().add(om.readValue(om.writeValueAsString(temp.get("l3-network")), Network.class));
193 return new AaiResponse(aaiGetNetworkCollectionDetails, null, HttpStatus.SC_OK);
195 catch (com.fasterxml.jackson.databind.JsonMappingException e) {
196 logger.error(EELFLoggerDelegate.errorLogger, "AAI response parsing Error", e);
197 return new AaiResponse(e.getCause(), "AAI response parsing Error" , HttpStatus.SC_INTERNAL_SERVER_ERROR);
199 catch (Exception e) {
200 logger.error(EELFLoggerDelegate.errorLogger,"Exception in aai response parsing", e);
201 return new AaiResponse(e.getCause(), "Got " + aaiResponse.getHttpCode() + " from a&ai" , aaiResponse.getHttpCode());
208 public AaiResponse getPNFData(String globalCustomerId, String serviceType, String modelVersionId, String modelInvariantId, String cloudRegion, String equipVendor, String equipModel) {
209 String siQuery = BUSINESS_CUSTOMER + globalCustomerId + SERVICE_SUBSCRIPTIONS_PATH + encodePathSegment(serviceType) + "/service-instances?model-version-id=" + modelVersionId + MODEL_INVARIANT_ID + modelInvariantId;
210 String pnfQuery = "query/pnf-fromModel-byRegion?cloudRegionId=" + encodePathSegment(cloudRegion) + "&equipVendor=" + encodePathSegment(equipVendor) + "&equipModel=" + encodePathSegment(equipModel);
211 String payload = "{\"start\":\"" + siQuery + "\",\"query\":\"" + pnfQuery + "\"}";
212 Response resp = doAaiPut(QUERY_FORMAT_SIMPLE, payload, false);
213 return processAaiResponse(resp, AaiGetPnfResponse.class, null);
218 public AaiResponse<Pnf> getSpecificPnf(String pnfId) {
219 Response resp = doAaiGet("network/pnfs/pnf/"+pnfId, false);
220 return processAaiResponse(resp, Pnf.class, null);
224 public AaiResponse getInstanceGroupsByVnfInstanceId(String vnfInstanceId){
225 Response resp = doAaiGet("network/generic-vnfs/generic-vnf/" + vnfInstanceId + "?depth=0", false);
226 return processAaiResponse(resp, AaiGetRelatedInstanceGroupsByVnfId.class , null, null);
231 public List<PortDetailsTranslator.PortDetails> getPortMirroringSourcePorts(String configurationID) {
232 String payload = "{\"start\":\"/network/configurations/configuration/" + configurationID + "\",\"query\":\"query/pserver-fromConfiguration\"}";
233 Response resp = doAaiPut(QUERY_FORMAT_SIMPLE, payload, false);
234 resp.bufferEntity(); // avoid later "Entity input stream has already been closed" problems
235 String rawPayload = resp.readEntity(String.class);
236 AaiResponse<CustomQuerySimpleResult> aaiResponse = processAaiResponse(resp, CustomQuerySimpleResult.class, rawPayload);
237 return portDetailsTranslator.extractPortDetails(aaiResponse, rawPayload);
242 public AaiResponse getServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId) {
243 String getServiceInstancePath = BUSINESS_CUSTOMERS_CUSTOMER + globalCustomerId+ SERVICE_SUBSCRIPTIONS_PATH +serviceType+ SERVICE_INSTANCE +serviceInstanceId;
244 Response resp = doAaiGet(getServiceInstancePath , false);
245 return processAaiResponse(resp, ServiceRelationships.class, null);
249 public AaiResponse getLogicalLink(String link) {
250 Response resp = doAaiGet("network/logical-links/logical-link/" + link , false);
251 return processAaiResponse(resp, LogicalLinkResponse.class, null);
255 public boolean isNodeTypeExistsByName(String name, ResourceType type) {
257 throw new GenericUncheckedException("Empty resource-name provided to searchNodeTypeByName; request is rejected as this will cause full resources listing");
260 URI path = Unchecked.toURI(String.format( // e.g. GET /aai/v$/nodes/vf-modules?vf-module-name={vf-module-name}
263 type.getNameFilter(),
264 encodePathSegment(name)
266 final ResponseWithRequestInfo responseWithRequestInfo = restController.RestGet(fromAppId, UUID.randomUUID().toString(), path, false, true);
268 return isResourceExistByStatusCode(responseWithRequestInfo);
271 public Map<String, Properties> getCloudRegionAndTenantByVnfId(String vnfId) {
272 String start = "/network/generic-vnfs/generic-vnf/" + vnfId;
273 String query = "/query/cloud-region-fromVnf";
275 String payload = "{\"start\":[\"" + start + "\"],\"query\":\"" + query + "\"}";
276 CustomQuerySimpleResult result = typedAaiRest(QUERY_FORMAT_SIMPLE, CustomQuerySimpleResult.class, payload, HttpMethod.PUT, false);
278 return result.getResults().stream()
279 .filter(res -> StringUtils.equals(res.getNodeType(), "tenant") ||
280 StringUtils.equals(res.getNodeType(), "cloud-region"))
281 .collect(toMap(SimpleResult::getNodeType, SimpleResult::getProperties));
284 private boolean isResourceExistByStatusCode(ResponseWithRequestInfo responseWithRequestInfo) {
286 // 404 - resource not found
287 Response.Status statusInfo = responseWithRequestInfo.getResponse().getStatusInfo().toEnum();
288 switch (statusInfo) {
294 throw new GenericUncheckedException("Unexpected response-code (only OK and NOT_FOUND are expected): " +
295 responseWithRequestInfo.getResponse().getStatusInfo());
300 public <T> T typedAaiGet(URI uri, Class<T> clz) {
301 return typedAaiRest(uri, clz, null, HttpMethod.GET, false);
304 public <T> T typedAaiRest(String path, Class<T> clz, String payload, HttpMethod method, boolean propagateExceptions) {
305 return typedAaiRest(Unchecked.toURI(path), clz, payload, method, propagateExceptions);
309 public <T> T typedAaiRest(URI path, Class<T> clz, String payload, HttpMethod method, boolean propagateExceptions) {
310 ResponseWithRequestInfo responseWithRequestInfo;
312 responseWithRequestInfo = restController.doRest(fromAppId, UUID.randomUUID().toString(), path, payload, method, false, propagateExceptions);
313 } catch (Exception e) {
314 responseWithRequestInfo = handleExceptionFromRestCall(propagateExceptions, "doAai"+method.name(), e);
317 final AaiResponseWithRequestInfo<T> aaiResponse = processAaiResponse(responseWithRequestInfo, clz, VidObjectMapperType.FASTERXML, true);
319 if (aaiResponse.getAaiResponse().getHttpCode() > 399 || aaiResponse.getAaiResponse().getT() == null) {
320 throw new ExceptionWithRequestInfo(aaiResponse.getHttpMethod(),
321 aaiResponse.getRequestedUrl(),
322 aaiResponse.getRawData(),
323 responseWithRequestInfo.getResponse().getStatus(),
324 new InvalidAAIResponseException(aaiResponse.getAaiResponse()));
327 return aaiResponse.getAaiResponse().getT();
331 private String getUrlFromLIst(String url, String paramKey, List<String> params){
333 for(String param: params){
335 url = url.concat(paramKey);
336 String encodedParam= param;
338 encodedParam= URLEncoder.encode(param, "UTF-8");
339 } catch (UnsupportedEncodingException e) {
340 String methodName = "getUrlFromList";
341 logger.error(EELFLoggerDelegate.errorLogger, methodName, e);
342 logger.debug(EELFLoggerDelegate.debugLogger, methodName, e);
344 url = url.concat(encodedParam);
345 if(i != params.size()){
346 url = url.concat("&");
355 public AaiResponse<SubscriberList> getAllSubscribers() {
356 return getFromCache("getAllSubscribers", this::getAllSubscribersNonCached, true, "Failed to get all subscribers");
359 private <K> AaiResponse getFromCache(String cacheName, Function<K, AaiResponse> function, K argument, String errorMessage) {
362 .aaiClientCacheFor(cacheName, function)
364 } catch (ExceptionWithRequestInfo exception) {
365 logger.error(errorMessage, exception);
366 return new AaiResponse(null, exception.getRawData(), exception.getHttpCode());
368 catch (Exception exception) {
369 logger.error(errorMessage, exception);
370 return new AaiResponse(null, exception.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
374 private AaiResponse<SubscriberList> getAllSubscribersNonCached(boolean propagateExceptions) {
375 AaiResponse<SubscriberList> aaiResponse = getAllSubscribers(propagateExceptions).getAaiResponse();
376 if (propagateExceptions && (aaiResponse.getT() == null || aaiResponse.getT().customer == null || aaiResponse.getT().customer.isEmpty())) {
377 throw new GenericUncheckedException("Failed to get Subscribers data. The data is null or empty.");
383 AaiResponseWithRequestInfo<SubscriberList> getAllSubscribers(boolean propagateExceptions){
385 ResponseWithRequestInfo aaiGetResult = doAaiGet("business/customers?subscriber-type=INFRA&depth=" + depth, false, propagateExceptions);
386 AaiResponseWithRequestInfo<SubscriberList> responseWithRequestInfo = processAaiResponse(aaiGetResult, SubscriberList.class, propagateExceptions);
387 responseWithRequestInfo.setRequestedUrl(aaiGetResult.getRequestUrl());
388 responseWithRequestInfo.setHttpMethod(aaiGetResult.getRequestHttpMethod());
389 return responseWithRequestInfo;
394 public AaiResponse getAllAicZones() {
395 Response resp = doAaiGet("network/zones", false);
396 return processAaiResponse(resp, AicZones.class, null);
400 public AaiResponse getVNFData(String globalSubscriberId, String serviceType) {
401 String payload = "{\"start\": [\"business/customers/customer/" + globalSubscriberId + SERVICE_SUBSCRIPTIONS_PATH + encodePathSegment(serviceType) +"/service-instances\"]," +
402 "\"query\": \"query/vnf-topology-fromServiceInstance\"}";
403 Response resp = doAaiPut(QUERY_FORMAT_SIMPLE, payload, false);
404 return processAaiResponse(resp, AaiGetVnfResponse.class, null);
408 public AaiResponse getVNFData(String globalSubscriberId, String serviceType, String serviceInstanceId) {
409 String payload = "{\"start\": [\"/business/customers/customer/" + globalSubscriberId + SERVICE_SUBSCRIPTIONS_PATH + encodePathSegment(serviceType) + SERVICE_INSTANCE + serviceInstanceId + "\"], \"query\": \"query/vnf-topology-fromServiceInstance\"}";
410 Response resp = doAaiPut(QUERY_FORMAT_SIMPLE, payload, false);
411 return processAaiResponse(resp, AaiGetVnfResponse.class, null);
415 public Response getVersionByInvariantId(List<String> modelInvariantId) {
416 if (modelInvariantId.isEmpty()) {
417 throw new GenericUncheckedException("Zero invariant-ids provided to getVersionByInvariantId; request is rejected as this will cause full models listing");
420 StringBuilder sb = new StringBuilder();
421 for (String id : modelInvariantId){
422 sb.append(MODEL_INVARIANT_ID);
426 return doAaiGet("service-design-and-creation/models?depth=2" + sb.toString(), false);
430 public ModelVer getLatestVersionByInvariantId(String modelInvariantId) {
431 if (modelInvariantId.isEmpty()) {
432 throw new GenericUncheckedException("no invariant-id provided to getLatestVersionByInvariantId; request is rejected");
435 Response response = doAaiPut("query?format=resource&depth=0", "{\"start\": [\"service-design-and-creation/models/model/" + modelInvariantId + "\"],\"query\": \"query/serviceModels-byDistributionStatus?distributionStatus=DISTRIBUTION_COMPLETE_OK\"}",false);
436 AaiResponse<ModelVersions> aaiResponse = processAaiResponse(response, ModelVersions.class, null, VidObjectMapperType.FASTERXML);
438 Stream<ModelVer> modelVerStream = toModelVerStream(aaiResponse.getT());
439 return maxModelVer(modelVerStream);
442 protected Stream<ModelVer> toModelVerStream(ModelVersions modelVersions) {
443 if (modelVersions == null)
446 if (modelVersions == null)
449 return Stream.of(modelVersions)
450 .map(ModelVersions::getResults)
451 .flatMap(java.util.Collection::stream)
452 .flatMap(map -> map.entrySet().stream())
453 .filter(kv -> StringUtils.equals(kv.getKey(), "model-ver"))
454 .map(Map.Entry::getValue);
458 protected ModelVer maxModelVer(Stream<ModelVer> modelVerStream) {
459 if (modelVerStream == null)
462 return modelVerStream
463 .filter(modelVer -> StringUtils.isNotEmpty(modelVer.getModelVersion()))
464 .max(comparing(ModelVer::getModelVersion, comparing(DefaultArtifactVersion::new)))
465 .orElseThrow(() -> new GenericUncheckedException("Could not find any version"));
469 public AaiResponse getSubscriberData(String subscriberId, boolean omitServiceInstances) {
470 String depth = omitServiceInstances ? "1" : "2";
471 AaiResponse subscriberDataResponse;
472 Response resp = doAaiGet(BUSINESS_CUSTOMERS_CUSTOMER + subscriberId + "?depth=" + depth, false);
473 subscriberDataResponse = processAaiResponse(resp, Services.class, null);
474 return subscriberDataResponse;
478 public AaiResponse getServices() {
479 Response resp = doAaiGet("service-design-and-creation/services", false);
480 return processAaiResponse(resp, GetServicesAAIRespone.class, null);
484 public AaiResponse getOperationalEnvironments(String operationalEnvironmentType, String operationalEnvironmentStatus) {
485 String url = "cloud-infrastructure/operational-environments";
486 URIBuilder urlBuilder = new URIBuilder();
487 if (operationalEnvironmentType != null)
488 urlBuilder.addParameter("operational-environment-type", operationalEnvironmentType);
489 if (operationalEnvironmentStatus != null)
490 urlBuilder.addParameter("operational-environment-status", operationalEnvironmentStatus);
491 url += urlBuilder.toString();
492 Response resp = doAaiGet(url, false);
493 return processAaiResponse(resp, OperationalEnvironmentList.class, null);
497 public AaiResponse getTenants(String globalCustomerId, String serviceType) {
498 if ((globalCustomerId == null || globalCustomerId.isEmpty()) || ((serviceType == null) || (serviceType.isEmpty()))){
499 return buildAaiResponseForGetTenantsFailure(" Failed to retrieve LCP Region & Tenants from A&AI, Subscriber ID or Service Type is missing.");
503 .aaiClientCacheFor("getTenants", this::getTenantsByKey)
504 .get(CacheProvider.compileKey(globalCustomerId, serviceType));
506 catch (ParsingGetTenantsResponseFailure exception) {
507 logger.error("Failed to get tenants ", exception);
508 return buildAaiResponseForGetTenantsFailure(exception.getMessage());
513 public AaiResponse getNodeTemplateInstances(String globalCustomerId, String serviceType, String modelVersionId, String modelInvariantId, String cloudRegion) {
515 String siQuery = BUSINESS_CUSTOMER + globalCustomerId + SERVICE_SUBSCRIPTIONS_PATH + encodePathSegment(serviceType) + "/service-instances?model-version-id=" + modelVersionId + MODEL_INVARIANT_ID + modelInvariantId;
516 String vnfQuery = "query/queryvnfFromModelbyRegion?cloudRegionId=" + encodePathSegment(cloudRegion);
517 String payload1 = "{\"start\":\"" + siQuery + "\",\"query\":\"" + vnfQuery + "\"}";
519 Response resp1 = doAaiPut(QUERY_FORMAT_SIMPLE, payload1, false);
520 AaiResponse aaiResponse1 = processAaiResponse(resp1, AaiGetVnfResponse.class, null);
521 logger.debug(EELFLoggerDelegate.debugLogger, "getNodeTemplateInstances AAI's response: {}", aaiResponse1);
526 public AaiResponse<JsonNode> getCloudRegionAndSourceByPortMirroringConfigurationId(String configurationId) {
527 final String start = "[\"network/configurations/configuration/" + configurationId + "\"]";
528 final String query = "\"query/cloud-region-and-source-FromConfiguration\"";
529 String payload = "{\"start\":" + start + ",\"query\":" + query + "}";
531 Response response = doAaiPut("query?format=simple&nodesOnly=true", payload, false);
532 AaiResponse<JsonNode> aaiResponse = processAaiResponse(response, JsonNode.class, null);
534 logger.debug(EELFLoggerDelegate.debugLogger, "getNodeTemplateInstances AAI's response: {}", aaiResponse);
538 private <T> AaiResponseWithRequestInfo<T> processAaiResponse(ResponseWithRequestInfo responseWithRequestInfo, Class<? extends T> classType, boolean propagateExceptions) {
539 return processAaiResponse(responseWithRequestInfo, classType, VidObjectMapperType.CODEHAUS, propagateExceptions);
542 private <T> AaiResponseWithRequestInfo<T> processAaiResponse(ResponseWithRequestInfo responseWithRequestInfo, Class<? extends T> classType, VidObjectMapperType omType, boolean propagateExceptions) {
543 String responseBody = null;
544 Integer responseHttpCode = null;
546 Response response = responseWithRequestInfo.getResponse();
547 responseHttpCode = (response != null) ? response.getStatus() : null;
548 responseBody = (response != null) ? response.readEntity(String.class) : null;
549 AaiResponse<T> processedAaiResponse = processAaiResponse(response, classType, responseBody, omType, propagateExceptions);
550 return new AaiResponseWithRequestInfo<>(responseWithRequestInfo.getRequestHttpMethod(), responseWithRequestInfo.getRequestUrl(), processedAaiResponse,
552 } catch (Exception e) {
553 throw new ExceptionWithRequestInfo(responseWithRequestInfo.getRequestHttpMethod(),
554 responseWithRequestInfo.getRequestUrl(), responseBody, responseHttpCode, e);
558 private AaiResponse processAaiResponse(Response resp, Class classType, String responseBody) {
559 return processAaiResponse(resp, classType, responseBody, VidObjectMapperType.CODEHAUS);
562 private <T> AaiResponse<T> processAaiResponse(Response resp, Class<? extends T> classType, String responseBody, VidObjectMapperType omType) {
563 return processAaiResponse(resp, classType, responseBody, omType, false);
566 private <T> AaiResponse<T> processAaiResponse(Response resp, Class<? extends T> classType, String responseBody, VidObjectMapperType omType, boolean propagateExceptions) {
567 AaiResponse<T> subscriberDataResponse;
569 subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
570 logger.debug(EELFLoggerDelegate.debugLogger, "Invalid response from AAI");
572 logger.debug(EELFLoggerDelegate.debugLogger, "getSubscribers() resp=" + resp.getStatusInfo().toString());
573 if (resp.getStatus() != HttpStatus.SC_OK) {
574 subscriberDataResponse = processFailureResponse(resp,responseBody);
576 subscriberDataResponse = processOkResponse(resp, classType, responseBody, omType, propagateExceptions);
579 return subscriberDataResponse;
582 private AaiResponse processFailureResponse(Response resp, String responseBody) {
583 logger.debug(EELFLoggerDelegate.debugLogger, "Invalid response from AAI");
585 if (responseBody != null) {
586 rawData = responseBody;
588 rawData = resp.readEntity(String.class);
590 return new AaiResponse<>(null, rawData, resp.getStatus());
593 private <T> AaiResponse<T> processOkResponse(Response resp, Class<? extends T> classType, String responseBody, VidObjectMapperType omType, boolean propagateExceptions) {
594 AaiResponse<T> subscriberDataResponse;
595 String finalResponse = null;
597 if (responseBody != null) {
598 finalResponse = responseBody;
600 finalResponse = resp.readEntity(String.class);
603 if(omType == VidObjectMapperType.CODEHAUS)
604 subscriberDataResponse = parseCodeHausObject(classType, finalResponse);
606 subscriberDataResponse = parseFasterXmlObject(classType, finalResponse);
608 } catch(Exception e){
609 if (propagateExceptions) {
610 throw new GenericUncheckedException(e);
612 subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
613 logger.error("Failed to parse aai response: \"{}\" to class {}", finalResponse, classType, e);
616 return subscriberDataResponse;
619 private <T> AaiResponse<T> parseFasterXmlObject(Class<? extends T> classType, String finalResponse) throws IOException {
620 return new AaiResponse<>((objectMapper.readValue(finalResponse, classType)), null, HttpStatus.SC_OK);
623 private <T> AaiResponse<T> parseCodeHausObject(Class<? extends T> classType, String finalResponse) throws IOException {
624 return new AaiResponse<>((objectMapper.readValue(finalResponse, classType)), null, HttpStatus.SC_OK);
628 public Response doAaiGet(String uri, boolean xml) {
629 return doAaiGet(uri, xml, false).getResponse();
633 public ResponseWithRequestInfo doAaiGet(String uri, boolean xml, boolean propagateExceptions) {
634 return doAaiGet(Unchecked.toURI(uri), xml, propagateExceptions);
637 public ResponseWithRequestInfo doAaiGet(URI uri, boolean xml, boolean propagateExceptions) {
638 String methodName = "doAaiGet";
639 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
641 ResponseWithRequestInfo resp;
643 resp = restController.RestGet(fromAppId, UUID.randomUUID().toString(), uri, xml, propagateExceptions);
645 } catch (Exception e) {
646 resp = handleExceptionFromRestCall(propagateExceptions, methodName, e);
652 protected ResponseWithRequestInfo handleExceptionFromRestCall(boolean propagateExceptions, String methodName, Exception e) {
653 ResponseWithRequestInfo resp;
654 if (propagateExceptions) {
655 throw (e instanceof RuntimeException) ? (RuntimeException)e : new GenericUncheckedException(e);
657 final Exception actual =
658 e instanceof ExceptionWithRequestInfo ? (Exception) e.getCause() : e;
660 final String message =
661 actual instanceof WebApplicationException ? ((WebApplicationException) actual).getResponse().readEntity(String.class) : e.toString();
663 //ToDo: change parameter of requestUrl to real url from doRest function
664 resp = new ResponseWithRequestInfo(null, null, org.springframework.http.HttpMethod.GET);
665 logger.info(EELFLoggerDelegate.errorLogger, methodName + message);
666 logger.debug(EELFLoggerDelegate.debugLogger, methodName + message);
671 private String parseForTenantsByServiceSubscription(String relatedToKey, String resp) {
672 String tenantList = "";
675 JSONParser jsonParser = new JSONParser();
677 JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
679 return parseServiceSubscriptionObjectForTenants(relatedToKey, jsonObject);
680 } catch (Exception ex) {
681 logger.debug(EELFLoggerDelegate.debugLogger, "parseForTenantsByServiceSubscription error while parsing tenants by service subscription", ex);
686 protected Response doAaiPut(String uri, String payload, boolean xml) {
687 return doAaiPut(uri, payload, xml, false).getResponse();
690 protected ResponseWithRequestInfo doAaiPut(String uri, String payload, boolean xml, boolean propagateExceptions) {
691 String methodName = "doAaiPut";
692 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
694 ResponseWithRequestInfo resp;
697 resp = restController.RestPut(fromAppId, uri, payload, xml, propagateExceptions);
699 } catch (Exception e) {
700 resp = handleExceptionFromRestCall(propagateExceptions, methodName, e);
706 private String parseServiceSubscriptionObjectForTenants(String relatedToKey, JSONObject jsonObject) {
707 JSONArray tenantArray = new JSONArray();
708 boolean bconvert = false;
710 JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");
711 if (relationShipListsObj != null) {
712 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
713 for (Object innerObj : defaultIfNull(rShipArray, emptyList())) {
714 if (innerObj != null) {
715 bconvert = parseTenant(relatedToKey, tenantArray, bconvert, (JSONObject) innerObj);
719 } catch (NullPointerException ex) {
720 logger.debug(EELFLoggerDelegate.debugLogger, "parseServiceSubscriptionObjectForTenants. error while parsing service subscription object for tenants", ex);
724 return tenantArray.toJSONString();
730 private static boolean parseTenant(String relatedToKey, JSONArray tenantArray, boolean bconvert, JSONObject inner1Obj) {
731 String relatedTo = checkForNull((String) inner1Obj.get("related-to"));
732 if (relatedTo.equalsIgnoreCase(relatedToKey)) {
733 JSONObject tenantNewObj = new JSONObject();
735 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
736 tenantNewObj.put("link", relatedLink);
738 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
739 for (Object innerObj : defaultIfNull(rDataArray, emptyList())) {
740 parseRelationShip(tenantNewObj, (JSONObject) innerObj);
743 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
744 for (Object innerObj : defaultIfNull(relatedTPropArray, emptyList())) {
745 parseRelatedTProp(tenantNewObj, (JSONObject) innerObj);
748 tenantArray.add(tenantNewObj);
753 private static void parseRelatedTProp(JSONObject tenantNewObj, JSONObject innerObj) {
754 if (innerObj == null)
757 String propKey = checkForNull((String) innerObj.get("property-key"));
758 String propVal = checkForNull((String) innerObj.get("property-value"));
759 if (equalsIgnoreCase(propKey, "tenant.tenant-name")) {
760 tenantNewObj.put("tenantName", propVal);
764 private static void parseRelationShip(JSONObject tenantNewObj, JSONObject inner2Obj) {
765 if (inner2Obj == null)
768 String rShipKey = checkForNull((String) inner2Obj.get("relationship-key"));
769 String rShipVal = checkForNull((String) inner2Obj.get("relationship-value"));
770 if (equalsIgnoreCase(rShipKey, "cloud-region.cloud-owner")) {
771 tenantNewObj.put("cloudOwner", rShipVal);
772 } else if (equalsIgnoreCase(rShipKey, "cloud-region.cloud-region-id")) {
773 tenantNewObj.put("cloudRegionID", rShipVal);
774 } else if (equalsIgnoreCase(rShipKey, "tenant.tenant-id")) {
775 tenantNewObj.put("tenantID", rShipVal);
779 private static String encodePathSegment(String segmentToEncode) {
780 return UriUtils.encodePathSegment(segmentToEncode, "UTF-8");
784 public ExternalComponentStatus probeComponent(){
785 long startTime = System.currentTimeMillis();
787 AaiResponseWithRequestInfo<SubscriberList> responseWithRequestInfo = getAllSubscribers(true);
788 AaiResponse<SubscriberList> aaiResponse = responseWithRequestInfo.getAaiResponse();
789 long duration = System.currentTimeMillis() - startTime;
791 SubscriberList subscribersList = (aaiResponse != null) ? aaiResponse.getT() : null;
792 boolean isAvailable = subscribersList != null && subscribersList.customer != null && !subscribersList.customer.isEmpty();
794 HttpRequestMetadata metadata = new HttpRequestMetadata(
795 responseWithRequestInfo.getHttpMethod(),
796 (aaiResponse != null) ? aaiResponse.getHttpCode() : 0,
797 responseWithRequestInfo.getRequestedUrl(),
798 responseWithRequestInfo.getRawData(),
799 isAvailable ? "OK" : "No subscriber received",
802 return new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, isAvailable, metadata);
804 } catch (ExceptionWithRequestInfo e) {
805 long duration = System.currentTimeMillis() - startTime;
806 return new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, false,
807 new HttpRequestMetadata(e, duration));
808 } catch (Exception e) {
809 long duration = System.currentTimeMillis() - startTime;
810 return new ExternalComponentStatus(ExternalComponentStatus.Component.AAI, false,
811 new ErrorMetadata(Logging.exceptionToDescription(e), duration));
816 public String getCloudOwnerByCloudRegionId(String cloudRegionId) {
818 .aaiClientCacheFor("getCloudOwnerByCloudRegionId", this::getCloudOwnerByCloudRegionIdNonCached)
824 public GetTenantsResponse getHomingDataByVfModule(String vnfInstanceId, String vfModuleId) {
826 if (isEmpty(vnfInstanceId)|| isEmpty(vfModuleId)){
827 throw new GenericUncheckedException("Failed to retrieve homing data associated to vfModule from A&AI, VNF InstanceId or VF Module Id is missing.");
829 Response resp = doAaiGet("network/generic-vnfs/generic-vnf/" + vnfInstanceId +"/vf-modules/vf-module/"+ vfModuleId, false);
830 String responseAsString = parseForTenantsByServiceSubscription("vserver",resp.readEntity(String.class));
831 if (isEmpty(responseAsString)){
832 throw new GenericUncheckedException( String.format("A&AI has no homing data associated to vfModule '%s' of vnf '%s'", vfModuleId, vnfInstanceId));
835 AaiResponse aaiResponse = processAaiResponse(resp, GetTenantsResponse[].class, responseAsString);
836 return ((GetTenantsResponse[])aaiResponse.getT())[0];
841 public void resetCache(String cacheName) {
842 cacheProvider.resetCache(cacheName);
845 String getCloudOwnerByCloudRegionIdNonCached(String cloudRegionId) {
846 String uri = "cloud-infrastructure/cloud-regions?cloud-region-id=" + encodePathSegment(cloudRegionId);
848 final CloudRegion.Collection cloudRegionCollection =
849 typedAaiGet(Unchecked.toURI(uri), CloudRegion.Collection.class);
851 return cloudRegionCollection
852 .getCloudRegions().stream()
853 .map(CloudRegion::getCloudOwner)
854 // from here we assure that the cloud owner is given, and not null
855 // and non-empty, and that if more than one cloud-owner is given -
856 // it is only a single value.
857 // exception is thrown if none or more than a single values are
859 .filter(StringUtils::isNotEmpty)
862 // will be invoked only if distinct() leaves more than a single element
863 throw new GenericUncheckedException("Conflicting cloud-owner found for " + cloudRegionId + ": '" + a + "' / '" + b + "'");
865 .orElseThrow(() -> new GenericUncheckedException("No cloud-owner found for " + cloudRegionId));
868 private AaiResponse getTenantsByKey(String key) {
869 String[] args = CacheProvider.decompileKey(key);
870 String globalCustomerId = safeGetFromArray(args, 0);
871 String serviceType = safeGetFromArray(args, 1);
872 return getTenantsNonCached(globalCustomerId, serviceType);
875 AaiResponse getTenantsNonCached(String globalCustomerId, String serviceType) {
876 String url = BUSINESS_CUSTOMERS_CUSTOMER + globalCustomerId + SERVICE_SUBSCRIPTIONS_PATH + serviceType;
878 Response resp = doAaiGet(url, false);
879 String responseAsString = parseForTenantsByServiceSubscription("tenant",resp.readEntity(String.class));
880 if (isEmpty(responseAsString)){
881 throw new ParsingGetTenantsResponseFailure(String.format("A&AI has no LCP Region & Tenants associated to subscriber '%s' and service type '%s'", globalCustomerId, serviceType));
884 return processAaiResponse(resp, GetTenantsResponse[].class, responseAsString);
888 public static class ParsingGetTenantsResponseFailure extends GenericUncheckedException {
890 public ParsingGetTenantsResponseFailure(String message) {
896 private AaiResponse<String> buildAaiResponseForGetTenantsFailure(String errorText) {
897 return new AaiResponse<>(null, String.format("{\"statusText\":\"%s\"}", errorText), HttpStatus.SC_INTERNAL_SERVER_ERROR);
900 private static String safeGetFromArray(String[] array, int i) {
901 if (i < 0 || i >= array.length) {