Format Java code with respect to ONAP Code Style
[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
14 package org.onap.nbi.apis.serviceinventory;
15
16 import java.util.LinkedHashMap;
17 import java.util.Map;
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;
29
30 @Service
31 public class AaiClient extends BaseClient {
32
33     public static final String CUSTOMER_ID = "$customerId";
34
35     @Value("${aai.host}")
36     private String aaiHost;
37
38     @Value("${aai.header.authorization}")
39     private String aaiHeaderAuthorization;
40
41     @Value("${aai.api.id}")
42     private String aaiApiId;
43
44     @Value("${aai.header.transaction.id}")
45     private String aaiTransactionId;
46
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";
51
52     private String aaiServiceUrl;
53     private String aaiServiceCustomerUrl;
54     private String aaiServicesUrl;
55     private String aaiServicesInstancesUrl;
56     private String aaiHealthCheckUrl;
57
58     @PostConstruct
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();
67         aaiHealthCheckUrl =
68                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_HEALTH_CHECK).toString();
69
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);
74
75     }
76
77     private HttpHeaders buildRequestHeaderForAAI() {
78
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);
85
86         return httpHeaders;
87
88     }
89
90     public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
91
92         String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
93         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
94         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
95
96         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
97         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
98             return (LinkedHashMap) response.getBody();
99         }
100         return null;
101     }
102
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();
109         }
110         return null;
111     }
112
113     public Map getServiceCustomer(String serviceId) {
114
115         String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
116         try {
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);
121             return null;
122         }
123     }
124
125     public void callCheckConnectivity() {
126         String customersUrl = new StringBuilder().append(aaiHealthCheckUrl).toString();
127         ResponseEntity<String> response = callApiGetHealthCheck(customersUrl, buildRequestHeaderForAAI());
128     }
129
130     public Map getVNF(String relatedLink) {
131
132         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
133         try {
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);
138             return null;
139         }
140     }
141
142     public Map getServicesInAaiForCustomer(String customerId) {
143         String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
144         try {
145             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
146             return (LinkedHashMap) response.getBody();
147         } catch (BackendFunctionalException e) {
148             LOGGER.error("error on calling {} , {}", callUrlFormated, e);
149             return null;
150         }
151     }
152
153     public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
154         String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
155         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
156
157         try {
158             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
159             return (LinkedHashMap) response.getBody();
160         } catch (BackendFunctionalException e) {
161             LOGGER.error("error on calling {} , {}", callUrlFormated, e);
162             return null;
163         }
164     }
165 }