300dae8fcaf26735912b9f9f2e324c65c7ef2668
[policy/distribution.git] /
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 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.common.parameters.ParameterService;
28 import org.onap.policy.distribution.forwarding.PolicyForwarder;
29 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
30 import org.onap.policy.distribution.model.Policy;
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.PolicyDecoder;
34 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
35 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters;
36
37 /**
38  * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base
39  * class by implementing the {@link #initializeReception(String)} method to perform the specific
40  * initialization required to receive inputs and by invoking {@link #inputReceived(PolicyInput)}
41  * when the reception handler receives input.
42  */
43 public abstract class AbstractReceptionHandler implements ReceptionHandler {
44
45     private static final Logger LOGGER = FlexLogger.getLogger(AbstractReceptionHandler.class);
46
47     private PluginHandler pluginHandler;
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public void initialize(final String parameterGroupName) throws PluginInitializationException {
54         final ReceptionHandlerParameters receptionHandlerParameters =
55                 (ReceptionHandlerParameters) ParameterService.get(parameterGroupName);
56         pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName());
57         initializeReception(receptionHandlerParameters.getReceptionHandlerConfigurationName());
58     }
59
60     /**
61      * Sub classes must implement this method to perform the specific initialization required to
62      * receive inputs, for example setting up subscriptions.
63      *
64      * @param parameterGroupName the parameter group name
65      * @throws PluginInitializationException if initialization of reception handler fails
66      */
67     protected abstract void initializeReception(String parameterGroupName) throws PluginInitializationException;
68
69     /**
70      * Handle input that has been received. The given input shall be decoded using the
71      * {@link PolicyDecoder}s configured for this reception handler and forwarded using the
72      * {@link PolicyForwarder}s configured for this reception handler.
73      *
74      * @param policyInput the input that has been received
75      * @throws PolicyDecodingException if an error occurs in decoding a policy from the received
76      *         input
77      */
78     protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
79
80         final Collection<Policy> policies = new ArrayList<>();
81         for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : getRelevantPolicyDecoders(policyInput)) {
82             policies.addAll(policyDecoder.decode(policyInput));
83         }
84
85         for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) {
86             try {
87                 policyForwarder.forward(policies);
88             } catch (final PolicyForwardingException policyForwardingException) {
89                 LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException);
90             }
91         }
92     }
93
94     private Collection<PolicyDecoder<PolicyInput, Policy>> getRelevantPolicyDecoders(final PolicyInput policyInput)
95             throws PolicyDecodingException {
96         final Collection<PolicyDecoder<PolicyInput, Policy>> relevantPolicyDecoders = new ArrayList<>();
97         for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : pluginHandler.getPolicyDecoders()) {
98             if (policyDecoder.canHandle(policyInput)) {
99                 relevantPolicyDecoders.add(policyDecoder);
100             }
101         }
102         if (relevantPolicyDecoders.isEmpty()) {
103             throw new PolicyDecodingException("No decoder available matching requirements");
104         }
105         return relevantPolicyDecoders;
106     }
107
108 }