X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-impl%2Faai%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Faai%2FAaiManager.java;h=76817dfc679db5f5427945b841c5c94f81318500;hb=f7da60fabf647665d61f142a0d6ba9a9fc940106;hp=bb772e6cad116dd3c4888096d1896f900e5c0be7;hpb=2141dc0263940c94bf4265c6ca0072d9130a8ead;p=policy%2Fmodels.git diff --git a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiManager.java b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiManager.java index bb772e6ca..76817dfc6 100644 --- a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiManager.java +++ b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiManager.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * aai * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,18 +22,22 @@ package org.onap.policy.aai; -import com.google.gson.JsonSyntaxException; - +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.UUID; - -import org.onap.policy.aai.util.Serialization; +import java.util.stream.Collectors; +import org.apache.commons.lang3.tuple.Pair; +import org.json.JSONArray; +import org.json.JSONObject; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.utils.NetLoggerUtil; import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.rest.RestManager; -import org.onap.policy.rest.RestManager.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,13 +46,26 @@ import org.slf4j.LoggerFactory; */ public final class AaiManager { + // TODO remove this class + /** The Constant logger. */ private static final Logger logger = LoggerFactory.getLogger(AaiManager.class); - /** The rest manager. */ + private static final String APPLICATION_JSON = "application/json"; + + private static final StandardCoder CODER = new StandardCoder(); + // The REST manager used for processing REST calls for this AAI manager private final RestManager restManager; + /** custom query and other AAI resource URLs. */ + private static final String CQ_URL = "/aai/v16/query?format=resource"; + private static final String TENANT_URL = "/aai/v16/search/nodes-query?" + + "search-node-type=vserver&filter=vserver-name:EQUALS:"; + private static final String PREFIX = "/aai/v16"; + private static final String PNF_URL = PREFIX + "/network/pnfs/pnf/"; + private static final String AAI_DEPTH_SUFFIX = "?depth=0"; + /** * Constructor, create the AAI manager with the specified REST manager. * @@ -59,104 +76,111 @@ public final class AaiManager { } /** - * Post a query to A&AI. + * Creates the custom query payload from a tenant query response. * - * @param url the A&AI URL - * @param username the user name for authentication - * @param password the password for authentication - * @param request the request to issue towards A&AI - * @param requestId the UUID of the request - * @return the response from A&AI + * @param getResponse response from the tenant query + * @return String Payload */ - public AaiNqResponse postQuery(String url, String username, String password, AaiNqRequest request, UUID requestId) { - - final Map headers = createHeaders(requestId); - - url = url + "/aai/search/named-query"; + private String createCustomQueryPayload(String getResponse) { - logger.debug("RestManager.post before"); - String requestJson = Serialization.gsonPretty.toJson(request); - NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, requestJson); - Pair httpDetails = - restManager.post(url, username, password, headers, "application/json", requestJson); - logger.debug("RestManager.post after"); - - if (httpDetails == null) { - logger.info("AAI POST Null Response to {}", url); + if (getResponse == null) { return null; - } - - int httpResponseCode = httpDetails.first; - - logger.info(url); - logger.info("{}", httpResponseCode); - logger.info(httpDetails.second); + } else { + JSONObject responseObj = new JSONObject(getResponse); + JSONArray resultsArray; + if (responseObj.has("result-data")) { + resultsArray = (JSONArray) responseObj.get("result-data"); + } else { + return null; + } + String resourceLink = resultsArray.getJSONObject(0).getString("resource-link"); + String start = resourceLink.replace(PREFIX, ""); + String query = "query/closed-loop"; + JSONObject payload = new JSONObject(); + payload.put("start", start); + payload.put("query", query); + return payload.toString(); - if (httpDetails.second != null) { - return composeResponse(httpDetails, url, AaiNqResponse.class); } - return null; } /** - * Perform a GET request for a particular virtual server towards A&AI. + * This method is used to get the information for custom query. * - * @param urlGet the A&AI URL - * @param username the user name for authentication - * @param password the password for authentication - * @param requestId the UUID of the request - * @param key the key of the virtual server - * @return the response for the virtual server from A&AI + * @param url url of the get method + * @param username Aai username + * @param password Aai password + * @param requestId request ID + * @param vserver Id of the vserver + * @return String */ - public AaiGetVserverResponse getQueryByVserverName(String urlGet, String username, String password, UUID requestId, - String key) { - return getQuery(urlGet, username, password, requestId, key, AaiGetVserverResponse.class); - } + private String getCustomQueryRequestPayload(String url, String username, String password, UUID requestId, + String vserver) { - /** - * Perform a GET request for a particular VNF by VNF ID towards A&AI. - * - * @param urlGet the A&AI URL - * @param username the user name for authentication - * @param password the password for authentication - * @param requestId the UUID of the request - * @param key the ID of the VNF - * @return the response for the virtual server from A&AI - */ - public AaiGetVnfResponse getQueryByVnfId(String urlGet, String username, String password, UUID requestId, - String key) { - return getQuery(urlGet, username, password, requestId, key, AaiGetVnfResponse.class); + String urlGet = url + TENANT_URL; + + String getResponse = getStringQuery(urlGet, username, password, requestId, vserver); + return createCustomQueryPayload(getResponse); } /** - * Perform a GET request for a particular VNF by VNF name towards A&AI. + * Calls Aai and returns a custom query response for a vserver. * - * @param urlGet the A&AI URL - * @param username the user name for authentication - * @param password the password for authentication - * @param requestId the UUID of the request - * @param key the name of the VNF - * @return the response for the virtual server from A&AI + * @param url Aai url + * @param username Aai Username + * @param password Aai Password + * @param requestId request ID + * @param vserver Vserver + * @return AaiCqResponse response from Aai for custom query */ - public AaiGetVnfResponse getQueryByVnfName(String urlGet, String username, String password, UUID requestId, - String key) { - return getQuery(urlGet, username, password, requestId, key, AaiGetVnfResponse.class); + public AaiCqResponse getCustomQueryResponse(String url, String username, String password, UUID requestId, + String vserver) { + + final Map headers = createHeaders(requestId); + + logger.debug("RestManager.put before"); + String requestJson = getCustomQueryRequestPayload(url, username, password, requestId, vserver); + NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, requestJson); + + url = url + CQ_URL; + + Pair httpDetails = this.restManager.put(url, username, password, headers, APPLICATION_JSON, + requestJson); + logger.debug("RestManager.put after"); + + if (httpDetails == null) { + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "AAI POST Null Response"); + logger.debug("AAI POST Null Response to {}", url); + return null; + } + + int httpResponseCode = httpDetails.getLeft(); + + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "Response code: " + httpResponseCode); + NetLoggerUtil.getNetworkLogger().debug(httpDetails.getRight()); + + logger.debug(url); + logger.debug("{}", httpResponseCode); + logger.debug(httpDetails.getRight()); + + if (httpDetails.getRight() != null) { + return new AaiCqResponse(httpDetails.getRight()); + } + return null; } /** - * Perform a GET query for a particular entity towards A&AI. + * Returns the string response of a get query. * - * @param the generic type for the response - * @param urlGet the A&AI URL - * @param username the user name for authentication - * @param password the password for authentication - * @param requestId the UUID of the request - * @param key the name of the VNF - * @param classOfT the class of the response to return - * @return the response for the virtual server from A&AI + * @param url Aai URL + * @param username Aai Username + * @param password Aai Password + * @param requestId AaiRequestId + * @param key Aai Key + * @return String returns the string from the get query */ - private T getQuery(final String url, final String username, final String password, final UUID requestId, - final String key, final Class classOfResponse) { + private String getStringQuery(final String url, final String username, final String password, final UUID requestId, + final String key) { Map headers = createHeaders(requestId); @@ -168,21 +192,22 @@ public final class AaiManager { NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", CommInfrastructure.REST, urlGet); Pair httpDetailsGet = restManager.get(urlGet, username, password, headers); if (httpDetailsGet == null) { - logger.info("AAI GET Null Response to {}", urlGet); + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "AAI POST Null Response"); + logger.debug("AAI GET Null Response to {}", urlGet); return null; } - int httpResponseCode = httpDetailsGet.first; + int httpResponseCode = httpDetailsGet.getLeft(); - logger.info(urlGet); - logger.info("{}", httpResponseCode); - logger.info(httpDetailsGet.second); + NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, "Response code: " + httpResponseCode); + NetLoggerUtil.getNetworkLogger().debug(httpDetailsGet.getRight()); - if (httpResponseCode == 200) { - T responseGet = composeResponse(httpDetailsGet, urlGet, classOfResponse); - if (responseGet != null) { - return responseGet; - } + logger.debug(urlGet); + logger.debug("{}", httpResponseCode); + logger.debug(httpDetailsGet.getRight()); + + if (httpResponseCode == 200 && httpDetailsGet.getRight() != null) { + return httpDetailsGet.getRight(); } try { Thread.sleep(1000); @@ -206,29 +231,43 @@ public final class AaiManager { headers.put("X-FromAppId", "POLICY"); headers.put("X-TransactionId", requestId.toString()); - headers.put("Accept", "application/json"); + headers.put("Accept", APPLICATION_JSON); return headers; } /** - * This method uses Google's GSON to create a response object from a JSON string. + * Perform a GET request for a particular PNF by PNF ID towards A&AI. * - * @param the generic type - * @param httpDetails the HTTP response - * @param url the URL from which the response came - * @param classOfResponse The response class - * @return an instance of the response class - * @throws JsonSyntaxException on GSON errors instantiating the response + * @param url the A&AI URL + * @param username the user name for authentication + * @param password the password for authentication + * @param requestId the UUID of the request + * @param pnfName the AAI unique identifier for PNF object + * @return HashMap of PNF properties */ - private T composeResponse(final Pair httpDetails, final String url, - final Class classOfResponse) { + public Map getPnf(String url, String username, String password, UUID requestId, String pnfName) { + String urlGet; + try { + urlGet = url + PNF_URL; + pnfName = URLEncoder.encode(pnfName, StandardCharsets.UTF_8.toString()) + AAI_DEPTH_SUFFIX; + } catch (UnsupportedEncodingException e) { + logger.error("Failed to encode the pnfName: {} using UTF-8", pnfName, e); + return null; + } + String responseGet = getStringQuery(urlGet, username, password, requestId, pnfName); + if (responseGet == null) { + logger.error("Null response from AAI for the url: {}.", urlGet); + return null; + } try { - T response = Serialization.gsonPretty.fromJson(httpDetails.second, classOfResponse); - NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, httpDetails.second); - return response; - } catch (JsonSyntaxException e) { - logger.error("postQuery threw: ", e); + @SuppressWarnings("unchecked") + Map pnfParams = CODER.decode(responseGet, HashMap.class); + // Map to AAI node.attribute notation + return pnfParams.entrySet().stream() + .collect(Collectors.toMap(e -> "pnf." + e.getKey(), Map.Entry::getValue)); + } catch (CoderException e) { + logger.error("Failed to fetch PNF from AAI", e); return null; } }