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