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;
17 import java.util.List;
19 import javax.inject.Inject;
20 import lombok.extern.slf4j.Slf4j;
21 import org.jvnet.hk2.annotations.Service;
22 import org.onap.holmes.common.aai.config.AaiConfig;
23 import org.onap.holmes.common.aai.entity.VmEntity;
24 import org.onap.holmes.common.aai.entity.VnfEntity;
25 import org.onap.holmes.common.config.MicroServiceConfig;
26 import org.onap.holmes.common.exception.CorrelationException;
27 import org.onap.holmes.common.utils.HttpsUtils;
31 public class AaiQuery {
34 private AaiResponseUtil aaiResponseUtil;
36 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
37 String response = getVnfDataResponse(vnfId, vnfName);
39 return aaiResponseUtil.convertJsonToVnfEntity(response);
40 } catch (Exception e) {
41 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
45 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
46 String url = getVmUrl(vserverId, vserverName);
47 String response = getResponse(url);
49 return aaiResponseUtil.convertJsonToVmEntity(response);
50 } catch (Exception e) {
51 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
55 private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
57 String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
58 String baseUrl = getBaseUrl("");
59 if (baseUrl.startsWith("http")) {
60 url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
62 url = baseUrl + resourceLinkUrl;
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.size() != 0) {
71 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
76 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
77 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
78 String response = getResponse(url);
79 if ("".equals(response) || "{}".equals(response)) {
80 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
81 response = getResponse(url);
86 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
87 String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+ "/" + vnfId);
88 String response = getResponse(url);
89 if ("".equals(response) || "{}".equals(response)) {
90 url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
91 response = getResponse(url);
96 private String getBaseUrl(String suffixUrl) {
99 String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
100 url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
101 } catch (Exception e) {
102 log.info("Failed to get msb address");
104 if ("".equals(url)) {
106 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
108 } catch (Exception e) {
109 log.info("Failed to get the address of A&AI.", e);
115 private String getMsbSuffixAddr(String suffixUrl) {
116 if (suffixUrl.length() <= 0) {
119 String[] addrSplits = suffixUrl.substring(1).split("/");
120 String[] conv = addrSplits[2].split("-");
121 addrSplits[2] = conv[0];
122 if (conv.length > 1) {
123 for(int i = 1; i < conv.length; i++) {
124 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
128 String ret = addrSplits[1];
129 addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
131 addrSplits[0] = "api";
132 StringBuffer stringBuffer = new StringBuffer();
133 for (String split : addrSplits) {
134 stringBuffer.append("/" + split);
136 return stringBuffer.toString();
139 private String getResponse(String url) throws CorrelationException {
140 String response = "";
142 response = HttpsUtils.get(url, getHeaders());
143 } catch (Exception e) {
144 throw new CorrelationException("Failed to get data from aai", e);
149 private Map getHeaders() {
150 Map<String, String> headers = new HashMap<>();
151 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
152 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
153 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
154 headers.put("Accept", "application/json");