Support of onap.policies.monitoring.tcagen2 2.0.0 44/130344/4
authorjhh <jorge.hernandez-herrero@att.com>
Thu, 18 Aug 2022 00:06:09 +0000 (19:06 -0500)
committerjhh <jorge.hernandez-herrero@att.com>
Wed, 24 Aug 2022 15:15:36 +0000 (10:15 -0500)
Issue-ID: POLICY-4317
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I1b1e2d4baa2ec82462e32cb79cc18bb183f7ded6
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
applications/monitoring/src/test/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplicationTest.java
main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java
main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java

index 7fab09b..b4a5579 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,9 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import com.att.research.xacml.api.Response;
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -59,13 +57,12 @@ import org.slf4j.LoggerFactory;
 public class MonitoringPdpApplicationTest {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
-    private static Properties properties = new Properties();
-    private static File propertiesFile;
+    private static final Properties properties = new Properties();
     private static XacmlApplicationServiceProvider service;
     private static DecisionRequest requestSinglePolicy;
     private static DecisionRequest requestPolicyType;
 
-    private static StandardCoder gson = new StandardCoder();
+    private static final StandardCoder gson = new StandardCoder();
 
     @ClassRule
     public static final TemporaryFolder policyFolder = new TemporaryFolder();
@@ -93,9 +90,9 @@ public class MonitoringPdpApplicationTest {
         //
         // Setup our temporary folder
         //
-        XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
-        propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
-                properties, myCreator);
+        XacmlPolicyUtils.FileCreator myCreator = policyFolder::newFile;
+        File propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
+            properties, myCreator);
         //
         // Load XacmlApplicationServiceProvider service
         //
