From 7711185bb36b387e3596653ca170262f919ff474 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Thu, 15 Jul 2021 15:27:05 -0400 Subject: Add exclusions parameter 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 --- .../pdp/application/guard/GuardPdpApplication.java | 4 +- .../parameters/XacmlApplicationParameters.java | 18 ++++++ .../pdpx/main/rest/XacmlPdpApplicationManager.java | 13 ++-- .../pdpx/main/parameters/CommonTestData.java | 7 ++- .../parameters/TestXacmlPdpParameterGroup.java | 1 + .../parameters/TestXacmlPdpParameterHandler.java | 15 +++++ .../main/rest/TestGuardOverrideApplication.java | 70 ++++++++++++++++++++++ .../main/rest/XacmlPdpApplicationManagerTest.java | 12 +++- ...lication.common.XacmlApplicationServiceProvider | 1 + .../XacmlPdpConfigParameters_Exclusions.json | 36 +++++++++++ 10 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 main/src/test/java/org/onap/policy/pdpx/main/rest/TestGuardOverrideApplication.java create mode 100644 main/src/test/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider create mode 100644 main/src/test/resources/parameters/XacmlPdpConfigParameters_Exclusions.json diff --git a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java index 1832945c..b2c9f125 100644 --- a/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java +++ b/applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/GuardPdpApplication.java @@ -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(); diff --git a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlApplicationParameters.java b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlApplicationParameters.java index f1ec19d3..ad840513 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlApplicationParameters.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/parameters/XacmlApplicationParameters.java @@ -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 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); + } + } diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java index 5ecf1aa5..6cd46838 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java @@ -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(); } /** diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java index 8b79ae9b..21475da8 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java @@ -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 getXacmlapplicationParametersMap(boolean isEmpty, String tempPath) { + public Map getXacmlapplicationParametersMap(boolean isEmpty, String tempPath, + String... exclusions) { final Map map = new TreeMap<>(); if (!isEmpty) { map.put("applicationPath", tempPath); + if (exclusions != null) { + map.put("exclusions", List.of(exclusions)); + } } return map; } diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java index f71c99c2..cb9825cc 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java @@ -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 diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java index 1e86fee7..00e95db7 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java @@ -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 index 00000000..1b8411e5 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestGuardOverrideApplication.java @@ -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; + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java index 4619b2ae..c79ebb52 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java @@ -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 index 00000000..fe9e8788 --- /dev/null +++ b/main/src/test/resources/META-INF/services/org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider @@ -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 index 00000000..1c3d8e88 --- /dev/null +++ b/main/src/test/resources/parameters/XacmlPdpConfigParameters_Exclusions.json @@ -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" + }] + } +} -- cgit 1.2.3-korg