b80c40ebfa13316cb036bce9b028f3cfdab9d3c5
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / AaiQuery.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.util.HashMap;
17 import java.util.Map;
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;
27
28 @Service
29 @Slf4j
30 public class AaiQuery {
31
32     @Inject
33     private AaiResponseUtil aaiResponseUtil;
34
35     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
36         String response = getVnfDataResponse(vnfId, vnfName);
37         try {
38             return aaiResponseUtil.convertJsonToVnfEntity(response);
39         } catch (Exception e) {
40             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
41         }
42     }
43
44     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
45         String url = getVmUrl(vserverId, vserverName);
46         String response = getResponse(url);
47         try {
48             return aaiResponseUtil.convertJsonToVmEntity(response);
49         } catch (Exception e) {
50             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
51         }
52     }
53
54     private String getVmUrl(String vserverId, String vserverName) throws CorrelationException {
55         String url = "";
56         String resourceLinkUrl = getVmResourceLinks(vserverId, vserverName);
57         String baseUrl = getBaseUrl("");
58         if (baseUrl.startsWith("http")) {
59             url = baseUrl + getMsbSuffixAddr(resourceLinkUrl);
60         } else {
61             url = baseUrl + resourceLinkUrl;
62         }
63         return url;
64     }
65
66     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
67         String response = getResourceLinksResponse(vserverId, vserverName);
68         try {
69             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
70         } catch (Exception e) {
71             throw new CorrelationException("Failed to get aai resource link", e);
72         }
73     }
74
75     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
76         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-id:EQUALS:" + vserverId);
77         String response = getResponse(url);
78         if ("".equals(response) || "{}".equals(response)) {
79             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VM_ADDR) + "vserver-name:EQUALS:" + vserverName);
80             response = getResponse(url);
81         }
82         return response;
83     }
84
85     private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException {
86         String url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR)+  "/" + vnfId);
87         String response = getResponse(url);
88         if ("".equals(response) || "{}".equals(response)) {
89             url = getBaseUrl(getMsbSuffixAddr(AaiConfig.AAI_VNF_ADDR) + "vnf-name=" + vnfName);
90             response = getResponse(url);
91         }
92         return response;
93     }
94
95     private String getBaseUrl(String suffixUrl) {
96         String url = "";
97         try {
98             String[] msbUrl = MicroServiceConfig.getMsbServerAddrWithHttpPrefix().split(":");
99             url = msbUrl[0] + ":" + msbUrl[1] + suffixUrl;
100         } catch (Exception e) {
101             log.info("Failed to get msb address");
102         }
103         if ("".equals(url)) {
104             try {
105                 url = "https://" + MicroServiceConfig.getServiceConfigInfoFromCBS("aai_config").replace("http://", "")
106                         + suffixUrl;
107             } catch (Exception e) {
108                 log.info("Failed to get the address of A&AI.", e);
109             }
110         }
111         return url;
112     }
113
114     private String getMsbSuffixAddr(String suffixUrl) {
115         String[] addrSplits = suffixUrl.substring(1).split("/");
116         String[] conv = addrSplits[2].split("-");
117         addrSplits[2] = conv[0];
118         if (conv.length > 1) {
119             for(int i = 1; i < conv.length; i++) {
120                 addrSplits[2] = addrSplits[2] + conv[i].substring(0, 1).toUpperCase() + conv[i]
121                         .substring(1);
122             }
123         }
124         String ret = addrSplits[1];
125         addrSplits[1] = addrSplits[0] + "-" + addrSplits[2];
126         addrSplits[2] = ret;
127         addrSplits[0] = "api";
128         StringBuffer stringBuffer = new StringBuffer();
129         for (String split : addrSplits) {
130             stringBuffer.append("/" + split);
131         }
132         return stringBuffer.toString();
133     }
134
135     private String getResponse(String url) throws CorrelationException {
136         String response = "";
137         try {
138             response = HttpsUtils.get(url, getHeaders());
139         } catch (Exception e) {
140             throw new CorrelationException("Failed to get data from aai", e);
141         }
142         return response;
143     }
144
145     private Map getHeaders() {
146         Map<String, String> headers = new HashMap<>();
147         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
148         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
149         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
150         headers.put("Accept", "application/json");
151         return headers;
152     }
153 }