52c34cecf60a5b0ee5349c5470697b1ab2702ce5
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Copyright (C) 2020 AT&T Inc.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.handling;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.onap.policy.distribution.forwarding.PolicyForwarder;
30 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
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 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base class by implementing
42  * the {@link #initializeReception(String)} method to perform the specific initialization required to receive inputs and
43  * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input.
44  */
45 public abstract class AbstractReceptionHandler implements ReceptionHandler {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReceptionHandler.class);
48
49     private PluginHandler pluginHandler;
50
51     /**
52      * {@inheritDoc}.
53      */
54     @Override
55     public void initialize(final String parameterGroupName) throws PluginInitializationException {
56         final ReceptionHandlerParameters receptionHandlerParameters = ParameterService.get(parameterGroupName);
57         pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName());
58         initializeReception(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 {@link PolicyDecoder}s configured
72      * for this reception handler and forwarded using the {@link PolicyForwarder}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         final Collection<ToscaEntity> policies = new ArrayList<>();
81         for (final PolicyDecoder<PolicyInput, ToscaEntity> 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, ToscaEntity>> getRelevantPolicyDecoders(final PolicyInput policyInput)
95             throws PolicyDecodingException {
96         final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> relevantPolicyDecoders = new ArrayList<>();
97         for (final PolicyDecoder<PolicyInput, ToscaEntity> 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 }