diff options
18 files changed, 345 insertions, 153 deletions
@@ -9,3 +9,5 @@ target .metadata/ /bin/ .log +logs +debug-logs diff --git a/forwarding/pom.xml b/forwarding/pom.xml index 6a7b123a..830f3724 100644 --- a/forwarding/pom.xml +++ b/forwarding/pom.xml @@ -42,5 +42,10 @@ <artifactId>common-parameters</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> + <dependency> + <groupId>org.onap.policy.common</groupId> + <artifactId>ONAP-Logging</artifactId> + <version>1.3.0-SNAPSHOT</version> + </dependency> </dependencies> </project> diff --git a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java index 3bde5009..57d04248 100644 --- a/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java +++ b/forwarding/src/main/java/org/onap/policy/distribution/forwarding/parameters/PolicyForwarderParameters.java @@ -20,6 +20,8 @@ package org.onap.policy.distribution.forwarding.parameters; +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.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; @@ -30,6 +32,9 @@ import org.onap.policy.common.parameters.ValidationStatus; * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ public class PolicyForwarderParameters implements ParameterGroup { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyForwarderParameters.class); + private String forwarderType; private String forwarderClassName; @@ -89,7 +94,8 @@ public class PolicyForwarderParameters implements ParameterGroup { private void validatePolicyForwarderClass(final GroupValidationResult validationResult) { try { Class.forName(forwarderClassName); - } catch (final ClassNotFoundException e) { + } catch (final ClassNotFoundException exp) { + LOGGER.error("policy forwarder class not found in classpath", exp); validationResult.setResult("forwarderClassName", ValidationStatus.INVALID, "policy forwarder class not found in classpath"); } 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 43eb4f80..d360b5dd 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 @@ -20,6 +20,9 @@ package org.onap.policy.distribution.main.startstop; +import java.util.HashMap; +import java.util.Map; + import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; @@ -41,6 +44,9 @@ public class DistributionActivator { // The parameters of this policy distribution activator private final DistributionParameterGroup distributionParameterGroup; + // The map of reception handlers initialized by this distribution activator + private final Map<String, AbstractReceptionHandler> receptionHandlersMap = new HashMap<>(); + /** * Instantiate the activator for policy distribution as a complete service. * @@ -66,6 +72,7 @@ public class DistributionActivator { (Class<AbstractReceptionHandler>) Class.forName(rHParameters.getReceptionHandlerClassName()); final AbstractReceptionHandler receptionHandler = receptionHandlerClass.newInstance(); receptionHandler.initialize(rHParameters.getName()); + receptionHandlersMap.put(rHParameters.getName(), receptionHandler); } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException | PolicyDecodingException | PolicyForwardingException exp) { throw new PolicyDistributionException(exp.getMessage(), exp); @@ -80,8 +87,16 @@ public class DistributionActivator { * @throws PolicyDistributionException on termination errors */ public void terminate() throws PolicyDistributionException { - // Shut down all handlers - deregisterToParameterService(distributionParameterGroup); + try { + for (final AbstractReceptionHandler handler : receptionHandlersMap.values()) { + handler.destroy(); + } + receptionHandlersMap.clear(); + deregisterToParameterService(distributionParameterGroup); + } catch (final Exception exp) { + LOGGER.error("Policy distribution service termination failed", exp); + throw new PolicyDistributionException(exp.getMessage(), exp); + } } /** diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.java index 8691a3ad..eb33a852 100644 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.java +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/pap/engine/XacmlPapServletPolicyForwarder.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========================================================= */ @@ -21,16 +21,17 @@ package org.onap.policy.distribution.forwarding.pap.engine; import java.util.Collection; + import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.model.Policy; /** - * Forwards policies to the XACML PAP Servlet + * Forwards policies to the XACML PAP Servlet. */ public class XacmlPapServletPolicyForwarder implements PolicyForwarder { @Override - public void forward(Collection<Policy> policies) { + public void forward(final Collection<Policy> policies) { // Send policies to PAP using common/policy-endpoints } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfiguration.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfiguration.java index 5f74e2b2..86d2a550 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfiguration.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfiguration.java @@ -5,25 +5,23 @@ * 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.sdc; -import java.io.File; -import java.util.ArrayList; import java.util.List; -import java.util.Properties; + import org.onap.sdc.api.consumer.IConfiguration; /** @@ -35,14 +33,14 @@ public class PSSDConfiguration implements IConfiguration { // Configuration file structure // Configuration file properties - private PSSDConfigurationParametersGroup configParameters=null; + private PSSDConfigurationParametersGroup configParameters = null; /** * Original constructor * * @param configParameters properties needed to be configured for the model loader */ - public PSSDConfiguration(PSSDConfigurationParametersGroup configParameters) { + public PSSDConfiguration(final PSSDConfigurationParametersGroup configParameters) { this.configParameters = configParameters; } diff --git a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfigurationParametersGroup.java b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfigurationParametersGroup.java index da603359..7fa81149 100644 --- a/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfigurationParametersGroup.java +++ b/plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/handling/sdc/PSSDConfigurationParametersGroup.java @@ -5,40 +5,36 @@ * 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.sdc; -import java.util.List; -import java.io.FileReader; -import java.io.IOException; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import java.util.List; import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterConstants; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; /** -* This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json -* format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK. -*/ -public class PSSDConfigurationParametersGroup implements ParameterGroup { - //Policy SDC Service Distribution specified field. + * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json + * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK. + */ +public class PSSDConfigurationParametersGroup implements ParameterGroup { + // Policy SDC Service Distribution specified field. private String name; - - //Interface of IConfiguration item + + // Interface of IConfiguration item private String asdcAddress; private List<String> messageBusAddress; private String user; @@ -55,11 +51,11 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { private boolean isFilterinEmptyResources; private Boolean isUseHttpsWithDmaap; - public String getAsdcAddress(){ + public String getAsdcAddress() { return asdcAddress; } - public List<String> getMsgBusAddress(){ + public List<String> getMsgBusAddress() { return messageBusAddress; } @@ -74,7 +70,7 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { public int getPollingInterval() { return pollingInterval; } - + public int getPollingTimeout() { return pollingTimeout; } @@ -83,7 +79,7 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { return consumerId; } - public List<String> getArtifactTypes(){ + public List<String> getArtifactTypes() { return artifactTypes; } @@ -117,8 +113,8 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { @Override public String toString() { - return "name =" + name +",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = " + messageBusAddress + - ", user = "+ user + "]"; + return "name =" + name + ",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = " + + messageBusAddress + ", user = " + user + "]"; } @Override @@ -128,14 +124,15 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { @Override public GroupValidationResult validate() { - GroupValidationResult validationResult = new GroupValidationResult(this); + final GroupValidationResult validationResult = new GroupValidationResult(this); if (name == null || name.trim().length() == 0) { validationResult.setResult("name", ValidationStatus.INVALID, "name must be a non-blank string"); } if (asdcAddress == null || asdcAddress.trim().length() == 0) { - validationResult.setResult("asdcAddress", ValidationStatus.INVALID, "asdcAddress must be a non-blank string"); + validationResult.setResult("asdcAddress", ValidationStatus.INVALID, + "asdcAddress must be a non-blank string"); } if (user == null || user.trim().length() == 0) { @@ -147,49 +144,52 @@ public class PSSDConfigurationParametersGroup implements ParameterGroup { } if (consumerGroup == null || consumerGroup.trim().length() == 0) { - validationResult.setResult("consumerGroup", ValidationStatus.INVALID, "consumerGroup must be a non-blank string"); + validationResult.setResult("consumerGroup", ValidationStatus.INVALID, + "consumerGroup must be a non-blank string"); } if (keystorePath == null || keystorePath.trim().length() == 0) { - validationResult.setResult("keystorePath", ValidationStatus.INVALID, "keystorePath must be a non-blank string"); + validationResult.setResult("keystorePath", ValidationStatus.INVALID, + "keystorePath must be a non-blank string"); } if (keystorePassword == null || keystorePassword.trim().length() == 0) { - validationResult.setResult("keystorePassword", ValidationStatus.INVALID, "keystorePassword must be a non-blank string"); + validationResult.setResult("keystorePassword", ValidationStatus.INVALID, + "keystorePassword must be a non-blank string"); } - if(messageBusAddress == null){ - validationResult.setResult("messageBusAddress", ValidationStatus.INVALID, - "messageBusAddress must be a list of non-blank string"); - }else { - for(String temp:messageBusAddress){ - if(temp.trim().length() == 0){ + if (messageBusAddress == null) { + validationResult.setResult("messageBusAddress", ValidationStatus.INVALID, + "messageBusAddress must be a list of non-blank string"); + } else { + for (final String temp : messageBusAddress) { + if (temp.trim().length() == 0) { validationResult.setResult("messageBusAddress", ValidationStatus.INVALID, - "the string of messageBusAddress must be a non-blank string"); + "the string of messageBusAddress must be a non-blank string"); } } } - if(artifactTypes == null){ - validationResult.setResult("artifactTypes", ValidationStatus.INVALID, - "artifactTypes must be a list of non-blank string"); - }else { - for(String temp:artifactTypes){ - if(temp.trim().length() == 0){ + if (artifactTypes == null) { + validationResult.setResult("artifactTypes", ValidationStatus.INVALID, + "artifactTypes must be a list of non-blank string"); + } else { + for (final String temp : artifactTypes) { + if (temp.trim().length() == 0) { validationResult.setResult("artifactTypes", ValidationStatus.INVALID, - "the string of artifactTypes must be a non-blank string"); + "the string of artifactTypes must be a non-blank string"); } } } - if (pollingInterval <= 0){ - validationResult.setResult("pollingInterval", ValidationStatus.INVALID, - "pollingInterval must be a positive integer"); + if (pollingInterval <= 0) { + validationResult.setResult("pollingInterval", ValidationStatus.INVALID, + "pollingInterval must be a positive integer"); } - if (pollingTimeout <= 0){ - validationResult.setResult("pollingTimeout", ValidationStatus.INVALID, - "pollingTimeout must be a positive integer"); + if (pollingTimeout <= 0) { + validationResult.setResult("pollingTimeout", ValidationStatus.INVALID, + "pollingTimeout must be a positive integer"); } return validationResult; 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 94bcc65a..eeb1ead1 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 @@ -34,10 +34,10 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecoder; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; 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 + * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input. */ public abstract class AbstractReceptionHandler implements ReceptionHandler { @@ -55,7 +55,7 @@ public abstract class AbstractReceptionHandler implements ReceptionHandler { /** * Sub classes must implement this method to perform the specific initialization required to receive inputs, for - * example setting up subscriptions + * example setting up subscriptions. * * @param parameterGroupName the parameter group name */ 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 7afc5814..37638b59 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 @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Map; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; @@ -40,6 +42,8 @@ import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters */ public class PluginHandler { + private static final Logger LOGGER = FlexLogger.getLogger(PluginHandler.class); + private Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders; private Collection<PolicyForwarder> policyForwarders; @@ -47,8 +51,8 @@ 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 + * @throws PolicyDecodingException exception if it occurs + * @throws PolicyForwardingException exception if it occurs */ public PluginHandler(final String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException { final PluginHandlerParameters params = (PluginHandlerParameters) ParameterService.get(parameterGroupName); @@ -77,41 +81,44 @@ public class PluginHandler { /** * Initialize policy decoders. * - * @param policyDecoderParameters - * @throws PolicyDecodingException + * @param policyDecoderParameters exception if it occurs + * @throws PolicyDecodingException exception if it occurs */ @SuppressWarnings("unchecked") private void initializePolicyDecoders(final Map<String, PolicyDecoderParameters> policyDecoderParameters) throws PolicyDecodingException { - policyDecoders = new ArrayList<PolicyDecoder<PolicyInput, Policy>>(); - for (final PolicyDecoderParameters pDParameters : policyDecoderParameters.values()) { + policyDecoders = new ArrayList<>(); + for (final PolicyDecoderParameters decoderParameters : policyDecoderParameters.values()) { try { final Class<PolicyDecoder<PolicyInput, Policy>> policyDecoderClass = - (Class<PolicyDecoder<PolicyInput, Policy>>) Class.forName(pDParameters.getDecoderClassName()); + (Class<PolicyDecoder<PolicyInput, Policy>>) Class + .forName(decoderParameters.getDecoderClassName()); final PolicyDecoder<PolicyInput, Policy> decoder = policyDecoderClass.newInstance(); policyDecoders.add(decoder); } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { - throw new PolicyDecodingException(exp.getMessage()); + LOGGER.error("exception occured while initializing decoders", exp); + throw new PolicyDecodingException(exp.getMessage(), exp.getCause()); } } } /** - * Initialize policy forwarders + * Initialize policy forwarders. * - * @param policyForwarderParameters - * @throws PolicyForwardingException + * @param policyForwarderParameters exception if it occurs + * @throws PolicyForwardingException exception if it occurs */ @SuppressWarnings("unchecked") private void initializePolicyForwarders(final Map<String, PolicyForwarderParameters> policyForwarderParameters) throws PolicyForwardingException { - policyForwarders = new ArrayList<PolicyForwarder>(); - for (final PolicyForwarderParameters pFParameters : policyForwarderParameters.values()) { + policyForwarders = new ArrayList<>(); + for (final PolicyForwarderParameters forwarderParameters : policyForwarderParameters.values()) { try { final Class<PolicyForwarder> policyForwarderClass = - (Class<PolicyForwarder>) Class.forName(pFParameters.getForwarderClassName()); + (Class<PolicyForwarder>) Class.forName(forwarderParameters.getForwarderClassName()); policyForwarders.add(policyForwarderClass.newInstance()); } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { + LOGGER.error("exception occured while initializing forwarders", 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 c3a7544d..5f2e3716 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 @@ -29,16 +29,16 @@ import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; public interface ReceptionHandler { /** - * Initialize the reception handler with the given parameters + * Initialize the reception handler with the given parameters. * * @param parameterGroupName the name of the parameter group containing the configuration for the reception handler - * @throws PolicyDecodingException - * @throws PolicyForwardingException + * @throws PolicyDecodingException exception if it occurs + * @throws PolicyForwardingException exception if it occurs */ void initialize(String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException; /** - * Destroy the reception handler, removing any subscriptions and releasing all resources + * Destroy the reception handler, removing any subscriptions and releasing all resources. */ void destroy(); 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 index 7e16518b..c752020b 100644 --- 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 @@ -105,7 +105,9 @@ public class PluginHandlerParameters implements ParameterGroup { } /** - * @param name the name to set + * Set the name of this group. + * + * @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 index 59c59e16..6157b8b6 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 @@ -20,6 +20,8 @@ package org.onap.policy.distribution.reception.parameters; +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.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; @@ -30,6 +32,9 @@ import org.onap.policy.common.parameters.ValidationStatus; * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ public class PolicyDecoderParameters implements ParameterGroup { + + private static final Logger LOGGER = FlexLogger.getLogger(PolicyDecoderParameters.class); + private String decoderType; private String decoderClassName; @@ -89,7 +94,8 @@ public class PolicyDecoderParameters implements ParameterGroup { private void validatePolicyDecoderClass(final GroupValidationResult validationResult) { try { Class.forName(decoderClassName); - } catch (final ClassNotFoundException e) { + } catch (final ClassNotFoundException exp) { + LOGGER.error("policy decoder class not found in classpath", exp); 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 index 54979ab2..974436aa 100644 --- 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 @@ -20,6 +20,8 @@ package org.onap.policy.distribution.reception.parameters; +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.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; @@ -30,6 +32,9 @@ import org.onap.policy.common.parameters.ValidationStatus; * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) */ public class ReceptionHandlerParameters implements ParameterGroup { + + private static final Logger LOGGER = FlexLogger.getLogger(ReceptionHandlerParameters.class); + private String name; private String receptionHandlerType; private String receptionHandlerClassName; @@ -109,13 +114,16 @@ public class ReceptionHandlerParameters implements ParameterGroup { private void validateReceptionHandlerClass(final GroupValidationResult validationResult) { try { Class.forName(receptionHandlerClassName); - } catch (final ClassNotFoundException e) { + } catch (final ClassNotFoundException exp) { + LOGGER.error("reception handler class not found in classpath", exp); validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID, "reception handler class not found in classpath"); } } /** + * Set the name of this group. + * * @param name the name to set */ public void setName(final String name) { diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java b/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java index eba19503..a5bc072e 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.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========================================================= */ @@ -21,20 +21,21 @@ package org.onap.policy.distribution.reception.decoding; import static org.junit.Assert.assertEquals; + import org.junit.Test; public class PolicyDecodingExceptionTest { @Test public void testPolicyDecodingExceptionString() { - PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message"); + final PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message"); assertEquals("error message", policyDecodingException.getMessage()); } @Test public void testPolicyDecodingExceptionStringThrowable() { - Exception cause = new IllegalArgumentException(); - PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message", cause); + final Exception cause = new IllegalArgumentException(); + final PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message", cause); assertEquals("error message", policyDecodingException.getMessage()); assertEquals(cause, policyDecodingException.getCause()); } diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java index 7f9bb403..bb9b542a 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 @@ -27,19 +27,38 @@ import java.lang.reflect.Field; import java.util.ArrayList; 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; 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; +/** + * Class to perform unit test of AbstractReceptionHandler. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ public class AbstractReceptionHandlerTest { - // These tests won't work any more because we use Parameter Service for starting the plugins. - // Will rewrite them while implementing AbstractReceptionHandler.inputRecieved() method. - // @Test + private static final String DISTRIBUTION_GROUP = "DummyDistributionGroup"; + private static final String DECODER_TYPE = "DummyDecoder"; + private static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyDecoder"; + private static final String DECODER_KEY = "DummyDecoderKey"; + private static final String FORWARDER_KEY = "DummyForwarderKey"; + private static final String FORWARDER_TYPE = "DummyForwarder"; + private static final String FORWARDER_CLASS_NAME = + "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder"; + + @Test public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -75,7 +94,7 @@ public class AbstractReceptionHandlerTest { assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2)); } - // @Test(expected = PolicyDecodingException.class) + @Test(expected = PolicyDecodingException.class) public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -87,7 +106,7 @@ public class AbstractReceptionHandlerTest { handler.inputReceived(new DummyPolicyInput()); } - // @Test(expected = PolicyDecodingException.class) + @Test(expected = PolicyDecodingException.class) public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -98,72 +117,23 @@ public class AbstractReceptionHandlerTest { handler.inputReceived(new DummyPolicyInput()); } - class DummyReceptionHandler extends AbstractReceptionHandler { - @Override - protected void initializeReception(final String parameterGroupName) {} - - @Override - public void destroy() {} - } - class DummyPolicyInput implements PolicyInput { } - class DummyPolicy1 implements Policy { - } - class DummyPolicy2 implements Policy { - } - - public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { - - private boolean canHandleValue; - private Collection<Policy> policesToReturn; - - public DummyDecoder(final boolean canHandleValue, final Collection<Policy> policesToReturn) { - this.canHandleValue = canHandleValue; - this.policesToReturn = policesToReturn; - } - @Override - public boolean canHandle(final PolicyInput policyInput) { - return canHandleValue; - } - - @Override - public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { - return policesToReturn; - } + class DummyPolicy1 implements Policy { } - public class DummyPolicyForwarder implements PolicyForwarder { - private int numberOfPoliciesReceived = 0; - private Collection<Policy> policiesReceived = new ArrayList<>(); - - @Override - public void forward(final Collection<Policy> policies) throws PolicyForwardingException { - numberOfPoliciesReceived += policies.size(); - policiesReceived.addAll(policies); - } - - public int getNumberOfPoliciesReceived() { - return numberOfPoliciesReceived; - } - - public boolean receivedPolicy(final Policy policy) { - return policiesReceived.contains(policy); - } + class DummyPolicy2 implements Policy { } - /** - * Only needed until code is added for instantiating plugins from paramater file - * - * @throws PolicyForwardingException - * @throws PolicyDecodingException - */ private void setUpPlugins(final AbstractReceptionHandler receptionHandler, final Collection<PolicyDecoder<PolicyInput, Policy>> decoders, final Collection<PolicyForwarder> forwarders) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyDecodingException, PolicyForwardingException { - final PluginHandler pluginHandler = new PluginHandler(""); + final PluginHandlerParameters pluginParameters = getPluginHandlerParameters(); + pluginParameters.setName(DISTRIBUTION_GROUP); + ParameterService.register(pluginParameters); + final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName()); final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders"); decodersField.setAccessible(true); @@ -176,6 +146,31 @@ public class AbstractReceptionHandlerTest { final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler"); pluginHandlerField.setAccessible(true); pluginHandlerField.set(receptionHandler, pluginHandler); + ParameterService.deregister(pluginParameters.getName()); + } + + private Map<String, PolicyDecoderParameters> getPolicyDecoders() { + final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>(); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME); + policyDecoders.put(DECODER_KEY, pDParameters); + return policyDecoders; + } + + private Map<String, PolicyForwarderParameters> getPolicyForwarders() { + final Map<String, PolicyForwarderParameters> policyForwarders = + new HashMap<String, PolicyForwarderParameters>(); + final PolicyForwarderParameters pFParameters = + new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME); + policyForwarders.put(FORWARDER_KEY, pFParameters); + return policyForwarders; + } + + private PluginHandlerParameters getPluginHandlerParameters() { + final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders(); + final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders(); + final PluginHandlerParameters pluginHandlerParameters = + new PluginHandlerParameters(policyDecoders, policyForwarders); + return pluginHandlerParameters; } } 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 new file mode 100644 index 00000000..2de1737e --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling; + +import java.util.Collection; + +import org.onap.policy.distribution.model.Policy; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * Class to create a dummy decoder for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> { + + private boolean canHandleValue; + private Collection<Policy> policesToReturn; + + public DummyDecoder() { + this.canHandleValue = false; + this.policesToReturn = null; + } + + public DummyDecoder(final boolean canHandleValue, final Collection<Policy> policesToReturn) { + this.canHandleValue = canHandleValue; + this.policesToReturn = policesToReturn; + } + + @Override + public boolean canHandle(final PolicyInput policyInput) { + return canHandleValue; + } + + @Override + public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException { + return policesToReturn; + } +} diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java new file mode 100644 index 00000000..acee4697 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java @@ -0,0 +1,52 @@ +/*- + * ============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.handling; + +import java.util.ArrayList; +import java.util.Collection; + +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.model.Policy; + +/** + * Class to create a dummy forwarder for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicyForwarder implements PolicyForwarder { + private int numberOfPoliciesReceived = 0; + private Collection<Policy> policiesReceived = new ArrayList<>(); + + @Override + public void forward(final Collection<Policy> policies) throws PolicyForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + public boolean receivedPolicy(final Policy policy) { + return policiesReceived.contains(policy); + } +} diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java new file mode 100644 index 00000000..43596ff2 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java @@ -0,0 +1,35 @@ +/*- + * ============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.handling; + +/** + * Class to create a dummy reception handler for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +class DummyReceptionHandler extends AbstractReceptionHandler { + + @Override + protected void initializeReception(final String parameterGroupName) {} + + @Override + public void destroy() {} +} |