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