diff options
43 files changed, 1939 insertions, 98 deletions
diff --git a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/PolicyForwarder.java b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/PolicyForwarder.java index 5272fc9b..5bd53804 100644 --- a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/PolicyForwarder.java +++ b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/PolicyForwarder.java @@ -25,10 +25,25 @@ import org.onap.policy.distribution.model.Policy; /** * Forwards polices. + * <p> + * To create a policy forwarder a class implementing this interface must be created, along with a + * concrete sub class of PolicyForwarderConfigurationParameterGroup to handle configuration + * parameters for the policy forwarder. */ public interface PolicyForwarder { /** + * Configure the policy forwarder. + * <p> + * This method will be invoked immediately after instantiation in order for the policy forwarder + * to configure itself. + * + * @param parameterGroupName the name of the parameter group which contains the configuration + * for the policy forwarder + */ + void configure(String parameterGroupName); + + /** * Forward the given policies. * * @param policies the policies to forward diff --git a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java index 28a1b1be..2426cccd 100644 --- a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java +++ b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java @@ -37,16 +37,20 @@ public class PolicyForwarderParameters implements ParameterGroup { private String forwarderType; private String forwarderClassName; + private String forwarderConfigurationName; /** * Constructor for instantiating PolicyForwarderParameters. * * @param forwarderType the policy forwarder type * @param forwarderClassName the policy forwarder class name + * @param forwarderConfigurationName the name of the configuration for the policy forwarder */ - public PolicyForwarderParameters(final String forwarderType, final String forwarderClassName) { + public PolicyForwarderParameters(final String forwarderType, final String forwarderClassName, + final String forwarderConfigurationName) { this.forwarderType = forwarderType; this.forwarderClassName = forwarderClassName; + this.forwarderConfigurationName = forwarderConfigurationName; } /** @@ -68,6 +72,15 @@ public class PolicyForwarderParameters implements ParameterGroup { } /** + * Return the name of the forwarder configuration of this PolicyForwarderParameters instance. + * + * @return the the name of the forwarder configuration + */ + public String getForwarderConfigurationName() { + return forwarderConfigurationName; + } + + /** * {@inheritDoc} */ @Override diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java index a188fb04..27291255 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java @@ -20,9 +20,9 @@ package org.onap.policy.distribution.main.parameters; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; - import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; @@ -38,6 +38,9 @@ public class DistributionParameterGroup implements ParameterGroup { private String name; private RestServerParameters restServerParameters; private Map<String, ReceptionHandlerParameters> receptionHandlerParameters; + private Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters = + new LinkedHashMap<>(); + /** * Create the distribution parameter group. @@ -45,10 +48,12 @@ public class DistributionParameterGroup implements ParameterGroup { * @param name the parameter group name */ public DistributionParameterGroup(final String name, final RestServerParameters restServerParameters, - final Map<String, ReceptionHandlerParameters> receptionHandlerParameters) { + final Map<String, ReceptionHandlerParameters> receptionHandlerParameters, + final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters) { this.name = name; this.restServerParameters = restServerParameters; this.receptionHandlerParameters = receptionHandlerParameters; + this.policyForwarderConfigurationParameters = policyForwarderConfigurationParameters; } /** @@ -89,6 +94,26 @@ public class DistributionParameterGroup implements ParameterGroup { return restServerParameters; } + + /** + * Gets the policy forwarder configuration parameter map. + * + * @return the policy forwarder configuration parameter map + */ + public Map<String, PolicyForwarderConfigurationParameterGroup> getPolicyForwarderConfigurationParameters() { + return policyForwarderConfigurationParameters; + } + + /** + * Sets the policy forwarder configuration parameter map. + * + * @param eventInputParameters the policy forwarder configuration parameters + */ + public void setPolicyForwarderConfigurationParameters( + final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters) { + this.policyForwarderConfigurationParameters = policyForwarderConfigurationParameters; + } + /** * Validate the parameter group. * @@ -107,6 +132,7 @@ public class DistributionParameterGroup implements ParameterGroup { validationResult.setResult("restServerParameters", restServerParameters.validate()); } validateReceptionHandlers(validationResult); + validateForwarderConfigurations(validationResult); return validationResult; } @@ -126,4 +152,12 @@ public class DistributionParameterGroup implements ParameterGroup { } } } + + private void validateForwarderConfigurations(final GroupValidationResult validationResult) { + for (final Entry<String, PolicyForwarderConfigurationParameterGroup> configurationParameters : policyForwarderConfigurationParameters + .entrySet()) { + validationResult.setResult("policyForwarderConfigurationParameters", configurationParameters.getKey(), + configurationParameters.getValue().validate()); + } + } } diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java index 3174b8c6..5cc507e6 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java @@ -22,9 +22,7 @@ package org.onap.policy.distribution.main.parameters; import com.google.gson.Gson; import com.google.gson.GsonBuilder; - import java.io.FileReader; - import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.GroupValidationResult; @@ -32,7 +30,8 @@ import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; /** - * This class handles reading, parsing and validating of policy distribution parameters from JSON files. + * This class handles reading, parsing and validating of policy distribution parameters from JSON + * files. */ public class DistributionParameterHandler { private static final Logger LOGGER = FlexLogger.getLogger(DistributionParameterHandler.class); @@ -51,7 +50,8 @@ public class DistributionParameterHandler { // Read the parameters try { // Read the parameters from JSON using Gson - final Gson gson = new GsonBuilder().create(); + final Gson gson = new GsonBuilder().registerTypeAdapter(PolicyForwarderConfigurationParameterGroup.class, + new PolicyForwarderConfigurationParametersJsonAdapter()).create(); distributionParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), DistributionParameterGroup.class); } catch (final Exception e) { diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParameterGroup.java index eb33a852..f2c496d4 100644 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.java +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParameterGroup.java @@ -18,21 +18,26 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.pap.engine; - -import java.util.Collection; +package org.onap.policy.distribution.main.parameters; +import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.distribution.forwarding.PolicyForwarder; -import org.onap.policy.distribution.model.Policy; /** - * Forwards policies to the XACML PAP Servlet. + * Base class of all {@link ParameterGroup} classes for configuration parameters for + * {@link PolicyForwarder} classes */ -public class XacmlPapServletPolicyForwarder implements PolicyForwarder { +public abstract class PolicyForwarderConfigurationParameterGroup implements ParameterGroup { + + private String name; @Override - public void forward(final Collection<Policy> policies) { - // Send policies to PAP using common/policy-endpoints + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; } } diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java new file mode 100644 index 00000000..5ce15a28 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderConfigurationParametersJsonAdapter.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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.distribution.main.parameters; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import java.lang.reflect.Type; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class deserialises policy forwarder parameters from JSON. + */ +public class PolicyForwarderConfigurationParametersJsonAdapter + implements JsonDeserializer<PolicyForwarderConfigurationParameterGroup> { + private static final XLogger LOGGER = + XLoggerFactory.getXLogger(PolicyForwarderConfigurationParametersJsonAdapter.class); + + private static final String PARAMETER_CLASS_NAME = "parameterClassName"; + private static final String POLICY_FORWARDER_PARAMETERS = "parameters"; + + @Override + public PolicyForwarderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) { + final JsonObject jsonObject = json.getAsJsonObject(); + + final String policyForwarderParameterClassName = getParameterGroupClassName(jsonObject); + Class<?> policyForwarderParameterClass = getParameterGroupClass(policyForwarderParameterClassName); + + return context.deserialize(jsonObject.get(POLICY_FORWARDER_PARAMETERS), policyForwarderParameterClass); + } + + private String getParameterGroupClassName(final JsonObject jsonObject) { + final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME)); + + if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) { + final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \"" + + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null") + + "\" invalid in JSON file"; + LOGGER.warn(errorMessage); + throw new IllegalArgumentException(errorMessage); + } + return classNameJsonPrimitive.getAsString().replaceAll("\\s+", ""); + } + + private Class<?> getParameterGroupClass(final String policyForwarderParameterClassName) { + Class<?> policyForwarderParameterClass = null; + try { + policyForwarderParameterClass = Class.forName(policyForwarderParameterClassName); + } catch (final ClassNotFoundException e) { + final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \"" + + policyForwarderParameterClassName + "\", could not find class"; + LOGGER.warn(errorMessage, e); + throw new IllegalArgumentException(errorMessage, e); + } + return policyForwarderParameterClass; + } + +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java index e68a37d5..d4b7fb79 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java @@ -22,20 +22,21 @@ package org.onap.policy.distribution.main.startstop; import java.util.HashMap; import java.util.Map; - +import java.util.Map.Entry; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; +import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup; import org.onap.policy.distribution.main.rest.DistributionRestServer; import org.onap.policy.distribution.reception.decoding.PluginInitializationException; import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** - * This class wraps a distributor so that it can be activated as a complete service together with all its distribution - * and forwarding handlers. + * This class wraps a distributor so that it can be activated as a complete service together with + * all its distribution and forwarding handlers. */ public class DistributionActivator { // The logger for this class @@ -148,6 +149,11 @@ public class DistributionActivator { ParameterService.register(params.getPssdConfigurationParametersGroup()); ParameterService.register(params.getPluginHandlerParameters()); } + for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration : distributionParameterGroup + .getPolicyForwarderConfigurationParameters().entrySet()) { + forwarderConfiguration.getValue().setName(forwarderConfiguration.getKey()); + ParameterService.register(forwarderConfiguration.getValue()); + } } /** @@ -163,6 +169,11 @@ public class DistributionActivator { ParameterService.deregister((params.getPssdConfigurationParametersGroup().getName())); ParameterService.deregister((params.getPluginHandlerParameters().getName())); } + for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration : distributionParameterGroup + .getPolicyForwarderConfigurationParameters().entrySet()) { + forwarderConfiguration.getValue().setName(forwarderConfiguration.getKey()); + ParameterService.deregister(forwarderConfiguration.getKey()); + } } /** diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java index ac58b830..186aa821 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java @@ -24,8 +24,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; - import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; +import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; +import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup.DummyPolicyForwarderParameterGroupBuilder; import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; import org.onap.policy.distribution.reception.parameters.PssdConfigurationParametersGroup; @@ -48,6 +49,8 @@ public class CommonTestData { public static final String FORWARDER_TYPE = "DummyForwarder"; public static final String FORWARDER_CLASS_NAME = "org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder"; + public static final String FORWARDER_CONFIGURATION_PARAMETERS = "dummyConfiguration"; + public static final String FORWARDER_HOST = "192.168.99.100"; public static final String RECEPTION_HANDLER_TYPE = "DummyReceptionHandler"; public static final String RECEPTION_HANDLER_CLASS_NAME = "org.onap.policy.distribution.main.testclasses.DummyReceptionHandler"; @@ -142,8 +145,8 @@ public class CommonTestData { final Map<String, PolicyForwarderParameters> policyForwarders = new HashMap<String, PolicyForwarderParameters>(); if (!isEmpty) { - final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(FORWARDER_TYPE, + FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS); policyForwarders.put(DUMMY_ENGINE_FORWARDER_KEY, pFParameters); } return policyForwarders; @@ -163,4 +166,24 @@ public class CommonTestData { } return policyDecoders; } + + /** + * Returns PolicyForwarderConfigurationParameterGroups for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return the PolicyForwarderConfigurationParameterGroups + */ + public Map<String, PolicyForwarderConfigurationParameterGroup> getPolicyForwarderConfigurationParameters( + final boolean isEmpty) { + final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters = + new HashMap<String, PolicyForwarderConfigurationParameterGroup>(); + if (!isEmpty) { + DummyPolicyForwarderParameterGroup dummyPolicyForwarderParameterGroup = + new DummyPolicyForwarderParameterGroupBuilder().setUseHttps(true).setHostname(FORWARDER_HOST) + .setPort(1234).setUserName("myUser").setPassword("myPassword").setIsManaged(true).build(); + policyForwarderConfigurationParameters.put(FORWARDER_CONFIGURATION_PARAMETERS, + dummyPolicyForwarderParameterGroup); + } + return policyForwarderConfigurationParameters; + } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java index a2d72585..0c8c80d6 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java @@ -26,9 +26,9 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Map; - import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** @@ -44,9 +44,12 @@ public class TestDistributionParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData .getReceptionHandlerParameters(false); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); - final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( - CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); + final DistributionParameterGroup distributionParameters = + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, + receptionHandlerParameters, forwarderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(restServerParameters.getHost(), distributionParameters.getRestServerParameters().getHost()); @@ -69,6 +72,9 @@ public class TestDistributionParameterGroup { .getPluginHandlerParameters(), distributionParameters.getReceptionHandlerParameters() .get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY).getPluginHandlerParameters()); + assertEquals(CommonTestData.FORWARDER_HOST, + ((DummyPolicyForwarderParameterGroup) distributionParameters.getPolicyForwarderConfigurationParameters() + .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS)).getHostname()); } @Test @@ -76,9 +82,11 @@ public class TestDistributionParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData .getReceptionHandlerParameters(false); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); final DistributionParameterGroup distributionParameters = new DistributionParameterGroup(null, - restServerParameters, receptionHandlerParameters); + restServerParameters, receptionHandlerParameters, forwarderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, distributionParameters.getName()); @@ -105,9 +113,11 @@ public class TestDistributionParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData .getReceptionHandlerParameters(false); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); final DistributionParameterGroup distributionParameters = new DistributionParameterGroup("", - restServerParameters, receptionHandlerParameters); + restServerParameters, receptionHandlerParameters, forwarderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", distributionParameters.getName()); @@ -131,9 +141,11 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_NullReceptionHandlerParameters() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); try { final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( - CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null); + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null, forwarderConfigurations); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -147,9 +159,11 @@ public class TestDistributionParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData .getReceptionHandlerParameters(true); - - final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( - CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); + final DistributionParameterGroup distributionParameters = + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, + receptionHandlerParameters, forwarderConfigurations); distributionParameters.validate(); GroupValidationResult result = distributionParameters.validate(); assertFalse(result.isValid()); @@ -161,9 +175,12 @@ public class TestDistributionParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData .getReceptionHandlerParameters(false); + final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = + commonTestData.getPolicyForwarderConfigurationParameters(false); - final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( - CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); + final DistributionParameterGroup distributionParameters = + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, + receptionHandlerParameters, forwarderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java index 8b474984..14d1a105 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java @@ -27,10 +27,10 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; - import org.junit.Test; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; +import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; /** * Class to perform unit test of DistributionParameterHandler. @@ -40,8 +40,7 @@ import org.onap.policy.distribution.main.startstop.DistributionCommandLineArgume public class TestDistributionParameterHandler { @Test public void testParameterHandlerNoParameterFile() throws PolicyDistributionException { - final String[] noArgumentString = - { "-c", "parameters/NoParameterFile.json" }; + final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"}; final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); @@ -56,8 +55,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerEmptyParameters() throws PolicyDistributionException { - final String[] emptyArgumentString = - { "-c", "parameters/EmptyParameters.json" }; + final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"}; final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments(); emptyArguments.parse(emptyArgumentString); @@ -72,8 +70,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerBadParameters() throws PolicyDistributionException { - final String[] badArgumentString = - { "-c", "parameters/BadParameters.json" }; + final String[] badArgumentString = {"-c", "parameters/BadParameters.json"}; final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments(); badArguments.parse(badArgumentString); @@ -90,8 +87,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerInvalidParameters() throws PolicyDistributionException { - final String[] invalidArgumentString = - { "-c", "parameters/InvalidParameters.json" }; + final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"}; final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments(); invalidArguments.parse(invalidArgumentString); @@ -108,8 +104,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerNoParameters() throws PolicyDistributionException { - final String[] noArgumentString = - { "-c", "parameters/NoParameters.json" }; + final String[] noArgumentString = {"-c", "parameters/NoParameters.json"}; final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); @@ -124,8 +119,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerMinumumParameters() throws PolicyDistributionException { - final String[] minArgumentString = - { "-c", "parameters/MinimumParameters.json" }; + final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"}; final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments(); minArguments.parse(minArgumentString); @@ -136,8 +130,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + final String[] distributionConfigParameters = {"-c", "parameters/DistributionConfigParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -155,12 +148,50 @@ public class TestDistributionParameterHandler { parGroup.getReceptionHandlerParameters().get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY) .getPluginHandlerParameters().getPolicyForwarders() .get(CommonTestData.DUMMY_ENGINE_FORWARDER_KEY).getForwarderType()); + assertEquals(CommonTestData.FORWARDER_HOST, + ((DummyPolicyForwarderParameterGroup) parGroup.getPolicyForwarderConfigurationParameters() + .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS)).getHostname()); + } + + @Test + public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName() + throws PolicyDistributionException { + final String[] distributionConfigParameters = + {"-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json"}; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("parameter \"parameterClassName\" value \"\" invalid in JSON file")); + } + } + + @Test + public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName() + throws PolicyDistributionException { + final String[] distributionConfigParameters = + {"-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json"}; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class")); + } } @Test public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidName.json" }; + {"-c", "parameters/DistributionConfigParameters_InvalidName.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -177,7 +208,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json" }; + {"-c", "parameters/DistributionConfigParameters_NoReceptionHandler.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -193,7 +224,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json" }; + {"-c", "parameters/DistributionConfigParameters_EmptyReceptionHandler.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -209,7 +240,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json" }; + {"-c", "parameters/DistributionConfigParameters_NoPolicyDecoder.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -225,7 +256,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json" }; + {"-c", "parameters/DistributionConfigParameters_NoPolicyForwarder.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -241,7 +272,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json" }; + {"-c", "parameters/DistributionConfigParameters_EmptyPolicyDecoder.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -257,7 +288,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json" }; + {"-c", "parameters/DistributionConfigParameters_EmptyPolicyForwarder.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -274,7 +305,7 @@ public class TestDistributionParameterHandler { public void testDistributionParameterGroup_InvalidReceptionHandlerParameters() throws PolicyDistributionException, IOException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json" }; + {"-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -294,7 +325,7 @@ public class TestDistributionParameterHandler { public void testDistributionParameterGroup_InvalidDecoderAndForwarderParameters() throws PolicyDistributionException, IOException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json" }; + {"-c", "parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -314,7 +345,7 @@ public class TestDistributionParameterHandler { public void testDistributionParameterGroup_InvalidRestServerParameters() throws PolicyDistributionException, IOException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidRestServerParameters.json" }; + {"-c", "parameters/DistributionConfigParameters_InvalidRestServerParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -332,8 +363,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionVersion() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-v" }; + final String[] distributionConfigParameters = {"-v"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String version = arguments.parse(distributionConfigParameters); assertTrue(version.startsWith("ONAP Policy Framework Distribution Service")); @@ -341,8 +371,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionHelp() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-h" }; + final String[] distributionConfigParameters = {"-h"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String help = arguments.parse(distributionConfigParameters); assertTrue(help.startsWith("usage:")); @@ -350,8 +379,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionInvalidOption() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-d" }; + final String[] distributionConfigParameters = {"-d"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); try { arguments.parse(distributionConfigParameters); diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java index 552ee50c..9eeae354 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java @@ -26,7 +26,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Map; - import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java index d64df1b3..9907bfb8 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java @@ -23,6 +23,9 @@ package org.onap.policy.distribution.main.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.onap.policy.distribution.main.parameters.CommonTestData.FORWARDER_CLASS_NAME; +import static org.onap.policy.distribution.main.parameters.CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS; +import static org.onap.policy.distribution.main.parameters.CommonTestData.FORWARDER_TYPE; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; @@ -38,20 +41,20 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, CommonTestData.FORWARDER_CLASS_NAME); + new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); - assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); + assertEquals(FORWARDER_TYPE, pFParameters.getForwarderType()); + assertEquals(FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertTrue(validationResult.isValid()); } @Test public void testPolicyForwarderParameters_InvalidForwarderType() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters("", CommonTestData.FORWARDER_CLASS_NAME); + new PolicyForwarderParameters("", FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals("", pFParameters.getForwarderType()); - assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); + assertEquals(FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"forwarderType\" type \"java.lang.String\" value \"\" INVALID, must be a non-blank string")); @@ -59,7 +62,8 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_InvalidForwarderClassName() { - final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, ""); + final PolicyForwarderParameters pFParameters = + new PolicyForwarderParameters(FORWARDER_TYPE, "", FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); assertEquals("", pFParameters.getForwarderClassName()); @@ -71,7 +75,8 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_InvalidForwarderTypeAndClassName() { - final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters("", ""); + final PolicyForwarderParameters pFParameters = + new PolicyForwarderParameters("", "", FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals("", pFParameters.getForwarderType()); assertEquals("", pFParameters.getForwarderClassName()); @@ -86,10 +91,10 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_NullForwarderType() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(null, CommonTestData.FORWARDER_CLASS_NAME); + new PolicyForwarderParameters(null, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals(null, pFParameters.getForwarderType()); - assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); + assertEquals(FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() .contains("field \"forwarderType\" type \"java.lang.String\" value \"null\" INVALID, " @@ -99,9 +104,9 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_NullForwarderClassName() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, null); + new PolicyForwarderParameters(FORWARDER_TYPE, null, FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); + assertEquals(FORWARDER_TYPE, pFParameters.getForwarderType()); assertEquals(null, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -111,11 +116,11 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_InvalidForwarderClass() { - final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, - CommonTestData.FORWARDER_CLASS_NAME + "Invalid"); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(FORWARDER_TYPE, + FORWARDER_CLASS_NAME + "Invalid", FORWARDER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); - assertEquals(CommonTestData.FORWARDER_CLASS_NAME + "Invalid", pFParameters.getForwarderClassName()); + assertEquals(FORWARDER_TYPE, pFParameters.getForwarderType()); + assertEquals(FORWARDER_CLASS_NAME + "Invalid", pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains("policy forwarder class not found in classpath")); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java index 294f9081..d877e2a7 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java @@ -29,6 +29,7 @@ import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.parameters.CommonTestData; import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; import org.onap.policy.distribution.main.parameters.DistributionParameterHandler; +import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; /** * Class to perform unit test of DistributionActivator. @@ -39,8 +40,7 @@ public class TestDistributionActivator { @Test public void testDistributionActivator() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + final String[] distributionConfigParameters = {"-c", "parameters/DistributionConfigParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(distributionConfigParameters); @@ -63,6 +63,9 @@ public class TestDistributionActivator { activator.getParameterGroup().getReceptionHandlerParameters() .get(CommonTestData.DUMMY_RECEPTION_HANDLER_KEY).getPluginHandlerParameters() .getPolicyForwarders().get(CommonTestData.DUMMY_ENGINE_FORWARDER_KEY).getForwarderType()); + assertEquals(DummyPolicyForwarderParameterGroup.class, + activator.getParameterGroup().getPolicyForwarderConfigurationParameters() + .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS).getClass()); activator.terminate(); assertFalse(DistributionActivator.isAlive()); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarder.java b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarder.java index 5cae0c6a..c9c7fef2 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarder.java +++ b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarder.java @@ -22,7 +22,6 @@ package org.onap.policy.distribution.main.testclasses; import java.util.ArrayList; import java.util.Collection; - import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; import org.onap.policy.distribution.model.Policy; @@ -49,4 +48,7 @@ public class DummyPolicyForwarder implements PolicyForwarder { public boolean receivedPolicy(final Policy policy) { return policiesReceived.contains(policy); } + + @Override + public void configure(String parameterGroupName) {} } diff --git a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarderParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarderParameterGroup.java new file mode 100644 index 00000000..66217d18 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyForwarderParameterGroup.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.testclasses; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup; + +/** + * Dummy policy forwarder parameter group. + */ +public class DummyPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup { + + private boolean useHttps; + private String hostname; + private int port; + private String userName; + private String password; + private boolean isManaged; + + public boolean isUseHttps() { + return useHttps; + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public String getUserName() { + return userName; + } + + public String getPassword() { + return password; + } + + public boolean isManaged() { + return isManaged; + } + + /** + * Builder for DummyPolicyForwarderParameterGroup. + */ + public static class DummyPolicyForwarderParameterGroupBuilder { + private boolean useHttps; + private String hostname; + private int port; + private String userName; + private String password; + private boolean isManaged; + + public DummyPolicyForwarderParameterGroupBuilder setUseHttps(final boolean useHttps) { + this.useHttps = useHttps; + return this; + } + + public DummyPolicyForwarderParameterGroupBuilder setHostname(final String hostname) { + this.hostname = hostname; + return this; + } + + public DummyPolicyForwarderParameterGroupBuilder setPort(final int port) { + this.port = port; + return this; + } + + public DummyPolicyForwarderParameterGroupBuilder setUserName(final String userName) { + this.userName = userName; + return this; + } + + public DummyPolicyForwarderParameterGroupBuilder setPassword(final String password) { + this.password = password; + return this; + } + + public DummyPolicyForwarderParameterGroupBuilder setIsManaged(final boolean isManaged) { + this.isManaged = isManaged; + return this; + } + + /** + * Creates a new DummyPolicyForwarderParameterGroup instance. + */ + public DummyPolicyForwarderParameterGroup build() { + return new DummyPolicyForwarderParameterGroup(this); + } + } + + /** + * Construct an instance. + * + * @param builder the builder create the instance from + */ + private DummyPolicyForwarderParameterGroup(final DummyPolicyForwarderParameterGroupBuilder builder) { + this.useHttps = builder.useHttps; + this.hostname = builder.hostname; + this.port = builder.port; + this.userName = builder.userName; + this.password = builder.password; + this.isManaged = builder.isManaged; + } + + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + return validationResult; + } + +} diff --git a/main/src/test/resources/parameters/DistributionConfigParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters.json index 29e3e467..7a557624 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters.json @@ -44,10 +44,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationParameters": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json index ce667771..d11ac9ea 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json @@ -40,10 +40,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json index 3eceef2d..c534154c 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json @@ -45,5 +45,18 @@ } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json index 74733007..b8010f8d 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json @@ -7,5 +7,18 @@ "password":"zb!XztG34" }, "receptionHandlerParameters":{ + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } }
\ No newline at end of file diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json index 4d1c4090..0c9caa6c 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json @@ -52,19 +52,35 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" }, "PAPStandaloneForwarder":{ "forwarderType":"", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.PapStandalonePolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.PapStandalonePolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" }, "PAPEngineForwarder":{ "forwarderType":"PAPEngineForwarder", - "forwarderClassName":"" + "forwarderClassName":"", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json new file mode 100644 index 00000000..ba895a67 --- /dev/null +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json @@ -0,0 +1,67 @@ +{ + "name":"SDCDistributionGroup", + "restServerParameters":{ + "host":"0.0.0.0", + "port":6969, + "userName":"healthcheck", + "password":"zb!XztG34" + }, + "receptionHandlerParameters":{ + "DummyReceptionHandler":{ + "receptionHandlerType":"DummyReceptionHandler", + "receptionHandlerClassName":"org.onap.policy.distribution.main.testclasses.DummyReceptionHandler", + "pssdConfiguration":{ + "asdcAddress": "localhost", + "messageBusAddress": [ + "a.com", + "b.com", + "c.com" + ], + "user": "tbdsdc-1480", + "password": "tbdsdc-1480", + "pollingInterval":20, + "pollingTimeout":30, + "consumerId": "policy-id", + "artifactTypes": [ + "TOSCA_CSAR", + "HEAT" + ], + "consumerGroup": "policy-group", + "environmentName": "environmentName", + "keystorePath": "null", + "keystorePassword": "null", + "activeserverTlsAuth": false, + "isFilterinEmptyResources": true, + "isUseHttpsWithDmaap": false + }, + "pluginHandlerParameters":{ + "policyDecoders":{ + "DummyDecoder":{ + "decoderType":"DummyDecoder", + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + } + }, + "policyForwarders":{ + "DummyForwarder":{ + "forwarderType":"DummyForwarder", + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationParameters": "dummyConfiguration" + } + } + } + } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } + } +} diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json index 377c70ec..7cafc266 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json @@ -44,10 +44,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json index 0ea171e8..793702fb 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json @@ -44,10 +44,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json index 85d450e6..0d650331 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json @@ -44,10 +44,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json index ffb4c728..f9392db5 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json @@ -38,10 +38,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json new file mode 100644 index 00000000..c189671d --- /dev/null +++ b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json @@ -0,0 +1,67 @@ +{ + "name":"SDCDistributionGroup", + "restServerParameters":{ + "host":"0.0.0.0", + "port":6969, + "userName":"healthcheck", + "password":"zb!XztG34" + }, + "receptionHandlerParameters":{ + "DummyReceptionHandler":{ + "receptionHandlerType":"DummyReceptionHandler", + "receptionHandlerClassName":"org.onap.policy.distribution.main.testclasses.DummyReceptionHandler", + "pssdConfiguration":{ + "asdcAddress": "localhost", + "messageBusAddress": [ + "a.com", + "b.com", + "c.com" + ], + "user": "tbdsdc-1480", + "password": "tbdsdc-1480", + "pollingInterval":20, + "pollingTimeout":30, + "consumerId": "policy-id", + "artifactTypes": [ + "TOSCA_CSAR", + "HEAT" + ], + "consumerGroup": "policy-group", + "environmentName": "environmentName", + "keystorePath": "null", + "keystorePassword": "null", + "activeserverTlsAuth": false, + "isFilterinEmptyResources": true, + "isUseHttpsWithDmaap": false + }, + "pluginHandlerParameters":{ + "policyDecoders":{ + "DummyDecoder":{ + "decoderType":"DummyDecoder", + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + } + }, + "policyForwarders":{ + "DummyForwarder":{ + "forwarderType":"DummyForwarder", + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationParameters": "dummyConfiguration" + } + } + } + } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.Unknown", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } + } +} diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json index c113d2ce..1b797def 100644 --- a/main/src/test/resources/parameters/MinimumParameters.json +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -44,10 +44,24 @@ "policyForwarders":{ "DummyForwarder":{ "forwarderType":"DummyForwarder", - "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder" + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationName": "dummyConfiguration" } } } } + }, + "policyForwarderConfigurationParameters":{ + "dummyConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup", + "parameters":{ + "useHttps": false, + "hostname": "192.168.99.100", + "port": 8081, + "userName": "user", + "password": "pw123", + "isManaged": true + } + } } } diff --git a/model/src/main/java/org/onap/policy/distribution/model/OptimizationPolicy.java b/model/src/main/java/org/onap/policy/distribution/model/OptimizationPolicy.java new file mode 100644 index 00000000..797ea4b9 --- /dev/null +++ b/model/src/main/java/org/onap/policy/distribution/model/OptimizationPolicy.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.model; + +import java.util.Date; + +/** + * An optimization policy + */ +public class OptimizationPolicy implements Policy { + + private static final String OPTIMIZATION = "Optimization"; + private String policyName; + private String policyDescription; + private String policyConfigType; + private String onapName; + private String configBody; + private String configBodyType; + private Date timetolive; + private String guard; + private String riskLevel; + private String riskType; + + @Override + public String getPolicyName() { + return policyName; + } + + @Override + public String getPolicyType() { + return OPTIMIZATION; + } + + public void setPolicyName(String policyName) { + this.policyName = policyName; + } + + public String getPolicyDescription() { + return policyDescription; + } + + public void setPolicyDescription(String policyDescription) { + this.policyDescription = policyDescription; + } + + public String getPolicyConfigType() { + return policyConfigType; + } + + public void setPolicyConfigType(String policyConfigType) { + this.policyConfigType = policyConfigType; + } + + public String getOnapName() { + return onapName; + } + + public void setOnapName(String onapName) { + this.onapName = onapName; + } + + public String getConfigBody() { + return configBody; + } + + public void setConfigBody(String configBody) { + this.configBody = configBody; + } + + public String getConfigBodyType() { + return configBodyType; + } + + public void setConfigBodyType(String configBodyType) { + this.configBodyType = configBodyType; + } + + public Date getTimetolive() { + return timetolive; + } + + public void setTimetolive(Date timetolive) { + this.timetolive = timetolive; + } + + public String getGuard() { + return guard; + } + + public void setGuard(String guard) { + this.guard = guard; + } + + public String getRiskLevel() { + return riskLevel; + } + + public void setRiskLevel(String riskLevel) { + this.riskLevel = riskLevel; + } + + public String getRiskType() { + return riskType; + } + + public void setRiskType(String riskType) { + this.riskType = riskType; + } +} diff --git a/model/src/main/java/org/onap/policy/distribution/model/Policy.java b/model/src/main/java/org/onap/policy/distribution/model/Policy.java index 74bc9e93..749f5b3d 100644 --- a/model/src/main/java/org/onap/policy/distribution/model/Policy.java +++ b/model/src/main/java/org/onap/policy/distribution/model/Policy.java @@ -25,6 +25,18 @@ package org.onap.policy.distribution.model; */ public interface Policy { - // Add methods for PolicyForwarder to use when composing REST call to PAP + /** + * Get the name of the policy. + * + * @return the name of the policy + */ + String getPolicyName(); + + /** + * Get the type of the policy. + * + * @return the type of the policy + */ + String getPolicyType(); } diff --git a/plugins/forwarding-plugins/pom.xml b/plugins/forwarding-plugins/pom.xml index 24985bbe..006caa95 100644 --- a/plugins/forwarding-plugins/pom.xml +++ b/plugins/forwarding-plugins/pom.xml @@ -36,5 +36,25 @@ <artifactId>forwarding</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.onap.policy.distribution</groupId> + <artifactId>main</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.policy.engine</groupId> + <artifactId>PolicyEngineAPI</artifactId> + <version>1.3.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>com.thoughtworks</groupId> + <artifactId>web-stub</artifactId> + <version>1.1.0</version> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java new file mode 100644 index 00000000..c50eac12 --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp; + +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.api.PushPolicyParameters; +import org.onap.policy.distribution.model.Policy; + +/** + * Adapts {@link Policy} objects to objects compatible with the XACML PDP API. + */ +public interface XacmlPdpPolicyAdapter<T extends Policy> { + + /** + * Get the policy. + * + * @return the policy + */ + T getPolicy(); + + /** + * Get as a {@link PolicyParameters} object. + * + * @returna {@link PolicyParameters} object + */ + PolicyParameters getAsPolicyParameters(); + + /** + * Get as a {@link PushPolicyParameters} object. + * + * @returna {@link PushPolicyParameters} object + */ + PushPolicyParameters getAsPushPolicyParameters(final String pdpGroups); + +} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java new file mode 100644 index 00000000..30d8f3ae --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java @@ -0,0 +1,135 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Collection; +import java.util.Collections; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.api.PushPolicyParameters; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter; +import org.onap.policy.distribution.model.OptimizationPolicy; +import org.onap.policy.distribution.model.Policy; +import org.springframework.http.HttpStatus; + +/** + * Forwards policies to the XACML PDP. + */ +public class XacmlPdpPolicyForwarder implements PolicyForwarder { + + private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class); + private static final String BASE_PATH = "pdp/api/"; + + private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null; + + + @Override + public void forward(final Collection<Policy> policies) { + for (Policy policy : policies) { + forward(policy); + } + } + + private void forward(Policy policy) { + XacmlPdpPolicyAdapter<?> policyAdapter = getXacmlPdpPolicyAdapter(policy); + + if (policyAdapter == null) { + LOGGER.error("Cannot forward policy " + policy + ". Unsupported policy type " + + policy.getClass().getSimpleName()); + return; + } + + boolean policyCreated = createPolicy(policyAdapter); + if (policyCreated) { + pushPolicy(policyAdapter); + } + } + + private XacmlPdpPolicyAdapter<?> getXacmlPdpPolicyAdapter(Policy policy) { + if (policy instanceof OptimizationPolicy) { + return new XacmlPdpOptimizationPolicyAdapter((OptimizationPolicy) policy); + } + return null; + } + + private boolean createPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) { + PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters(); + Entity<PolicyParameters> entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON); + + return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getPolicyName()); + } + + private boolean pushPolicy(XacmlPdpPolicyAdapter<?> policyAdapter) { + PushPolicyParameters pushPolicyParameters = + policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup()); + Entity<PushPolicyParameters> entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON); + + return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getPolicyName()); + } + + private boolean invokeHttpClient(final Entity<?> entity, final String method, final String policyName) { + + try { + Response response = getHttpClient().put(method, entity, + Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth())); + + if (response.getStatus() != HttpStatus.OK.value()) { + LOGGER.error( + "Invocation of method " + method + " failed for policy " + policyName + ". Response status: " + + response.getStatus() + ", Response status info: " + response.getStatusInfo()); + return false; + } + } catch (KeyManagementException | NoSuchAlgorithmException exception) { + LOGGER.error("Invocation of method " + method + " failed for policy " + policyName + + " due to error opening Http client", exception); + return false; + } + return true; + } + + private HttpClient getHttpClient() throws KeyManagementException, NoSuchAlgorithmException { + boolean useHttps = configurationParameters.isUseHttps(); + String hostname = configurationParameters.getHostname(); + int port = configurationParameters.getPort(); + String userName = configurationParameters.getUserName(); + String password = configurationParameters.getPassword(); + boolean managed = configurationParameters.isManaged(); + BusTopicParams params = BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname) + .port(port).userName(userName).password(password).basePath(BASE_PATH).managed(managed).build(); + return HttpClient.factory.build(params); + } + + @Override + public void configure(String parameterGroupName) { + configurationParameters = ParameterService.get(parameterGroupName); + } + +} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java new file mode 100644 index 00000000..e72e64ae --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java @@ -0,0 +1,182 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup; + +/** + * Holds the parameters for the{@link XacmlPdpPolicyForwarder} + */ +public class XacmlPdpPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup { + + public static final String POLICY_FORWARDER_PLUGIN_CLASS = XacmlPdpPolicyForwarder.class.getCanonicalName(); + + private boolean useHttps; + private String hostname; + private int port; + private String userName; + private String password; + private String clientAuth; + private boolean isManaged; + private String pdpGroup; + + public boolean isUseHttps() { + return useHttps; + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public String getUserName() { + return userName; + } + + public String getPassword() { + return password; + } + + public String getClientAuth() { + return clientAuth; + } + + public boolean isManaged() { + return isManaged; + } + + public String getPdpGroup() { + return pdpGroup; + } + + /** + * Builder for XacmlPdpPolicyForwarderParameterGroup. + */ + public static class XacmlPdpPolicyForwarderParameterGroupBuilder { + private boolean useHttps = false; + private String hostname; + private int port; + private String userName; + private String password; + private String clientAuth; + private boolean isManaged = true; + private String pdpGroup; + + public XacmlPdpPolicyForwarderParameterGroupBuilder setUseHttps(final boolean useHttps) { + this.useHttps = useHttps; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setHostname(final String hostname) { + this.hostname = hostname; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setPort(final int port) { + this.port = port; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setUserName(final String userName) { + this.userName = userName; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setPassword(final String password) { + this.password = password; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setClientAuth(final String clientAuth) { + this.clientAuth = clientAuth; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setIsManaged(final boolean isManaged) { + this.isManaged = isManaged; + return this; + } + + public XacmlPdpPolicyForwarderParameterGroupBuilder setPdpGroup(final String pdpGroup) { + this.pdpGroup = pdpGroup; + return this; + } + + /** + * Creates a new XacmlPapServletPolicyForwarderParameterGroup instance. + */ + public XacmlPdpPolicyForwarderParameterGroup build() { + return new XacmlPdpPolicyForwarderParameterGroup(this); + } + } + + /** + * Construct an instance + * + * @param builder the builder create the instance from + */ + private XacmlPdpPolicyForwarderParameterGroup(final XacmlPdpPolicyForwarderParameterGroupBuilder builder) { + this.useHttps = builder.useHttps; + this.hostname = builder.hostname; + this.port = builder.port; + this.userName = builder.userName; + this.password = builder.password; + this.clientAuth = builder.clientAuth; + this.isManaged = builder.isManaged; + this.pdpGroup = builder.pdpGroup; + } + + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (!ParameterValidationUtils.validateStringParameter(hostname)) { + validationResult.setResult("hostname", ValidationStatus.INVALID, + "must be a non-blank string containing hostname/ipaddress"); + } + if (!ParameterValidationUtils.validateIntParameter(port)) { + validationResult.setResult("port", ValidationStatus.INVALID, "must be a positive integer containing port"); + } + if (!ParameterValidationUtils.validateStringParameter(userName)) { + validationResult.setResult("userName", ValidationStatus.INVALID, + "must be a non-blank string containing userName"); + } + if (!ParameterValidationUtils.validateStringParameter(password)) { + validationResult.setResult("password", ValidationStatus.INVALID, + "must be a non-blank string containing password"); + } + if (!ParameterValidationUtils.validateStringParameter(clientAuth)) { + validationResult.setResult("clientAuth", ValidationStatus.INVALID, + "must be a non-blank string containing clientAuth"); + } + if (!ParameterValidationUtils.validateStringParameter(pdpGroup)) { + validationResult.setResult("pdpGroup", ValidationStatus.INVALID, + "must be a non-blank string containing pdpGroup"); + } + return validationResult; + } + +} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java new file mode 100644 index 00000000..c1365166 --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.adapters; + +import org.onap.policy.api.PushPolicyParameters; +import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyAdapter; +import org.onap.policy.distribution.model.Policy; + +/** + * Base class for {@link XacmlPdpPolicyAdapter} implementations. + * + * @param <T> the type of policy the adapter handles + */ +public abstract class AbstractXacmlPdpPolicyAdapter<T extends Policy> implements XacmlPdpPolicyAdapter<T> { + + private T policy; + + protected AbstractXacmlPdpPolicyAdapter(T policy) { + this.policy = policy; + } + + @Override + public T getPolicy() { + return policy; + } + + @Override + public PushPolicyParameters getAsPushPolicyParameters(String pdpGroups) { + PushPolicyParameters pushPolicyParameters = new PushPolicyParameters(); + pushPolicyParameters.setPolicyName(policy.getPolicyName()); + pushPolicyParameters.setPolicyType(policy.getPolicyType()); + pushPolicyParameters.setPdpGroup(pdpGroups); + return pushPolicyParameters; + } + +} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java new file mode 100644 index 00000000..1ec654fa --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.adapters; + +import org.onap.policy.api.PolicyConfigType; +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.distribution.model.OptimizationPolicy; + +/** + * Adapts {@link OptimizationPolicy} objects to objects compatible with the XACML PDP API. + */ +public class XacmlPdpOptimizationPolicyAdapter extends AbstractXacmlPdpPolicyAdapter<OptimizationPolicy> { + + /** + * Create an instance to adapt the given {@link OptimizationPolicy}. + * + * @param optimizationPolicy the {@link OptimizationPolicy} to be adapted + */ + public XacmlPdpOptimizationPolicyAdapter(final OptimizationPolicy optimizationPolicy) { + super(optimizationPolicy); + } + + @Override + public PolicyParameters getAsPolicyParameters() { + PolicyParameters policyParameters = new PolicyParameters(); + policyParameters.setPolicyName(getPolicy().getPolicyName()); + policyParameters.setPolicyDescription(getPolicy().getPolicyDescription()); + policyParameters.setPolicyConfigType(PolicyConfigType.valueOf(getPolicy().getPolicyConfigType())); + policyParameters.setOnapName(getPolicy().getOnapName()); + policyParameters.setRiskLevel(getPolicy().getRiskLevel()); + policyParameters.setConfigBody(getPolicy().getConfigBody()); + policyParameters.setRiskType(getPolicy().getRiskType()); + return policyParameters; + } + +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java new file mode 100644 index 00000000..1241b7f3 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.engine; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup; +import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup.XacmlPdpPolicyForwarderParameterGroupBuilder; + +public class XacmlPdpPolicyForwarderParameterGroupTest { + + @Test + public void testBuilderAndGetters() { + XacmlPdpPolicyForwarderParameterGroupBuilder builder = + new XacmlPdpPolicyForwarderParameterGroupBuilder(); + XacmlPdpPolicyForwarderParameterGroup configurationParameters = + builder.setUseHttps(true).setHostname("10.10.10.10").setPort(1234).setUserName("myUser") + .setPassword("myPassword").setClientAuth("myClientAuth").setIsManaged(false).build(); + + assertTrue(configurationParameters.isUseHttps()); + assertEquals("10.10.10.10", configurationParameters.getHostname()); + assertEquals(1234, configurationParameters.getPort()); + assertEquals("myUser", configurationParameters.getUserName()); + assertEquals("myPassword", configurationParameters.getPassword()); + assertEquals("myClientAuth", configurationParameters.getClientAuth()); + assertFalse(configurationParameters.isManaged()); + } + + @Test + public void testInvalidHostName() { + XacmlPdpPolicyForwarderParameterGroupBuilder builder = + new XacmlPdpPolicyForwarderParameterGroupBuilder(); + XacmlPdpPolicyForwarderParameterGroup configurationParameters = builder.setUseHttps(true).setHostname("") + .setPort(1234).setUserName("myUser").setPassword("myPassword").setIsManaged(false).build(); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPort() { + XacmlPdpPolicyForwarderParameterGroupBuilder builder = + new XacmlPdpPolicyForwarderParameterGroupBuilder(); + XacmlPdpPolicyForwarderParameterGroup configurationParameters = + builder.setUseHttps(true).setHostname("10.10.10.10").setPort(-1234).setUserName("myUser") + .setPassword("myPassword").setIsManaged(false).build(); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidUserName() { + XacmlPdpPolicyForwarderParameterGroupBuilder builder = + new XacmlPdpPolicyForwarderParameterGroupBuilder(); + XacmlPdpPolicyForwarderParameterGroup configurationParameters = + builder.setUseHttps(true).setHostname("10.10.10.10").setPort(1234).setUserName("") + .setPassword("myPassword").setIsManaged(false).build(); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPassword() { + XacmlPdpPolicyForwarderParameterGroupBuilder builder = + new XacmlPdpPolicyForwarderParameterGroupBuilder(); + XacmlPdpPolicyForwarderParameterGroup configurationParameters = + builder.setUseHttps(true).setHostname("10.10.10.10").setPort(1234).setUserName("myUser").setPassword("") + .setIsManaged(false).build(); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java new file mode 100644 index 00000000..d851b640 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java @@ -0,0 +1,338 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.engine; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.api.PushPolicyParameters; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactory; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; +import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup.XacmlPdpPolicyForwarderParameterGroupBuilder; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.model.OptimizationPolicy; +import org.onap.policy.distribution.model.Policy; + +public class XacmlPdpPolicyForwarderTest { + + private static final BusTopicParams BUS_TOPIC_PARAMS = BusTopicParams.builder().useHttps(false).hostname("myHost") + .port(1234).userName("myUser").password("myPassword").managed(true).build(); + private static final String CLIENT_AUTH = "ClientAuth"; + private static final String CLIENT_AUTH_VALUE = "myClientAuth"; + private static final String PDP_GROUP_VALUE = "myPdpGroup"; + private HashMap<String, Object> headers = new HashMap<>(); + private BusTopicParamsMatcher matcher = new BusTopicParamsMatcher(BUS_TOPIC_PARAMS); + + /** + * Set up. + */ + @BeforeClass + public static void setUp() { + ParameterGroup parameterGroup = new XacmlPdpPolicyForwarderParameterGroupBuilder() + .setUseHttps(BUS_TOPIC_PARAMS.isUseHttps()).setHostname(BUS_TOPIC_PARAMS.getHostname()) + .setPort(BUS_TOPIC_PARAMS.getPort()).setUserName(BUS_TOPIC_PARAMS.getUserName()) + .setPassword(BUS_TOPIC_PARAMS.getPassword()).setClientAuth(CLIENT_AUTH_VALUE) + .setIsManaged(BUS_TOPIC_PARAMS.isManaged()).setPdpGroup(PDP_GROUP_VALUE).build(); + parameterGroup.setName("xacmlPdpConfiguration"); + ParameterService.register(parameterGroup); + } + + @Test + public void testForwardPolicy() throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException, PolicyDistributionException { + + HttpClient httpClientMock = mock(HttpClient.class); + headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); + when(httpClientMock.put(eq("createPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + + HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); + when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); + + overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + + XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + forwarder.configure("xacmlPdpConfiguration"); + + Collection<Policy> policies = new ArrayList<>(); + + OptimizationPolicy policy1 = new OptimizationPolicy(); + policy1.setPolicyName("policy1"); + policy1.setPolicyConfigType("Optimization"); + policies.add(policy1); + + Policy policy2 = new UnsupportedPolicy(); + policies.add(policy2); + + OptimizationPolicy policy3 = new OptimizationPolicy(); + policy3.setPolicyName("policy3"); + policy3.setPolicyConfigType("Optimization"); + policies.add(policy3); + + forwarder.forward(policies); + + verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy1)), + eq(headers)); + verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy3)), + eq(headers)); + verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy1)), + eq(headers)); + verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy3)), + eq(headers)); + } + + @Test + public void testForwardPolicy_CreateFailsPushNotInvoked() + throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, PolicyDistributionException { + + HttpClient httpClientMock = mock(HttpClient.class); + headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); + when(httpClientMock.put(eq("createPolicy"), anyObject(), eq(headers))).thenReturn(Response.status(400).build()); + when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + + HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); + when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); + + overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + + XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + forwarder.configure("xacmlPdpConfiguration"); + + Collection<Policy> policies = new ArrayList<>(); + OptimizationPolicy policy = new OptimizationPolicy(); + policy.setPolicyName("policy"); + policy.setPolicyConfigType("Optimization"); + policies.add(policy); + forwarder.forward(policies); + + verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); + verify(httpClientMock, times(0)).put(eq("pushPolicy"), anyObject(), anyObject()); + } + + @Test + public void testForwardPolicy_PushFails() + throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, PolicyDistributionException { + + HttpClient httpClientMock = mock(HttpClient.class); + headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); + when(httpClientMock.put(eq("createPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.status(400).build()); + + HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); + when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); + + overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + + XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + forwarder.configure("xacmlPdpConfiguration"); + + Collection<Policy> policies = new ArrayList<>(); + OptimizationPolicy policy = new OptimizationPolicy(); + policy.setPolicyName("policy"); + policy.setPolicyConfigType("Optimization"); + policies.add(policy); + forwarder.forward(policies); + + verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); + verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy)), + eq(headers)); + } + + @Test + public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() + throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, PolicyDistributionException { + + HttpClient httpClientMock = mock(HttpClient.class); + headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); + when(httpClientMock.put(eq("createPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.status(400).build()); + + HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); + when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new KeyManagementException()); + + overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + + XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + forwarder.configure("xacmlPdpConfiguration"); + + Collection<Policy> policies = new ArrayList<>(); + OptimizationPolicy policy = new OptimizationPolicy(); + policy.setPolicyName("policy"); + policy.setPolicyConfigType("Optimization"); + policies.add(policy); + forwarder.forward(policies); + + verify(httpClientMock, times(0)).put(eq("createPolicy"), anyObject(), anyObject()); + verify(httpClientMock, times(0)).put(eq("pushPolicy"), anyObject(), anyObject()); + } + + @Test + public void testForwardPolicy_HttpClientInitFailureForPolicyPush() + throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException, PolicyDistributionException { + + HttpClient httpClientMock = mock(HttpClient.class); + headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); + when(httpClientMock.put(eq("createPolicy"), anyObject(), eq(headers))).thenReturn(Response.ok().build()); + when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.status(400).build()); + + HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); + when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock) + .thenThrow(new KeyManagementException()); + + overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + + XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + forwarder.configure("xacmlPdpConfiguration"); + + Collection<Policy> policies = new ArrayList<>(); + OptimizationPolicy policy = new OptimizationPolicy(); + policy.setPolicyName("policy"); + policy.setPolicyConfigType("Optimization"); + policies.add(policy); + forwarder.forward(policies); + + verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); + verify(httpClientMock, times(0)).put(eq("pushPolicy"), anyObject(), anyObject()); + } + + private void overwriteField(final Class<?> clazz, final String fieldName, final Object object, final Object value) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + Field field = clazz.getField(fieldName); + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(object, value); + } + + class BusTopicParamsMatcher extends BaseMatcher<BusTopicParams> { + + private BusTopicParams busTopicParams; + + BusTopicParamsMatcher(final BusTopicParams busTopicParams) { + this.busTopicParams = busTopicParams; + } + + @Override + public boolean matches(Object arg0) { + if (arg0 instanceof BusTopicParams) { + BusTopicParams toCompareTo = (BusTopicParams) arg0; + return toCompareTo.isUseHttps() == busTopicParams.isUseHttps() + && toCompareTo.getHostname().equals(busTopicParams.getHostname()) + && toCompareTo.getPort() == busTopicParams.getPort() + && toCompareTo.getUserName().equals(busTopicParams.getUserName()) + && toCompareTo.getPassword().equals(busTopicParams.getPassword()) + && toCompareTo.isManaged() == busTopicParams.isManaged(); + } + return false; + } + + @Override + public void describeTo(Description arg0) {} + } + + class PolicyParametersEntityMatcher extends BaseMatcher<Entity<PolicyParameters>> { + + private OptimizationPolicy policy; + + PolicyParametersEntityMatcher(final OptimizationPolicy policy) { + this.policy = policy; + } + + @SuppressWarnings("unchecked") + @Override + public boolean matches(Object arg0) { + if (arg0 instanceof Entity) { + PolicyParameters toCompareTo = ((Entity<PolicyParameters>) arg0).getEntity(); + return toCompareTo.getPolicyName().equals(policy.getPolicyName()) + && toCompareTo.getPolicyConfigType().toString().equals(policy.getPolicyConfigType()); + } + return false; + } + + @Override + public void describeTo(Description arg0) {} + } + + class PushPolicyParametersEntityMatcher extends BaseMatcher<Entity<PushPolicyParameters>> { + + private Policy policy; + + PushPolicyParametersEntityMatcher(final Policy policy) { + this.policy = policy; + } + + @SuppressWarnings("unchecked") + @Override + public boolean matches(Object arg0) { + if (arg0 instanceof Entity) { + PushPolicyParameters toCompareTo = ((Entity<PushPolicyParameters>) arg0).getEntity(); + return toCompareTo.getPolicyName().equals(policy.getPolicyName()) + && toCompareTo.getPolicyType().equals(policy.getPolicyType()) + && toCompareTo.getPdpGroup().equals(PDP_GROUP_VALUE); + } + return false; + } + + @Override + public void describeTo(Description arg0) {} + } + + class UnsupportedPolicy implements Policy { + + @Override + public String getPolicyName() { + return "unsupported"; + } + + @Override + public String getPolicyType() { + return "unsupported"; + } + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java new file mode 100644 index 00000000..e09357cf --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.testclasses; + +import java.util.Collection; + +import org.onap.policy.distribution.model.Policy; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * Class to create a dummy decoder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { + + private boolean canHandleValue; + private Collection<Policy> policesToReturn; + + public DummyDecoder() { + this.canHandleValue = false; + this.policesToReturn = null; + } + + public DummyDecoder(final boolean canHandleValue, final Collection<Policy> policesToReturn) { + this.canHandleValue = canHandleValue; + this.policesToReturn = policesToReturn; + } + + @Override + public boolean canHandle(final PolicyInput policyInput) { + return canHandleValue; + } + + @Override + public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { + return policesToReturn; + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java new file mode 100644 index 00000000..c0934812 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.xacml.pdp.testclasses; + +import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; + +/** + * Class to create a dummy reception handler for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyReceptionHandler extends AbstractReceptionHandler { + + @Override + public void initializeReception(final String parameterGroupName) {} + + @Override + public void destroy() {} +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java index 6e0eb3bd..135d2564 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PdpxPolicy.java @@ -28,4 +28,14 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; */ public class PdpxPolicy implements Policy { + @Override + public String getPolicyName() { + return null; + } + + @Override + public String getPolicyType() { + return null; + } + } diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java b/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java index eb1a33a5..4f3f41e3 100644 --- a/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java +++ b/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java @@ -23,7 +23,6 @@ package org.onap.policy.distribution.reception.handling; import java.util.ArrayList; import java.util.Collection; import java.util.Map; - import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; @@ -114,7 +113,9 @@ public class PluginHandler { try { final Class<PolicyForwarder> policyForwarderClass = (Class<PolicyForwarder>) Class.forName(forwarderParameters.getForwarderClassName()); - policyForwarders.add(policyForwarderClass.newInstance()); + PolicyForwarder policyForwarder = policyForwarderClass.newInstance(); + policyForwarder.configure(forwarderParameters.getForwarderConfigurationName()); + policyForwarders.add(policyForwarder); } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { LOGGER.error("exception occured while initializing forwarders", exp); throw new PluginInitializationException(exp.getMessage(), exp.getCause()); diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java index 83fe5a45..d339507c 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java @@ -29,7 +29,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.junit.Test; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; @@ -57,6 +56,7 @@ public class AbstractReceptionHandlerTest { private static final String FORWARDER_TYPE = "DummyForwarder"; private static final String FORWARDER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder"; + private static final String FORWARDER_CONFIGURATION_PARAMETERS = "DummyConfiguration"; @Test public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException, @@ -121,9 +121,29 @@ public class AbstractReceptionHandlerTest { } class DummyPolicy1 implements Policy { + + @Override + public String getPolicyName() { + return null; + } + + @Override + public String getPolicyType() { + return null; + } } class DummyPolicy2 implements Policy { + + @Override + public String getPolicyName() { + return null; + } + + @Override + public String getPolicyType() { + return null; + } } private void setUpPlugins(final AbstractReceptionHandler receptionHandler, @@ -160,7 +180,7 @@ public class AbstractReceptionHandlerTest { final Map<String, PolicyForwarderParameters> policyForwarders = new HashMap<String, PolicyForwarderParameters>(); final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME); + new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS); policyForwarders.put(FORWARDER_KEY, pFParameters); return policyForwarders; } diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java index acee4697..a665c546 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java @@ -22,7 +22,6 @@ package org.onap.policy.distribution.reception.handling; import java.util.ArrayList; import java.util.Collection; - import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; import org.onap.policy.distribution.model.Policy; @@ -49,4 +48,7 @@ public class DummyPolicyForwarder implements PolicyForwarder { public boolean receivedPolicy(final Policy policy) { return policiesReceived.contains(policy); } + + @Override + public void configure(String parameterGroupName) {} } |