Add serviceOrder rest services
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / MultiClient.java
1 package org.onap.nbi.apis.serviceorder;
2
3 import java.util.HashMap;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
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;
22
23 @Service
24 public class MultiClient {
25
26     @Autowired
27     private RestTemplate restTemplate;
28
29     @Value("${aai.host}")
30     private String aaiHost;
31
32     @Value("${aai.header.authorization}")
33     private String aaiHeaderAuthorization;
34
35     @Value("${aai.api.id}")
36     private String aaiApiId;
37
38     @Value("${onap.lcpCloudRegionId}")
39     private String lcpCloudRegionId;
40
41     @Value("${onap.tenantId}")
42     private String tenantId;
43
44     @Value("${onap.cloudOwner}")
45     private String cloudOwner;
46
47     @Autowired
48     private ServiceCatalogUrl serviceCatalogUrl;
49
50     @Autowired
51     private ServiceInventoryUrl serviceInventoryUrl;
52
53
54     private static final String HEADER_AUTHORIZATION = "Authorization";
55     private static final String X_FROM_APP_ID = "X-FromAppId";
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(MultiClient.class);
58
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);
62         return response;
63     }
64
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);
70
71         ResponseEntity<Object> response = callApiGet(callURL.toString(), new HttpHeaders(), param);
72         if (response == null || !response.getStatusCode().equals(HttpStatus.OK)) {
73             return false;
74         }
75         return true;
76     }
77
78
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");
85         return httpHeaders;
86     }
87
88
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);
93
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"))) {
100                     return true;
101                 }
102             }
103         }
104         return false;
105     }
106
107     public boolean isCustomerPresentInAAI(String customerId) {
108         StringBuilder callURL = new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH)
109                 .append(customerId);
110         ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI(), null);
111         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
112             return true;
113         }
114         return false;
115     }
116
117
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");
123         String callURL =
124                 aaiHost + OnapComponentsUrlPaths.AAI_GET_CUSTOMER_PATH + subscriberInfo.getGlobalSubscriberId();
125
126         putRequest(param, callURL, buildRequestHeaderForAAI());
127     }
128
129
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);
134
135         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI(), null);
136         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
137             return (LinkedHashMap) response.getBody();
138         }
139         return null;
140     }
141
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());
148     }
149
150
151     private void putRequest(Map<String, String> param, String callUrl, HttpHeaders httpHeaders) {
152         try {
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());
159             }
160         } catch (BackendFunctionalException e) {
161             LOGGER.error("error on calling " + callUrl + " ," + e);
162         }
163     }
164
165     private ResponseEntity<Object> callApiGet(String callURL, HttpHeaders httpHeaders, Map<String, String> param) {
166
167
168         try {
169
170             UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(callURL);
171             if (param != null) {
172                 for (String paramName : param.keySet()) {
173                     builder.queryParam(paramName, param.get(paramName));
174                 }
175             }
176             String uriBuilder = builder.build().encode().toUriString();
177
178
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());
186             }
187             return response;
188
189         } catch (BackendFunctionalException e) {
190             LOGGER.error("error on calling " + callURL + " ," + e);
191             return null;
192         }
193     }
194
195
196 }