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 java.util.HashMap;
19 import java.util.LinkedHashMap;
20 import java.util.List;
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;
39 public class MultiClient {
42 private RestTemplate restTemplate;
45 private String aaiHost;
47 @Value("${aai.header.authorization}")
48 private String aaiHeaderAuthorization;
50 @Value("${aai.api.id}")
51 private String aaiApiId;
53 @Value("${onap.lcpCloudRegionId}")
54 private String lcpCloudRegionId;
56 @Value("${onap.tenantId}")
57 private String tenantId;
59 @Value("${onap.cloudOwner}")
60 private String cloudOwner;
63 private ServiceCatalogUrl serviceCatalogUrl;
66 private ServiceInventoryUrl serviceInventoryUrl;
69 private static final String HEADER_AUTHORIZATION = "Authorization";
70 private static final String X_FROM_APP_ID = "X-FromAppId";
72 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
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);
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);
86 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
87 if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
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");
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);
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"))) {
122 public boolean isCustomerPresentInAAI(String customerId) {
123 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
125 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
126 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
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");
139 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
141 putRequest(param, callURL, buildRequestHeaderForAAI());
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);
150 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
151 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
152 return (LinkedHashMap) response.getBody();
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());
166 private void putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
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());
175 } catch (BackendFunctionalException e) {
176 LOGGER.error("error on calling " + callUrl + " ," + e);
180 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
185 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
187 for (String paramName : param.keySet()) {
188 builder.queryParam(paramName, param.get(paramName));
191 String uriBuilder = builder.build().encode().toUriString();
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());
204 } catch (BackendFunctionalException e) {
205 LOGGER.error("error on calling " + callURL + " ," + e);