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;
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.ResourceAccessException;
37 import org.springframework.web.client.RestTemplate;
38 import org.springframework.web.util.UriComponentsBuilder;
41 public class MultiClient {
44 private RestTemplate restTemplate;
47 private String aaiHost;
49 @Value("${aai.header.authorization}")
50 private String aaiHeaderAuthorization;
52 @Value("${aai.api.id}")
53 private String aaiApiId;
55 @Value("${onap.lcpCloudRegionId}")
56 private String lcpCloudRegionId;
58 @Value("${onap.tenantId}")
59 private String tenantId;
61 @Value("${onap.cloudOwner}")
62 private String cloudOwner;
65 private ServiceCatalogUrl serviceCatalogUrl;
68 private ServiceInventoryUrl serviceInventoryUrl;
71 private static final String HEADER_AUTHORIZATION = "Authorization";
72 private static final String X_FROM_APP_ID = "X-FromAppId";
74 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
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);
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);
88 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
89 if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
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");
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);
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"))) {
124 public boolean isCustomerPresentInAAI(String customerId) {
125 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
127 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
128 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
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");
141 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
143 ResponseEntity<Object> response = putRequest(param, callURL, buildRequestHeaderForAAI());
144 if (response != null && response.getStatusCode().equals(HttpStatus.CREATED)) {
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);
156 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
157 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
158 return (LinkedHashMap) response.getBody();
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)) {
176 private ResponseEntity<Object> putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
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());
186 } catch (BackendFunctionalException|ResourceAccessException e) {
187 LOGGER.error("error on calling " + callUrl + " ," + e);
192 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
197 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
199 for (String paramName : param.keySet()) {
200 builder.queryParam(paramName, param.get(paramName));
203 URI uri = builder.build().encode().toUri();
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());
216 } catch (BackendFunctionalException|ResourceAccessException e) {
217 LOGGER.error("error on calling " + callURL + " ," + e);