Fixed the CLM Issues
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.holmes.common.aai;
15
16 import lombok.extern.slf4j.Slf4j;
17 import org.apache.http.HttpResponse;
18 import org.apache.http.client.methods.HttpGet;
19 import org.apache.http.impl.client.CloseableHttpClient;
20 import org.jvnet.hk2.annotations.Service;
21 import org.onap.holmes.common.aai.config.AaiConfig;
22 import org.onap.holmes.common.aai.entity.VmEntity;
23 import org.onap.holmes.common.aai.entity.VnfEntity;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.HttpsUtils;
26
27 import javax.inject.Inject;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 @Service
34 @Slf4j
35 public class AaiQuery {
36
37     @Inject
38     private AaiResponseUtil aaiResponseUtil;
39
40     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
41         String response = getVnfDataResponse(vnfId, vnfName);
42         try {
43             return aaiResponseUtil.convertJsonToVnfEntity(response);
44         } catch (Exception e) {
45             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
46         }
47     }
48
49     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
50         String url = getVmUrl(vserverId, vserverName);
51         String response = getResponse(url);
52         try {
53             return aaiResponseUtil.convertJsonToVmEntity(response);
54         } catch (Exception e) {
55             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
56         }
57     }
58
59     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
60         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
61         return getBaseUrl("") + resourceLinkUrl;
62     }
63
64     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
65         String response = getResourceLinksResponse(vserverId, vserverName);
66         List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
67         if (!linkList.isEmpty()) {
68             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
69         }
70         return "";
71     }
72
73     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
74         String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-id:EQUALS:" + vserverId);
75         String response = getResponse(url);
76         if ("".equals(response) || "{}".equals(response)) {
77             url = getBaseUrl(AaiConfig.AaiConsts.AAI_VM_ADDR + "vserver-name:EQUALS:" + vserverName);
78             response = getResponse(url);
79         }
80         return response;
81     }
82
83     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
84         String url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "/" + vnfId);
85         String response = getResponse(url);
86         if ("".equals(response) || "{}".equals(response)) {
87             url = getBaseUrl(AaiConfig.AaiConsts.AAI_VNF_ADDR + "?vnf-name=" + vnfName);
88             response = getResponse(url);
89         }
90         return response;
91     }
92
93     private String getBaseUrl(String suffixUrl) {
94         return "https://aai.onap:8443" + suffixUrl;
95     }
96
97     private String getResponse(String url) throws CorrelationException {
98         String response;
99         CloseableHttpClient httpClient = null;
100         HttpGet httpGet = new HttpGet(url);
101         try {
102             httpClient = HttpsUtils.getHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
103             HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
104             response = HttpsUtils.extractResponseEntity(httpResponse);
105         } catch (Exception e) {
106             throw new CorrelationException("Failed to get data from aai", e);
107         } finally {
108             httpGet.releaseConnection();
109             if (httpClient != null) {
110                 try {
111                     httpClient.close();
112                 } catch (IOException e) {
113                     log.warn("Failed to close http client!");
114                 }
115             }
116         }
117         return response;
118     }
119
120     private Map getHeaders() {
121         Map<String, String> headers = new HashMap<>();
122         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
123         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
124         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
125         headers.put("Accept", "application/json");
126         return headers;
127     }
128 }