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 org.onap.holmes.common.aai.config.AaiConfig;
19 import org.onap.holmes.common.aai.entity.VmEntity;
20 import org.onap.holmes.common.aai.entity.VnfEntity;
21 import org.onap.holmes.common.config.MicroServiceConfig;
22 import org.onap.holmes.common.exception.CorrelationException;
23 import org.onap.holmes.common.utils.HttpsUtils;
25 public class AaiQuery {
27 private AaiResponseUtil aaiResponseUtil;
29 public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
30 String response = getVnfDataResponse(vnfId, vnfName);
32 return aaiResponseUtil.convertJsonToVnfEntity(response);
33 } catch (Exception e) {
34 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
38 public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
39 String url = MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(vserverId, vserverName);
40 String response = getResponse(url);
42 return aaiResponseUtil.convertJsonToVmEntity(response);
43 } catch (Exception e) {
44 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
48 private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
49 String response = getResourceLinksResponse(vserverId, vserverName);
51 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
52 } catch (Exception e) {
53 throw new CorrelationException("Failed to get aai resource link", e);
57 private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
59 MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + "vserver-id:EQUALS:"
61 String response = getResponse(url);
62 if (response.equals("")) {
63 url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR
64 + "vserver-name:EQUALS:" + vserverName;
65 response = getResponse(url);
70 private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
71 String url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id=" + vnfId;
72 String response = getResponse(url);
73 if (response.equals("")) {
74 url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name="
76 response = getResponse(url);
81 private String getResponse(String url) throws CorrelationException {
84 response = HttpsUtils.get(url, getHeaders());
85 } catch (Exception e) {
86 throw new CorrelationException("Failed to get data from aai", e);
91 private Map getHeaders() {
92 Map<String, String> headers = new HashMap<>();
93 headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
94 headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
95 headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
96 headers.put("Accept", "application/json");