2 * Copyright 2017 ZTE Corporation.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.holmes.common.aai;
16 import java.io.IOException;
17 import java.util.HashMap;
18 import java.util.List;
20 import javax.inject.Inject;
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;
35 public class AaiQuery {
38 private AaiResponseUtil aaiResponseUtil;
40 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
41 String response = getVnfDataResponse(vnfId, vnfName);
43 return aaiResponseUtil.convertJsonToVnfEntity(response);
44 } catch (Exception e) {
45 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
49 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
50 String url = getVmUrl(vserverId, vserverName);
51 String response = getResponse(url);
53 return aaiResponseUtil.convertJsonToVmEntity(response);
54 } catch (Exception e) {
55 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
59 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
60 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
61 return getBaseUrl("") + resourceLinkUrl;
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();
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);
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);
93 private String getBaseUrl(String suffixUrl) {
94 return "https://aai.onap:8443" + suffixUrl;
97 private String getMsbSuffixAddr(String suffixUrl) {
98 if (suffixUrl.length() <= 0) {
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]
110 String ret = addrSplits[1];
111 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
113 addrSplits[0] = "api";
114 StringBuilder stringBuffer = new StringBuilder();
115 for (String split : addrSplits) {
116 stringBuffer.append("/" + split);
118 return stringBuffer.toString();
121 private String getResponse(String url) throws CorrelationException {
123 CloseableHttpClient httpClient = null;
124 HttpGet httpGet = new HttpGet(url);
126 httpClient = HttpsUtils.getHttpsClient(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);
132 httpGet.releaseConnection();
133 if (httpClient != null) {
136 } catch (IOException e) {
137 log.warn("Failed to close http client!");
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");