2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
14 package org.onap.nbi.apis.serviceinventory;
16 import java.util.LinkedHashMap;
18 import javax.annotation.PostConstruct;
19 import org.onap.nbi.OnapComponentsUrlPaths;
20 import org.onap.nbi.exceptions.BackendFunctionalException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.http.HttpHeaders;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.http.converter.StringHttpMessageConverter;
28 import org.springframework.stereotype.Service;
31 public class AaiClient extends BaseClient {
33 public static final String CUSTOMER_ID = "$customerId";
36 private String aaiHost;
38 @Value("${aai.header.authorization}")
39 private String aaiHeaderAuthorization;
41 @Value("${aai.api.id}")
42 private String aaiApiId;
44 @Value("${aai.header.transaction.id}")
45 private String aaiTransactionId;
47 private static final String HEADER_AUTHORIZATION = "Authorization";
48 private static final String X_FROM_APP_ID = "X-FromAppId";
49 private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
50 private static final String X_TRANSACTION_ID = "X-TransactionId";
52 private String aaiServiceUrl;
53 private String aaiServiceCustomerUrl;
54 private String aaiServicesUrl;
55 private String aaiServicesInstancesUrl;
56 private String aaiHealthCheckUrl;
59 private void setUpAndlogAAIUrl() {
60 aaiServiceUrl = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE).toString();
61 aaiServiceCustomerUrl =
62 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_CUSTOMER).toString();
63 aaiServicesUrl = new StringBuilder().append(aaiHost)
64 .append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH).toString();
65 aaiServicesInstancesUrl = new StringBuilder().append(aaiHost)
66 .append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH).toString();
68 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_HEALTH_CHECK).toString();
70 LOGGER.info("AAI service url : " + aaiServiceUrl);
71 LOGGER.info("AAI services url : " + aaiServicesUrl);
72 LOGGER.info("AAI service instances url : " + aaiServicesInstancesUrl);
73 LOGGER.info("AAI aaiHealthCheckUrl : " + aaiHealthCheckUrl);
77 private HttpHeaders buildRequestHeaderForAAI() {
79 HttpHeaders httpHeaders = new HttpHeaders();
80 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
81 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
82 httpHeaders.add("Accept", "application/json");
83 httpHeaders.add("Content-Type", "application/json");
84 httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
90 public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
92 String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
93 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
94 callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
96 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
97 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
98 return (LinkedHashMap) response.getBody();
103 public Map getService(String serviceId) {
104 // Retrieve the Service Instance using AAI node query
105 String callUrlFormated = aaiServiceUrl.replace("$serviceId", serviceId);
106 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
107 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
108 return (LinkedHashMap) response.getBody();
113 public Map getServiceCustomer(String serviceId) {
115 String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
117 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
118 return (LinkedHashMap) response.getBody();
119 } catch (BackendFunctionalException e) {
120 LOGGER.error("error on calling {} , {}", callUrlFormated.toString(), e);
125 public void callCheckConnectivity() {
126 String customersUrl = new StringBuilder().append(aaiHealthCheckUrl).toString();
127 ResponseEntity<String> response = callApiGetHealthCheck(customersUrl, buildRequestHeaderForAAI());
130 public Map getVNF(String relatedLink) {
132 StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
134 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
135 return (LinkedHashMap) response.getBody();
136 } catch (BackendFunctionalException e) {
137 LOGGER.error("error on calling {} , {}", callURL.toString(), e);
142 public Map getServicesInAaiForCustomer(String customerId) {
143 String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
145 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
146 return (LinkedHashMap) response.getBody();
147 } catch (BackendFunctionalException e) {
148 LOGGER.error("error on calling {} , {}", callUrlFormated, e);
153 public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
154 String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
155 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
158 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
159 return (LinkedHashMap) response.getBody();
160 } catch (BackendFunctionalException e) {
161 LOGGER.error("error on calling {} , {}", callUrlFormated, e);