start up failed without msb
[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_FOR_CUSTOMER_PATH).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
99     public Map getVNF(String relatedLink) {
100
101         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
102         try{
103             ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
104             return (LinkedHashMap) response.getBody();
105         } catch (BackendFunctionalException e) {
106             LOGGER.error("error on calling {} , {}" , callURL.toString(), e);
107             return null;
108         }
109     }
110
111     public Map getServicesInAaiForCustomer(String customerId) {
112         String callUrlFormated = aaiServicesUrl.replace(CUSTOMER_ID, customerId);
113         try{
114             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
115             return (LinkedHashMap) response.getBody();
116         } catch (BackendFunctionalException e) {
117             LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
118             return null;
119         }
120     }
121
122     public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
123         String callUrlFormated = aaiServicesInstancesUrl.replace(CUSTOMER_ID, customerId);
124         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
125
126         try{
127             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
128             return (LinkedHashMap) response.getBody();
129         } catch (BackendFunctionalException e) {
130             LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
131             return null;
132         }
133     }
134 }