2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.aai;
24 import com.google.gson.JsonSyntaxException;
26 import java.util.HashMap;
28 import java.util.UUID;
30 import org.onap.policy.aai.util.Serialization;
31 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
33 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
34 import org.onap.policy.rest.RestManager;
35 import org.onap.policy.rest.RestManager.Pair;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * This class handles communication towards and responses from A&AI for this module.
42 public final class AaiManager {
44 /** The Constant logger. */
45 private static final Logger logger = LoggerFactory.getLogger(AaiManager.class);
47 /** The rest manager. */
48 // The REST manager used for processing REST calls for this AAI manager
49 private final RestManager restManager;
52 * Constructor, create the AAI manager with the specified REST manager.
54 * @param restManager the rest manager to use for REST calls
56 public AaiManager(final RestManager restManager) {
57 this.restManager = restManager;
61 * Post a query to A&AI.
63 * @param url the A&AI URL
64 * @param username the user name for authentication
65 * @param password the password for authentication
66 * @param request the request to issue towards A&AI
67 * @param requestId the UUID of the request
68 * @return the response from A&AI
70 public AaiNqResponse postQuery(String url, String username, String password, AaiNqRequest request, UUID requestId) {
72 final Map<String, String> headers = createHeaders(requestId);
74 url = url + "/aai/search/named-query";
76 logger.debug("RestManager.post before");
77 String requestJson = Serialization.gsonPretty.toJson(request);
78 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, requestJson);
79 Pair<Integer, String> httpDetails =
80 restManager.post(url, username, password, headers, "application/json", requestJson);
81 logger.debug("RestManager.post after");
83 if (httpDetails == null) {
84 logger.info("AAI POST Null Response to {}", url);
88 int httpResponseCode = httpDetails.first;
91 logger.info("{}", httpResponseCode);
92 logger.info(httpDetails.second);
94 if (httpDetails.second != null) {
95 return composeResponse(httpDetails, url, AaiNqResponse.class);
101 * Perform a GET request for a particular virtual server towards A&AI.
103 * @param urlGet the A&AI URL
104 * @param username the user name for authentication
105 * @param password the password for authentication
106 * @param requestId the UUID of the request
107 * @param key the key of the virtual server
108 * @return the response for the virtual server from A&AI
110 public AaiGetVserverResponse getQueryByVserverName(String urlGet, String username, String password, UUID requestId,
112 return getQuery(urlGet, username, password, requestId, key, AaiGetVserverResponse.class);
116 * Perform a GET request for a particular VNF by VNF ID towards A&AI.
118 * @param urlGet the A&AI URL
119 * @param username the user name for authentication
120 * @param password the password for authentication
121 * @param requestId the UUID of the request
122 * @param key the ID of the VNF
123 * @return the response for the virtual server from A&AI
125 public AaiGetVnfResponse getQueryByVnfId(String urlGet, String username, String password, UUID requestId,
127 return getQuery(urlGet, username, password, requestId, key, AaiGetVnfResponse.class);
131 * Perform a GET request for a particular VNF by VNF name towards A&AI.
133 * @param urlGet the A&AI URL
134 * @param username the user name for authentication
135 * @param password the password for authentication
136 * @param requestId the UUID of the request
137 * @param key the name of the VNF
138 * @return the response for the virtual server from A&AI
140 public AaiGetVnfResponse getQueryByVnfName(String urlGet, String username, String password, UUID requestId,
142 return getQuery(urlGet, username, password, requestId, key, AaiGetVnfResponse.class);
146 * Perform a GET query for a particular entity towards A&AI.
148 * @param <T> the generic type for the response
149 * @param urlGet the A&AI URL
150 * @param username the user name for authentication
151 * @param password the password for authentication
152 * @param requestId the UUID of the request
153 * @param key the name of the VNF
154 * @param classOfT the class of the response to return
155 * @return the response for the virtual server from A&AI
157 private <T> T getQuery(final String url, final String username, final String password, final UUID requestId,
158 final String key, final Class<T> classOfResponse) {
160 Map<String, String> headers = createHeaders(requestId);
162 String urlGet = url + key;
164 int attemptsLeft = 3;
166 while (attemptsLeft-- > 0) {
167 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", CommInfrastructure.REST, urlGet);
168 Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers);
169 if (httpDetailsGet == null) {
170 logger.info("AAI GET Null Response to {}", urlGet);
174 int httpResponseCode = httpDetailsGet.first;
177 logger.info("{}", httpResponseCode);
178 logger.info(httpDetailsGet.second);
180 if (httpResponseCode == 200) {
181 T responseGet = composeResponse(httpDetailsGet, urlGet, classOfResponse);
182 if (responseGet != null) {
188 } catch (InterruptedException e) {
189 Thread.currentThread().interrupt();
198 * Create the headers for the HTTP request.
200 * @param requestId the request ID to insert in the headers
201 * @return the HTTP headers
203 private Map<String, String> createHeaders(final UUID requestId) {
204 Map<String, String> headers = new HashMap<>();
206 headers.put("X-FromAppId", "POLICY");
207 headers.put("X-TransactionId", requestId.toString());
208 headers.put("Accept", "application/json");
214 * This method uses Google's GSON to create a response object from a JSON string.
216 * @param <T> the generic type
217 * @param httpDetails the HTTP response
218 * @param url the URL from which the response came
219 * @param classOfResponse The response class
220 * @return an instance of the response class
221 * @throws JsonSyntaxException on GSON errors instantiating the response
223 private <T> T composeResponse(final Pair<Integer, String> httpDetails, final String url,
224 final Class<T> classOfResponse) {
226 T response = Serialization.gsonPretty.fromJson(httpDetails.second, classOfResponse);
227 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, httpDetails.second);
229 } catch (JsonSyntaxException e) {
230 logger.error("postQuery threw: ", e);