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.util.HashMap;
18 import javax.inject.Inject;
19 import lombok.extern.slf4j.Slf4j;
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.config.MicroServiceConfig;
25 import org.onap.holmes.common.exception.CorrelationException;
26 import org.onap.holmes.common.utils.HttpsUtils;
30 public class AaiQuery {
33 private AaiResponseUtil aaiResponseUtil;
35 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
36 String response = getVnfDataResponse(vnfId, vnfName);
38 return aaiResponseUtil.convertJsonToVnfEntity(response);
39 } catch (Exception e) {
40 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
44 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
45 String url = getVmUrl(vserverId, vserverName);
46 String response = getResponse(url);
48 return aaiResponseUtil.convertJsonToVmEntity(response);
49 } catch (Exception e) {
50 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
54 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
56 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
57 String baseUrl = getBaseUrl("");
58 if (baseUrl.startsWith("http")) {
59 url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
61 url = baseUrl + resourceLinkUrl;
66 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
67 String response = getResourceLinksResponse(vserverId, vserverName);
68 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
71 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
72 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
73 String response = getResponse(url);
74 if ("".equals(response) || "{}".equals(response)) {
75 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
76 response = getResponse(url);
81 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
82 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+ "/" + vnfId);
83 String response = getResponse(url);
84 if ("".equals(response) || "{}".equals(response)) {
85 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
86 response = getResponse(url);
91 private String getBaseUrl(String suffixUrl) {
94 String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
95 url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
96 } catch (Exception e) {
97 log.info("Failed to get msb address");
101 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
103 } catch (Exception e) {
104 log.info("Failed to get the address of A&AI.", e);
110 private String getMsbSuffixAddr(String suffixUrl) {
111 String[] addrSplits = suffixUrl.substring(1).split("/");
112 String[] conv = addrSplits[2].split("-");
113 addrSplits[2] = conv[0];
114 if (conv.length > 1) {
115 for(int i = 1; i < conv.length; i++) {
116 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
120 String ret = addrSplits[1];
121 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
123 addrSplits[0] = "api";
124 StringBuffer stringBuffer = new StringBuffer();
125 for (String split : addrSplits) {
126 stringBuffer.append("/" + split);
128 return stringBuffer.toString();
131 private String getResponse(String url) throws CorrelationException {
132 String response = "";
134 response = HttpsUtils.get(url, getHeaders());
135 } catch (Exception e) {
136 throw new CorrelationException("Failed to get data from aai", e);
141 private Map getHeaders() {
142 Map<String, String> headers = new HashMap<>();
143 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
144 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
145 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
146 headers.put("Accept", "application/json");