@@ -105,9 +102,7 @@ public class MonitoringPdpApplicationTest {
         // Look for our class instance and save it
         //
         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
-        Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
-        while (iterator.hasNext()) {
-            XacmlApplicationServiceProvider application = iterator.next();
+        for (XacmlApplicationServiceProvider application : applicationLoader) {
             //
             // Is it our service?
             //
@@ -143,9 +138,14 @@ public class MonitoringPdpApplicationTest {
         //
         assertThat(service.canSupportPolicyType(
                 new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"))).isTrue();
+        assertThat(service.canSupportPolicyType(
+            new ToscaConceptIdentifier("onap.policies.monitoring.tcagen2", "2.0.0"))).isTrue();
         assertThat(service.canSupportPolicyType(
                 new ToscaConceptIdentifier(
                 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
+        assertThat(service.canSupportPolicyType(
+            new ToscaConceptIdentifier(
+                "onap.policies.monitoring.foobar", "2.0.1"))).isTrue();
         assertThat(service.canSupportPolicyType(
                 new ToscaConceptIdentifier("onap.foobar", "1.0.0"))).isFalse();
         //
@@ -185,14 +185,33 @@ public class MonitoringPdpApplicationTest {
         //
         // Monitoring applications should not have this information returned
         //
-        assertThat(decision.getKey().getAdvice()).isNull();
-        assertThat(decision.getKey().getObligations()).isNull();
-        assertThat(decision.getKey().getAttributes()).isNull();
+        assertNoInfo(decision);
+    }
+
+    @Test
+    public void tes3AddvDnsPolicy() throws CoderException, XacmlApplicationException {
+        testAddPolicy("src/test/resources/vDNS.policy.input.yaml",
+            "onap.policies.monitoring.cdap.tca.hi.lo.app",
+            "onap.scaleout.tca");
+    }
+
+    @Test
+    public void tes4AddvFirewall1Policy() throws CoderException, XacmlApplicationException {
+        testAddPolicy("policies/vFirewall.policy.monitoring.input.tosca.yaml",
+            "onap.policies.monitoring.tcagen2",
+            "onap.vfirewall.tca");
     }
 
-    @SuppressWarnings("unchecked")
     @Test
-    public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
+    public void tes5AddvFirewall2Policy() throws CoderException, XacmlApplicationException {
+        testAddPolicy("policies/vFirewall.policy.monitoring.input.tosca.v2.yaml",
+            "onap.policies.monitoring.tcagen2",
+            "onap.vfirewall.tca");
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testAddPolicy(String policyResource, String policyType, String policyId)
+        throws CoderException, XacmlApplicationException {
         //
         // Now load the vDNS Policy - make sure
         // the pdp can support it and have it load
@@ -201,8 +220,13 @@ public class MonitoringPdpApplicationTest {
         //
         // Now load the monitoring policies
         //
-        final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
-                service);
+        final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies(policyResource, service);
+
+        //
+        // Set the policy-id for the decision request.
+        //
+        requestSinglePolicy.getResource().put("policy-id", policyId);
+
         //
         // Ask for a decision
         //
@@ -216,13 +240,17 @@ public class MonitoringPdpApplicationTest {
         //
         // Monitoring applications should not have this information returned
         //
-        assertThat(decision.getKey().getAdvice()).isNull();
-        assertThat(decision.getKey().getObligations()).isNull();
-        assertThat(decision.getKey().getAttributes()).isNull();
+        assertNoInfo(decision);
         //
         // Dump it out as Json
         //
         LOGGER.info(gson.encode(decision.getKey()));
+
+        //
+        // Set the policy-type for the decision request.
+        //
+        requestPolicyType.getResource().put("policy-type", policyType);
+
         //
         // Ask for a decision based on policy-type
         //
@@ -236,13 +264,11 @@ public class MonitoringPdpApplicationTest {
         //
         // Monitoring applications should not have this information returned
         //
-        assertThat(decision.getKey().getAdvice()).isNull();
-        assertThat(decision.getKey().getObligations()).isNull();
-        assertThat(decision.getKey().getAttributes()).isNull();
+        assertNoInfo(decision);
         //
         // Validate the full policy is returned
         //
-        Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
+        Map<String, Object> jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
         assertThat(jsonPolicy).isNotNull();
         assertThat(jsonPolicy.get("properties")).isNotNull();
         //
@@ -264,13 +290,11 @@ public class MonitoringPdpApplicationTest {
         //
         // Monitoring applications should not have this information returned
         //
-        assertThat(decision.getKey().getAdvice()).isNull();
-        assertThat(decision.getKey().getObligations()).isNull();
-        assertThat(decision.getKey().getAttributes()).isNull();
+        assertNoInfo(decision);
         //
         // Validate an abbreviated policy is returned
         //
-        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
+        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
         assertThat(jsonPolicy).isNotNull().doesNotContainKey("properties");
         //
         // Don't Ask for abbreviated results
@@ -287,13 +311,11 @@ public class MonitoringPdpApplicationTest {
         //
         // Monitoring applications should not have this information returned
         //
-        assertThat(decision.getKey().getAdvice()).isNull();
-        assertThat(decision.getKey().getObligations()).isNull();
-        assertThat(decision.getKey().getAttributes()).isNull();
+        assertNoInfo(decision);
         //
         // And should have full policy returned
         //
-        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
+        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
         assertThat(jsonPolicy).isNotNull();
         assertThat(jsonPolicy.get("properties")).isNotNull();
         //
@@ -306,7 +328,7 @@ public class MonitoringPdpApplicationTest {
 
         assertThat(decision.getKey()).isNotNull();
         assertThat(decision.getKey().getPolicies()).hasSize(1);
-        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get("onap.scaleout.tca");
+        jsonPolicy = (Map<String, Object>) decision.getKey().getPolicies().get(policyId);
         assertThat(jsonPolicy).isNotNull();
         assertThat(jsonPolicy.get("properties")).isNotNull();
         //
@@ -330,4 +352,10 @@ public class MonitoringPdpApplicationTest {
         assertThat(decision.getKey().getPolicies()).isEmpty();
     }
 
+    private void assertNoInfo(Pair<DecisionResponse, Response> decision) {
+        assertThat(decision.getKey().getAdvice()).isNull();
+        assertThat(decision.getKey().getObligations()).isNull();
+        assertThat(decision.getKey().getAttributes()).isNull();
+    }
+
 }
index 10f2046..996d227 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -50,10 +50,10 @@ public class XacmlPdpApplicationManager {
     @Setter
     private static XacmlPdpApplicationManager current;
 
-    private ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
-    private Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
-    private List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
-    private Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
+    private final ServiceLoader<XacmlApplicationServiceProvider> applicationLoader;
+    private final Map<String, XacmlApplicationServiceProvider> providerActionMap = new HashMap<>();
+    private final List<ToscaConceptIdentifier> toscaPolicyTypeIdents = new ArrayList<>();
+    private final Map<ToscaPolicy, XacmlApplicationServiceProvider> mapLoadedPolicies = new HashMap<>();
 
 
     /**
index b6c84b1..b04f44f 100644 (file)
@@ -55,7 +55,7 @@ public class XacmlPdpApplicationManagerTest {
     private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
     private static Path appsDirectory;
     private static ToscaServiceTemplate completedJtst;
-    private static CommonTestData testData = new CommonTestData();
+    private static final CommonTestData testData = new CommonTestData();
 
     @ClassRule
     public static final TemporaryFolder appsFolder = new TemporaryFolder();
@@ -97,9 +97,7 @@ public class XacmlPdpApplicationManagerTest {
         //
         Path src = Paths.get("src/test/resources/apps");
         File apps = appsFolder.newFolder("apps");
-        Files.walk(src).forEach(source -> {
-            copy(source, apps.toPath().resolve(src.relativize(source)));
-        });
+        Files.walk(src).forEach(source -> copy(source, apps.toPath().resolve(src.relativize(source))));
         appsDirectory = apps.toPath();
     }