2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with 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
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
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.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;
58 private void setUpAndlogAAIUrl() {
59 aaiServiceUrl = new StringBuilder().append(aaiHost)
60 .append(OnapComponentsUrlPaths.AAI_GET_SERVICE).toString();
61 aaiServiceCustomerUrl = new StringBuilder().append(aaiHost)
62 .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();
69 LOGGER.info("AAI service url : " + aaiServiceUrl);
70 LOGGER.info("AAI services url : " + aaiServicesUrl);
71 LOGGER.info("AAI service instances url : " + aaiServicesInstancesUrl);
76 private HttpHeaders buildRequestHeaderForAAI() {
78 HttpHeaders httpHeaders = new HttpHeaders();
79 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
80 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
81 httpHeaders.add("Accept", "application/json");
82 httpHeaders.add("Content-Type", "application/json");
83 httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
89 public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
91 String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
92 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
93 callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
95 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
96 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
97 return (LinkedHashMap) response.getBody();
102 public Map getService(String serviceId) {
103 // Retrieve the Service Instance using AAI node query
104 String callUrlFormated = aaiServiceUrl.replace("$serviceId", serviceId);
105 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
106 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
107 return (LinkedHashMap) response.getBody();
112 public Map getServiceCustomer(String serviceId) {
114 String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
115 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
116 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
117 return (LinkedHashMap) response.getBody();
122 public Map getVNF(String relatedLink) {
124 StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
126 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
127 return (LinkedHashMap) response.getBody();
128 } catch (BackendFunctionalException e) {
129 LOGGER.error("error on calling {} , {}", callURL.toString(), e);
134 public Map getServicesInAaiForCustomer(String customerId) {
135 String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
137 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
138 return (LinkedHashMap) response.getBody();
139 } catch (BackendFunctionalException e) {
140 LOGGER.error("error on calling {} , {}", callUrlFormated, e);
145 public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
146 String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
147 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
150 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
151 return (LinkedHashMap) response.getBody();
152 } catch (BackendFunctionalException e) {
153 LOGGER.error("error on calling {} , {}", callUrlFormated, e);