2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.nbi.apis.serviceinventory;
18 import java.util.LinkedHashMap;
20 import javax.annotation.PostConstruct;
21 import org.onap.nbi.OnapComponentsUrlPaths;
22 import org.onap.nbi.exceptions.BackendFunctionalException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Service;
32 public class AaiClient extends BaseClient {
34 public static final String CUSTOMER_ID = "$customerId";
37 private String aaiHost;
39 @Value("${aai.header.authorization}")
40 private String aaiHeaderAuthorization;
42 @Value("${aai.api.id}")
43 private String aaiApiId;
45 @Value("${aai.header.transaction.id}")
46 private String aaiTransactionId;
48 private static final String HEADER_AUTHORIZATION = "Authorization";
49 private static final String X_FROM_APP_ID = "X-FromAppId";
50 private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
51 private static final String X_TRANSACTION_ID = "X-TransactionId";
54 private String aaiServiceUrl;
55 private String aaiServicesUrl;
56 private String aaiServicesInstancesUrl;
59 private void setUpAndlogAAIUrl() {
60 aaiServiceUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_FOR_CUSTOMER_PATH).toString();
61 aaiServicesUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH).toString();
62 aaiServicesInstancesUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH).toString();
65 LOGGER.info("AAI service url : "+aaiServiceUrl);
66 LOGGER.info("AAI services url : "+aaiServicesUrl);
67 LOGGER.info("AAI service instances url : "+aaiServicesInstancesUrl);
72 private HttpHeaders buildRequestHeaderForAAI() {
74 HttpHeaders httpHeaders = new HttpHeaders();
75 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
76 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
77 httpHeaders.add("Accept", "application/json");
78 httpHeaders.add("Content-Type", "application/json");
79 httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
85 public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
87 String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
88 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
89 callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
91 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
92 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
93 return (LinkedHashMap) response.getBody();
99 public Map getVNF(String relatedLink) {
101 StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
103 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
104 return (LinkedHashMap) response.getBody();
105 } catch (BackendFunctionalException e) {
106 LOGGER.error("error on calling {} , {}" , callURL.toString(), e);
111 public Map getServicesInAaiForCustomer(String customerId) {
112 String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
114 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
115 return (LinkedHashMap) response.getBody();
116 } catch (BackendFunctionalException e) {
117 LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
122 public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
123 String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
124 callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
127 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
128 return (LinkedHashMap) response.getBody();
129 } catch (BackendFunctionalException e) {
130 LOGGER.error("error on calling {} , {}" , callUrlFormated, e);