Remove 'All rights reserved.' on apache 2 license
[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.springframework.beans.factory.annotation.Value;
21 import org.springframework.http.HttpHeaders;
22 import org.springframework.http.HttpStatus;
23 import org.springframework.http.ResponseEntity;
24 import org.springframework.stereotype.Service;
25
26 @Service
27 public class AaiClient extends BaseClient {
28
29     public static final String CUSTOMER_ID = "$customerId";
30
31     @Value("${aai.host}")
32     private String aaiHost;
33
34     @Value("${aai.header.authorization}")
35     private String aaiHeaderAuthorization;
36
37     @Value("${aai.api.id}")
38     private String aaiApiId;
39
40     private static final String HEADER_AUTHORIZATION = "Authorization";
41     private static final String X_FROM_APP_ID = "X-FromAppId";
42
43     private HttpHeaders buildRequestHeaderForAAI() {
44
45         HttpHeaders httpHeaders = new HttpHeaders();
46         httpHeaders.add(HEADER_AUTHORIZATION, aaiHeaderAuthorization);
47         httpHeaders.add(X_FROM_APP_ID, aaiApiId);
48         httpHeaders.add("Accept", "application/json");
49         httpHeaders.add("Content-Type", "application/json");
50         return httpHeaders;
51
52     }
53
54     public LinkedHashMap getCatalogService(String customerId, String serviceSpecName, String serviceId) {
55
56         StringBuilder callURL =
57                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_FOR_CUSTOMER_PATH);
58         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
59         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceSpecName);
60         callUrlFormated = callUrlFormated.replace("$serviceId", serviceId);
61
62         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
63         if (response != null && response.getStatusCode().equals(HttpStatus.OK)) {
64             return (LinkedHashMap) response.getBody();
65         }
66         return null;
67     }
68
69
70     public LinkedHashMap getVNF(String relatedLink) {
71
72         StringBuilder callURL = new StringBuilder().append(aaiHost).append(relatedLink);
73
74         ResponseEntity<Object> response = callApiGet(callURL.toString(), buildRequestHeaderForAAI());
75         return (LinkedHashMap) response.getBody();
76
77     }
78
79     public LinkedHashMap getServicesInAaiForCustomer(String customerId) {
80         StringBuilder callURL =
81                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICES_FOR_CUSTOMER_PATH);
82         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
83
84         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
85         return (LinkedHashMap) response.getBody();
86     }
87
88     public LinkedHashMap getServiceInstancesInAaiForCustomer(String customerId, String serviceType) {
89         StringBuilder callURL =
90                 new StringBuilder().append(aaiHost).append(OnapComponentsUrlPaths.AAI_GET_SERVICE_INSTANCES_PATH);
91         String callUrlFormated = callURL.toString().replace(CUSTOMER_ID, customerId);
92         callUrlFormated = callUrlFormated.replace("$serviceSpecName", serviceType);
93
94         ResponseEntity<Object> response = callApiGet(callUrlFormated, buildRequestHeaderForAAI());
95         return (LinkedHashMap) response.getBody();
96     }
97 }