From e80efa0dbe903e976f5b2799144658c7ba02e534 Mon Sep 17 00:00:00 2001 From: ramverma Date: Thu, 13 Sep 2018 16:31:35 +0100 Subject: Adding policy decoder to extract file from csar * Adding decoder configuration parameters infrastructure to support plugin based architecture. Adding a new policy decoder after this will be just creating a new decoder class and its corresponding parameter class. * Adding a new decoder which extracts policy file from given csar. It is written in a generic way to extract file for any pdp like apex, drools. * Adding configuration parameters for the new decoder. The policy file name and policy type is passed as parameter to the decoder. * Fixing few broken package declaration in pdpx decoder tests. * Adding test cases for all code changes. Change-Id: I95e68cebce0f9747ca63b090f9b9116ce8836939 Issue-ID: POLICY-1101 Signed-off-by: ramverma --- .../reception/decoding/PolicyDecoder.java | 8 +++ .../reception/handling/PluginHandler.java | 1 + .../PolicyDecoderConfigurationParameterGroup.java | 45 ++++++++++++ ...yDecoderConfigurationParametersJsonAdapter.java | 84 ++++++++++++++++++++++ .../parameters/PolicyDecoderParameters.java | 15 +++- .../handling/AbstractReceptionHandlerTest.java | 5 +- .../reception/handling/DummyDecoder.java | 3 + 7 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParameterGroup.java create mode 100644 reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderConfigurationParametersJsonAdapter.java (limited to 'reception') 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 @@ -33,6 +33,14 @@ import org.onap.policy.distribution.model.PolicyInput; */ public interface PolicyDecoder { + /** + * 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. * 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>) Class .forName(decoderParameters.getDecoderClassName()); final PolicyDecoder 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 { + 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; } /** @@ -67,6 +71,15 @@ public class PolicyDecoderParameters implements ParameterGroup { return decoderClassName; } + /** + * 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} */ 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 getPolicyDecoders() { final Map policyDecoders = new HashMap(); - 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 { public Collection decode(final PolicyInput input) throws PolicyDecodingException { return policesToReturn; } + + @Override + public void configure(final String parameterGroupName) {} } -- cgit 1.2.3-korg