1 package org.onap.nbi.apis.serviceorder;
3 import java.util.HashMap;
4 import java.util.LinkedHashMap;
7 import org.onap.nbi.OnapComponentsUrlPaths;
8 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
9 import org.onap.nbi.exceptions.BackendFunctionalException;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.beans.factory.annotation.Value;
14 import org.springframework.http.HttpEntity;
15 import org.springframework.http.HttpHeaders;
16 import org.springframework.http.HttpMethod;
17 import org.springframework.http.HttpStatus;
18 import org.springframework.http.ResponseEntity;
19 import org.springframework.stereotype.Service;
20 import org.springframework.web.client.RestTemplate;
21 import org.springframework.web.util.UriComponentsBuilder;
24 public class MultiClient {
27 private RestTemplate restTemplate;
30 private String aaiHost;
32 @Value("${aai.header.authorization}")
33 private String aaiHeaderAuthorization;
35 @Value("${aai.api.id}")
36 private String aaiApiId;
38 @Value("${onap.lcpCloudRegionId}")
39 private String lcpCloudRegionId;
41 @Value("${onap.tenantId}")
42 private String tenantId;
44 @Value("${onap.cloudOwner}")
45 private String cloudOwner;
48 private ServiceCatalogUrl serviceCatalogUrl;
51 private ServiceInventoryUrl serviceInventoryUrl;
54 private static final String HEADER_AUTHORIZATION = "Authorization";
55 private static final String X_FROM_APP_ID = "X-FromAppId";
57 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
59 public ResponseEntity<Object> getServiceCatalog(String id) {
60 StringBuilder callURL = new StringBuilder().append(serviceCatalogUrl.getServiceCatalogUrl()).append(id);
61 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), null);
65 public boolean doesServiceExistInServiceInventory(String id, String serviceName, String globalSubscriberId) {
66 StringBuilder callURL = new StringBuilder().append(serviceInventoryUrl.getServiceInventoryUrl()).append(id);
67 Map<String, String> param = new HashMap<>();
68 param.put("serviceSpecification.name", serviceName);
69 param.put("relatedParty.id", globalSubscriberId);
71 ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
72 if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
79 private HttpHeaders buildRequestHeaderForAAI() {
80 HttpHeaders httpHeaders = new HttpHeaders();
81 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
82 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
83 httpHeaders.add("Accept", "application/json");
84 httpHeaders.add("Content-Type", "application/json");
89 public boolean isTenantIdPresentInAAI() {
90 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_TENANTS_PATH);
91 String callUrlFormated = callURL.toString().replace("$onap.lcpCloudRegionId", lcpCloudRegionId);
92 callUrlFormated = callUrlFormated.replace("$onap.cloudOwner", cloudOwner);
94 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
95 if (response != null) {
96 LinkedHashMap body = (LinkedHashMap) response.getBody();
97 List<LinkedHashMap> tenants = (List<LinkedHashMap>) body.get("tenant");
98 for (LinkedHashMap tenant : tenants) {
99 if (tenantId.equalsIgnoreCase((String) tenant.get("tenant-id"))) {
107 public boolean isCustomerPresentInAAI(String customerId) {
108 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
110 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
111 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
118 public void putCustomer(SubscriberInfo subscriberInfo) {
119 Map<String, String> param = new HashMap<>();
120 param.put("global-customer-id", subscriberInfo.getGlobalSubscriberId());
121 param.put("subscriber-name", subscriberInfo.getSubscriberName());
122 param.put("subscriber-type", "BSS");
124 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
126 putRequest(param, callURL, buildRequestHeaderForAAI());
130 public LinkedHashMap getServicesInAaiForCustomer(String customerId) {
131 StringBuilder callURL =
132 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
133 String callUrlFormated = callURL.toString().replace("$customerId", customerId);
135 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
136 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
137 return (LinkedHashMap) response.getBody();
142 public void putServiceType(String globalSubscriberId, String serviceName) {
143 Map<String, String> param = new HashMap<>();
144 param.put("service-type", serviceName);
145 String callURL = aaiHost + OnapComponentsUrlPaths.AAI_PUT_SERVICE_FOR_CUSTOMER_PATH + serviceName;
146 String callUrlFormated = callURL.toString().replace("$customerId", globalSubscriberId);
147 putRequest(param, callUrlFormated, buildRequestHeaderForAAI());
151 private void putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
153 ResponseEntity<Object> response =
154 restTemplate.exchange(callUrl, HttpMethod.PUT, new HttpEntity<>(param, httpHeaders), Object.class);
155 LOGGER.info("response status : " + response.getStatusCodeValue());
156 if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
157 LOGGER.warn("HTTP call on " + callUrl + " returns " + response.getStatusCodeValue() + ", "
158 + response.getBody().toString());
160 } catch (BackendFunctionalException e) {
161 LOGGER.error("error on calling " + callUrl + " ," + e);
165 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
170 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
172 for (String paramName : param.keySet()) {
173 builder.queryParam(paramName, param.get(paramName));
176 String uriBuilder = builder.build().encode().toUriString();
179 ResponseEntity<Object> response =
180 restTemplate.exchange(uriBuilder, HttpMethod.GET, new HttpEntity<>(httpHeaders), Object.class);
181 LOGGER.debug("response body : " + response.getBody().toString());
182 LOGGER.info("response status : " + response.getStatusCodeValue());
183 if (!response.getStatusCode().equals(HttpStatus.OK)) {
184 LOGGER.warn("HTTP call on " + callURL + " returns " + response.getStatusCodeValue() + ", "
185 + response.getBody().toString());
189 } catch (BackendFunctionalException e) {
190 LOGGER.error("error on calling " + callURL + " ," + e);