Add some logs to AAI queries
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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 java.io.IOException;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import javax.inject.Inject;
21 import lombok.extern.slf4j.Slf4j;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.client.methods.HttpGet;
24 import org.apache.http.client.methods.HttpRequestBase;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.jvnet.hk2.annotations.Service;
27 import org.onap.holmes.common.aai.config.AaiConfig;
28 import org.onap.holmes.common.aai.entity.VmEntity;
29 import org.onap.holmes.common.aai.entity.VnfEntity;
30 import org.onap.holmes.common.config.MicroServiceConfig;
31 import org.onap.holmes.common.exception.CorrelationException;
32 import org.onap.holmes.common.utils.HttpsUtils;
33
34 @Service
35 @Slf4j
36 public class AaiQuery {
37
38     @Inject
39     private AaiResponseUtil aaiResponseUtil;
40
41     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
42         String response = getVnfDataResponse(vnfId, vnfName);
43         try {
44             return aaiResponseUtil.convertJsonToVnfEntity(response);
45         } catch (Exception e) {
46             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
47         }
48     }
49
50     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
51         String url = getVmUrl(vserverId, vserverName);
52         String response = getResponse(url);
53         try {
54             return aaiResponseUtil.convertJsonToVmEntity(response);
55         } catch (Exception e) {
56             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
57         }
58     }
59
60     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
61         String url = "";
62         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
63         String baseUrl = getBaseUrl("");
64         if (baseUrl.startsWith("http")) {
65             url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
66         } else {
67             url = baseUrl + resourceLinkUrl;
68         }
69         return url;
70     }
71
72     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
73         String response = getResourceLinksResponse(vserverId, vserverName);
74         List linkList = aaiResponseUtil.convertJsonToVmResourceLink(response);
75         if (!linkList.isEmpty()) {
76             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
77         }
78         return  "";
79     }
80
81     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
82         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
83         String response = getResponse(url);
84         if ("".equals(response) || "{}".equals(response)) {
85             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
86             response = getResponse(url);
87         }
88         return response;
89     }
90
91     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
92         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR)+  "/" + vnfId);
93         String response = getResponse(url);
94         if ("".equals(response) || "{}".equals(response)) {
95             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AaiConsts.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
96             response = getResponse(url);
97         }
98         return response;
99     }
100
101     private String getBaseUrl(String suffixUrl) {
102         String url = "";
103         try {
104             url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix()+ suffixUrl;
105         } catch (Exception e) {
106             log.info("Failed to get msb address");
107         }
108         if ("".equals(url)) {
109             try {
110                 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
111                         + suffixUrl;
112             } catch (Exception e) {
113                 log.info("Failed to get the address of A&AI.", e);
114             }
115         }
116         return url;
117     }
118
119     private String getMsbSuffixAddr(String suffixUrl) {
120         if (suffixUrl.length() <= 0) {
121             return "";
122         }
123         String[] addrSplits = suffixUrl.substring(1).split("/");
124         String[] conv = addrSplits[2].split("-");
125         addrSplits[2] = conv[0];
126         if (conv.length > 1) {
127             for(int i = 1; i < conv.length; i++) {
128                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
129                         .substring(1);
130             }
131         }
132         String ret = addrSplits[1];
133         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
134         addrSplits[2] = ret;
135         addrSplits[0] = "api";
136         StringBuilder stringBuffer = new StringBuilder();
137         for (String split : addrSplits) {
138             stringBuffer.append("/" + split);
139         }
140         return stringBuffer.toString();
141     }
142
143     private String getResponse(String url) throws CorrelationException {
144         String response;
145         CloseableHttpClient httpClient = null;
146         HttpGet httpGet = new HttpGet(url);
147         try {
148             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
149             HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
150             response = HttpsUtils.extractResponseEntity(httpResponse);
151         } catch (Exception e) {
152             throw new CorrelationException("Failed to get data from aai", e);
153         } finally {
154             httpGet.releaseConnection();
155             if (httpClient != null) {
156                 try {
157                     httpClient.close();
158                 } catch (IOException e) {
159                     log.warn("Failed to close http client!");
160                 }
161             }
162         }
163         return response;
164     }
165
166     private Map getHeaders() {
167         Map<String, String> headers = new HashMap<>();
168         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
169         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
170         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
171         headers.put("Accept", "application/json");
172         return headers;
173     }
174 }