diff options
63 files changed, 1350 insertions, 428 deletions
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 1f3e48e6..4bcd1a32 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 @@ -23,10 +23,12 @@ 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; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; @@ -43,6 +45,9 @@ public class DistributionParameterGroup implements ParameterGroup { new LinkedHashMap<>(); private Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters = new LinkedHashMap<>(); + private Map<String, PolicyDecoderConfigurationParameterGroup> policyDecoderConfigurationParameters = + new LinkedHashMap<>(); + /** @@ -53,12 +58,14 @@ public class DistributionParameterGroup implements ParameterGroup { public DistributionParameterGroup(final String name, final RestServerParameters restServerParameters, final Map<String, ReceptionHandlerParameters> receptionHandlerParameters, final Map<String, ReceptionHandlerConfigurationParameterGroup> receptionHandlerConfigurationParameters, - final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters) { + final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters, + final Map<String, PolicyDecoderConfigurationParameterGroup> policyDecoderConfigurationParameters) { this.name = name; this.restServerParameters = restServerParameters; this.receptionHandlerParameters = receptionHandlerParameters; this.receptionHandlerConfigurationParameters = receptionHandlerConfigurationParameters; this.policyForwarderConfigurationParameters = policyForwarderConfigurationParameters; + this.policyDecoderConfigurationParameters = policyDecoderConfigurationParameters; } /** @@ -130,7 +137,7 @@ public class DistributionParameterGroup implements ParameterGroup { /** * Sets the policy forwarder configuration parameter map. * - * @param eventInputParameters the policy forwarder configuration parameters + * @param policyForwarderConfigurationParameters the policy forwarder configuration parameters */ public void setPolicyForwarderConfigurationParameters( final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters) { @@ -138,6 +145,25 @@ public class DistributionParameterGroup implements ParameterGroup { } /** + * Returns the policy decoder configuration parameter map. + * + * @return the policyDecoderConfigurationParameters + */ + public Map<String, PolicyDecoderConfigurationParameterGroup> getPolicyDecoderConfigurationParameters() { + return policyDecoderConfigurationParameters; + } + + /** + * Set the policy decoder configuration parameter map. + * + * @param policyDecoderConfigurationParameters the policyDecoderConfigurationParameters to set + */ + public void setPolicyDecoderConfigurationParameters( + final Map<String, PolicyDecoderConfigurationParameterGroup> policyDecoderConfigurationParameters) { + this.policyDecoderConfigurationParameters = policyDecoderConfigurationParameters; + } + + /** * Validate the parameter group. * * @return the result of the validation @@ -156,13 +182,10 @@ public class DistributionParameterGroup implements ParameterGroup { } validateReceptionHandlers(validationResult); validateForwarderConfigurations(validationResult); + validateDecoderConfigurations(validationResult); return validationResult; } - /** - * Validate the reception handlers. - * - */ private void validateReceptionHandlers(final GroupValidationResult validationResult) { if (receptionHandlerParameters == null || receptionHandlerParameters.size() == 0) { validationResult.setResult("receptionHandlerParameters", ValidationStatus.INVALID, @@ -183,4 +206,12 @@ public class DistributionParameterGroup implements ParameterGroup { configurationParameters.getValue().validate()); } } + + private void validateDecoderConfigurations(final GroupValidationResult validationResult) { + for (final Entry<String, PolicyDecoderConfigurationParameterGroup> configurationParameters : policyDecoderConfigurationParameters + .entrySet()) { + validationResult.setResult("policyDecoderConfigurationParameters", 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 c1c64f7e..603c9869 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,18 +22,21 @@ 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; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParametersJsonAdapter; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParametersJsonAdapter; /** - * 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); @@ -57,6 +60,8 @@ public class DistributionParameterHandler { new PolicyForwarderConfigurationParametersJsonAdapter()) .registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class, new ReceptionHandlerConfigurationParametersJsonAdapter()) + .registerTypeAdapter(PolicyDecoderConfigurationParameterGroup.class, + new PolicyDecoderConfigurationParametersJsonAdapter()) .create(); distributionParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), DistributionParameterGroup.class); 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 91052580..1395b283 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 @@ -23,6 +23,7 @@ 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; @@ -32,12 +33,13 @@ import org.onap.policy.distribution.main.parameters.PolicyForwarderConfiguration 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.PolicyDecoderConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; 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,16 +150,23 @@ public class DistributionActivator { ParameterService.register(params); ParameterService.register(params.getPluginHandlerParameters()); } - for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration : distributionParameterGroup - .getPolicyForwarderConfigurationParameters().entrySet()) { + //@formatter:off + for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration + : distributionParameterGroup.getPolicyForwarderConfigurationParameters().entrySet()) { forwarderConfiguration.getValue().setName(forwarderConfiguration.getKey()); ParameterService.register(forwarderConfiguration.getValue()); } - for (final Entry<String, ReceptionHandlerConfigurationParameterGroup> receptionHandlerConfiguration : distributionParameterGroup - .getReceptionHandlerConfigurationParameters().entrySet()) { + for (final Entry<String, ReceptionHandlerConfigurationParameterGroup> receptionHandlerConfiguration + : distributionParameterGroup.getReceptionHandlerConfigurationParameters().entrySet()) { receptionHandlerConfiguration.getValue().setName(receptionHandlerConfiguration.getKey()); ParameterService.register(receptionHandlerConfiguration.getValue()); } + for (final Entry<String, PolicyDecoderConfigurationParameterGroup> decoderConfiguration + : distributionParameterGroup.getPolicyDecoderConfigurationParameters().entrySet()) { + decoderConfiguration.getValue().setName(decoderConfiguration.getKey()); + ParameterService.register(decoderConfiguration.getValue()); + } + //@formatter:on } /** @@ -172,16 +181,23 @@ public class DistributionActivator { ParameterService.deregister((params.getName())); ParameterService.deregister((params.getPluginHandlerParameters().getName())); } - for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration : distributionParameterGroup - .getPolicyForwarderConfigurationParameters().entrySet()) { + //@formatter:off + for (final Entry<String, PolicyForwarderConfigurationParameterGroup> forwarderConfiguration + : distributionParameterGroup.getPolicyForwarderConfigurationParameters().entrySet()) { forwarderConfiguration.getValue().setName(forwarderConfiguration.getKey()); ParameterService.deregister(forwarderConfiguration.getKey()); } - for (final Entry<String, ReceptionHandlerConfigurationParameterGroup> receptionHandlerConfiguration : distributionParameterGroup - .getReceptionHandlerConfigurationParameters().entrySet()) { + for (final Entry<String, ReceptionHandlerConfigurationParameterGroup> receptionHandlerConfiguration + : distributionParameterGroup.getReceptionHandlerConfigurationParameters().entrySet()) { receptionHandlerConfiguration.getValue().setName(receptionHandlerConfiguration.getKey()); ParameterService.deregister(receptionHandlerConfiguration.getKey()); } + for (final Entry<String, PolicyDecoderConfigurationParameterGroup> decoderConfiguration + : distributionParameterGroup.getPolicyDecoderConfigurationParameters().entrySet()) { + decoderConfiguration.getValue().setName(decoderConfiguration.getKey()); + ParameterService.deregister(decoderConfiguration.getKey()); + } + //@formatter:on } /** 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 b1903152..96ec9f93 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,12 +24,15 @@ 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.DummyPolicyDecoderParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup.DummyPolicyForwarderParameterGroupBuilder; import org.onap.policy.distribution.main.testclasses.DummyReceptionHandlerParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyReceptionHandlerParameterGroup.DummyReceptionHandlerParameterGroupBuilder; import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; @@ -65,6 +68,10 @@ public class CommonTestData { public static final String DUMMY_ENGINE_FORWARDER_KEY = "DummyForwarder"; public static final String DUMMY_DECODER_KEY = "DummyDecoder"; + public static final String POLICY_TYPE = "DUMMY"; + public static final String POLICY_NAME = "SamplePolicy"; + public static final String DECODER_CONFIGURATION_PARAMETERS = "dummyDecoderConfiguration"; + /** * Returns an instance of ReceptionHandlerParameters for test cases. * @@ -117,7 +124,7 @@ public class CommonTestData { messageBusAddress.add("localhost"); final List<String> artifactTypes = new ArrayList<>(); artifactTypes.add("TOSCA_CSAR"); - DummyReceptionHandlerParameterGroup dummyReceptionHandlerParameterGroup = + final DummyReceptionHandlerParameterGroup dummyReceptionHandlerParameterGroup = new DummyReceptionHandlerParameterGroupBuilder().setMyStringParameter(MY_STRING_PARAMETER_VALUE) .setMyIntegerParameter(MY_INTEGER_PARAMETER_VALUE) .setMyBooleanParameter(MY_BOOLEAN_PARAMETER_VALUE).build(); @@ -167,7 +174,8 @@ public class CommonTestData { public Map<String, PolicyDecoderParameters> getPolicyDecoders(final boolean isEmpty) { final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>(); if (!isEmpty) { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS); policyDecoders.put(DUMMY_DECODER_KEY, pDParameters); } return policyDecoders; @@ -184,7 +192,7 @@ public class CommonTestData { final Map<String, PolicyForwarderConfigurationParameterGroup> policyForwarderConfigurationParameters = new HashMap<String, PolicyForwarderConfigurationParameterGroup>(); if (!isEmpty) { - DummyPolicyForwarderParameterGroup dummyPolicyForwarderParameterGroup = + final DummyPolicyForwarderParameterGroup dummyPolicyForwarderParameterGroup = new DummyPolicyForwarderParameterGroupBuilder().setUseHttps(true).setHostname(FORWARDER_HOST) .setPort(1234).setUserName("myUser").setPassword("myPassword").setIsManaged(true).build(); policyForwarderConfigurationParameters.put(FORWARDER_CONFIGURATION_PARAMETERS, @@ -192,4 +200,23 @@ public class CommonTestData { } return policyForwarderConfigurationParameters; } + + /** + * Returns PolicyDecoderConfigurationParameterGroups for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return the PolicyDecoderConfigurationParameterGroups + */ + public Map<String, PolicyDecoderConfigurationParameterGroup> getPolicyDecoderConfigurationParameters( + final boolean isEmpty) { + final Map<String, PolicyDecoderConfigurationParameterGroup> policyDecoderConfigurationParameters = + new HashMap<String, PolicyDecoderConfigurationParameterGroup>(); + if (!isEmpty) { + final DummyPolicyDecoderParameterGroup dummyPolicyForwarderParameterGroup = + new DummyPolicyDecoderParameterGroup(POLICY_NAME, POLICY_TYPE); + policyDecoderConfigurationParameters.put(DECODER_CONFIGURATION_PARAMETERS, + dummyPolicyForwarderParameterGroup); + } + return policyDecoderConfigurationParameters; + } } 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 6f6ddd81..8a33ac2c 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,10 +26,12 @@ 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.main.testclasses.DummyReceptionHandlerParameterGroup; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup; import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; @@ -50,10 +52,12 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, - receptionHandlerParameters, receptionHandlerConfigurations, forwarderConfigurations); + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters, + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(restServerParameters.getHost(), distributionParameters.getRestServerParameters().getHost()); @@ -102,10 +106,12 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); final DistributionParameterGroup distributionParameters = new DistributionParameterGroup(null, restServerParameters, receptionHandlerParameters, - receptionHandlerConfigurations, forwarderConfigurations); + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, distributionParameters.getName()); @@ -135,10 +141,12 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); final DistributionParameterGroup distributionParameters = new DistributionParameterGroup("", restServerParameters, receptionHandlerParameters, - receptionHandlerConfigurations, forwarderConfigurations); + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", distributionParameters.getName()); @@ -166,10 +174,12 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); try { final DistributionParameterGroup distributionParameters = new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null, - receptionHandlerConfigurations, forwarderConfigurations); + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -187,11 +197,13 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, - receptionHandlerParameters, receptionHandlerConfigurations, forwarderConfigurations); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters, + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); distributionParameters.validate(); - GroupValidationResult result = distributionParameters.validate(); + final GroupValidationResult result = distributionParameters.validate(); assertFalse(result.isValid()); assertTrue(result.getResult().endsWith("must have at least one reception handler\n")); } @@ -205,10 +217,12 @@ public class TestDistributionParameterGroup { commonTestData.getReceptionHandlerConfigurationParameters(false); final Map<String, PolicyForwarderConfigurationParameterGroup> forwarderConfigurations = commonTestData.getPolicyForwarderConfigurationParameters(false); + final Map<String, PolicyDecoderConfigurationParameterGroup> decoderConfigurations = + commonTestData.getPolicyDecoderConfigurationParameters(false); - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, - receptionHandlerParameters, receptionHandlerConfigurations, forwarderConfigurations); + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters, + receptionHandlerConfigurations, forwarderConfigurations, decoderConfigurations); 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 a5eab8b1..b66da8e6 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 @@ -31,6 +31,7 @@ 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.DummyPolicyDecoderParameterGroup; import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParameterGroup; /** @@ -42,7 +43,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerNoParameterFile() throws PolicyDistributionException { final String[] noArgumentString = - { "-c", "parameters/NoParameterFile.json" }; + { "-c", "parameters/NoParameterFile.json" }; final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); @@ -58,7 +59,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerEmptyParameters() throws PolicyDistributionException { final String[] emptyArgumentString = - { "-c", "parameters/EmptyParameters.json" }; + { "-c", "parameters/EmptyParameters.json" }; final DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments(); emptyArguments.parse(emptyArgumentString); @@ -74,7 +75,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerBadParameters() throws PolicyDistributionException { final String[] badArgumentString = - { "-c", "parameters/BadParameters.json" }; + { "-c", "parameters/BadParameters.json" }; final DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments(); badArguments.parse(badArgumentString); @@ -92,7 +93,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerInvalidParameters() throws PolicyDistributionException { final String[] invalidArgumentString = - { "-c", "parameters/InvalidParameters.json" }; + { "-c", "parameters/InvalidParameters.json" }; final DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments(); invalidArguments.parse(invalidArgumentString); @@ -110,7 +111,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerNoParameters() throws PolicyDistributionException { final String[] noArgumentString = - { "-c", "parameters/NoParameters.json" }; + { "-c", "parameters/NoParameters.json" }; final DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); noArguments.parse(noArgumentString); @@ -126,7 +127,7 @@ public class TestDistributionParameterHandler { @Test public void testParameterHandlerMinumumParameters() throws PolicyDistributionException { final String[] minArgumentString = - { "-c", "parameters/MinimumParameters.json" }; + { "-c", "parameters/MinimumParameters.json" }; final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments(); minArguments.parse(minArgumentString); @@ -138,7 +139,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + { "-c", "parameters/DistributionConfigParameters.json" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -159,13 +160,19 @@ public class TestDistributionParameterHandler { assertEquals(CommonTestData.FORWARDER_HOST, ((DummyPolicyForwarderParameterGroup) parGroup.getPolicyForwarderConfigurationParameters() .get(CommonTestData.FORWARDER_CONFIGURATION_PARAMETERS)).getHostname()); + assertEquals(CommonTestData.POLICY_TYPE, + ((DummyPolicyDecoderParameterGroup) parGroup.getPolicyDecoderConfigurationParameters() + .get(CommonTestData.DECODER_CONFIGURATION_PARAMETERS)).getPolicyType()); + assertEquals(CommonTestData.POLICY_NAME, + ((DummyPolicyDecoderParameterGroup) parGroup.getPolicyDecoderConfigurationParameters() + .get(CommonTestData.DECODER_CONFIGURATION_PARAMETERS)).getPolicyName()); } @Test public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json" }; + { "-c", "parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -182,7 +189,7 @@ public class TestDistributionParameterHandler { public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json" }; + { "-c", "parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -199,7 +206,7 @@ public class TestDistributionParameterHandler { @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); @@ -216,7 +223,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); @@ -232,7 +239,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); @@ -248,7 +255,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); @@ -264,7 +271,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); @@ -280,7 +287,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); @@ -296,7 +303,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); @@ -313,7 +320,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); @@ -333,7 +340,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); @@ -353,7 +360,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); @@ -372,7 +379,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionVersion() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-v" }; + { "-v" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String version = arguments.parse(distributionConfigParameters); assertTrue(version.startsWith("ONAP Policy Framework Distribution Service")); @@ -381,7 +388,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionHelp() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-h" }; + { "-h" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String help = arguments.parse(distributionConfigParameters); assertTrue(help.startsWith("usage:")); @@ -390,7 +397,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionInvalidOption() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-d" }; + { "-d" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); try { arguments.parse(distributionConfigParameters); @@ -402,7 +409,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" }; + { "-c", "parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -417,7 +424,7 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_EmptyReceptionHandlerClass() throws PolicyDistributionException { final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json" }; + { "-c", "parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json" }; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -428,4 +435,39 @@ public class TestDistributionParameterHandler { assertTrue(e.getMessage().contains("invalid in JSON file")); } } + + @Test + public void testDistributionParameterGroup_InvalidDecoderConfigurationClassName() + throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidDecoderConfigurationClassName.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_UnknownDecoderConfigurationClassName() + throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_UnknownDecoderConfigurationClassName.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")); + } + } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java index c2ab38d8..7b9e6095 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.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.DECODER_CLASS_NAME; +import static org.onap.policy.distribution.main.parameters.CommonTestData.DECODER_CONFIGURATION_PARAMETERS; +import static org.onap.policy.distribution.main.parameters.CommonTestData.DECODER_TYPE; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; @@ -38,19 +41,21 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters() { final PolicyDecoderParameters pDParameters = - new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, CommonTestData.DECODER_CLASS_NAME); + new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); - assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); + assertEquals(DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); + assertEquals(DECODER_CONFIGURATION_PARAMETERS, pDParameters.getDecoderConfigurationName()); assertTrue(validationResult.isValid()); } @Test public void testPolicyDecoderParameters_InvalidDecoderType() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("", CommonTestData.DECODER_CLASS_NAME); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters("", DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); assertEquals("", pDParameters.getDecoderType()); - assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); + assertEquals(DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"decoderType\" type \"java.lang.String\" value \"\" INVALID, must be a non-blank string")); @@ -58,9 +63,10 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_InvalidDecoderClassName() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, ""); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters(DECODER_TYPE, "", DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(DECODER_TYPE, pDParameters.getDecoderType()); assertEquals("", pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -70,7 +76,8 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_InvalidDecoderTypeAndClassName() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("", ""); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters("", "", DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); assertEquals("", pDParameters.getDecoderType()); assertEquals("", pDParameters.getDecoderClassName()); @@ -84,11 +91,11 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_NullDecoderType() { - final PolicyDecoderParameters pDParameters = - new PolicyDecoderParameters(null, CommonTestData.DECODER_CLASS_NAME); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters(null, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); assertEquals(null, pDParameters.getDecoderType()); - assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); + assertEquals(DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"decoderType\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string")); @@ -96,9 +103,10 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_NullDecoderClassName() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, null); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters(DECODER_TYPE, null, DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(DECODER_TYPE, pDParameters.getDecoderType()); assertEquals(null, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -108,11 +116,11 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_InvalidDecoderClass() { - final PolicyDecoderParameters pDParameters = - new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, CommonTestData.DECODER_CLASS_NAME + "Invalid"); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, + DECODER_CLASS_NAME + "Invalid", DECODER_CONFIGURATION_PARAMETERS); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); - assertEquals(CommonTestData.DECODER_CLASS_NAME + "Invalid", pDParameters.getDecoderClassName()); + assertEquals(DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(DECODER_CLASS_NAME + "Invalid", pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains("policy decoder class not found in classpath")); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyDecoder.java b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyDecoder.java index 19e30933..d210d970 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyDecoder.java +++ b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyDecoder.java @@ -56,4 +56,7 @@ public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { return policesToReturn; } + + @Override + public void configure(final String parameterGroupName) {} } diff --git a/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyDecoderParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyDecoderParameterGroup.java new file mode 100644 index 00000000..5d1738c5 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/testclasses/DummyPolicyDecoderParameterGroup.java @@ -0,0 +1,64 @@ +/*- + * ============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.reception.parameters.PolicyDecoderConfigurationParameterGroup; + +/** + * Dummy policy decoder parameter group. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicyDecoderParameterGroup extends PolicyDecoderConfigurationParameterGroup { + + private String policyName; + private String policyType; + + /** + * Constructor for instantiating the {@link DummyPolicyDecoderParameterGroup} class. + * + * @param policyName the policy name + * @param policyType the policy type + */ + public DummyPolicyDecoderParameterGroup(final String policyName, final String policyType) { + super(); + this.policyName = policyName; + this.policyType = policyType; + } + + + public String getPolicyName() { + return policyName; + } + + + public String getPolicyType() { + return policyType; + } + + @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 acf56764..71ca92e3 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json index e22af281..9e83ac19 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyDecoder.json @@ -46,5 +46,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json index 4d271308..2a7174e0 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyPolicyForwarder.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -45,5 +46,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json index c25b2e35..3560dbca 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandler.json @@ -30,5 +30,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } }
\ No newline at end of file diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json index ef129fd6..65ba903f 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_EmptyReceptionHandlerClass.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json index bd52e186..28f2195e 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json @@ -15,15 +15,18 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" }, "APEXDecoder":{ "decoderType":"", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.PolicyDecoderApexPdpa" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.PolicyDecoderApexPdpa", + "decoderConfigurationParameters": "dummyDecoderConfiguration" }, "DROOLSDecoder":{ "decoderType":"DROOLS", - "decoderClassName":"" + "decoderClassName":"", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -69,5 +72,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderConfigurationClassName.json new file mode 100644 index 00000000..cba76644 --- /dev/null +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidDecoderConfigurationClassName.json @@ -0,0 +1,64 @@ +{ + "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", + "receptionHandlerConfigurationName":"dummyReceptionHandlerConfiguration", + "pluginHandlerParameters":{ + "policyDecoders":{ + "DummyDecoder":{ + "decoderType":"DummyDecoder", + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" + } + }, + "policyForwarders":{ + "DummyForwarder":{ + "forwarderType":"DummyForwarder", + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationParameters": "dummyConfiguration" + } + } + } + } + }, + "receptionHandlerConfigurationParameters":{ + "dummyReceptionHandlerConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyReceptionHandlerParameterGroup", + "parameters":{ + "myStringParameter": "stringValue", + "myIntegerParameter":20, + "myBooleanParameter": true + } + } + }, + "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 + } + } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } + } +} diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json index 60c28e08..13265c8a 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidForwarderConfigurationClassName.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json index 46207204..9a0b309f 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidName.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json index d033a730..350ace9e 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerClass.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json index 123ff203..6776c370 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidReceptionHandlerParameters.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json index 47528ce0..b5fd9a7a 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_InvalidRestServerParameters.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json index f78bb65f..a007c30b 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyDecoder.json @@ -44,5 +44,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyForwarder.json b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyForwarder.json index 3ac54f73..72608b3b 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyForwarder.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_NoPolicyForwarder.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } } } @@ -30,5 +31,14 @@ "myBooleanParameter": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_UnknownDecoderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownDecoderConfigurationClassName.json new file mode 100644 index 00000000..2ce665f9 --- /dev/null +++ b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownDecoderConfigurationClassName.json @@ -0,0 +1,64 @@ +{ + "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", + "receptionHandlerConfigurationName":"dummyReceptionHandlerConfiguration", + "pluginHandlerParameters":{ + "policyDecoders":{ + "DummyDecoder":{ + "decoderType":"DummyDecoder", + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" + } + }, + "policyForwarders":{ + "DummyForwarder":{ + "forwarderType":"DummyForwarder", + "forwarderClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyForwarder", + "forwarderConfigurationParameters": "dummyConfiguration" + } + } + } + } + }, + "receptionHandlerConfigurationParameters":{ + "dummyReceptionHandlerConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyReceptionHandlerParameterGroup", + "parameters":{ + "myStringParameter": "stringValue", + "myIntegerParameter":20, + "myBooleanParameter": true + } + } + }, + "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 + } + } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.Unknown", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } + } +} diff --git a/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json index 3118631b..1883b221 100644 --- a/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json +++ b/main/src/test/resources/parameters/DistributionConfigParameters_UnknownForwarderConfigurationClassName.json @@ -10,35 +10,13 @@ "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 - }, + "receptionHandlerConfigurationName":"dummyReceptionHandlerConfiguration", "pluginHandlerParameters":{ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationParameters": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -73,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json index fc6b16aa..5401d924 100644 --- a/main/src/test/resources/parameters/MinimumParameters.json +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -15,7 +15,8 @@ "policyDecoders":{ "DummyDecoder":{ "decoderType":"DummyDecoder", - "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder" + "decoderClassName":"org.onap.policy.distribution.main.testclasses.DummyDecoder", + "decoderConfigurationName": "dummyDecoderConfiguration" } }, "policyForwarders":{ @@ -50,5 +51,14 @@ "isManaged": true } } + }, + "policyDecoderConfigurationParameters":{ + "dummyDecoderConfiguration":{ + "parameterClassName":"org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup", + "parameters":{ + "policyName": "SamplePolicy", + "policyType": "DUMMY" + } + } } } 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 index 797ea4b9..971077cc 100644 --- a/model/src/main/java/org/onap/policy/distribution/model/OptimizationPolicy.java +++ b/model/src/main/java/org/onap/policy/distribution/model/OptimizationPolicy.java @@ -5,24 +5,25 @@ * 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 + * An optimization policy. */ public class OptimizationPolicy implements Policy { @@ -48,7 +49,7 @@ public class OptimizationPolicy implements Policy { return OPTIMIZATION; } - public void setPolicyName(String policyName) { + public void setPolicyName(final String policyName) { this.policyName = policyName; } @@ -56,7 +57,7 @@ public class OptimizationPolicy implements Policy { return policyDescription; } - public void setPolicyDescription(String policyDescription) { + public void setPolicyDescription(final String policyDescription) { this.policyDescription = policyDescription; } @@ -64,7 +65,7 @@ public class OptimizationPolicy implements Policy { return policyConfigType; } - public void setPolicyConfigType(String policyConfigType) { + public void setPolicyConfigType(final String policyConfigType) { this.policyConfigType = policyConfigType; } @@ -72,7 +73,7 @@ public class OptimizationPolicy implements Policy { return onapName; } - public void setOnapName(String onapName) { + public void setOnapName(final String onapName) { this.onapName = onapName; } @@ -80,7 +81,7 @@ public class OptimizationPolicy implements Policy { return configBody; } - public void setConfigBody(String configBody) { + public void setConfigBody(final String configBody) { this.configBody = configBody; } @@ -88,7 +89,7 @@ public class OptimizationPolicy implements Policy { return configBodyType; } - public void setConfigBodyType(String configBodyType) { + public void setConfigBodyType(final String configBodyType) { this.configBodyType = configBodyType; } @@ -96,7 +97,7 @@ public class OptimizationPolicy implements Policy { return timetolive; } - public void setTimetolive(Date timetolive) { + public void setTimetolive(final Date timetolive) { this.timetolive = timetolive; } @@ -104,7 +105,7 @@ public class OptimizationPolicy implements Policy { return guard; } - public void setGuard(String guard) { + public void setGuard(final String guard) { this.guard = guard; } @@ -112,7 +113,7 @@ public class OptimizationPolicy implements Policy { return riskLevel; } - public void setRiskLevel(String riskLevel) { + public void setRiskLevel(final String riskLevel) { this.riskLevel = riskLevel; } @@ -120,7 +121,7 @@ public class OptimizationPolicy implements Policy { return riskType; } - public void setRiskType(String riskType) { + public void setRiskType(final String riskType) { this.riskType = riskType; } } diff --git a/model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java b/model/src/main/java/org/onap/policy/distribution/model/PolicyAsString.java index a8f45764..1600d8f7 100644 --- a/model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java +++ b/model/src/main/java/org/onap/policy/distribution/model/PolicyAsString.java @@ -20,35 +20,36 @@ package org.onap.policy.distribution.model; -import java.io.InputStream; - /** - * This class represents an apex-pdp policy which can be decoded by a relevant {@link PolicyDecoder}. + * This class represents a policy which can be decoded by a relevant {@link PolicyDecoder}. * * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ -public class ApexPdpPolicy implements Policy { +public class PolicyAsString implements Policy { private String policyName; - private InputStream policyInputStream; + private String policyType; + private String policy; /** - * Constructor for creating instance of {@link ApexPdpPolicy}. + * Constructor for creating instance of {@link PolicyAsString}. * * @param policyName the policy file name - * @param policyInputStream the input stream + * @param policyType the policy type + * @param policy the policy */ - public ApexPdpPolicy(final String policyName, final InputStream policyInputStream) { + public PolicyAsString(final String policyName, final String policyType, final String policy) { this.policyName = policyName; - this.policyInputStream = policyInputStream; + this.policyType = policyType; + this.policy = policy; } /** - * Returns the policyInputStream of this ApexPdpPolicy instance. + * Returns the policy of this {@link PolicyAsString} instance. * - * @return the policyInputStream + * @return the policy */ - public InputStream getPolicyInputStream() { - return policyInputStream; + public String getPolicy() { + return policy; } /** @@ -64,6 +65,6 @@ public class ApexPdpPolicy implements Policy { */ @Override public String getPolicyType() { - return "Method not supported"; + return policyType; } } diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java index 1a603f04..f71c6aee 100644 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java @@ -20,8 +20,11 @@ package org.onap.policy.distribution.forwarding.apex.pdp; +import java.io.IOException; +import java.io.InputStream; import java.util.Collection; +import org.apache.commons.io.IOUtils; import org.onap.policy.apex.core.deployment.EngineServiceFacade; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -30,8 +33,8 @@ import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; -import org.onap.policy.distribution.model.ApexPdpPolicy; import org.onap.policy.distribution.model.Policy; +import org.onap.policy.distribution.model.PolicyAsString; /** * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to @@ -42,6 +45,7 @@ import org.onap.policy.distribution.model.Policy; public class ApexPdpPolicyForwarder implements PolicyForwarder { private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class); + private static final String POLICY_TYPE = "APEX"; private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters; private EngineServiceFacade engineServiceFacade; @@ -69,8 +73,9 @@ public class ApexPdpPolicyForwarder implements PolicyForwarder { } else { final Policy policy = (Policy) policies.toArray()[0]; - if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) { - forwardPolicy((ApexPdpPolicy) policy); + if (policy.getClass().isAssignableFrom(PolicyAsString.class) + && policy.getPolicyType().equalsIgnoreCase(POLICY_TYPE)) { + forwardPolicy((PolicyAsString) policy); } else { final String message = "Ignoring the policy as it is not an apex-pdp policy"; LOGGER.debug(message); @@ -85,16 +90,17 @@ public class ApexPdpPolicyForwarder implements PolicyForwarder { * @param apexPolicy the apex policy * @throws PolicyForwardingException if any exception occurs while forwarding policy */ - private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException { + private void forwardPolicy(final PolicyAsString apexPolicy) throws PolicyForwardingException { try { engineServiceFacade.init(); - engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(), + final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.getPolicy(), "UTF-8"); + engineServiceFacade.deployModel(apexPolicy.getPolicyName(), policyInputStream, apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate()); LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at " + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort()); - } catch (final ApexException exp) { + } catch (final ApexException | IOException exp) { final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort(); LOGGER.error(message, exp); diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java index ca0efb5c..7feef452 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java @@ -27,6 +27,11 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.ValidationStatus; +/** + * Class to perform unit test of {@link ApexPdpPolicyForwarderParameterGroup}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ public class ApexPdpPolicyForwarderParameterGroupTest { @Test diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java index abf5b508..a4d1b9fd 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java @@ -22,11 +22,11 @@ package org.onap.policy.distribution.forwarding.apex.pdp; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Field; @@ -45,9 +45,14 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwardingException; -import org.onap.policy.distribution.model.ApexPdpPolicy; import org.onap.policy.distribution.model.Policy; +import org.onap.policy.distribution.model.PolicyAsString; +/** + * Class to perform unit test of {@link ApexPdpPolicyForwarder}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ @RunWith(MockitoJUnitRunner.class) public class ApexPdpPolicyForwarderTest { @@ -84,7 +89,6 @@ public class ApexPdpPolicyForwarderTest { public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null)); final Collection<Policy> policies = new ArrayList<>(); final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); forwarder.configure(GROUP_NAME); @@ -93,13 +97,14 @@ public class ApexPdpPolicyForwarderTest { forwarderField.setAccessible(true); forwarderField.set(forwarder, engineServiceFacade); - final ApexPdpPolicy policy = new ApexPdpPolicy("policy", fis); + final PolicyAsString policy = new PolicyAsString("policy", "APEX", "Sample Policy of apex"); policies.add(policy); try { forwarder.forward(policies); verify(engineServiceFacade, times(1)).init(); - verify(engineServiceFacade, times(1)).deployModel("policy", fis, IGNORE_CONFLICTS, FORCE_UPDATE); + verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS), + eq(FORCE_UPDATE)); } catch (final Exception exp) { fail("Test must not throw an exception"); } @@ -110,19 +115,18 @@ public class ApexPdpPolicyForwarderTest { throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null)); final Collection<Policy> policies = new ArrayList<>(); final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); forwarder.configure(GROUP_NAME); - Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel("policy1", fis, - IGNORE_CONFLICTS, FORCE_UPDATE); + Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(), + eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE)); final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade"); decodersField.setAccessible(true); decodersField.set(forwarder, engineServiceFacade); - final ApexPdpPolicy policy1 = new ApexPdpPolicy("policy1", fis); + final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex"); policies.add(policy1); try { @@ -147,12 +151,10 @@ public class ApexPdpPolicyForwarderTest { forwarderField.setAccessible(true); forwarderField.set(forwarder, engineServiceFacade); - final ApexPdpPolicy policy1 = - new ApexPdpPolicy("policy1", new FileInputStream(File.createTempFile("policy1", null))); + final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex"); policies.add(policy1); - final ApexPdpPolicy policy2 = - new ApexPdpPolicy("policy2", new FileInputStream(File.createTempFile("policy2", null))); + final PolicyAsString policy2 = new PolicyAsString("policy2", "APEX", "Sample Policy of apex"); policies.add(policy2); try { 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 index e09357cf..c90917b8 100644 --- 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 @@ -56,4 +56,7 @@ public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { return policesToReturn; } + + @Override + public void configure(final String parameterGroupName) {} } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java index c86bae9e..9962894e 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Attribute.java @@ -33,7 +33,7 @@ class Attribute { @SerializedName(value = "attribute_value") private String attributeValue; - public void setAttributeName(String attributeName) { + public void setAttributeName(final String attributeName) { this.attributeName = attributeName; } @@ -41,13 +41,12 @@ class Attribute { return attributeName; } - public void setAttributeValue(String attributeValue) { + public void setAttributeValue(final String attributeValue) { this.attributeValue = attributeValue; } public String getAttributeValue() { return attributeValue; } - } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java index 264ba8a6..77d1a9b7 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Content.java @@ -20,8 +20,8 @@ package org.onap.policy.distribution.reception.decoding.pdpx; -import java.util.List; import java.util.ArrayList; +import java.util.List; /** * The content acts the high level abstraction which to be used by OOF to do Optimization. @@ -35,15 +35,15 @@ class Content { private String policyType = "Optimization"; private List<FlavorFeature> flavorFeatures = new ArrayList<>(); - public void setResources(String resources) { + public void setResources(final String resources) { this.resources = resources; } public String getResources() { return resources; - } + } - public void setIdentity(String identity) { + public void setIdentity(final String identity) { this.identity = identity; } @@ -55,7 +55,7 @@ class Content { return policyScope; } - public void setPolicyType(String policyType) { + public void setPolicyType(final String policyType) { this.policyType = policyType; } @@ -66,6 +66,5 @@ class Content { public List<FlavorFeature> getFlavorFeatures() { return flavorFeatures; } - } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java index c04fc91d..6df5bc38 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/Directive.java @@ -20,22 +20,21 @@ package org.onap.policy.distribution.reception.decoding.pdpx; -import java.util.List; import java.util.ArrayList; +import java.util.List; /** - * The attribute acts an abstraction to indicate OOF which supports two different Models - * (Heat and TOSCA), two areas are wrapped: in the VNFC level to indicate the flavor, - * in the hpa_feature level to contains specified information. + * The attribute acts an abstraction to indicate OOF which supports two different Models (Heat and TOSCA), two areas are + * wrapped: in the VNFC level to indicate the flavor, in the hpa_feature level to contains specified information. * * @author Libo Zhu (libo.zhu@intel.com) */ -class Directive{ +class Directive { private String type; private List<Attribute> attributes = new ArrayList<>(); - public void setType(String type) { + public void setType(final String type) { this.type = type; } @@ -46,6 +45,5 @@ class Directive{ public List<Attribute> getAttributes() { return attributes; } - } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java index 3fba96d4..4e7f9411 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/ExtractFromNode.java @@ -5,49 +5,38 @@ * 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.reception.decoding.pdpx; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.HashMap; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import java.io.FileWriter; import java.io.Writer; - +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.annotations.SerializedName; - import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; - import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; -import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl; -import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; -import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; - -import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.CapabilityAssignment; import org.onap.sdc.toscaparser.api.CapabilityAssignments; -import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.onap.sdc.toscaparser.api.NodeTemplate; /** * Extract concerned info from NodeTemplate, currently ONLY HPA Feature. @@ -57,167 +46,156 @@ import org.onap.sdc.toscaparser.api.elements.Metadata; public class ExtractFromNode { private static final Logger LOGGER = FlexLogger.getLogger(ExtractFromNode.class); - private static final String CONTENT_RESOURCES = "name"; private static final String VDU_TYPE = "tosca.nodes.nfv.Vdu.Compute"; private static final String VDU_CP_TYPE = "tosca.nodes.nfv.VduCp"; private static final String VIRTUAL_MEM_SIZE_PATH = "virtual_memory#virtual_mem_size"; private static final String NUM_VIRTUAL_CPU_PATH = "virtual_cpu#num_virtual_cpu"; private static final String CPU_ARCHITECTURE_PATH = "virtual_cpu#cpu_architecture"; - private static final String NUMBER_OF_PAGES_PATH = "virtual_memory#vdu_memory_requirements#numberOfPages"; private static final String BASIC_CAPABILITIES = "BasicCapabilities"; - ISdcCsarHelper sdcCsarHelper; - final Gson gson = new GsonBuilder() - .serializeNulls() - .setPrettyPrinting() - .disableHtmlEscaping() - .create(); - + final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); - public void setSdcCsarHelper(ISdcCsarHelper sdcCsarHelper) { + public void setSdcCsarHelper(final ISdcCsarHelper sdcCsarHelper) { this.sdcCsarHelper = sdcCsarHelper; } - /* - * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated PdpxPolicy - * if it has got Hpa feature info or else return null. - * + /** + * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated PdpxPolicy if it has + * got Hpa feature info or else return null. + * * @param node the NodeTemplate * @return the extracted info from input node * @throws PolicyDecodingException if extract fails */ - public PdpxPolicy extractInfo(NodeTemplate node) throws PolicyDecodingException { - PdpxPolicy pdpxPolicy = new PdpxPolicy(); - Content content = pdpxPolicy.getContent(); + public PdpxPolicy extractInfo(final NodeTemplate node) throws PolicyDecodingException { - String outputFile = sdcCsarHelper.getNodeTemplateMetadata(node).getValue("name"); - outputFile += ".json"; LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(node)); - LOGGER.debug("outputFile = " + outputFile); - - List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node); + final List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node); LOGGER.debug("the size of lnodeChild = " + lnodeChild.size()); - //Store all the VDUs under one VNF - List<NodeTemplate> lnodeVdu = new ArrayList<>(); - //Store all the Cps under one VNF - List<NodeTemplate> lnodeVduCp = new ArrayList<>(); - for ( NodeTemplate nodeChild : lnodeChild) { - String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild); + // Store all the VDUs under one VNF + final List<NodeTemplate> lnodeVdu = new ArrayList<>(); + // Store all the Cps under one VNF + final List<NodeTemplate> lnodeVduCp = new ArrayList<>(); + for (final NodeTemplate nodeChild : lnodeChild) { + final String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild); LOGGER.debug("the type of this nodeChild = " + type); LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild)); - if ( type.equalsIgnoreCase(VDU_TYPE)) { + if (type.equalsIgnoreCase(VDU_TYPE)) { lnodeVdu.add(nodeChild); - } else if ( type.equalsIgnoreCase(VDU_CP_TYPE)) { + } else if (type.equalsIgnoreCase(VDU_CP_TYPE)) { lnodeVduCp.add(nodeChild); } } - LOGGER.debug("the size of vdu is =" + lnodeVdu.size()); LOGGER.debug("the size of cp is =" + lnodeVduCp.size()); + final PdpxPolicy pdpxPolicy = new PdpxPolicy(); + final Content content = pdpxPolicy.getContent(); extractInfoVdu(lnodeVdu, content); extractInfoVduCp(lnodeVduCp, content); - - if (content.getFlavorFeatures().isEmpty() ){ + if (content.getFlavorFeatures().isEmpty()) { return null; - } - + } + String outputFile = sdcCsarHelper.getNodeTemplateMetadata(node).getValue("name"); + outputFile += ".json"; + LOGGER.debug("outputFile = " + outputFile); try (Writer writer = new FileWriter(outputFile)) { gson.toJson(pdpxPolicy, writer); - } catch (Exception e) { - LOGGER.error("can't write generated policies to file " , e); - throw new PolicyDecodingException ("Exception caught when writing generated policies to file ", e); + } catch (final Exception exp) { + final String message = "Failed writing generated policies to file"; + LOGGER.error(message, exp); + throw new PolicyDecodingException(message, exp); } return pdpxPolicy; } - /* + /** * ExtractInfofromVdu, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. - * + * * @param lnodeVdu the list of Vdu node * @param content to be change based on lnodeVdu */ - public void extractInfoVdu(final List<NodeTemplate> lnodeVdu, Content content) { - //each VDU <=> FlavorFeature - for ( NodeTemplate node : lnodeVdu) { - String id = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, "name"); - FlavorFeature flavorFeature = new FlavorFeature(); - flavorFeature.setId(id); - Attribute flavorAttribute = new Attribute(); + public void extractInfoVdu(final List<NodeTemplate> lnodeVdu, final Content content) { + // each VDU <=> FlavorFeature + for (final NodeTemplate node : lnodeVdu) { + final Attribute flavorAttribute = new Attribute(); flavorAttribute.setAttributeName("flavorName"); flavorAttribute.setAttributeValue(""); - Directive flavorDirective = new Directive(); + final Directive flavorDirective = new Directive(); flavorDirective.setType("flavor_directive"); flavorDirective.getAttributes().add(flavorAttribute); + final FlavorFeature flavorFeature = new FlavorFeature(); + flavorFeature.setId(sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, "name")); flavorFeature.getDirectives().add(flavorDirective); - - CapabilityAssignments capabilityAssignments = sdcCsarHelper.getCapabilitiesOf(node); - CapabilityAssignment capabilityAssignment = capabilityAssignments.getCapabilityByName("virtual_compute"); + + final CapabilityAssignments capabilityAssignments = sdcCsarHelper.getCapabilitiesOf(node); + final CapabilityAssignment capabilityAssignment = + capabilityAssignments.getCapabilityByName("virtual_compute"); if (capabilityAssignment != null) { generateBasicCapability(capabilityAssignment, flavorFeature); - generateHugePages(capabilityAssignment, flavorFeature); + generateHugePages(capabilityAssignment); } - - content.getFlavorFeatures().add(flavorFeature); + content.getFlavorFeatures().add(flavorFeature); } } - /* + /** * GenerateBasicCapability, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. * * @param capabilityAssignment represents the capability of node * @param flavorFeature represents all the features of specified flavor */ - private void generateBasicCapability(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){ - //the format is xxx MB/GB like 4096 MB - String virtualMemSize = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - VIRTUAL_MEM_SIZE_PATH); - if (virtualMemSize != null) { - LOGGER.debug("the virtualMemSize = " + virtualMemSize); - HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("virtualMemSize", virtualMemSize); - FlavorProperty flavorProperty = new FlavorProperty(); - flavorProperty.setHpaFeature(BASIC_CAPABILITIES); - flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); - flavorFeature.getFlavorProperties().add(flavorProperty); - } - - //the format is int like 2 - String numVirtualCpu = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - NUM_VIRTUAL_CPU_PATH); - if (numVirtualCpu != null) { - LOGGER.debug("the numVirtualCpu = " + numVirtualCpu); - HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu); - String cpuArchitecture = sdcCsarHelper.getCapabilityPropertyLeafValue - (capabilityAssignment,CPU_ARCHITECTURE_PATH); - FlavorProperty flavorProperty = new FlavorProperty(); - flavorProperty.setHpaFeature(BASIC_CAPABILITIES); - if (cpuArchitecture != null) { - flavorProperty.setArchitecture(cpuArchitecture); - } - flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); - flavorFeature.getFlavorProperties().add(flavorProperty); + private void generateBasicCapability(final CapabilityAssignment capabilityAssignment, + final FlavorFeature flavorFeature) { + // the format is xxx MB/GB like 4096 MB + final String virtualMemSize = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, VIRTUAL_MEM_SIZE_PATH); + if (virtualMemSize != null) { + LOGGER.debug("the virtualMemSize = " + virtualMemSize); + final HpaFeatureAttribute hpaFeatureAttribute = + generateHpaFeatureAttribute("virtualMemSize", virtualMemSize); + final FlavorProperty flavorProperty = new FlavorProperty(); + flavorProperty.setHpaFeature(BASIC_CAPABILITIES); + flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); + flavorFeature.getFlavorProperties().add(flavorProperty); + } + + // the format is int like 2 + final String numVirtualCpu = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, NUM_VIRTUAL_CPU_PATH); + if (numVirtualCpu != null) { + LOGGER.debug("the numVirtualCpu = " + numVirtualCpu); + final HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu); + final String cpuArchitecture = + sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, CPU_ARCHITECTURE_PATH); + final FlavorProperty flavorProperty = new FlavorProperty(); + flavorProperty.setHpaFeature(BASIC_CAPABILITIES); + if (cpuArchitecture != null) { + flavorProperty.setArchitecture(cpuArchitecture); } + flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute); + flavorFeature.getFlavorProperties().add(flavorProperty); + } } - /* - * GenerateHpaFeatureAttribute based on the value of featureValue. - * the format: "hpa-attribute-key": "pciVendorId", "hpa-attribute-value": "1234", "operator": "=", "unit": "xxx". + /** + * GenerateHpaFeatureAttribute based on the value of featureValue. the format: "hpa-attribute-key": "pciVendorId", + * "hpa-attribute-value": "1234", "operator": "=", "unit": "xxx". * * @param hpaAttributeKey get from the high layer tosca DM * @param featureValue get from the high layer tosca DM - * @return the format used in underlayer component */ - private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue){ + private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue) { - HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute(); + final HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute(); hpaFeatureAttribute.setHpaAttributeKey(hpaAttributeKey); - String tmp = featureValue.replace(" ",""); - String pattern = "(\\D*)(\\d+)(\\D*)"; - Pattern r = Pattern.compile(pattern); - Matcher m = r.matcher(tmp); + final String tmp = featureValue.replace(" ", ""); + final String pattern = "(\\D*)(\\d+)(\\D*)"; + final Pattern r = Pattern.compile(pattern); + final Matcher m = r.matcher(tmp); if (m.find()) { LOGGER.debug("operator = " + m.group(1)); LOGGER.debug("value = " + m.group(2)); @@ -229,35 +207,27 @@ public class ExtractFromNode { return hpaFeatureAttribute; } - /* - * GenerateHugePages, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. + /** + * GenerateHugePages, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. The format is + * a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", "mandatory": true, + * "configuration-value": "2 MB"} * * @param capabilityAssignment represents the capability of node * @param flavorFeature represents all the features of specified flavor */ - private void generateHugePages(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){ - //the format is a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", - // "mandatory": true, "configuration-value": "2 MB"} - String numberOfPages = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, - NUMBER_OF_PAGES_PATH); - if (numberOfPages != null) { - LOGGER.debug("the virtualMemSize = " + numberOfPages); - //TODO add HugePages support - } + private void generateHugePages(final CapabilityAssignment capabilityAssignment) { + // add HugePages support } - /* - * ExtractInfoVduCp, supposted hpa features, under the virtual_network_interface_requirements of - * tosca.nodes.nfv.VduCp. - * + /** + * ExtractInfoVduCp, supported hpa features, under the virtual_network_interface_requirements of + * tosca.nodes.nfv.VduCp. + * * @param lnodeVduCp the list of VduCp node * @param content to be change based on lnodeVduCp */ - @SuppressWarnings("unchecked") - public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, Content content) { - for ( NodeTemplate node : lnodeVduCp) { - //TODO to add VDU cp Hpa feature extract - } + public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, final Content content) { + // to add VDU cp Hpa feature extract } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java index 551fbdee..b8f4b6ac 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorFeature.java @@ -20,8 +20,8 @@ package org.onap.policy.distribution.reception.decoding.pdpx; -import java.util.List; import java.util.ArrayList; +import java.util.List; /** * The FlavorFeature includes all the specified flavor infos used in multicloud, it represents one VDU of TOSCA. @@ -29,12 +29,12 @@ import java.util.ArrayList; * @author Libo Zhu (libo.zhu@intel.com) */ class FlavorFeature { - private String id ; + private String id; private String type = "tosca.node.nfv.Vdu.Compute"; private List<Directive> directives = new ArrayList<>(); private List<FlavorProperty> flavorProperties = new ArrayList<>(); - - public void setId(String id) { + + public void setId(final String id) { this.id = id; } @@ -42,7 +42,7 @@ class FlavorFeature { return id; } - public void setType(String type) { + public void setType(final String type) { this.type = type; } @@ -50,18 +50,12 @@ class FlavorFeature { return type; } - public List<FlavorProperty> getFlavorProperties() { + public List<FlavorProperty> getFlavorProperties() { return flavorProperties; } public List<Directive> getDirectives() { return directives; } - - @Override - public String toString() { - return "{ id = " + id + ", type = " + type + ", \n" + "directivies:["+directives + ",\n" - + flavorProperties + "}"; - } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java index 552164c5..b800f355 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/FlavorProperty.java @@ -20,17 +20,17 @@ package org.onap.policy.distribution.reception.decoding.pdpx; -import java.util.List; -import java.util.ArrayList; - import com.google.gson.annotations.SerializedName; +import java.util.ArrayList; +import java.util.List; + /** * The FlavorProperty includes all the properties of Flavor. * * @author Libo Zhu (libo.zhu@intel.com) */ -class FlavorProperty{ +class FlavorProperty { @SerializedName(value = "hpa-feature") private String hpaFeature; private String mandatory = "true"; @@ -41,7 +41,7 @@ class FlavorProperty{ @SerializedName(value = "hpa-feature-attributes") private List<HpaFeatureAttribute> hpaFeatureAttributes = new ArrayList<>(); - public void setHpaFeature(String hpaFeature) { + public void setHpaFeature(final String hpaFeature) { this.hpaFeature = hpaFeature; } @@ -49,7 +49,7 @@ class FlavorProperty{ return hpaFeature; } - public void setMandatory(String mandatory) { + public void setMandatory(final String mandatory) { this.mandatory = mandatory; } @@ -57,7 +57,7 @@ class FlavorProperty{ return mandatory; } - public void setArchitecture(String architecture) { + public void setArchitecture(final String architecture) { this.architecture = architecture; } @@ -65,7 +65,7 @@ class FlavorProperty{ return architecture; } - public void setHpaVersion(String hpaVersion) { + public void setHpaVersion(final String hpaVersion) { this.hpaVersion = hpaVersion; } @@ -80,12 +80,5 @@ class FlavorProperty{ public List<HpaFeatureAttribute> getHpaFeatureAttributes() { return hpaFeatureAttributes; } - - @Override - public String toString() { - return "{ hpaFeature:" + hpaFeature + ", mandatory = " + mandatory + ",architecture:" + architecture - + ", hpaFeatureAttributes : [" + hpaFeatureAttributes + "]"; - } - } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java index 7574ee87..1617956f 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/HpaFeatureAttribute.java @@ -27,7 +27,7 @@ import com.google.gson.annotations.SerializedName; * * @author Libo Zhu (libo.zhu@intel.com) */ -class HpaFeatureAttribute{ +class HpaFeatureAttribute { @SerializedName(value = "hpa-attribute-key") private String hpaAttributeKey; @SerializedName(value = "hap-attribute-value") @@ -35,17 +35,15 @@ class HpaFeatureAttribute{ private String operator; private String unit; - public HpaFeatureAttribute(){} - - public void setHpaAttributeKey(String hpaAttributeKey) { + public void setHpaAttributeKey(final String hpaAttributeKey) { this.hpaAttributeKey = hpaAttributeKey; } - + public String getHpaAttributeKey() { return hpaAttributeKey; } - public void setHpaAttributeValue(String hpaAttributeValue) { + public void setHpaAttributeValue(final String hpaAttributeValue) { this.hpaAttributeValue = hpaAttributeValue; } @@ -53,15 +51,15 @@ class HpaFeatureAttribute{ return hpaAttributeValue; } - public void setOperator(String operator) { + public void setOperator(final String operator) { this.operator = operator; } public String getOperator() { return operator; } - - public void setUnit(String unit) { + + public void setUnit(final String unit) { this.unit = unit; } @@ -70,5 +68,4 @@ class HpaFeatureAttribute{ } } - 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 179024a8..e7a0e8ab 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,15 +28,15 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; */ public class PdpxPolicy implements Policy { + private String guard; private String service; private String policyName; private String description; private String templateVersion; private String version; - private String priority; private String riskType; + private String priority; private String riskLevel; - private String guard; private Content content = new Content(); @Override @@ -49,30 +49,18 @@ public class PdpxPolicy implements Policy { return content.getPolicyType(); } - public void setService(String service) { - this.service = service; + public String getGuard() { + return guard; } public String getService() { return service; - } - - public void setPolicyName(String policyName) { - this.policyName = policyName; - } - - public void setDescription(String description) { - this.description = description; } public String getDescription() { return description; } - public void setTemplateVersion(String templateVersion) { - this.templateVersion = templateVersion; - } - public String getTemplateVersion() { return templateVersion; } @@ -81,48 +69,59 @@ public class PdpxPolicy implements Policy { return version; } - public void setVersion(String version) { - this.version = version; + public String getRiskType() { + return riskType; } public String getPriority() { return priority; } - public void setPriority(String priority) { - this.priority = priority; + public String getRiskLevel() { + return riskLevel; } - public String getGuard() { - return guard; + public Content getContent() { + return content; } - public void setGuard(String guard) { + public void setGuard(final String guard) { this.guard = guard; } - public String getRiskLevel() { - return riskLevel; + public void setService(final String service) { + this.service = service; } - public void setRiskLevel(String riskLevel) { - this.riskLevel = riskLevel; + public void setPolicyName(final String policyName) { + this.policyName = policyName; } - public String getRiskType() { - return riskType; + public void setDescription(final String description) { + this.description = description; + } + + public void setTemplateVersion(final String templateVersion) { + this.templateVersion = templateVersion; + } + + public void setVersion(final String version) { + this.version = version; } - public void setRiskType(String riskType) { + public void setRiskType(final String riskType) { this.riskType = riskType; } - public Content getContent(){ - return content; + public void setPriority(final String priority) { + this.priority = priority; } - public void setContent(Content content) { - this.content = content; - } + public void setRiskLevel(final String riskLevel) { + this.riskLevel = riskLevel; + } + public void setContent(final Content content) { + this.content = content; + } } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java index 0cd7bc16..4585ce73 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderCsarPdpx.java @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -22,45 +22,36 @@ package org.onap.policy.distribution.reception.decoding.pdpx; import java.io.File; import java.util.ArrayList; -import java.util.List; - import java.util.Collection; -import java.util.Collections; - +import java.util.List; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; - -import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; - import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; -import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; - import org.onap.sdc.toscaparser.api.NodeTemplate; -import org.onap.sdc.toscaparser.api.elements.Metadata; /** - * Decodes PDP-X policies from a TOSCA file. + * Decodes PDP-X policies from a CSAR file. */ public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderCsarPdpx.class); @Override - public Collection<PdpxPolicy> decode(Csar csar) throws PolicyDecodingException { - // logic for generating the policies from the CSAR. - List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); - ISdcCsarHelper sdcCsarHelper = parseCsar(csar); - List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); + public Collection<PdpxPolicy> decode(final Csar csar) throws PolicyDecodingException { + final List<PdpxPolicy> lPdpxPolicy = new ArrayList<>(); + final ISdcCsarHelper sdcCsarHelper = parseCsar(csar); + final List<NodeTemplate> lnodeVf = sdcCsarHelper.getServiceVfList(); LOGGER.debug("the size of Vf = " + lnodeVf.size()); - ExtractFromNode extractFromNode = new ExtractFromNode(); + final ExtractFromNode extractFromNode = new ExtractFromNode(); extractFromNode.setSdcCsarHelper(sdcCsarHelper); - for ( NodeTemplate node : lnodeVf) { - PdpxPolicy ret = extractFromNode.extractInfo(node); + for (final NodeTemplate node : lnodeVf) { + final PdpxPolicy ret = extractFromNode.extractInfo(node); if (ret != null) { lPdpxPolicy.add(ret); } @@ -69,34 +60,32 @@ public class PolicyDecoderCsarPdpx implements PolicyDecoder<Csar, PdpxPolicy> { } @Override - public boolean canHandle(PolicyInput policyInput) { + public boolean canHandle(final PolicyInput policyInput) { return policyInput.getClass().isAssignableFrom(Csar.class); } - /** - * Parse the input Csar by SDC tosca tool. + /** + * Parse the input Csar using SDC TOSCA parser. * * @param csar represents the service TOSCA Csar * @return the object to represents the content of input csar * @throws PolicyDecodingException if parse fails */ - public ISdcCsarHelper parseCsar(Csar csar) throws PolicyDecodingException { - ISdcCsarHelper sdcCsarHelper ; + public ISdcCsarHelper parseCsar(final Csar csar) throws PolicyDecodingException { + ISdcCsarHelper sdcCsarHelper; try { - - SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();//Autoclosable - - LOGGER.debug("tosca File Path = " + csar.getCsarPath()); - - File spoolFile = new File(csar.getCsarPath()); - - sdcCsarHelper = factory.getSdcCsarHelper(spoolFile.getAbsolutePath()); - - } catch (Exception e) { - LOGGER.error("Exception got in parseTosca",e); - throw new PolicyDecodingException ("Exception caught when passing the csar file to the parser ", e); + final SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance(); + LOGGER.debug("Csar File Path = " + csar.getCsarPath()); + final File csarFile = new File(csar.getCsarPath()); + sdcCsarHelper = factory.getSdcCsarHelper(csarFile.getAbsolutePath()); + } catch (final Exception exp) { + final String message = "Failed passing the csar file"; + LOGGER.error(message, exp); + throw new PolicyDecodingException(message, exp); } return sdcCsarHelper; } + @Override + public void configure(final String parameterGroupName) {} } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java index fa993579..40b33359 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/pdpx/PolicyDecoderToscaPdpx.java @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -22,6 +22,7 @@ package org.onap.policy.distribution.reception.decoding.pdpx; import java.util.Collection; import java.util.Collections; + import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.model.Tosca; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; @@ -32,14 +33,17 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; public class PolicyDecoderToscaPdpx implements PolicyDecoder<Tosca, PdpxPolicy> { @Override - public Collection<PdpxPolicy> decode(Tosca tosca) { + public Collection<PdpxPolicy> decode(final Tosca tosca) { // Add logic for generating the policies from the TOSCA return Collections.emptySet(); } @Override - public boolean canHandle(PolicyInput policyInput) { + public boolean canHandle(final PolicyInput policyInput) { return policyInput.getClass().isAssignableFrom(Tosca.class); } + @Override + public void configure(final String parameterGroupName) {} + } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java new file mode 100644 index 00000000..9516ad70 --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java @@ -0,0 +1,94 @@ +/*- + * ============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.reception.decoding.policy.file; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.commons.io.IOUtils; +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.model.Csar; +import org.onap.policy.distribution.model.PolicyAsString; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * This class extracts policy files from a CSAR file. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, PolicyAsString> { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderFileInCsarToPolicy.class); + PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters; + + /** + * {@inheritDoc}. + */ + @Override + public void configure(final String parameterGroupName) { + decoderParameters = ParameterService.get(parameterGroupName); + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean canHandle(final PolicyInput policyInput) { + return policyInput.getClass().isAssignableFrom(Csar.class); + } + + /** + * {@inheritDoc}. + */ + @Override + public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException { + final Collection<PolicyAsString> policyList = new ArrayList<>(); + + try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) { + final Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + final ZipEntry entry = entries.nextElement(); + if (entry.getName().contains(decoderParameters.getPolicyFileName())) { + final StringWriter writer = new StringWriter(); + IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8"); + final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(), + decoderParameters.getPolicyType(), writer.toString()); + policyList.add(poilcy); + } + } + } catch (final IOException exp) { + final String message = "Failed decoding the policy"; + LOGGER.error(message, exp); + throw new PolicyDecodingException(message, exp); + } + + return policyList; + } +} diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java new file mode 100644 index 00000000..958180db --- /dev/null +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroup.java @@ -0,0 +1,70 @@ +/*- + * ============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.reception.decoding.policy.file; + +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.reception.parameters.PolicyDecoderConfigurationParameterGroup; + +/** + * Holds the parameters for the{@link PolicyDecoderFileInCsarToPolicy}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicyParameterGroup extends PolicyDecoderConfigurationParameterGroup { + + private String policyFileName; + private String policyType; + + /** + * Constructor for instantiating {@link PolicyDecoderFileInCsarToPolicyParameterGroup} class. + * + * @param policyFileName the policy file name + * @param policyType the policy type + */ + public PolicyDecoderFileInCsarToPolicyParameterGroup(final String policyFileName, final String policyType) { + this.policyFileName = policyFileName; + this.policyType = policyType; + } + + public String getPolicyFileName() { + return policyFileName; + } + + public String getPolicyType() { + return policyType; + } + + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (!ParameterValidationUtils.validateStringParameter(policyFileName)) { + validationResult.setResult("policyFileName", ValidationStatus.INVALID, + "must be a non-blank string containing the policy file name"); + } + if (!ParameterValidationUtils.validateStringParameter(policyType)) { + validationResult.setResult("policyType", ValidationStatus.INVALID, + "must be a non-blank string containing the policy type"); + } + return validationResult; + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestAttribute.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestAttribute.java index f11a7ecd..f11a7ecd 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestAttribute.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestAttribute.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestContent.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestContent.java index a329a3ee..a329a3ee 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestContent.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestContent.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestDirective.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestDirective.java index 48d11d32..48d11d32 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestDirective.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestDirective.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorFeature.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorFeature.java index 47b4e343..47b4e343 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorFeature.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorFeature.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorProperty.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorProperty.java index b5a535d7..b5a535d7 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestFlavorProperty.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestFlavorProperty.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestHpaFeatureAttribute.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestHpaFeatureAttribute.java index 40c30eb7..40c30eb7 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestHpaFeatureAttribute.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestHpaFeatureAttribute.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestPolicyDecoderCsarPdpx.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java index 1ce5786c..1ce5786c 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/decoding/pdpx/TestPolicyDecoderCsarPdpx.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/pdpx/TestPolicyDecoderCsarPdpx.java diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java new file mode 100644 index 00000000..0ea08905 --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyParameterGroupTest.java @@ -0,0 +1,65 @@ +/*- + * ============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.reception.decoding.policy.file; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.distribution.reception.decoding.policy.file.PolicyDecoderFileInCsarToPolicyParameterGroup; + +/** + * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicyParameterGroup}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderFileInCsarToPolicyParameterGroupTest { + + @Test + public void testConstructorAndGetters() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", "APEX"); + configurationParameters.setName("myConfiguration"); + + assertEquals("myConfiguration", configurationParameters.getName()); + assertEquals("SamplePolicy", configurationParameters.getPolicyFileName()); + assertEquals("APEX", configurationParameters.getPolicyType()); + assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPolicyFileName() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("", "APEX"); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } + + @Test + public void testInvalidPolicyType() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup("SamplePolicy", ""); + configurationParameters.setName("myConfiguration"); + + assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java new file mode 100644 index 00000000..e40ab3fe --- /dev/null +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicyTest.java @@ -0,0 +1,109 @@ +/*- + * ============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.reception.decoding.policy.file; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.model.Csar; +import org.onap.policy.distribution.model.PolicyAsString; + +/** + * Class to perform unit test of {@link PolicyDecoderFileInCsarToPolicy}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +@RunWith(MockitoJUnitRunner.class) +public class PolicyDecoderFileInCsarToPolicyTest { + + private static final String POLICY_FILE_NAME = "SamplePolicyModelJAVASCRIPT"; + private static final String POLICY_TYPE = "APEX"; + private static final String GROUP_NAME = "apexPdpDecoderConfiguration"; + + /** + * Set up. + */ + @BeforeClass + public static void setUp() { + final PolicyDecoderFileInCsarToPolicyParameterGroup configurationParameters = + new PolicyDecoderFileInCsarToPolicyParameterGroup(POLICY_FILE_NAME, POLICY_TYPE); + configurationParameters.setName(GROUP_NAME); + ParameterService.register(configurationParameters); + } + + /** + * Tear down. + */ + @AfterClass + public static void tearDown() { + ParameterService.deregister(GROUP_NAME); + } + + @Test + public void testDecodePolicy() { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(GROUP_NAME); + + final File file = new File("src/test/resources/sampleTestService.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + try { + decoder.canHandle(csar); + final Collection<PolicyAsString> policyHolders = decoder.decode(csar); + for (final PolicyAsString policy : policyHolders) { + assertEquals(POLICY_FILE_NAME, policy.getPolicyName()); + assertEquals(POLICY_TYPE, policy.getPolicyType()); + } + } catch (final Exception exp) { + fail("Test must not throw an exception"); + } + } + + @Test + public void testDecodePolicyError() throws IOException { + + final PolicyDecoderFileInCsarToPolicy decoder = new PolicyDecoderFileInCsarToPolicy(); + decoder.configure(GROUP_NAME); + + final File file = new File("unknown.csar"); + final Csar csar = new Csar(file.getAbsolutePath()); + + try { + decoder.canHandle(csar); + decoder.decode(csar); + fail("Test must throw an exception"); + } catch (final Exception exp) { + assertTrue(exp.getMessage().contains("Failed decoding the policy")); + } + } +} diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java index 6a33e787..fd61c43d 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/DummyDecoder.java @@ -64,4 +64,7 @@ public class DummyDecoder implements PolicyDecoder<Csar, DummyPolicy> { public DummyPolicy getDecodedPolicy() { return decodedPolicy; } + + @Override + public void configure(final String parameterGroupName) {} } diff --git a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java index a8d15cb7..bf42476f 100644 --- a/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java +++ b/plugins/reception-plugins/src/test/java/org/onap/policy/distribution/reception/handling/sdc/TestSdcReceptionHandler.java @@ -336,7 +336,7 @@ public class TestSdcReceptionHandler { private Map<String, PolicyDecoderParameters> getPolicyDecoders() { final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>(); final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("DummyDecoder", - "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder"); + "org.onap.policy.distribution.reception.handling.sdc.DummyDecoder", "DummyDecoderConfiguration"); policyDecoders.put("DummyDecoderKey", pDParameters); return policyDecoders; } diff --git a/plugins/reception-plugins/src/test/resources/sampleTestService.csar b/plugins/reception-plugins/src/test/resources/sampleTestService.csar Binary files differnew file mode 100644 index 00000000..387f1bae --- /dev/null +++ b/plugins/reception-plugins/src/test/resources/sampleTestService.csar diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecoder.java b/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecoder.java index 2a07ec72..9cd660a9 100644 --- a/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecoder.java +++ b/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecoder.java @@ -34,6 +34,14 @@ import org.onap.policy.distribution.model.PolicyInput; public interface PolicyDecoder<S extends PolicyInput, T extends Policy> { /** + * Configure the policy decoder. This method will be invoked immediately after instantiation in order for the policy + * decoder to configure itself. + * + * @param parameterGroupName the name of the parameter group which contains the configuration for the policy decoder + */ + void configure(String parameterGroupName); + + /** * Can the decoder handle input of the specified type. * * @param policyInput the type 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 e6bfefea..74b8eb16 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 @@ -92,6 +92,7 @@ public class PluginHandler { (Class<PolicyDecoder<PolicyInput, Policy>>) Class .forName(decoderParameters.getDecoderClassName()); final PolicyDecoder<PolicyInput, Policy> decoder = policyDecoderClass.newInstance(); + decoder.configure(decoderParameters.getDecoderConfigurationName()); policyDecoders.add(decoder); } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { LOGGER.error("exception occured while initializing decoders", exp); diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParameterGroup.java b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParameterGroup.java new file mode 100644 index 00000000..00ae0a3a --- /dev/null +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParameterGroup.java @@ -0,0 +1,45 @@ +/*- + * ============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.reception.parameters; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; + +/** + * Base class of all {@link ParameterGroup} classes for configuration parameters for {@link PolicyDecoder} classes. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public abstract class PolicyDecoderConfigurationParameterGroup implements ParameterGroup { + + private String name; + + @Override + public String getName() { + return name; + } + + @Override + public void setName(final String name) { + this.name = name; + } + +} diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParametersJsonAdapter.java b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParametersJsonAdapter.java new file mode 100644 index 00000000..64eb4ed7 --- /dev/null +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParametersJsonAdapter.java @@ -0,0 +1,84 @@ +/*- + * ============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.reception.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 decoder parameters from JSON. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderConfigurationParametersJsonAdapter + implements JsonDeserializer<PolicyDecoderConfigurationParameterGroup> { + private static final XLogger LOGGER = + XLoggerFactory.getXLogger(PolicyDecoderConfigurationParametersJsonAdapter.class); + + private static final String PARAMETER_CLASS_NAME = "parameterClassName"; + private static final String POLICY_DECODER_PARAMETERS = "parameters"; + + @Override + public PolicyDecoderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) { + final JsonObject jsonObject = json.getAsJsonObject(); + + final String policyDecoderParameterClassName = getParameterGroupClassName(jsonObject); + final Class<?> policyDecoderParameterClass = getParameterGroupClass(policyDecoderParameterClassName); + + return context.deserialize(jsonObject.get(POLICY_DECODER_PARAMETERS), policyDecoderParameterClass); + } + + 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 policyDecoderParameterClassName) { + Class<?> policyDecoderParameterClass = null; + try { + policyDecoderParameterClass = Class.forName(policyDecoderParameterClassName); + } catch (final ClassNotFoundException exp) { + final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \"" + + policyDecoderParameterClassName + "\", could not find class"; + LOGGER.warn(errorMessage, exp); + throw new IllegalArgumentException(errorMessage, exp); + } + return policyDecoderParameterClass; + } + +} diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderParameters.java b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderParameters.java index 0b7ae0b7..91ada5ff 100644 --- a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderParameters.java +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderParameters.java @@ -37,16 +37,20 @@ public class PolicyDecoderParameters implements ParameterGroup { private String decoderType; private String decoderClassName; + private String decoderConfigurationName; /** * Constructor for instantiating PolicyDecoderParameters. * * @param decoderType the policy decoder type * @param decoderClassName the policy decoder class name + * @param decoderConfigurationName the policy decoder configuration name */ - public PolicyDecoderParameters(final String decoderType, final String decoderClassName) { + public PolicyDecoderParameters(final String decoderType, final String decoderClassName, + final String decoderConfigurationName) { this.decoderType = decoderType; this.decoderClassName = decoderClassName; + this.decoderConfigurationName = decoderConfigurationName; } /** @@ -68,6 +72,15 @@ public class PolicyDecoderParameters implements ParameterGroup { } /** + * Return the name of the decoder configuration of this {@link PolicyDecoderParameters} instance. + * + * @return the the name of the decoder configuration + */ + public String getDecoderConfigurationName() { + return decoderConfigurationName; + } + + /** * {@inheritDoc} */ @Override 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 d339507c..8dc84621 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,6 +29,7 @@ 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 +58,7 @@ public class AbstractReceptionHandlerTest { private static final String FORWARDER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder"; private static final String FORWARDER_CONFIGURATION_PARAMETERS = "DummyConfiguration"; + private static final String DECODER_CONFIGURATION_PARAMETERS = "DummyDecoderConfiguration"; @Test public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException, @@ -171,7 +173,8 @@ public class AbstractReceptionHandlerTest { private Map<String, PolicyDecoderParameters> getPolicyDecoders() { final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>(); - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME); + final PolicyDecoderParameters pDParameters = + new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS); policyDecoders.put(DECODER_KEY, pDParameters); return policyDecoders; } diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java index 2de1737e..74792b1e 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java @@ -56,4 +56,7 @@ public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { return policesToReturn; } + + @Override + public void configure(final String parameterGroupName) {} } |