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.
13 package org.onap.nbi.apis.serviceinventory;
15 import java.util.LinkedHashMap;
17 import javax.annotation.PostConstruct;
18 import org.onap.nbi.OnapComponentsUrlPaths;
19 import org.onap.nbi.exceptions.BackendFunctionalException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.http.HttpHeaders;
24 import org.springframework.http.HttpStatus;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.http.converter.StringHttpMessageConverter;
27 import org.springframework.stereotype.Service;
30 public class AaiClient extends BaseClient {
32 public static final String CUSTOMER_ID = "$customerId";
35 private String aaiHost;
37 @Value("${aai.header.authorization}")
38 private String aaiHeaderAuthorization;
40 @Value("${aai.api.id}")
41 private String aaiApiId;
43 @Value("${aai.header.transaction.id}")
44 private String aaiTransactionId;
46 private static final String HEADER_AUTHORIZATION = "Authorization";
47 private static final String X_FROM_APP_ID = "X-FromAppId";
48 private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
49 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;
60 private void setUpAndlogAAIUrl() {
61 aaiServiceUrl = new StringBuilder().append(aaiHost)
62 .append(OnapComponentsUrlPaths.AAI_GET_SERVICE).toString();
63 aaiServiceCustomerUrl = new StringBuilder().append(aaiHost)
64 .append(OnapComponentsUrlPaths.AAI_GET_SERVICE_CUSTOMER).toString();
65 aaiServicesUrl = new StringBuilder().append(aaiHost)
66 .append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH).toString();
67 aaiServicesInstancesUrl = new StringBuilder().append(aaiHost)
68 .append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH).toString();
69 aaiHealthCheckUrl = new StringBuilder().append(aaiHost)
70 .append(OnapComponentsUrlPaths.AAI_HEALTH_CHECK).toString();
72 LOGGER.info("AAI service url : " + aaiServiceUrl);
73 LOGGER.info("AAI services url : " + aaiServicesUrl);
74 LOGGER.info("AAI service instances url : " + aaiServicesInstancesUrl);
75 LOGGER.info("AAI aaiHealthCheckUrl : " + aaiHealthCheckUrl);
80 private HttpHeaders buildRequestHeaderForAAI() {
82 HttpHeaders httpHeaders = new HttpHeaders();
83 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
84 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
85 httpHeaders.add("Accept", "application/json");
86 httpHeaders.add("Content-Type", "application/json");
87 httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
93 public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
95 String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
96 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
97 callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
99 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
100 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
101 return (LinkedHashMap) response.getBody();
106 public Map getService(String serviceId) {
107 // Retrieve the Service Instance using AAI node query
108 String callUrlFormated = aaiServiceUrl.replace("$serviceId", serviceId);
109 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
110 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
111 return (LinkedHashMap) response.getBody();
116 public Map getServiceCustomer(String serviceId) {
118 String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
120 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
121 return (LinkedHashMap) response.getBody();
122 } catch (BackendFunctionalException e) {
123 LOGGER.error("error on calling {} , {}", callUrlFormated.toString(), e);
128 public void callCheckConnectivity() {
129 String customersUrl = new StringBuilder().append(aaiHealthCheckUrl).toString();
130 ResponseEntity<String> response = callApiGetHealthCheck(customersUrl, buildRequestHeaderForAAI());
133 public Map getVNF(String relatedLink) {
135 StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
137 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
138 return (LinkedHashMap) response.getBody();
139 } catch (BackendFunctionalException e) {
140 LOGGER.error("error on calling {} , {}", callURL.toString(), e);
145 public Map getServicesInAaiForCustomer(String customerId) {
146 String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
148 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
149 return (LinkedHashMap) response.getBody();
150 } catch (BackendFunctionalException e) {
151 LOGGER.error("error on calling {} , {}", callUrlFormated, e);
156 public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
157 String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
158 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
161 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
162 return (LinkedHashMap) response.getBody();
163 } catch (BackendFunctionalException e) {
164 LOGGER.error("error on calling {} , {}", callUrlFormated, e);