2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.nbi.apis.serviceorder;
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;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
37 public class MultiClient {
40 private RestTemplate restTemplate;
43 private String aaiHost;
45 @Value("${aai.header.authorization}")
46 private String aaiHeaderAuthorization;
48 @Value("${aai.api.id}")
49 private String aaiApiId;
51 @Value("${onap.lcpCloudRegionId}")
52 private String lcpCloudRegionId;
54 @Value("${onap.tenantId}")
55 private String tenantId;
57 @Value("${onap.cloudOwner}")
58 private String cloudOwner;
61 private ServiceCatalogUrl serviceCatalogUrl;
64 private ServiceInventoryUrl serviceInventoryUrl;
67 private static final String HEADER_AUTHORIZATION = "Authorization";
68 private static final String X_FROM_APP_ID = "X-FromAppId";
70 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
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);
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);
84 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
85 if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
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");
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);
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"))) {
120 public boolean isCustomerPresentInAAI(String customerId) {
121 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
123 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
124 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
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");
137 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
139 ResponseEntity<Object> response = putRequest(param, callURL, buildRequestHeaderForAAI());
140 if (response != null && response.getStatusCode().equals(HttpStatus.CREATED)) {
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);
152 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
153 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
154 return (LinkedHashMap) response.getBody();
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)) {
172 private ResponseEntity<Object> putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
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());
182 } catch (BackendFunctionalException e) {
183 LOGGER.error("error on calling " + callUrl + " ," + e);
188 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
193 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
195 for (String paramName : param.keySet()) {
196 builder.queryParam(paramName, param.get(paramName));
199 URI uri = builder.build().encode().toUri();
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());
212 } catch (BackendFunctionalException e) {
213 LOGGER.error("error on calling " + callURL + " ," + e);