5424db50ed5d25851f6657aedc1317cf1d7ace84
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceinventory / AaiClient.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.nbi.apis.serviceinventory;
15
16 import java.util.LinkedHashMap;
17 import java.util.Map;
18 import javax.annotation.PostConstruct;
19 import org.onap.nbi.OnapComponentsUrlPaths;
20 import org.onap.nbi.exceptions.BackendFunctionalException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.http.HttpHeaders;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.stereotype.Service;
28
29 @Service
30 public class AaiClient extends BaseClient {
31
32   public static final String CUSTOMER_ID = "$customerId";
33
34   @Value("${aai.host}")
35   private String aaiHost;
36
37   @Value("${aai.header.authorization}")
38   private String aaiHeaderAuthorization;
39
40   @Value("${aai.api.id}")
41   private String aaiApiId;
42
43   @Value("${aai.header.transaction.id}")
44   private String aaiTransactionId;
45
46   private static final String HEADER_AUTHORIZATION = "Authorization";
47   private static final String X_FROM_APP_ID = "X-FromAppId";
48   private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
49   private static final String X_TRANSACTION_ID = "X-TransactionId";
50
51
52   private String aaiServiceUrl;
53   private String aaiServiceCustomerUrl;
54   private String aaiServicesUrl;
55   private String aaiServicesInstancesUrl;
56
57   @PostConstruct
58   private void setUpAndlogAAIUrl() {
59     aaiServiceUrl = new StringBuilder().append(aaiHost)
60         .append(OnapComponentsUrlPaths.AAI_GET_SERVICE).toString();
61     aaiServiceCustomerUrl = new StringBuilder().append(aaiHost)
62         .append(OnapComponentsUrlPaths.AAI_GET_SERVICE_CUSTOMER).toString();
63     aaiServicesUrl = new StringBuilder().append(aaiHost)
64         .append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH).toString();
65     aaiServicesInstancesUrl = new StringBuilder().append(aaiHost)
66         .append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH).toString();
67
68
69     LOGGER.info("AAI service url :  " + aaiServiceUrl);
70     LOGGER.info("AAI services url :  " + aaiServicesUrl);
71     LOGGER.info("AAI service instances url :  " + aaiServicesInstancesUrl);
72
73   }
74
75
76   private HttpHeaders buildRequestHeaderForAAI() {
77
78     HttpHeaders httpHeaders = new HttpHeaders();
79     httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
80     httpHeaders.add(X_FROM_APP_ID, aaiApiId);
81     httpHeaders.add("Accept", "application/json");
82     httpHeaders.add("Content-Type", "application/json");
83     httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
84
85     return httpHeaders;
86
87   }
88
89   public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
90
91     String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
92     callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
93     callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
94
95     ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
96     if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
97       return (LinkedHashMap) response.getBody();
98     }
99     return null;
100   }
101
102   public Map getService(String serviceId) {
103     // Retrieve the Service Instance using AAI node query
104     String callUrlFormated = aaiServiceUrl.replace("$serviceId", serviceId);
105     ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
106     if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
107       return (LinkedHashMap) response.getBody();
108     }
109     return null;
110   }
111
112   public Map getServiceCustomer(String serviceId) {
113
114     String callUrlFormated = aaiServiceCustomerUrl.replace("$serviceId", serviceId);
115     ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
116     if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
117       return (LinkedHashMap) response.getBody();
118     }
119     return null;
120   }
121
122   public Map getVNF(String relatedLink) {
123
124     StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
125     try {
126       ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
127       return (LinkedHashMap) response.getBody();
128     } catch (BackendFunctionalException e) {
129       LOGGER.error("error on calling {} , {}", callURL.toString(), e);
130       return null;
131     }
132   }
133
134   public Map getServicesInAaiForCustomer(String customerId) {
135     String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
136     try {
137       ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
138       return (LinkedHashMap) response.getBody();
139     } catch (BackendFunctionalException e) {
140       LOGGER.error("error on calling {} , {}", callUrlFormated, e);
141       return null;
142     }
143   }
144
145   public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
146     String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
147     callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
148
149     try {
150       ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
151       return (LinkedHashMap) response.getBody();
152     } catch (BackendFunctionalException e) {
153       LOGGER.error("error on calling {} , {}", callUrlFormated, e);
154       return null;
155     }
156   }
157 }