0becb0262439d3f0dbafc137f41784d273810750
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceinventory / AaiClient.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  */
13 package org.onap.nbi.apis.serviceinventory;
14
15 import java.util.LinkedHashMap;
16 import java.util.Map;
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;
28
29 @Service
30 public class AaiClient extends BaseClient {
31
32     public static final String CUSTOMER_ID = "$customerId";
33
34     @Value("${aai.host}")
35     private String aaiHost;
36
37     @Value("${aai.header.authorization}")
38     private String aaiHeaderAuthorization;
39
40     @Value("${aai.api.id}")
41     private String aaiApiId;
42
43     @Value("${aai.header.transaction.id}")
44     private String aaiTransactionId;
45
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";
50
51
52     private String aaiServiceUrl;
53     private String aaiServiceCustomerUrl;
54     private String aaiServicesUrl;
55     private String aaiServicesInstancesUrl;
56     private String aaiHealthCheckUrl;
57
58
59     @PostConstruct
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();
71
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);
76
77     }
78
79
80     private HttpHeaders buildRequestHeaderForAAI() {
81
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);
88
89         return httpHeaders;
90
91     }
92
93     public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
94
95         String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
96         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
97         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
98
99         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
100         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
101             return (LinkedHashMap) response.getBody();
102         }
103         return null;
104     }
105
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();
112         }
113         return null;
114     }
115
116     public Map getServiceCustomer(String serviceId) {
117
118         String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
119         try {
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);
124             return null;
125         }
126     }
127
128     public void callCheckConnectivity() {
129         String customersUrl = new StringBuilder().append(aaiHealthCheckUrl).toString();
130         ResponseEntity<String> response = callApiGetHealthCheck(customersUrl, buildRequestHeaderForAAI());
131     }
132
133     public Map getVNF(String relatedLink) {
134
135         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
136         try {
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);
141             return null;
142         }
143     }
144
145     public Map getServicesInAaiForCustomer(String customerId) {
146         String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
147         try {
148             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
149             return (LinkedHashMap) response.getBody();
150         } catch (BackendFunctionalException e) {
151             LOGGER.error("error on calling {} , {}", callUrlFormated, e);
152             return null;
153         }
154     }
155
156     public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
157         String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
158         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
159
160         try {
161             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
162             return (LinkedHashMap) response.getBody();
163         } catch (BackendFunctionalException e) {
164             LOGGER.error("error on calling {} , {}", callUrlFormated, e);
165             return null;
166         }
167     }
168 }