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 java.util.Map.Entry;
24 import org.onap.nbi.OnapComponentsUrlPaths;
25 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
26 import org.onap.nbi.exceptions.BackendFunctionalException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.http.HttpEntity;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37 import org.springframework.web.client.ResourceAccessException;
38 import org.springframework.web.client.RestTemplate;
39 import org.springframework.web.util.UriComponentsBuilder;
42 public class MultiClient {
45 private RestTemplate restTemplate;
48 private String aaiHost;
50 @Value("${aai.header.authorization}")
51 private String aaiHeaderAuthorization;
53 @Value("${aai.api.id}")
54 private String aaiApiId;
56 @Value("${onap.lcpCloudRegionId}")
57 private String lcpCloudRegionId;
59 @Value("${onap.tenantId}")
60 private String tenantId;
62 @Value("${onap.cloudOwner}")
63 private String cloudOwner;
66 private ServiceCatalogUrl serviceCatalogUrl;
69 private ServiceInventoryUrl serviceInventoryUrl;
72 private static final String HEADER_AUTHORIZATION = "Authorization";
73 private static final String X_FROM_APP_ID = "X-FromAppId";
75 private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
77 public ResponseEntity<Object> getServiceCatalog(String id) {
78 StringBuilder callURL = new StringBuilder().append(serviceCatalogUrl.getServiceCatalogUrl()).append(id);
79 return 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 return response != null && response.getStatusCode().equals(HttpStatus.OK);
93 private HttpHeaders buildRequestHeaderForAAI() {
94 HttpHeaders httpHeaders = new HttpHeaders();
95 httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
96 httpHeaders.add(X_FROM_APP_ID, aaiApiId);
97 httpHeaders.add("Accept", "application/json");
98 httpHeaders.add("Content-Type", "application/json");
103 public boolean isTenantIdPresentInAAI() {
104 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_TENANTS_PATH);
105 String callUrlFormated = callURL.toString().replace("$onap.lcpCloudRegionId", lcpCloudRegionId);
106 callUrlFormated = callUrlFormated.replace("$onap.cloudOwner", cloudOwner);
108 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
109 if (response != null) {
110 LinkedHashMap body = (LinkedHashMap) response.getBody();
111 List<LinkedHashMap> tenants = (List<LinkedHashMap>) body.get("tenant");
112 for (LinkedHashMap tenant : tenants) {
113 if (tenantId.equalsIgnoreCase((String) tenant.get("tenant-id"))) {
121 public boolean isCustomerPresentInAAI(String customerId) {
122 StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
124 ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
125 return(response != null && response.getStatusCode().equals(HttpStatus.OK));
129 public boolean putCustomer(SubscriberInfo subscriberInfo) {
130 Map<String, String> param = new HashMap<>();
131 param.put("global-customer-id", subscriberInfo.getGlobalSubscriberId());
132 param.put("subscriber-name", subscriberInfo.getSubscriberName());
133 param.put("subscriber-type", "BSS");
135 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
137 ResponseEntity<Object> response = putRequest(param, callURL, buildRequestHeaderForAAI());
138 return response != null && response.getStatusCode().equals(HttpStatus.CREATED);
142 public Map getServicesInAaiForCustomer(String customerId) {
143 StringBuilder callURL =
144 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
145 String callUrlFormated = callURL.toString().replace("$customerId", customerId);
147 ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
148 if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
149 return (LinkedHashMap) response.getBody();
154 public boolean putServiceType(String globalSubscriberId, String serviceName) {
155 Map<String, String> param = new HashMap<>();
156 param.put("service-type", serviceName);
157 String callURL = aaiHost + OnapComponentsUrlPaths.AAI_PUT_SERVICE_FOR_CUSTOMER_PATH + serviceName;
158 String callUrlFormated = callURL.replace("$customerId", globalSubscriberId);
159 ResponseEntity<Object> response = putRequest(param, callUrlFormated, buildRequestHeaderForAAI());
160 return response != null && response.getStatusCode().equals(HttpStatus.CREATED);
164 private ResponseEntity<Object> putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
166 ResponseEntity<Object> response =
167 restTemplate.exchange(callUrl, HttpMethod.PUT, new HttpEntity<>(param, httpHeaders), Object.class);
168 LOGGER.info("response status : " + response.getStatusCodeValue());
169 if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.CREATED)) {
170 LOGGER.warn("HTTP call on {} returns {} , {}", callUrl , response.getStatusCodeValue(), response.getBody().toString());
173 } catch (BackendFunctionalException|ResourceAccessException e) {
174 LOGGER.error("error on calling " + callUrl + " ," + e);
179 private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
184 UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
186 for (Entry<String, String> stringEntry : param.entrySet()) {
187 builder.queryParam(stringEntry.getKey(), stringEntry.getValue());
191 URI uri = builder.build().encode().toUri();
194 ResponseEntity<Object> response =
195 restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(httpHeaders), Object.class);
196 if(LOGGER.isDebugEnabled()){
197 LOGGER.debug("response body : {}", response.getBody().toString());
199 LOGGER.info("response status : {}", response.getStatusCodeValue());
200 if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
201 LOGGER.warn("HTTP call on {} returns {} , {}", callURL , response.getStatusCodeValue(), response.getBody().toString());
206 } catch (BackendFunctionalException|ResourceAccessException e) {
207 LOGGER.error("error on calling " + callURL + " ," + e);