From 73e3ea2f9183fc7ed6ba8c0f221e41a804fbe2c7 Mon Sep 17 00:00:00 2001 From: Rashmi Pujar Date: Tue, 29 Oct 2019 16:13:05 -0400 Subject: PNF support changes in policy/models AAI Enrichment method for PNF target-type and unit tests. Addition of a hashmap field to ControlLoopEvent class to hold event specific parameters. Update CDS actor to include additional event parameters Issue-ID: POLICY-1187 Signed-off-by: Rashmi Pujar Change-Id: Ie0ceb320943531de6e6bc8675844b29a358dfb7e --- .../main/java/org/onap/policy/aai/AaiManager.java | 59 ++++++++++++++++++---- .../java/org/onap/policy/aai/AaiManagerTest.java | 43 ++++++++++------ .../onap/policy/controlloop/ControlLoopEvent.java | 31 +++--------- .../policy/controlloop/ControlLoopTargetType.java | 1 + .../controlloop/ControlLoopTargetTypeTest.java | 1 + 5 files changed, 85 insertions(+), 50 deletions(-) (limited to 'models-interactions/model-impl') 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 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 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 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 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 httpResponseOk; - Pair httpResponseErr0; - Pair httpResponseErr1; - Pair httpResponseWait; - Pair httpTenantResponseOk; - Pair 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 httpResponseOk; + private Pair httpResponseErr0; + private Pair httpResponseErr1; + private Pair httpResponseWait; + private Pair httpTenantResponseOk; + private Pair httpCqResponseOk; /** * Set up test cases. @@ -73,11 +72,6 @@ public class AaiManagerTest { public void beforeTestAaiManager() throws Exception { restManagerMock = mock(RestManager.class); - Map 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 pnfHttpResponse = restManagerMock.new Pair<>(200, pnfResponse); + when(restManagerMock.get(contains(pnfName), eq(DOROTHY), eq("Gale"), anyMap())) + .thenReturn(pnfHttpResponse); + + Map pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName); + assertNotNull(pnfParams); + assertEquals(pnfName, pnfParams.get("pnf.pnf-name")); + } } diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java index 534e843f2..3ff7aa256 100644 --- a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java @@ -23,6 +23,7 @@ package org.onap.policy.controlloop; import com.google.gson.annotations.SerializedName; import java.io.Serializable; +import java.util.Map; import java.util.UUID; import lombok.Getter; import lombok.Setter; @@ -33,45 +34,27 @@ public abstract class ControlLoopEvent implements Serializable { private static final long serialVersionUID = 2391252138583119195L; - @SerializedName("closedLoopControlName") - private String closedLoopControlName; - - @SerializedName("version") - private String version = "1.0.2"; - @SerializedName("requestID") private UUID requestId; - - @SerializedName("closedLoopEventClient") - private String closedLoopEventClient; - @SerializedName("target_type") private String targetType; - - @SerializedName("target") + private String closedLoopControlName; + private String version = "1.0.2"; + private String closedLoopEventClient; private String target; - - @SerializedName("from") private String from; - - @SerializedName("policyScope") private String policyScope; - - @SerializedName("policyName") private String policyName; - - @SerializedName("policyVersion") private String policyVersion; - - @SerializedName("closedLoopEventStatus") private ControlLoopEventStatus closedLoopEventStatus; + private Map additionalEventParams; public ControlLoopEvent() { } /** - * Construct an instace from an existing instance. + * Construct an instance from an existing instance. * * @param event the existing instance */ @@ -79,6 +62,7 @@ public abstract class ControlLoopEvent implements Serializable { if (event == null) { return; } + this.version = event.version; this.closedLoopControlName = event.closedLoopControlName; this.requestId = event.requestId; this.closedLoopEventClient = event.closedLoopEventClient; @@ -89,6 +73,7 @@ public abstract class ControlLoopEvent implements Serializable { this.policyName = event.policyName; this.policyVersion = event.policyVersion; this.closedLoopEventStatus = event.closedLoopEventStatus; + this.additionalEventParams = event.additionalEventParams; } public boolean isEventStatusValid() { diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopTargetType.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopTargetType.java index 1ca182563..b79140a12 100644 --- a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopTargetType.java +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopTargetType.java @@ -26,4 +26,5 @@ public class ControlLoopTargetType { public static final String VF = "VF"; public static final String VFC = "VFC"; public static final String VNF = "VNF"; + public static final String PNF = "PNF"; } diff --git a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/ControlLoopTargetTypeTest.java b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/ControlLoopTargetTypeTest.java index daf6bb814..ac700c866 100644 --- a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/ControlLoopTargetTypeTest.java +++ b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/ControlLoopTargetTypeTest.java @@ -33,5 +33,6 @@ public class ControlLoopTargetTypeTest { assertEquals("VF", ControlLoopTargetType.VF); assertEquals("VFC", ControlLoopTargetType.VFC); assertEquals("VNF", ControlLoopTargetType.VNF); + assertEquals("PNF", ControlLoopTargetType.PNF); } } -- cgit 1.2.3-korg