3 * Copyright (c) 2017 Orange. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.nbi.apis.serviceorder;
19 import java.util.HashMap;
20 import java.util.LinkedHashMap;
21 import java.util.List;
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;
40 public class MultiClient {
43 private RestTemplate restTemplate;
46 private String aaiHost;
48 @Value("${aai.header.authorization}")
49 private String aaiHeaderAuthorization;
51 @Value("${aai.api.id}")
52 private String aaiApiId;
54 @Value("${onap.lcpCloudRegionId}")
55 private String lcpCloudRegionId;
57 @Value("${onap.tenantId}")
58 private String tenantId;
60 @Value("${onap.cloudOwner}")
61 private String cloudOwner;
64 private ServiceCatalogUrl serviceCatalogUrl;
67 private ServiceInventoryUrl serviceInventoryUrl;
70 private static final String HEADER_AUTHORIZATION = "Authorization";
71 private static final String X_FROM_APP_ID = "X-FromAppId";
73 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
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);
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);
87 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
88 if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
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");
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);
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"))) {
123 public boolean isCustomerPresentInAAI(String customerId) {
124 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
126 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
127 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
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");
140 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
142 putRequest(param, callURL, buildRequestHeaderForAAI());
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);
151 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
152 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
153 return (LinkedHashMap) response.getBody();
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());
167 private void putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
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());
176 } catch (BackendFunctionalException e) {
177 LOGGER.error("error on calling " + callUrl + " ," + e);
181 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
186 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
188 for (String paramName : param.keySet()) {
189 builder.queryParam(paramName, param.get(paramName));
192 String uriBuilder = builder.build().encode().toUriString();
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());
205 } catch (BackendFunctionalException e) {
206 LOGGER.error("error on calling " + callURL + " ," + e);