node query for service instance query
[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");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nbi.apis.serviceinventory;
17
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20 import javax.annotation.PostConstruct;
21 import org.onap.nbi.OnapComponentsUrlPaths;
22 import org.onap.nbi.exceptions.BackendFunctionalException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Service;
30
31 @Service
32 public class AaiClient extends BaseClient {
33
34     public static final String CUSTOMER_ID = "$customerId";
35
36     @Value("${aai.host}")
37     private String aaiHost;
38
39     @Value("${aai.header.authorization}")
40     private String aaiHeaderAuthorization;
41
42     @Value("${aai.api.id}")
43     private String aaiApiId;
44
45     @Value("${aai.header.transaction.id}")
46     private String aaiTransactionId;
47
48     private static final String HEADER_AUTHORIZATION = "Authorization";
49     private static final String X_FROM_APP_ID = "X-FromAppId";
50     private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
51     private static final String X_TRANSACTION_ID = "X-TransactionId";
52
53
54     private String aaiServiceUrl;
55     private String aaiServicesUrl;
56     private String aaiServicesInstancesUrl;
57
58     @PostConstruct
59     private void setUpAndlogAAIUrl() {
60         aaiServiceUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE).toString();
61         aaiServicesUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH).toString();
62         aaiServicesInstancesUrl= new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH).toString();
63
64
65         LOGGER.info("AAI service url :  "+aaiServiceUrl);
66         LOGGER.info("AAI services url :  "+aaiServicesUrl);
67         LOGGER.info("AAI service instances url :  "+aaiServicesInstancesUrl);
68
69     }
70
71
72     private HttpHeaders buildRequestHeaderForAAI() {
73
74         HttpHeaders httpHeaders = new HttpHeaders();
75         httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
76         httpHeaders.add(X_FROM_APP_ID, aaiApiId);
77         httpHeaders.add("Accept", "application/json");
78         httpHeaders.add("Content-Type", "application/json");
79         httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
80
81         return httpHeaders;
82
83     }
84
85     public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
86
87         String callUrlFormated = aaiServiceUrl.replace(CUSTOMER_ID, customerId);
88         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
89         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
90
91         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
92         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
93             return (LinkedHashMap) response.getBody();
94         }
95         return null;
96     }
97     
98     public Map getService(String serviceId) {
99
100         String callUrlFormated = aaiServiceUrl.replace("$serviceId", serviceId);
101         
102         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
103         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
104             return (LinkedHashMap) response.getBody();
105         }
106         return null;
107     }
108
109     public Map getVNF(String relatedLink) {
110
111         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
112         try{
113             ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
114             return (LinkedHashMap) response.getBody();
115         } catch (BackendFunctionalException e) {
116             LOGGER.error("error on calling {} , {}" , callURL.toString(), e);
117             return null;
118         }
119     }
120
121     public Map getServicesInAaiForCustomer(String customerId) {
122         String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
123         try{
124             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
125             return (LinkedHashMap) response.getBody();
126         } catch (BackendFunctionalException e) {
127             LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
128             return null;
129         }
130     }
131
132     public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
133         String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
134         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
135
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 }