enable merge sdc client part into Distribution release image
[multicloud/framework.git] / artifactbroker / reception / src / main / java / org / onap / policy / distribution / reception / handling / AbstractReceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.onap.policy.distribution.forwarding.ArtifactForwarder;
30 import org.onap.policy.distribution.forwarding.ArtifactForwardingException;
31 import org.onap.policy.distribution.model.PolicyInput;
32 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
33 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
34 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters;
35
36 import org.onap.sdc.api.notification.IArtifactInfo;
37 /**
38  * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base class by implementing
39  * the {@link #initializeReception(String)} method to perform the specific initialization required to receive inputs and
40  * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input.
41  */
42 public abstract class AbstractReceptionHandler implements ReceptionHandler {
43
44     private static final Logger LOGGER = FlexLogger.getLogger(AbstractReceptionHandler.class);
45
46     private PluginHandler pluginHandler;
47
48     /**
49      * {@inheritDoc}.
50      */
51     @Override
52     public void initialize(final String parameterGroupName) throws PluginInitializationException {
53         final ReceptionHandlerParameters receptionHandlerParameters = ParameterService.get(parameterGroupName);
54         pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName());
55         initializeReception(receptionHandlerParameters.getReceptionHandlerConfigurationName());
56         LOGGER.debug("Policy distribution , parameterGroupName = " + parameterGroupName);
57         LOGGER.debug("Policy distribution , pluginhandler name = " + receptionHandlerParameters.getPluginHandlerParameters().getName());
58         LOGGER.debug("Policy distribution , recetipionhandler name = " + receptionHandlerParameters.getReceptionHandlerConfigurationName());
59     }
60
61     /**
62      * Sub classes must implement this method to perform the specific initialization required to receive inputs, for
63      * example setting up subscriptions.
64      *
65      * @param parameterGroupName the parameter group name
66      * @throws PluginInitializationException if initialization of reception handler fails
67      */
68     protected abstract void initializeReception(String parameterGroupName) throws PluginInitializationException;
69
70     /**
71      * Handle input that has been received. The given input shall be decoded using the s configured
72      * for this reception handler and forwarded using the {@link ArtifactForwarder}s configured for this reception
73      * handler.
74      *
75      * @param policyInput the input that has been received
76      * @throws PolicyDecodingException if an error occurs in decoding a policy from the received input
77      */
78     protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
79
80
81         for (final ArtifactForwarder policyForwarder : pluginHandler.getArtifactForwarders()) {
82             try {
83                 policyForwarder.forward(policyInput);
84             } catch (final ArtifactForwardingException policyForwardingException) {
85                 LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException);
86             }
87         }
88     }
89
90
91 }