Custom Query Code
[policy/models.git] / models-interactions / model-impl / aai / src / main / java / org / onap / policy / aai / AaiManager.java
index bb772e6..f50f0cb 100644 (file)
 package org.onap.policy.aai;
 
 import com.google.gson.JsonSyntaxException;
-
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
-
+import org.json.JSONArray;
+import org.json.JSONObject;
 import org.onap.policy.aai.util.Serialization;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
@@ -49,6 +49,12 @@ public final class AaiManager {
     // The REST manager used for processing REST calls for this AAI manager
     private final RestManager restManager;
 
+    /** custom query URLs. */
+    private static String cqUrl =  "/aai/v16/query?format=resource";
+    private static String tenantUrl = "/aai/v16/search/nodes-query?search-node-type=vserver&filter=vserver-name:";
+    private static String prefix = "/aai/v16";
+
+
     /**
      * Constructor, create the AAI manager with the specified REST manager.
      *
@@ -58,6 +64,151 @@ public final class AaiManager {
         this.restManager = restManager;
     }
 
+    /**
+     * Creates the custom query payload from a tenant query response.
+     *
+     * @param getResponse response from the tenant query
+     * @return String Payload
+     */
+    private String createCustomQueryPayload(String getResponse) {
+
+        if (getResponse == null) {
+            return null;
+        } else {
+            JSONObject responseObj = new JSONObject(getResponse);
+            JSONArray resultsArray = new JSONArray();
+            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();
+
+        }
+    }
+
+
+    /**
+     * This method is used to get the information for custom query.
+     *
+     * @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
+     */
+    private String getCustomQueryRequestPayload(String url, String username, String password, UUID requestId,
+            String vserver) {
+
+        String urlGet = url + tenantUrl;
+        String getResponse = getStringQuery(urlGet, username, password, requestId, vserver);
+        return createCustomQueryPayload(getResponse);
+    }
+
+
+
+    /**
+     * Calls Aai and returns a custom query response for a vserver.
+     *
+     * @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 AaiCqResponse getCustomQueryResponse(String url, String username, String password, UUID requestId,
+            String vserver) {
+
+        final Map<String, String> headers = createHeaders(requestId);
+
+        url = url + cqUrl;
+
+        logger.debug("RestManager.put before");
+        String requestJson = getCustomQueryRequestPayload(url, username, password, requestId, vserver);
+        NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, requestJson);
+        Pair<Integer, String> httpDetails =
+                this.restManager.put(url, username, password, headers, "application/json", requestJson);
+        logger.debug("RestManager.put after");
+
+        if (httpDetails == null) {
+            logger.info("AAI POST Null Response to {}", url);
+            return null;
+        }
+
+        int httpResponseCode = httpDetails.first;
+
+        logger.info(url);
+        logger.info("{}", httpResponseCode);
+        logger.info(httpDetails.second);
+
+        if (httpDetails.second != null) {
+            String resp = httpDetails.second;
+            return new AaiCqResponse(resp);
+        }
+        return null;
+    }
+
+
+
+    /**
+     * Returns the string response of a get query.
+     *
+     * @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 String getStringQuery(final String url, final String username, final String password, final UUID requestId,
+            final String key) {
+
+        Map<String, String> headers = createHeaders(requestId);
+
+        String urlGet = url + key;
+
+        int attemptsLeft = 3;
+
+        while (attemptsLeft-- > 0) {
+            NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|]", CommInfrastructure.REST, urlGet);
+            Pair<Integer, String> httpDetailsGet = restManager.get(urlGet, username, password, headers);
+            if (httpDetailsGet == null) {
+                logger.info("AAI GET Null Response to {}", urlGet);
+                return null;
+            }
+
+            int httpResponseCode = httpDetailsGet.first;
+
+            logger.info(urlGet);
+            logger.info("{}", httpResponseCode);
+            logger.info(httpDetailsGet.second);
+
+            if (httpResponseCode == 200) {
+                String responseGet = httpDetailsGet.second;
+                if (responseGet != null) {
+                    return responseGet;
+                }
+            }
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+            }
+
+        }
+
+        return null;
+    }
+
+
     /**
      * Post a query to A&AI.
      *