summaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/aai/src
diff options
context:
space:
mode:
authorLiam Fallon <liam.fallon@est.tech>2019-11-04 17:59:31 +0000
committerGerrit Code Review <gerrit@onap.org>2019-11-04 17:59:31 +0000
commitc95ab27887a8f408be9c0c7f2c7e8c7811eec74d (patch)
tree7c0506c4dc11cb342f83fe64c2b4fe7e93c7985b /models-interactions/model-impl/aai/src
parent1633ecbdc61fbfe769f55970d046263d721ea489 (diff)
parent73e3ea2f9183fc7ed6ba8c0f221e41a804fbe2c7 (diff)
Merge "PNF support changes in policy/models"
Diffstat (limited to 'models-interactions/model-impl/aai/src')
-rw-r--r--models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiManager.java59
-rw-r--r--models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java43
2 files changed, 75 insertions, 27 deletions
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 20998ae0c..d95068b2b 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
@@ -23,15 +23,21 @@
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 java.util.stream.Collectors;
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;
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;
@@ -47,16 +53,19 @@ public final class AaiManager {
private static final String APPLICATION_JSON = "application/json";
+ private static final StandardCoder CODER = new StandardCoder();
+
/** The rest manager. */
// The REST manager used for processing REST calls for this AAI manager
private final RestManager restManager;
- /** custom query URLs. */
+ /** 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.
@@ -96,7 +105,6 @@ public final class AaiManager {
}
}
-
/**
* This method is used to get the information for custom query.
*
@@ -116,8 +124,6 @@ public final class AaiManager {
return createCustomQueryPayload(getResponse);
}
-
-
/**
* Calls Aai and returns a custom query response for a vserver.
*
@@ -161,8 +167,6 @@ public final class AaiManager {
return null;
}
-
-
/**
* Returns the string response of a get query.
*
@@ -213,7 +217,6 @@ public final class AaiManager {
return null;
}
-
/**
* Post a query to A&AI.
*
@@ -303,12 +306,12 @@ public final class AaiManager {
* Perform a GET query for a particular entity towards A&AI.
*
* @param <T> the generic type for the response
- * @param urlGet the A&AI URL
+ * @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 key the name of the VNF
- * @param classOfT the class of the response to return
+ * @param classOfResponse the class of the response to return
* @return the response for the virtual server from A&AI
*/
private <T> T getQuery(final String url, final String username, final String password, final UUID requestId,
@@ -367,7 +370,6 @@ public final class AaiManager {
return headers;
}
-
/**
* This method uses Google's GSON to create a response object from a JSON string.
*
@@ -389,4 +391,39 @@ public final class AaiManager {
return null;
}
}
+
+ /**
+ * Perform a GET request for a particular PNF by PNF ID towards A&AI.
+ *
+ * @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
+ */
+ public Map<String, String> 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 encoding. {}", 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 {
+ Map<String, String> 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");
+ return null;
+ }
+ }
}
diff --git a/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java
index 9a8d7d260..ff86577b9 100644
--- a/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java
+++ b/models-interactions/model-impl/aai/src/test/java/org/onap/policy/aai/AaiManagerTest.java
@@ -21,10 +21,12 @@
package org.onap.policy.aai;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.startsWith;
@@ -34,7 +36,6 @@ import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
-import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.Before;
@@ -50,19 +51,17 @@ public class AaiManagerTest {
private static final String DOROTHY = "Dorothy";
private static final String SOME_URL = "http://somewhere.over.the.rainbow";
private static final String ANOTHER_URL = "http://somewhere.under.the.rainbow";
- RestManager restManagerMock;
- UUID aaiNqRequestUuid = UUID.randomUUID();
- Pair<Integer, String> httpResponseOk;
- Pair<Integer, String> httpResponseErr0;
- Pair<Integer, String> httpResponseErr1;
- Pair<Integer, String> httpResponseWait;
- Pair<Integer, String> httpTenantResponseOk;
- Pair<Integer, String> httpCqResponseOk;
-
private static final String TENANT_RESPONSE_SAMPLE =
"src/test/resources/org/onap/policy/aai/AaiTenantResponse.json";
-
+ private RestManager restManagerMock;
+ private UUID aaiNqRequestUuid = UUID.randomUUID();
+ private Pair<Integer, String> httpResponseOk;
+ private Pair<Integer, String> httpResponseErr0;
+ private Pair<Integer, String> httpResponseErr1;
+ private Pair<Integer, String> httpResponseWait;
+ private Pair<Integer, String> httpTenantResponseOk;
+ private Pair<Integer, String> httpCqResponseOk;
/**
* Set up test cases.
@@ -73,11 +72,6 @@ public class AaiManagerTest {
public void beforeTestAaiManager() throws Exception {
restManagerMock = mock(RestManager.class);
- Map<String, String> expectedHeaders = new HashMap<>();
- expectedHeaders.put("X-FromAppId", "POLICY");
- expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
- expectedHeaders.put("Accept", "application/json");
-
String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
String tenantResponse = this.getTenantQueryResponse();
httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse);
@@ -235,4 +229,21 @@ public class AaiManagerTest {
"Gale", vserverNameRequestId, "vnfName");
assertNotNull(vnfResponse);
}
+
+ @Test
+ public void testAaiManagerGetPnf() {
+ AaiManager aaiManager = new AaiManager(restManagerMock);
+ assertNotNull(aaiManager);
+ String pnfName = "test-pnf";
+ String pnfResponse = "{\"pnf-name\":" + pnfName
+ + ",\"pnf-id\":\"123456\",\"in-maint\":false,\"ipaddress-v4-oam\":\"1.1.1.1\"}";
+
+ Pair<Integer, String> pnfHttpResponse = restManagerMock.new Pair<>(200, pnfResponse);
+ when(restManagerMock.get(contains(pnfName), eq(DOROTHY), eq("Gale"), anyMap()))
+ .thenReturn(pnfHttpResponse);
+
+ Map<String, String> pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName);
+ assertNotNull(pnfParams);
+ assertEquals(pnfName, pnfParams.get("pnf.pnf-name"));
+ }
}