diff options
Diffstat (limited to 'reception/src/main/java')
6 files changed, 439 insertions, 44 deletions
diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java b/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java index f728fc33..94bcc65a 100644 --- a/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java +++ b/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.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,70 +22,73 @@ package org.onap.policy.distribution.reception.handling; import java.util.ArrayList; import java.util.Collection; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; import org.onap.policy.distribution.model.Policy; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /*** - * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base - * class by implementing the {@link #initializeReception(String)} method to perform the - * specific initialization required to receive inputs and by invoking - * {@link #inputReceived(PolicyInput)} when the reception handler receives input + * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base class by implementing + * the {@link #initializeReception(String)} method to perform the specific initialization required to receive inputs and + * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input */ public abstract class AbstractReceptionHandler implements ReceptionHandler { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractReceptionHandler.class); + private static final Logger LOGGER = FlexLogger.getLogger(AbstractReceptionHandler.class); private PluginHandler pluginHandler; @Override - public void initialize(String parameterGroupName) { - pluginHandler = new PluginHandler(parameterGroupName); + public void initialize(final String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException { + final ReceptionHandlerParameters receptionHandlerParameters = + (ReceptionHandlerParameters) ParameterService.get(parameterGroupName); + pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName()); initializeReception(parameterGroupName); } /** - * Sub classes must implement this method to perform the specific initialization required to - * receive inputs, for example setting up subscriptions - * + * Sub classes must implement this method to perform the specific initialization required to receive inputs, for + * example setting up subscriptions + * * @param parameterGroupName the parameter group name */ protected abstract void initializeReception(String parameterGroupName); /** - * Handle input that has been received. The given input shall be decoded using the - * {@link PolicyDecoder}s configured for this reception handler and forwarded using the - * {@link PolicyForwarder}s configured for this reception handler. - * + * Handle input that has been received. The given input shall be decoded using the {@link PolicyDecoder}s configured + * for this reception handler and forwarded using the {@link PolicyForwarder}s configured for this reception + * handler. + * * @param policyInput the input that has been received - * @throws PolicyDecodingException if an error occurs in decoding a policy from the received - * input + * @throws PolicyDecodingException if an error occurs in decoding a policy from the received input */ - protected void inputReceived(PolicyInput policyInput) throws PolicyDecodingException { + protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException { - Collection<Policy> policies = new ArrayList<>(); - for (PolicyDecoder<PolicyInput, Policy> policyDecoder : getRelevantPolicyDecoders(policyInput)) { + final Collection<Policy> policies = new ArrayList<>(); + for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : getRelevantPolicyDecoders(policyInput)) { policies.addAll(policyDecoder.decode(policyInput)); } - for (PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) { + for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) { try { policyForwarder.forward(policies); - } catch (PolicyForwardingException policyForwardingException) { + } catch (final PolicyForwardingException policyForwardingException) { LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException); } } } - private Collection<PolicyDecoder<PolicyInput, Policy>> getRelevantPolicyDecoders(PolicyInput policyInput) + private Collection<PolicyDecoder<PolicyInput, Policy>> getRelevantPolicyDecoders(final PolicyInput policyInput) throws PolicyDecodingException { - Collection<PolicyDecoder<PolicyInput, Policy>> relevantPolicyDecoders = new ArrayList<>(); - for (PolicyDecoder<PolicyInput, Policy> policyDecoder : pluginHandler.getPolicyDecoders()) { + final Collection<PolicyDecoder<PolicyInput, Policy>> relevantPolicyDecoders = new ArrayList<>(); + for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : pluginHandler.getPolicyDecoders()) { if (policyDecoder.canHandle(policyInput)) { relevantPolicyDecoders.add(policyDecoder); } 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 d10fe0b0..7afc5814 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 @@ -5,26 +5,35 @@ * 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.handling; +import java.util.ArrayList; import java.util.Collection; +import java.util.Map; + +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.parameters.PolicyForwarderParameters; import org.onap.policy.distribution.model.Policy; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; /** * Handles the plugins to policy distribution. @@ -36,16 +45,20 @@ public class PluginHandler { /** * Create an instance to instantiate plugins based on the given parameter group. - * + * * @param parameterGroupName the name of the parameter group + * @throws PolicyDecodingException + * @throws PolicyForwardingException */ - public PluginHandler(String parameterGroupName) { - // Read configuration using common/common-parameters and instantiate decoders and forwarders + public PluginHandler(final String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException { + final PluginHandlerParameters params = (PluginHandlerParameters) ParameterService.get(parameterGroupName); + initializePolicyDecoders(params.getPolicyDecoders()); + initializePolicyForwarders(params.getPolicyForwarders()); } /** * Get the policy decoders. - * + * * @return the policy decoders */ public Collection<PolicyDecoder<PolicyInput, Policy>> getPolicyDecoders() { @@ -54,13 +67,54 @@ public class PluginHandler { /** * Get the policy forwarders. - * + * * @return the policy forwarders */ public Collection<PolicyForwarder> getPolicyForwarders() { return policyForwarders; } + /** + * Initialize policy decoders. + * + * @param policyDecoderParameters + * @throws PolicyDecodingException + */ + @SuppressWarnings("unchecked") + private void initializePolicyDecoders(final Map<String, PolicyDecoderParameters> policyDecoderParameters) + throws PolicyDecodingException { + policyDecoders = new ArrayList<PolicyDecoder<PolicyInput, Policy>>(); + for (final PolicyDecoderParameters pDParameters : policyDecoderParameters.values()) { + try { + final Class<PolicyDecoder<PolicyInput, Policy>> policyDecoderClass = + (Class<PolicyDecoder<PolicyInput, Policy>>) Class.forName(pDParameters.getDecoderClassName()); + final PolicyDecoder<PolicyInput, Policy> decoder = policyDecoderClass.newInstance(); + policyDecoders.add(decoder); + } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { + throw new PolicyDecodingException(exp.getMessage()); + } + } + } + /** + * Initialize policy forwarders + * + * @param policyForwarderParameters + * @throws PolicyForwardingException + */ + @SuppressWarnings("unchecked") + private void initializePolicyForwarders(final Map<String, PolicyForwarderParameters> policyForwarderParameters) + throws PolicyForwardingException { + policyForwarders = new ArrayList<PolicyForwarder>(); + for (final PolicyForwarderParameters pFParameters : policyForwarderParameters.values()) { + try { + final Class<PolicyForwarder> policyForwarderClass = + (Class<PolicyForwarder>) Class.forName(pFParameters.getForwarderClassName()); + policyForwarders.add(policyForwarderClass.newInstance()); + } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { + throw new PolicyForwardingException(exp.getMessage(), exp.getCause()); + } + } + } } diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java b/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java index 85cc1db1..c3a7544d 100644 --- a/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java +++ b/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java @@ -5,21 +5,24 @@ * 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.handling; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + /** * Handles input into Policy Distribution which may be decoded into a Policy. */ @@ -27,11 +30,12 @@ public interface ReceptionHandler { /** * Initialize the reception handler with the given parameters - * - * @param parameterGroupName the name of the parameter group containing the configuration for - * the reception handler + * + * @param parameterGroupName the name of the parameter group containing the configuration for the reception handler + * @throws PolicyDecodingException + * @throws PolicyForwardingException */ - void initialize(String parameterGroupName); + void initialize(String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException; /** * Destroy the reception handler, removing any subscriptions and releasing all resources diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java new file mode 100644 index 00000000..7e16518b --- /dev/null +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java @@ -0,0 +1,113 @@ +/*- + * ============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 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.distribution.forwarding.parameters.PolicyForwarderParameters; + +/** + * Class to hold all the plugin handler parameters. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PluginHandlerParameters implements ParameterGroup { + + private static final String PLUGIN_HANDLER = "_PluginHandler"; + + private String name; + private Map<String, PolicyDecoderParameters> policyDecoders; + private Map<String, PolicyForwarderParameters> policyForwarders; + + /** + * Constructor for instantiating PluginHandlerParameters. + * + * @param policyDecoders the map of policy decoders + * @param policyForwarders the map of policy forwarders + */ + public PluginHandlerParameters(final Map<String, PolicyDecoderParameters> policyDecoders, + final Map<String, PolicyForwarderParameters> policyForwarders) { + this.policyDecoders = policyDecoders; + this.policyForwarders = policyForwarders; + } + + /** + * Return the policyDecoders of this PluginHandlerParameters instance. + * + * @return the policyDecoders + */ + public Map<String, PolicyDecoderParameters> getPolicyDecoders() { + return policyDecoders; + } + + /** + * Return the policyForwarders of this PluginHandlerParameters instance. + * + * @return the policyForwarders + */ + public Map<String, PolicyForwarderParameters> getPolicyForwarders() { + return policyForwarders; + } + + @Override + public String getName() { + return name + PLUGIN_HANDLER; + } + + /** + * Validate the plugin handler parameters. + * + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (policyDecoders == null || policyDecoders.size() == 0) { + validationResult.setResult("policyDecoders", ValidationStatus.INVALID, + "must have at least one policy decoder"); + } else { + for (final Entry<String, PolicyDecoderParameters> nestedGroupEntry : policyDecoders.entrySet()) { + validationResult.setResult("policyDecoders", nestedGroupEntry.getKey(), + nestedGroupEntry.getValue().validate()); + } + } + if (policyForwarders == null || policyForwarders.size() == 0) { + validationResult.setResult("policyForwarders", ValidationStatus.INVALID, + "must have at least one policy forwarder"); + } else { + for (final Entry<String, PolicyForwarderParameters> nestedGroupEntry : policyForwarders.entrySet()) { + validationResult.setResult("policyForwarders", nestedGroupEntry.getKey(), + nestedGroupEntry.getValue().validate()); + } + } + return validationResult; + } + + /** + * @param name the name to set + */ + public void setName(final String name) { + this.name = name; + } +} 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 new file mode 100644 index 00000000..59c59e16 --- /dev/null +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PolicyDecoderParameters.java @@ -0,0 +1,97 @@ +/*- + * ============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.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationStatus; + +/** + * Class to hold all the policy decoder parameters. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PolicyDecoderParameters implements ParameterGroup { + private String decoderType; + private String decoderClassName; + + /** + * Constructor for instantiating PolicyDecoderParameters. + * + * @param decoderType the policy decoder type + * @param decoderClassName the policy decoder class name + */ + public PolicyDecoderParameters(final String decoderType, final String decoderClassName) { + this.decoderType = decoderType; + this.decoderClassName = decoderClassName; + } + + /** + * Return the decoderType of this PolicyDecoderParameters instance. + * + * @return the decoderType + */ + public String getDecoderType() { + return decoderType; + } + + /** + * Return the decoderClassName of this PolicyDecoderParameters instance. + * + * @return the decoderClassName + */ + public String getDecoderClassName() { + return decoderClassName; + } + + @Override + public String getName() { + return null; + } + + /** + * Validate the policy decoder parameters. + * + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (decoderType == null || decoderType.trim().length() == 0) { + validationResult.setResult("decoderType", ValidationStatus.INVALID, "must be a non-blank string"); + } + if (decoderClassName == null || decoderClassName.trim().length() == 0) { + validationResult.setResult("decoderClassName", ValidationStatus.INVALID, + "must be a non-blank string containing full class name of the decoder"); + } else { + validatePolicyDecoderClass(validationResult); + } + return validationResult; + } + + private void validatePolicyDecoderClass(final GroupValidationResult validationResult) { + try { + Class.forName(decoderClassName); + } catch (final ClassNotFoundException e) { + validationResult.setResult("decoderClassName", ValidationStatus.INVALID, + "policy decoder class not found in classpath"); + } + } +} diff --git a/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java new file mode 100644 index 00000000..54979ab2 --- /dev/null +++ b/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java @@ -0,0 +1,124 @@ +/*- + * ============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.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationStatus; + +/** + * Class to hold all the reception handler parameters. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class ReceptionHandlerParameters implements ParameterGroup { + private String name; + private String receptionHandlerType; + private String receptionHandlerClassName; + private PluginHandlerParameters pluginHandlerParameters; + + /** + * Constructor for instantiating ReceptionHandlerParameters. + * + * @param receptionHandlerType the reception handler type + * @param receptionHandlerClassName the reception handler class name + * @param pluginHandlerParameters the plugin handler parameters + */ + public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName, + final PluginHandlerParameters pluginHandlerParameters) { + this.receptionHandlerType = receptionHandlerType; + this.receptionHandlerClassName = receptionHandlerClassName; + this.pluginHandlerParameters = pluginHandlerParameters; + } + + /** + * Return the receptionHandlerType of this ReceptionHandlerParameters instance. + * + * @return the receptionHandlerType + */ + public String getReceptionHandlerType() { + return receptionHandlerType; + } + + /** + * Return the receptionHandlerClassName of this ReceptionHandlerParameters instance. + * + * @return the receptionHandlerClassName + */ + public String getReceptionHandlerClassName() { + return receptionHandlerClassName; + } + + /** + * Return the pluginHandlerParameters of this ReceptionHandlerParameters instance. + * + * @return the pluginHandlerParameters + */ + public PluginHandlerParameters getPluginHandlerParameters() { + return pluginHandlerParameters; + } + + @Override + public String getName() { + return name + "_" + receptionHandlerType; + } + + /** + * Validate the reception handler parameters. + * + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (receptionHandlerType == null || receptionHandlerType.trim().length() == 0) { + validationResult.setResult("receptionHandlerType", ValidationStatus.INVALID, "must be a non-blank string"); + } + if (receptionHandlerClassName == null || receptionHandlerClassName.trim().length() == 0) { + validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID, + "must be a non-blank string containing full class name of the reception handler"); + } else { + validateReceptionHandlerClass(validationResult); + } + if (pluginHandlerParameters == null) { + validationResult.setResult("pluginHandlerParameters", ValidationStatus.INVALID, + "must have a plugin handler"); + } else { + validationResult.setResult("pluginHandlerParameters", pluginHandlerParameters.validate()); + } + return validationResult; + } + + private void validateReceptionHandlerClass(final GroupValidationResult validationResult) { + try { + Class.forName(receptionHandlerClassName); + } catch (final ClassNotFoundException e) { + validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID, + "reception handler class not found in classpath"); + } + } + + /** + * @param name the name to set + */ + public void setName(final String name) { + this.name = name; + } +} |