add httpsutil and modify query from aai
[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 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;
24
25 public class AaiQuery {
26
27     private AaiResponseUtil aaiResponseUtil;
28
29     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
30         String response = getVnfDataResponse(vnfId, vnfName);
31         try {
32             return aaiResponseUtil.convertJsonToVnfEntity(response);
33         } catch (Exception e) {
34             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
35         }
36     }
37
38     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
39         String url = MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(vserverId, vserverName);
40         String response = getResponse(url);
41         try {
42             return aaiResponseUtil.convertJsonToVmEntity(response);
43         } catch (Exception e) {
44             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
45         }
46     }
47
48     private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException {
49         String response = getResourceLinksResponse(vserverId, vserverName);
50         try {
51             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
52         } catch (Exception e) {
53             throw new CorrelationException("Failed to get aai resource link", e);
54         }
55     }
56
57     private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException {
58         String url =
59                 MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + "vserver-id:EQUALS:"
60                         + vserverId;
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);
66         }
67         return response;
68     }
69
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="
75                     + vnfName;
76             response = getResponse(url);
77         }
78         return response;
79     }
80
81     private String getResponse(String url) throws CorrelationException {
82         String response = "";
83         try {
84             response = HttpsUtils.get(url, getHeaders());
85         } catch (Exception e) {
86             throw new CorrelationException("Failed to get data from aai", e);
87         }
88         return response;
89     }
90
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");
97         return headers;
98     }
99 }