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 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;
27 public class AaiQuery {
29 private AaiResponseUtil aaiResponseUtil;
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="
36 String response = webTarget.request("application/json").headers(getHeaders()).get()
37 .readEntity(String.class);
38 if (response == null) {
40 .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name="
42 response = webTarget.request("application/json").headers(getHeaders()).get()
43 .readEntity(String.class);
46 return aaiResponseUtil.convertJsonToVnfEntity(response);
47 } catch (Exception e) {
48 throw new CorrelationException("Failed to convert aai vnf response data to vnf entity", e);
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);
59 return aaiResponseUtil.convertJsonToVmEntity(response);
60 } catch (Exception e) {
61 throw new CorrelationException("Failed to convert aai vm response data to vm entity", e);
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);
78 return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink();
79 } catch (Exception e) {
80 throw new CorrelationException("Failed to get aai resource link", e);
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());