71472ed581bffb607437b4462f4dfd7918fba466
[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 javax.ws.rs.client.Client;
17 import javax.ws.rs.client.ClientBuilder;
18 import javax.ws.rs.client.WebTarget;
19 import javax.ws.rs.core.MultivaluedHashMap;
20 import org.glassfish.jersey.client.ClientConfig;
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
27 public class AaiQuery {
28
29     private AaiResponseUtil aaiResponseUtil;
30
31     public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException {
32         Client client = ClientBuilder.newClient(new ClientConfig());
33         WebTarget webTarget = client
34                 .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id="
35                         + vnfId);
36         String response = webTarget.request("application/json").headers(getHeaders()).get()
37                 .readEntity(String.class);
38         if (response == null) {
39             webTarget = client
40                     .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name="
41                             + vnfName);
42             response = webTarget.request("application/json").headers(getHeaders()).get()
43                     .readEntity(String.class);
44         }
45         try {
46             return aaiResponseUtil.convertJsonToVnfEntity(response);
47         } catch (Exception e) {
48             throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
49         }
50     }
51
52     public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException {
53         Client client = ClientBuilder.newClient(new ClientConfig());
54         String response = client
55                 .target(MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(client,
56                         vserverId, vserverName)).request("application/json").headers(getHeaders())
57                 .get().readEntity(String.class);
58         try {
59             return aaiResponseUtil.convertJsonToVmEntity(response);
60         } catch (Exception e) {
61             throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
62         }
63     }
64
65     private String getVmResourceLinks(Client client, String vserverId, String vserverName) throws CorrelationException {
66         WebTarget webTarget = client
67                 .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR
68                         + "vserver-id:EQUALS:" + vserverId);
69         String response = webTarget.request("application/json").headers(getHeaders()).get()
70                 .readEntity(String.class);
71         if (response == null) {
72             webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR
73                     + "vserver-name:EQUALS:" + vserverName);
74             response = webTarget.request("application/json").headers(getHeaders()).get()
75                     .readEntity(String.class);
76         }
77         try {
78             return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
79         } catch (Exception e) {
80             throw new CorrelationException("Failed to get aai resource link", e);
81         }
82     }
83
84     private MultivaluedHashMap getHeaders() {
85         MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>();
86         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
87         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
88         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
89         return headers;
90     }
91 }