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