Add exclusions parameter 67/122667/5
authorPamela Dragosh <pd1248@att.com>
Thu, 15 Jul 2021 19:27:05 +0000 (15:27 -0400)
committerPamela Dragosh <pd1248@att.com>
Fri, 16 Jul 2021 13:42:38 +0000 (09:42 -0400)
Adds the exclusions parameter to exclude applications
that are in the java classpath. This allows overriding
default packaged applications with another that can
support new policy types and/or modify existing
functionality.

Issue-ID: POLICY-3326
Change-Id: Iaad8d26fc4122ad17226ad1e83b72c9f284e6ebd
Signed-off-by: Pamela Dragosh <pd1248@att.com>
applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java
main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlApplicationParameters.java
main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java
main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java
main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java
main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java
main/src/test/java/org/onap/policy/pdpx/main/rest/TestGuardOverrideApplication.java [new file with mode: 0644]
main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java
main/src/test/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider [new file with mode: 0644]
main/src/test/resources/parameters/XacmlPdpConfigParameters_Exclusions.json [new file with mode: 0644]

index 1832945..b2c9f12 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory;
  */
 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
-    private static final String STRING_VERSION100 = "1.0.0";
+    protected static final String STRING_VERSION100 = "1.0.0";
 
     private GuardTranslator guardTranslator = new GuardTranslator();
     private CoordinationGuardTranslator coordinationTranslator = new CoordinationGuardTranslator();
index f1ec19d..ad84051 100644 (file)
@@ -20,7 +20,9 @@
 
 package org.onap.policy.pdpx.main.parameters;
 
+import java.util.List;
 import lombok.Getter;
+import lombok.NonNull;
 import org.onap.policy.common.parameters.ParameterGroupImpl;
 import org.onap.policy.common.parameters.annotations.NotBlank;
 import org.onap.policy.common.parameters.annotations.NotNull;
@@ -32,8 +34,24 @@ public class XacmlApplicationParameters extends ParameterGroupImpl {
     @NotNull
     private String applicationPath;
 
+    private List<String> exclusions;
+
     public XacmlApplicationParameters() {
         super(XacmlApplicationParameters.class.getSimpleName());
     }
 
+    /**
+     * Looks for an application class that has been configured
+     * as excluded.
+     *
+     * @param canonicalName The classname
+     * @return true if excluded
+     */
+    public boolean isExcluded(@NonNull String canonicalName) {
+        if (exclusions == null) {
+            return false;
+        }
+        return exclusions.contains(canonicalName);
+    }
+
 }
index 5ecf1aa..6cd4683 100644 (file)
@@ -77,6 +77,13 @@ public class XacmlPdpApplicationManager {
                     application.supportedPolicyTypes());
             }
             //
+            // Check for exclusions
+            //
+            if (applicationParameters.isExcluded(application.getClass().getName())) {
+                LOGGER.info("excluded {}", application.getClass().getName());
+                continue;
+            }
+            //
             // We are not going to make this available unless the application can
             // install correctly.
             //
