b9afe64b940db7e78fb64ed4ea24136d270c6259
[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 org.onap.nbi.OnapComponentsUrlPaths;
21 import org.onap.nbi.exceptions.BackendFunctionalException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.ResponseEntity;
28 import org.springframework.stereotype.Service;
29
30 @Service
31 public class AaiClient extends BaseClient {
32
33     public static final String CUSTOMER_ID = "$customerId";
34
35     @Value("${aai.host}")
36     private String aaiHost;
37
38     @Value("${aai.header.authorization}")
39     private String aaiHeaderAuthorization;
40
41     @Value("${aai.api.id}")
42     private String aaiApiId;
43
44     @Value("${aai.header.transaction.id}")
45     private String aaiTransactionId;
46
47     private static final String HEADER_AUTHORIZATION = "Authorization";
48     private static final String X_FROM_APP_ID = "X-FromAppId";
49     private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
50     private static final String X_TRANSACTION_ID = "X-TransactionId";
51
52     private HttpHeaders buildRequestHeaderForAAI() {
53
54         HttpHeaders httpHeaders = new HttpHeaders();
55         httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
56         httpHeaders.add(X_FROM_APP_ID, aaiApiId);
57         httpHeaders.add("Accept", "application/json");
58         httpHeaders.add("Content-Type", "application/json");
59         httpHeaders.add(X_TRANSACTION_ID, aaiTransactionId);
60
61         return httpHeaders;
62
63     }
64
65     public Map getCatalogService(String customerId, String serviceSpecName, String serviceId) {
66
67         StringBuilder callURL =
68                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_FOR_CUSTOMER_PATH);
69         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
70         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
71         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
72
73         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
74         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
75             return (LinkedHashMap) response.getBody();
76         }
77         return null;
78     }
79
80
81     public Map getVNF(String relatedLink) {
82
83         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
84         try{
85             ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
86             return (LinkedHashMap) response.getBody();
87         } catch (BackendFunctionalException e) {
88             LOGGER.error("error on calling {} , {}" , callURL.toString(), e);
89             return null;
90         }
91     }
92
93     public Map getServicesInAaiForCustomer(String customerId) {
94         StringBuilder callURL =
95                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
96         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
97         try{
98             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
99             return (LinkedHashMap) response.getBody();
100         } catch (BackendFunctionalException e) {
101             LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
102             return null;
103         }
104     }
105
106     public Map getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
107         StringBuilder callURL =
108                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH);
109         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
110         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
111
112         try{
113             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
114             return (LinkedHashMap) response.getBody();
115         } catch (BackendFunctionalException e) {
116             LOGGER.error("error on calling {} , {}" , callUrlFormated, e);
117             return null;
118         }
119     }
120 }