From f0944b0dffa4904161cf40d5da13d5d2d34b0cbe Mon Sep 17 00:00:00 2001 From: liboNet Date: Wed, 27 Mar 2019 00:49:32 +0800 Subject: add SDC client and reception handle . add reception framework wraps handle and subplug interface . add plugins include sdc handler now. . add forward acts the post interface for specified item like artifact . add model acts for the sepcifeid item like CSAR . update pom.xml and package/pom.xml to includes all submodules . Fix the compile issue by typo and change groupid to framework Change-Id: Idbff0ca612045c6ee52ba23cd305f2764c9600f8 Issue-ID: MULTICLOUD-514 Signed-off-by: liboNet --- .../decoding/PluginInitializationException.java | 51 +++++++ .../decoding/PluginTerminationException.java | 51 +++++++ .../decoding/PolicyDecodingException.java | 49 +++++++ .../handling/AbstractReceptionHandler.java | 89 ++++++++++++ .../reception/handling/PluginHandler.java | 88 ++++++++++++ .../reception/handling/ReceptionHandler.java | 46 ++++++ .../parameters/PluginHandlerParameters.java | 94 ++++++++++++ ...eceptionHandlerConfigurationParameterGroup.java | 44 ++++++ ...nHandlerConfigurationParametersJsonAdapter.java | 80 +++++++++++ .../parameters/ReceptionHandlerParameters.java | 147 +++++++++++++++++++ .../statistics/DistributionStatisticsManager.java | 160 +++++++++++++++++++++ .../PluginInitializationExceptionTest.java | 50 +++++++ .../decoding/PluginTerminationExceptionTest.java | 49 +++++++ .../decoding/PolicyDecodingExceptionTest.java | 43 ++++++ .../reception/handling/DummyArtifactForwarder.java | 54 +++++++ .../reception/handling/DummyReceptionHandler.java | 35 +++++ 16 files changed, 1130 insertions(+) create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginInitializationException.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginTerminationException.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingException.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParameterGroup.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParametersJsonAdapter.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java create mode 100644 artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/statistics/DistributionStatisticsManager.java create mode 100644 artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginInitializationExceptionTest.java create mode 100644 artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginTerminationExceptionTest.java create mode 100644 artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java create mode 100644 artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyArtifactForwarder.java create mode 100644 artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java (limited to 'artifactbroker/reception/src') diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginInitializationException.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginInitializationException.java new file mode 100644 index 0000000..4dd36a7 --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginInitializationException.java @@ -0,0 +1,51 @@ +/*- + * ============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; + +/** + * This exception will be called if an error occurs while initializing distribution plugins. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PluginInitializationException extends Exception { + + private static final long serialVersionUID = 3809376274411309160L; + + /** + * Construct an instance with the given message. + * + * @param message the error message + */ + public PluginInitializationException(final String message) { + super(message); + } + + /** + * Construct an instance with the given message and cause. + * + * @param message the error message + * @param cause the cause + */ + public PluginInitializationException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginTerminationException.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginTerminationException.java new file mode 100644 index 0000000..be9f51c --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PluginTerminationException.java @@ -0,0 +1,51 @@ +/*- + * ============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; + +/** + * This exception will be called if an error occurs while terminating distribution plugins. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PluginTerminationException extends Exception { + + private static final long serialVersionUID = 3809376274411309160L; + + /** + * Construct an instance with the given message. + * + * @param message the error message + */ + public PluginTerminationException(final String message) { + super(message); + } + + /** + * Construct an instance with the given message and cause. + * + * @param message the error message + * @param cause the cause + */ + public PluginTerminationException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingException.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingException.java new file mode 100644 index 0000000..5f2923d --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingException.java @@ -0,0 +1,49 @@ +/*- + * ============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; + +/** + * An error has occured while decoding a policy. + */ +public class PolicyDecodingException extends Exception { + + private static final long serialVersionUID = 3809376274411309160L; + + /** + * Construct an instance with the given message. + * + * @param message the error message + */ + public PolicyDecodingException(String message) { + super(message); + } + + /** + * Construct an instance with the given message and cause. + * + * @param message the error message + * @param cause the cause + */ + public PolicyDecodingException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java new file mode 100644 index 0000000..7081dc7 --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandler.java @@ -0,0 +1,89 @@ +/*- + * ============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.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.ArtifactForwarder; +import org.onap.policy.distribution.forwarding.ArtifactForwardingException; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PluginInitializationException; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; + +import org.onap.sdc.api.notification.IArtifactInfo; +/** + * 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 Logger LOGGER = FlexLogger.getLogger(AbstractReceptionHandler.class); + + private PluginHandler pluginHandler; + + /** + * {@inheritDoc}. + */ + @Override + public void initialize(final String parameterGroupName) throws PluginInitializationException { + final ReceptionHandlerParameters receptionHandlerParameters = ParameterService.get(parameterGroupName); + pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName()); + initializeReception(receptionHandlerParameters.getReceptionHandlerConfigurationName()); + } + + /** + * 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 + * @throws PluginInitializationException if initialization of reception handler fails + */ + protected abstract void initializeReception(String parameterGroupName) throws PluginInitializationException; + + /** + * Handle input that has been received. The given input shall be decoded using the s configured + * for this reception handler and forwarded using the {@link ArtifactForwarder}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 + */ + protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException { + + final Collection policies = new ArrayList<>(); + + for (final ArtifactForwarder policyForwarder : pluginHandler.getArtifactForwarders()) { + try { + policyForwarder.forward(policies); + } catch (final ArtifactForwardingException policyForwardingException) { + LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException); + } + } + } + + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java new file mode 100644 index 0000000..e7e77ba --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java @@ -0,0 +1,88 @@ +/*- + * ============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 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.ArtifactForwarder; +import org.onap.policy.distribution.forwarding.parameters.ArtifactForwarderParameters; +import org.onap.policy.distribution.reception.decoding.PluginInitializationException; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; + +/** + * Handles the plugins to policy distribution. + */ +public class PluginHandler { + + private static final Logger LOGGER = FlexLogger.getLogger(PluginHandler.class); + + private Collection artifactForwarders; + + /** + * Create an instance to instantiate plugins based on the given parameter group. + * + * @param parameterGroupName the name of the parameter group + * @throws PluginInitializationException exception if it occurs + */ + public PluginHandler(final String parameterGroupName) throws PluginInitializationException { + final PluginHandlerParameters params = ParameterService.get(parameterGroupName); + initArtifactForwarders(params.getArtifactForwarders()); + } + + /** + * Get the policy forwarders. + * + * @return the policy forwarders + */ + public Collection getArtifactForwarders() { + return artifactForwarders; + } + + /** + * Initialize policy forwarders. + * + * @param artifactForwarderParameters exception if it occurs + * @throws PluginInitializationException exception if it occurs + */ + @SuppressWarnings("unchecked") + private void initArtifactForwarders(final Map artifactForwarderParameters) + throws PluginInitializationException { + artifactForwarders = new ArrayList<>(); + for (final ArtifactForwarderParameters forwarderParameters : artifactForwarderParameters.values()) { + try { + final Class artifactForwarderClass = + (Class) Class.forName(forwarderParameters.getForwarderClassName()); + final ArtifactForwarder artifactForwarder = artifactForwarderClass.newInstance(); + artifactForwarder.configure(forwarderParameters.getForwarderConfigurationName()); + artifactForwarders.add(artifactForwarder); + } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) { + LOGGER.error("exception occured while initializing forwarders", exp); + throw new PluginInitializationException(exp.getMessage(), exp.getCause()); + } + } + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java new file mode 100644 index 0000000..3678c69 --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/handling/ReceptionHandler.java @@ -0,0 +1,46 @@ +/*- + * ============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 org.onap.policy.distribution.reception.decoding.PluginInitializationException; +import org.onap.policy.distribution.reception.decoding.PluginTerminationException; + +/** + * Handles input into Policy Distribution which may be decoded into a Policy. + */ +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 + * @throws PluginInitializationException exception if it occurs + */ + void initialize(String parameterGroupName) throws PluginInitializationException; + + /** + * Destroy the reception handler, removing any subscriptions and releasing all resources. + * + * @throws PluginTerminationException if it occurs + */ + void destroy() throws PluginTerminationException; + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.java new file mode 100644 index 0000000..70ab18e --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/PluginHandlerParameters.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.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.ArtifactForwarderParameters; + +/** + * 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 artifactForwarders; + + /** + * Constructor for instantiating PluginHandlerParameters. + * + * @param artifactForwarders the map of policy forwarders + */ + public PluginHandlerParameters( + final Map artifactForwarders) { + this.artifactForwarders = artifactForwarders; + } + + /** + * Return the artifactForwarders of this PluginHandlerParameters instance. + * + * @return the artifactForwarders + */ + public Map getArtifactForwarders() { + return artifactForwarders; + } + + @Override + public String getName() { + return name + PLUGIN_HANDLER; + } + + /** + * Validate the plugin handler parameters. + * + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (artifactForwarders == null || artifactForwarders.size() == 0) { + validationResult.setResult("artifactForwarders", ValidationStatus.INVALID, + "must have at least one policy forwarder"); + } else { + for (final Entry nestedGroupEntry : artifactForwarders.entrySet()) { + validationResult.setResult("artifactForwarders", nestedGroupEntry.getKey(), + nestedGroupEntry.getValue().validate()); + } + } + return validationResult; + } + + /** + * Set the name of this group. + * + * @param name the name to set. + */ + public void setName(final String name) { + this.name = name; + } +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParameterGroup.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParameterGroup.java new file mode 100644 index 0000000..3fa877d --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParameterGroup.java @@ -0,0 +1,44 @@ +/*- + * ============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.handling.ReceptionHandler; + +/** + * Base class of all {@link ParameterGroup} classes for configuration parameters for + * {@link ReceptionHandler} classes. + */ +public abstract class ReceptionHandlerConfigurationParameterGroup implements ParameterGroup { + + private String name; + + @Override + public String getName() { + return name; + } + + @Override + public void setName(final String name) { + this.name = name; + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParametersJsonAdapter.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParametersJsonAdapter.java new file mode 100644 index 0000000..0890a2a --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerConfigurationParametersJsonAdapter.java @@ -0,0 +1,80 @@ +/*- + * ============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 reception handler parameters from JSON. + */ +public class ReceptionHandlerConfigurationParametersJsonAdapter + implements JsonDeserializer { + private static final XLogger LOGGER = + XLoggerFactory.getXLogger(ReceptionHandlerConfigurationParametersJsonAdapter.class); + + private static final String PARAMETER_CLASS_NAME = "parameterClassName"; + private static final String RECEPTION_HANDLER_PARAMETERS = "parameters"; + + @Override + public ReceptionHandlerConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) { + final JsonObject jsonObject = json.getAsJsonObject(); + + final String receptionHandlerParameterClassName = getParameterGroupClassName(jsonObject); + Class receptionHandlerParameterClass = getParameterGroupClass(receptionHandlerParameterClassName); + + return context.deserialize(jsonObject.get(RECEPTION_HANDLER_PARAMETERS), receptionHandlerParameterClass); + } + + 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 receptionHAndlerParameterClassName) { + Class receptionHandlerParameterClass = null; + try { + receptionHandlerParameterClass = Class.forName(receptionHAndlerParameterClassName); + } catch (final ClassNotFoundException e) { + final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \"" + + receptionHAndlerParameterClassName + "\", could not find class"; + LOGGER.warn(errorMessage, e); + throw new IllegalArgumentException(errorMessage, e); + } + return receptionHandlerParameterClass; + } + +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java new file mode 100644 index 0000000..3839c44 --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/parameters/ReceptionHandlerParameters.java @@ -0,0 +1,147 @@ +/*- + * ============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.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; + +/** + * Class to hold all the reception handler parameters. + * + * @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; + private String receptionHandlerConfigurationName; + private PluginHandlerParameters pluginHandlerParameters; + + /** + * Constructor for instantiating ReceptionHandlerParameters. + * + * @param receptionHandlerType the reception handler type + * @param receptionHandlerClassName the reception handler class name + * @param receptionHandlerConfigurationName the name of the configuration for the reception + * handler + * @param pluginHandlerParameters the plugin handler parameters + */ + public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName, + final String receptionHandlerConfigurationName, final PluginHandlerParameters pluginHandlerParameters) { + this.receptionHandlerType = receptionHandlerType; + this.receptionHandlerClassName = receptionHandlerClassName; + this.receptionHandlerConfigurationName = receptionHandlerConfigurationName; + 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 name of the reception handler configuration for this ReceptionHandlerParameters + * instance. + * + * @return the PssdConfigurationParametersGroup + */ + public String getReceptionHandlerConfigurationName() { + return receptionHandlerConfigurationName; + } + + /** + * 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 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 + */ + @Override + public void setName(final String name) { + this.name = name; + } +} diff --git a/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/statistics/DistributionStatisticsManager.java b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/statistics/DistributionStatisticsManager.java new file mode 100644 index 0000000..3b66ee1 --- /dev/null +++ b/artifactbroker/reception/src/main/java/org/onap/policy/distribution/reception/statistics/DistributionStatisticsManager.java @@ -0,0 +1,160 @@ +/*- + * ============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.statistics; + +/** + * Class to hold statistical data for distribution component. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DistributionStatisticsManager { + + private static long totalDistributionCount; + private static long distributionSuccessCount; + private static long distributionFailureCount; + private static long totalDownloadCount; + private static long downloadSuccessCount; + private static long downloadFailureCount; + + private DistributionStatisticsManager() { + throw new IllegalStateException("Instantiation of the class is not allowed"); + } + + /** + * Method to update the total distribution count. + * + * @return the updated value of totalDistributionCount + */ + public static long updateTotalDistributionCount() { + return ++totalDistributionCount; + } + + /** + * Method to update the distribution success count. + * + * @return the updated value of distributionSuccessCount + */ + public static long updateDistributionSuccessCount() { + return ++distributionSuccessCount; + } + + /** + * Method to update the distribution failure count. + * + * @return the updated value of distributionFailureCount + */ + public static long updateDistributionFailureCount() { + return ++distributionFailureCount; + } + + /** + * Method to update the total download count. + * + * @return the updated value of totalDownloadCount + */ + public static long updateTotalDownloadCount() { + return ++totalDownloadCount; + } + + /** + * Method to update the download success count. + * + * @return the updated value of downloadSuccessCount + */ + public static long updateDownloadSuccessCount() { + return ++downloadSuccessCount; + } + + /** + * Method to update the download failure count. + * + * @return the updated value of downloadFailureCount + */ + public static long updateDownloadFailureCount() { + return ++downloadFailureCount; + } + + /** + * Returns the current value of totalDistributionCount. + * + * @return the totalDistributionCount + */ + public static long getTotalDistributionCount() { + return totalDistributionCount; + } + + /** + * Returns the current value of distributionSuccessCount. + * + * @return the distributionSuccessCount + */ + public static long getDistributionSuccessCount() { + return distributionSuccessCount; + } + + /** + * Returns the current value of distributionFailureCount. + * + * @return the distributionFailureCount + */ + public static long getDistributionFailureCount() { + return distributionFailureCount; + } + + /** + * Returns the current value of totalDownloadCount. + * + * @return the totalDownloadCount + */ + public static long getTotalDownloadCount() { + return totalDownloadCount; + } + + /** + * Returns the current value of downloadSuccessCount. + * + * @return the downloadSuccessCount + */ + public static long getDownloadSuccessCount() { + return downloadSuccessCount; + } + + /** + * Returns the current value of downloadFailureCount. + * + * @return the downloadFailureCount + */ + public static long getDownloadFailureCount() { + return downloadFailureCount; + } + + /** + * Reset all the statistics counts to 0. + */ + public static void resetAllStatistics() { + totalDistributionCount = 0L; + distributionSuccessCount = 0L; + distributionFailureCount = 0L; + totalDownloadCount = 0L; + downloadSuccessCount = 0L; + downloadFailureCount = 0L; + } +} diff --git a/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginInitializationExceptionTest.java b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginInitializationExceptionTest.java new file mode 100644 index 0000000..b63e667 --- /dev/null +++ b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginInitializationExceptionTest.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.decoding; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Class to perform unit test of {@link PluginInitializationException}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PluginInitializationExceptionTest { + + @Test + public void testPluginInitializationExceptionString() { + final PluginInitializationException pluginInitializationException = + new PluginInitializationException("error message"); + assertEquals("error message", pluginInitializationException.getMessage()); + } + + @Test + public void testPluginInitializationExceptionStringThrowable() { + final Exception cause = new IllegalArgumentException(); + final PluginInitializationException pluginInitializationException = + new PluginInitializationException("error message", cause); + assertEquals("error message", pluginInitializationException.getMessage()); + assertEquals(cause, pluginInitializationException.getCause()); + } + +} diff --git a/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginTerminationExceptionTest.java b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginTerminationExceptionTest.java new file mode 100644 index 0000000..c2fc6a9 --- /dev/null +++ b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PluginTerminationExceptionTest.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.decoding; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Class to perform unit test of {@link PluginTerminationException}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class PluginTerminationExceptionTest { + + @Test + public void testPluginTerminationExceptionString() { + final PluginTerminationException pluginTerminationException = new PluginTerminationException("error message"); + assertEquals("error message", pluginTerminationException.getMessage()); + } + + @Test + public void testPluginTerminationExceptionStringThrowable() { + final Exception cause = new IllegalArgumentException(); + final PluginTerminationException pluginTerminationException = new PluginTerminationException("error message", + cause); + assertEquals("error message", pluginTerminationException.getMessage()); + assertEquals(cause, pluginTerminationException.getCause()); + } + +} diff --git a/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java new file mode 100644 index 0000000..a5bc072 --- /dev/null +++ b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.decoding; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class PolicyDecodingExceptionTest { + + @Test + public void testPolicyDecodingExceptionString() { + final PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message"); + assertEquals("error message", policyDecodingException.getMessage()); + } + + @Test + public void testPolicyDecodingExceptionStringThrowable() { + 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/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyArtifactForwarder.java b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyArtifactForwarder.java new file mode 100644 index 0000000..5785927 --- /dev/null +++ b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyArtifactForwarder.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.reception.handling; + +import java.util.ArrayList; +import java.util.Collection; +import org.onap.policy.distribution.forwarding.ArtifactForwarder; +import org.onap.policy.distribution.forwarding.ArtifactForwardingException; +import org.onap.sdc.api.notification.IArtifactInfo; + +/** + * Class to create a dummy forwarder for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyArtifactForwarder implements ArtifactForwarder { + private int numberOfPoliciesReceived = 0; + private Collection policiesReceived = new ArrayList<>(); + + @Override + public void forward(final Collection policies) throws ArtifactForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + public boolean receivedPolicy(final IArtifactInfo policy) { + return policiesReceived.contains(policy); + } + + @Override + public void configure(String parameterGroupName) {} +} diff --git a/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java b/artifactbroker/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java new file mode 100644 index 0000000..43596ff --- /dev/null +++ b/artifactbroker/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() {} +} -- cgit 1.2.3-korg