@@ -218,11 +225,7 @@ public class XacmlPdpApplicationManager {
      * @return Total count added from all applications
      */
     public long getPolicyTypeCount() {
-        long types = 0;
-        for (XacmlApplicationServiceProvider application : applicationLoader) {
-            types += application.supportedPolicyTypes().size();
-        }
-        return types;
+        return toscaPolicyTypeIdents.size();
     }
 
     /**
index 8b79ae9..21475da 100644 (file)
@@ -176,12 +176,17 @@ public class CommonTestData {
      *
      * @param isEmpty boolean value to represent that object created should be empty or not
      * @param tempPath Application Path string
+     * @param exclusions An optional list of application classnames for exclusion
      * @return a property map suitable for constructing an object
      */
-    public Map<String, Object> getXacmlapplicationParametersMap(boolean isEmpty, String tempPath) {
+    public Map<String, Object> getXacmlapplicationParametersMap(boolean isEmpty, String tempPath,
+            String... exclusions) {
         final Map<String, Object> map = new TreeMap<>();
         if (!isEmpty) {
             map.put("applicationPath", tempPath);
+            if (exclusions != null) {
+                map.put("exclusions", List.of(exclusions));
+            }
         }
         return map;
     }
index f71c99c..cb9825c 100644 (file)
@@ -81,6 +81,7 @@ public class TestXacmlPdpParameterGroup {
         assertEquals("flavor", pdpxParameters.getPdpType());
         assertFalse(pdpxParameters.getRestServerParameters().isHttps());
         assertFalse(pdpxParameters.getRestServerParameters().isAaf());
+        assertThat(pdpxParameters.getApplicationParameters().getExclusions()).isEmpty();
     }
 
     @Test
index 1e86fee..00e95db 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.policy.pdpx.main.parameters;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -168,6 +169,20 @@ public class TestXacmlPdpParameterHandler {
                         + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\"");
     }
 
+    @Test
+    public void testXacmlPdpParameterGroup_Exclusions() throws PolicyXacmlPdpException {
+        final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_Exclusions.json"};
+
+        final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments();
+        arguments.parse(xacmlPdpConfigParameters);
+
+        final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments);
+        assertTrue(arguments.checkSetConfigurationFilePath());
+        assertEquals(CommonTestData.PDPX_PARAMETER_GROUP_NAME, parGroup.getName());
+        assertEquals(CommonTestData.PDPX_GROUP, parGroup.getPdpGroup());
+        assertThat(parGroup.getApplicationParameters().getExclusions()).hasSize(2);
+    }
+
     @Test
     public void testXacmlPdpVersion() throws PolicyXacmlPdpException {
         final String[] xacmlPdpConfigParameters = {"-v"};
diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestGuardOverrideApplication.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestGuardOverrideApplication.java
new file mode 100644 (file)
index 0000000..1b8411e
--- /dev/null
@@ -0,0 +1,70 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdpx.main.rest;
+
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
+import org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication;
+import org.onap.policy.xacml.pdp.application.guard.GuardTranslator;
+
+public class TestGuardOverrideApplication extends GuardPdpApplication {
+    public static final String MY_EXTRAGUARD_POLICY_TYPE = "onap.policies.controlloop.guard.common.myGuard";
+
+    private static class MyTranslator extends GuardTranslator {
+
+    }
+
+    private final GuardTranslator myTranslator = new MyTranslator();
+
+    /**
+     * Constructor calls the super to add all the default policy types,
+     * and then adds the extra supported guard policy type.
+     */
+    public TestGuardOverrideApplication() {
+        super();
+        this.supportedPolicyTypes.add(new ToscaConceptIdentifier(
+                MY_EXTRAGUARD_POLICY_TYPE,
+                STRING_VERSION100));
+
+    }
+
+    @Override
+    protected ToscaPolicyTranslator getTranslator(String type) {
+        if (MY_EXTRAGUARD_POLICY_TYPE.equals(type)) {
+            return myTranslator;
+        }
+        return super.getTranslator(type);
+    }
+
+    @Override
+    public boolean canSupportPolicyType(ToscaConceptIdentifier policyTypeId) {
+        boolean canSuper = super.canSupportPolicyType(policyTypeId);
+        if (canSuper) {
+            return canSuper;
+        }
+        if (MY_EXTRAGUARD_POLICY_TYPE.equals(policyTypeId.getName())) {
+            return true;
+        }
+        return false;
+    }
+}
index 4619b2a..c79ebb5 100644 (file)
@@ -46,7 +46,6 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
 import org.onap.policy.pdpx.main.parameters.CommonTestData;
 import org.onap.policy.pdpx.main.parameters.XacmlApplicationParameters;
-import org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication;
 import org.onap.policy.xacml.pdp.application.nativ.NativePdpApplication;
 import org.onap.policy.xacml.pdp.application.optimization.OptimizationPdpApplication;
 import org.slf4j.Logger;
@@ -142,9 +141,11 @@ public class XacmlPdpApplicationManagerTest {
 
     @Test
     public void testXacmlPdpApplicationManagerSimple() {
+        final String[] exclusions = {"org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication",
+            "org.onap.policy.xacml.pdp.application.match.MatchPdpApplication" };
         final XacmlApplicationParameters xacmlApplicationParameters =
                 testData.toObject(testData.getXacmlapplicationParametersMap(false,
-                        appsDirectory.toString()), XacmlApplicationParameters.class);
+                        appsDirectory.toString(), exclusions), XacmlApplicationParameters.class);
         XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(xacmlApplicationParameters, params);
         //
         // Test the basics from the startup
@@ -162,7 +163,12 @@ public class XacmlPdpApplicationManagerTest {
         request.setAction("optimize");
         assertThat(manager.findApplication(request)).isInstanceOf(OptimizationPdpApplication.class);
         request.setAction("guard");
-        assertThat(manager.findApplication(request)).isInstanceOf(GuardPdpApplication.class);
+        assertThat(manager.findApplication(request)).isInstanceOf(TestGuardOverrideApplication.class);
+        //
+        // Test Exclusion
+        //
+        request.setAction("match");
+        assertThat(manager.findApplication(request)).isNull();
         //
         // Try to unload a policy that isn't loaded
         //
diff --git a/main/src/test/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider b/main/src/test/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider
new file mode 100644 (file)
index 0000000..fe9e878
--- /dev/null
@@ -0,0 +1 @@
+org.onap.policy.pdpx.main.rest.TestGuardOverrideApplication
diff --git a/main/src/test/resources/parameters/XacmlPdpConfigParameters_Exclusions.json b/main/src/test/resources/parameters/XacmlPdpConfigParameters_Exclusions.json
new file mode 100644 (file)
index 0000000..1c3d8e8
--- /dev/null
@@ -0,0 +1,36 @@
+{
+    "name": "XacmlPdpParameters",
+    "pdpGroup": "XacmlPdpGroup",
+    "pdpType": "xacml",
+    "restServerParameters": {
+        "host": "0.0.0.0",
+        "port": 6969,
+        "userName": "healthcheck",
+        "password": "zb!XztG34"
+    },
+    "policyApiParameters": {
+        "hostname": "0.0.0.0",
+        "port": 6970,
+        "userName": "healthcheck",
+        "password": "zb!XztG34"
+    },
+    "applicationParameters": {
+        "applicationPath": "src/test/resources/apps",
+        "exclusions": [
+            "org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication",
+            "org.onap.policy.xacml.pdp.application.monitoring.MonitoringPdpApplication"
+        ]
+    },
+    "topicParameterGroup": {
+        "topicSources" : [{
+            "topic" : "POLICY-PDP-PAP",
+            "servers" : [ "anyserver" ],
+            "topicCommInfrastructure" : "noop"
+        }],
+        "topicSinks" : [{
+            "topic" : "POLICY-PDP-PAP",
+            "servers" : [ "anyserver" ],
+            "topicCommInfrastructure" : "noop"
+        }]
+    }
+}