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