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