919f3292932713a3164e43fe065c99ccbf9a8cf9
[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.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.exception.CorrelationException;
31 import org.onap.holmes.common.utils.HttpsUtils;
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 getMsbSuffixAddr(String suffixUrl) {
98         if (suffixUrl.length() <= 0) {
99             return "";
100         }
101         String[] addrSplits = suffixUrl.substring(1).split("/");
102         String[] conv = addrSplits[2].split("-");
103         addrSplits[2] = conv[0];
104         if (conv.length > 1) {
105             for (int i = 1; i < conv.length; i++) {
106                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
107                         .substring(1);
108             }
109         }
110         String ret = addrSplits[1];
111         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
112         addrSplits[2] = ret;
113         addrSplits[0] = "api";
114         StringBuilder stringBuffer = new StringBuilder();
115         for (String split : addrSplits) {
116             stringBuffer.append("/" + split);
117         }
118         return stringBuffer.toString();
119     }
120
121     private String getResponse(String url) throws CorrelationException {
122         String response;
123         CloseableHttpClient httpClient = null;
124         HttpGet httpGet = new HttpGet(url);
125         try {
126             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
127             HttpResponse httpResponse = HttpsUtils.get(httpGet, getHeaders(), httpClient);
128             response = HttpsUtils.extractResponseEntity(httpResponse);
129         } catch (Exception e) {
130             throw new CorrelationException("Failed to get data from aai", e);
131         } finally {
132             httpGet.releaseConnection();
133             if (httpClient != null) {
134                 try {
135                     httpClient.close();
136                 } catch (IOException e) {
137                     log.warn("Failed to close http client!");
138                 }
139             }
140         }
141         return response;
142     }
143
144     private Map getHeaders() {
145         Map<String, String> headers = new HashMap<>();
146         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
147         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
148         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
149         headers.put("Accept", "application/json");
150         return headers;
151     }
152 }