273e2873480ff0ea404b0fd4d7d89b21a7884899
[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 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     private static final String HEADER_AUTHORIZATION = "Authorization";
44     private static final String X_FROM_APP_ID = "X-FromAppId";
45     private static final Logger LOGGER = LoggerFactory.getLogger(AaiClient.class);
46
47     private HttpHeaders buildRequestHeaderForAAI() {
48
49         HttpHeaders httpHeaders = new HttpHeaders();
50         httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
51         httpHeaders.add(X_FROM_APP_ID, aaiApiId);
52         httpHeaders.add("Accept", "application/json");
53         httpHeaders.add("Content-Type", "application/json");
54         return httpHeaders;
55
56     }
57
58     public LinkedHashMap getCatalogService(String customerId, String serviceSpecName, String serviceId) {
59
60         StringBuilder callURL =
61                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_FOR_CUSTOMER_PATH);
62         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
63         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
64         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
65
66         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
67         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
68             return (LinkedHashMap) response.getBody();
69         }
70         return null;
71     }
72
73
74     public LinkedHashMap getVNF(String relatedLink) {
75
76         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
77
78         ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
79         return (LinkedHashMap) response.getBody();
80
81     }
82
83     public LinkedHashMap getServicesInAaiForCustomer(String customerId) {
84         StringBuilder callURL =
85                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
86         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
87         try{
88             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
89             return (LinkedHashMap) response.getBody();
90         } catch (BackendFunctionalException e) {
91             LOGGER.error("error on calling {0} , {1}" , callUrlFormated, e);
92             return null;
93         }
94     }
95
96     public LinkedHashMap getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
97         StringBuilder callURL =
98                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH);
99         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
100         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
101
102         try{
103             ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
104             return (LinkedHashMap) response.getBody();
105         } catch (BackendFunctionalException e) {
106             LOGGER.error("error on calling {0} , {1}" , callUrlFormated, e);
107             return null;
108         }
109     }
110 }