9b777d802a334436ee1c88486eac7713320cf9d0
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / MultiClient.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
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
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nbi.apis.serviceorder;
17
18 import org.onap.nbi.OnapComponentsUrlPaths;
19 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
20 import org.onap.nbi.exceptions.BackendFunctionalException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.http.*;
26 import org.springframework.stereotype.Service;
27 import org.springframework.web.client.RestTemplate;
28 import org.springframework.web.util.UriComponentsBuilder;
29
30 import java.net.URI;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 @Service
37 public class MultiClient {
38
39     @Autowired
40     private RestTemplate restTemplate;
41
42     @Value("${aai.host}")
43     private String aaiHost;
44
45     @Value("${aai.header.authorization}")
46     private String aaiHeaderAuthorization;
47
48     @Value("${aai.api.id}")
49     private String aaiApiId;
50
51     @Value("${onap.lcpCloudRegionId}")
52     private String lcpCloudRegionId;
53
54     @Value("${onap.tenantId}")
55     private String tenantId;
56
57     @Value("${onap.cloudOwner}")
58     private String cloudOwner;
59
60     @Autowired
61     private ServiceCatalogUrl serviceCatalogUrl;
62
63     @Autowired
64     private ServiceInventoryUrl serviceInventoryUrl;
65
66
67     private static final String HEADER_AUTHORIZATION = "Authorization";
68     private static final String X_FROM_APP_ID = "X-FromAppId";
69
70     private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
71
72     public ResponseEntity<Object> getServiceCatalog(String id) {
73         StringBuilder callURL = new StringBuilder().append(serviceCatalogUrl.getServiceCatalogUrl()).append(id);
74         ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), null);
75         return response;
76     }
77
78     public boolean doesServiceExistInServiceInventory(String id, String serviceName, String globalSubscriberId) {
79         StringBuilder callURL = new StringBuilder().append(serviceInventoryUrl.getServiceInventoryUrl()).append(id);
80         Map<String, String> param = new HashMap<>();
81         param.put("serviceSpecification.name", serviceName);
82         param.put("relatedParty.id", globalSubscriberId);
83
84         ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
85         if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
86             return false;
87         }
88         return true;
89     }
90
91
92     private HttpHeaders buildRequestHeaderForAAI() {
93         HttpHeaders httpHeaders = new HttpHeaders();
94         httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
95         httpHeaders.add(X_FROM_APP_ID, aaiApiId);
96         httpHeaders.add("Accept", "application/json");
97         httpHeaders.add("Content-Type", "application/json");
98         return httpHeaders;
99     }
100
101
102     public boolean isTenantIdPresentInAAI() {
103         StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_TENANTS_PATH);
104         String callUrlFormated = callURL.toString().replace("$onap.lcpCloudRegionId", lcpCloudRegionId);
105         callUrlFormated = callUrlFormated.replace("$onap.cloudOwner", cloudOwner);
106
107         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
108         if (response != null) {
109             LinkedHashMap body = (LinkedHashMap) response.getBody();
110             List<LinkedHashMap> tenants = (List<LinkedHashMap>) body.get("tenant");
111             for (LinkedHashMap tenant : tenants) {
112                 if (tenantId.equalsIgnoreCase((String) tenant.get("tenant-id"))) {
113                     return true;
114                 }
115             }
116         }
117         return false;
118     }
119
120     public boolean isCustomerPresentInAAI(String customerId) {
121         StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
122                 .append(customerId);
123         ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
124         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
125             return true;
126         }
127         return false;
128     }
129
130
131     public boolean putCustomer(SubscriberInfo subscriberInfo) {
132         Map<String, String> param = new HashMap<>();
133         param.put("global-customer-id", subscriberInfo.getGlobalSubscriberId());
134         param.put("subscriber-name", subscriberInfo.getSubscriberName());
135         param.put("subscriber-type", "BSS");
136         String callURL =
137                 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
138
139         ResponseEntity<Object> response = putRequest(param, callURL, buildRequestHeaderForAAI());
140         if (response != null && response.getStatusCode().equals(HttpStatus.CREATED)) {
141             return true;
142         }
143         return false;
144     }
145
146
147     public LinkedHashMap getServicesInAaiForCustomer(String customerId) {
148         StringBuilder callURL =
149                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
150         String callUrlFormated = callURL.toString().replace("$customerId", customerId);
151
152         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
153         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
154             return (LinkedHashMap) response.getBody();
155         }
156         return null;
157     }
158
159     public boolean putServiceType(String globalSubscriberId, String serviceName) {
160         Map<String, String> param = new HashMap<>();
161         param.put("service-type", serviceName);
162         String callURL = aaiHost + OnapComponentsUrlPaths.AAI_PUT_SERVICE_FOR_CUSTOMER_PATH + serviceName;
163         String callUrlFormated = callURL.replace("$customerId", globalSubscriberId);
164         ResponseEntity<Object> response =  putRequest(param, callUrlFormated, buildRequestHeaderForAAI());
165         if (response != null && response.getStatusCode().equals(HttpStatus.CREATED)) {
166             return true;
167         }
168         return false;
169     }
170
171
172     private  ResponseEntity<Object> putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
173         try {
174             ResponseEntity<Object> response =
175                     restTemplate.exchange(callUrl, HttpMethod.PUT, new HttpEntity<>(param, httpHeaders), Object.class);
176             LOGGER.info("response status : " + response.getStatusCodeValue());
177             if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
178                 LOGGER.warn("HTTP call on " + callUrl + " returns " + response.getStatusCodeValue() + ", "
179                         + response.getBody().toString());
180             }
181             return response;
182         } catch (BackendFunctionalException e) {
183             LOGGER.error("error on calling " + callUrl + " ," + e);
184             return null;
185         }
186     }
187
188     private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
189
190
191         try {
192
193             UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
194             if (param != null) {
195                 for (String paramName : param.keySet()) {
196                     builder.queryParam(paramName, param.get(paramName));
197                 }
198             }
199             URI uri = builder.build().encode().toUri();
200
201
202             ResponseEntity<Object> response =
203                     restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(httpHeaders), Object.class);
204             LOGGER.debug("response body : " + response.getBody().toString());
205             LOGGER.info("response status : " + response.getStatusCodeValue());
206             if (!response.getStatusCode().equals(HttpStatus.OK)) {
207                 LOGGER.error("HTTP call on " + callURL + " returns " + response.getStatusCodeValue() + ", "
208                         + response.getBody().toString());
209             }
210             return response;
211
212         } catch (BackendFunctionalException e) {
213             LOGGER.error("error on calling " + callURL + " ," + e);
214             return null;
215         }
216     }
217
218
219 }