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