8ad293c3e0351820c540722bcf9e2c0a7bbaaa3a
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
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 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.PolicyInput;
31 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
32 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
33 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
34 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base class by implementing
41  * the {@link #initializeReception(String)} method to perform the specific initialization required to receive inputs and
42  * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input.
43  */
44 public abstract class AbstractReceptionHandler implements ReceptionHandler {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReceptionHandler.class);
47
48     private PluginHandler pluginHandler;
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public void initialize(final String parameterGroupName) throws PluginInitializationException {
55         final var receptionHandlerParameters = (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 receive inputs, for
62      * 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 {@link PolicyDecoder}s configured
71      * for this reception handler and forwarded using the {@link PolicyForwarder}s configured for this reception
72      * 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 input
76      */
77     protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
78
79         final Collection<ToscaEntity> policies = new ArrayList<>();
80         for (final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder : getRelevantPolicyDecoders(policyInput)) {
81             policies.addAll(policyDecoder.decode(policyInput));
82         }
83
84         for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) {
85             try {
86                 policyForwarder.forward(policies);
87             } catch (final PolicyForwardingException policyForwardingException) {
88                 LOGGER.error("Error when forwarding policies to {}", policyForwarder, policyForwardingException);
89             }
90         }
91     }
92
93     private Collection<PolicyDecoder<PolicyInput, ToscaEntity>> getRelevantPolicyDecoders(final PolicyInput policyInput)
94             throws PolicyDecodingException {
95         final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> relevantPolicyDecoders = new ArrayList<>();
96         for (final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder : pluginHandler.getPolicyDecoders()) {
97             if (policyDecoder.canHandle(policyInput)) {
98                 relevantPolicyDecoders.add(policyDecoder);
99             }
100         }
101         if (relevantPolicyDecoders.isEmpty()) {
102             throw new PolicyDecodingException("No decoder available matching requirements");
103         }
104         return relevantPolicyDecoders;
105     }
106
107 }