Merge "Add new Operational Policy Type"
[policy/models.git] / models-interactions / model-impl / aai / src / main / java / org / onap / policy / aai / AaiManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * aai
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.aai;
24
25 import java.io.UnsupportedEncodingException;
26 import java.net.URLEncoder;
27 import java.nio.charset.StandardCharsets;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.UUID;
31 import java.util.stream.Collectors;
32
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
36 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
37 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.rest.RestManager.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * This class handles communication towards and responses from A&AI for this module.
47  */
48 public final class AaiManager {
49
50     /** The Constant logger. */
51     private static final Logger logger = LoggerFactory.getLogger(AaiManager.class);
52
53     private static final String APPLICATION_JSON = "application/json";
54
55     private static final StandardCoder CODER = new StandardCoder();
56
57     // The REST manager used for processing REST calls for this AAI manager
58     private final RestManager restManager;
59
60     /** custom query and other AAI resource URLs. */
61     private static final String CQ_URL = "/aai/v16/query?format=resource";
62     private static final String TENANT_URL = "/aai/v16/search/nodes-query?"
63                     + "search-node-type=vserver&filter=vserver-name:EQUALS:";
64     private static final String PREFIX = "/aai/v16";
65     private static final String PNF_URL = PREFIX + "/network/pnfs/pnf/";
66     private static final String AAI_DEPTH_SUFFIX = "?depth=0";
67
68     /**
69      * Constructor, create the AAI manager with the specified REST manager.
70      *
71      * @param restManager the rest manager to use for REST calls
72      */
73     public AaiManager(final RestManager restManager) {
74         this.restManager = restManager;
75     }
76
77     /**
78      * Creates the custom query payload from a tenant query response.
79      *
80      * @param getResponse response from the tenant query
81      * @return String Payload
82      */
83     private String createCustomQueryPayload(String getResponse) {
84
85         if (getResponse == null) {
86             return null;
87         } else {
88             JSONObject responseObj = new JSONObject(getResponse);
89             JSONArray resultsArray;
90             if (responseObj.has("result-data")) {
91                 resultsArray = (JSONArray) responseObj.get("result-data");
92             } else {
93                 return null;
94             }
95             String resourceLink = resultsArray.getJSONObject(0).getString("resource-link");
96             String start = resourceLink.replace(PREFIX, "");
97             String query = "query/closed-loop";
98             JSONObject payload = new JSONObject();
99             payload.put("start", start);
100             payload.put("query", query);
101             return payload.toString();
102
103         }
104     }
105
106     /**
107      * This method is used to get the information for custom query.
108      *
109      * @param url url of the get method
110      * @param username Aai username
111      * @param password Aai password
112      * @param requestId request ID
113      * @param vserver Id of the vserver
114      * @return String
115      */
116     private String getCustomQueryRequestPayload(String url, String username, String password, UUID requestId,
117                     String vserver) {
118
119         String urlGet = url + TENANT_URL;
120
121         String getResponse = getStringQuery(urlGet, username, password, requestId, vserver);
122         return createCustomQueryPayload(getResponse);
123     }
124
125     /**
126      * Calls Aai and returns a custom query response for a vserver.
127      *
128      * @param url Aai url
129      * @param username Aai Username
130      * @param password Aai Password
131      * @param requestId request ID
132      * @param vserver Vserver
133      * @return AaiCqResponse response from Aai for custom query
134      */
135     public AaiCqResponse getCustomQueryResponse(String url, String username, String password, UUID requestId,
136                     String vserver) {
137
138         final Map<String, String> headers = createHeaders(requestId);
139
140         logger.debug("RestManager.put before");
141         String requestJson = getCustomQueryRequestPayload(url, username, password, requestId, vserver);
142         NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, requestJson);
143
144         url = url + CQ_URL;
145
146         Pair<Integer, String> httpDetails = this.restManager.put(url, username, password, headers, APPLICATION_JSON,
147                         requestJson);
148         logger.debug("RestManager.put after");
149
150         if (httpDetails == null) {
151             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "AAI POST Null Response");
152             logger.debug("AAI POST Null Response to {}", url);
153             return null;
154         }
155
156         int httpResponseCode = httpDetails.first;
157
158         NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "Response code: " + httpResponseCode);
159         NetLoggerUtil.getNetworkLogger().debug(httpDetails.second);
160
161         logger.debug(url);
162         logger.debug("{}", httpResponseCode);
163         logger.debug(httpDetails.second);
164
165         if (httpDetails.second != null) {
166             return new AaiCqResponse(httpDetails.second);
167         }
168         return null;
169     }
170
171     /**
172      * Returns the string response of a get query.
173      *
174      * @param url Aai URL
175      * @param username Aai Username
176      * @param password Aai Password
177      * @param requestId AaiRequestId
178      * @param key Aai Key
179      * @return String returns the string from the get query
180      */
181     private String getStringQuery(final String url, final String username, final String password, final UUID requestId,
182                     final String key) {
183
184         Map<String, String> headers = createHeaders(requestId);
185
186         String urlGet = url + key;
187
188         int attemptsLeft = 3;
189
190         while (attemptsLeft-- > 0) {
191             NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", CommInfrastructure.REST, urlGet);
192             Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers);
193             if (httpDetailsGet == null) {
194                 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "AAI POST Null Response");
195                 logger.debug("AAI GET Null Response to {}", urlGet);
196                 return null;
197             }
198
199             int httpResponseCode = httpDetailsGet.first;
200
201             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "Response code: " + httpResponseCode);
202             NetLoggerUtil.getNetworkLogger().debug(httpDetailsGet.second);
203
204             logger.debug(urlGet);
205             logger.debug("{}", httpResponseCode);
206             logger.debug(httpDetailsGet.second);
207
208             if (httpResponseCode == 200 && httpDetailsGet.second != null) {
209                 return httpDetailsGet.second;
210             }
211             try {
212                 Thread.sleep(1000);
213             } catch (InterruptedException e) {
214                 Thread.currentThread().interrupt();
215             }
216
217         }
218
219         return null;
220     }
221
222     /**
223      * Create the headers for the HTTP request.
224      *
225      * @param requestId the request ID to insert in the headers
226      * @return the HTTP headers
227      */
228     private Map<String, String> createHeaders(final UUID requestId) {
229         Map<String, String> headers = new HashMap<>();
230
231         headers.put("X-FromAppId", "POLICY");
232         headers.put("X-TransactionId", requestId.toString());
233         headers.put("Accept", APPLICATION_JSON);
234
235         return headers;
236     }
237
238     /**
239      * Perform a GET request for a particular PNF by PNF ID towards A&AI.
240      *
241      * @param url the A&AI URL
242      * @param username the user name for authentication
243      * @param password the password for authentication
244      * @param requestId the UUID of the request
245      * @param pnfName the AAI unique identifier for PNF object
246      * @return HashMap of PNF properties
247      */
248     public Map<String, String> getPnf(String url, String username, String password, UUID requestId, String pnfName) {
249         String urlGet;
250         try {
251             urlGet = url + PNF_URL;
252             pnfName = URLEncoder.encode(pnfName, StandardCharsets.UTF_8.toString()) + AAI_DEPTH_SUFFIX;
253         } catch (UnsupportedEncodingException e) {
254             logger.error("Failed to encode the pnfName: {} using UTF-8 encoding. {}", pnfName, e);
255             return null;
256         }
257         String responseGet = getStringQuery(urlGet, username, password, requestId, pnfName);
258         if (responseGet == null) {
259             logger.error("Null response from AAI for the url: {}.", urlGet);
260             return null;
261         }
262         try {
263             @SuppressWarnings("unchecked")
264             Map<String, String> pnfParams = CODER.decode(responseGet, HashMap.class);
265             // Map to AAI node.attribute notation
266             return pnfParams.entrySet().stream()
267                             .collect(Collectors.toMap(e -> "pnf." + e.getKey(), Map.Entry::getValue));
268         } catch (CoderException e) {
269             logger.error("Failed to fetch PNF from AAI", e);
270             return null;
271         }
272     }
273 }