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