2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.reception.handling;
24 import java.util.ArrayList;
25 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;
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.
44 public abstract class AbstractReceptionHandler implements ReceptionHandler {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReceptionHandler.class);
48 private PluginHandler pluginHandler;
54 public void initialize(final String parameterGroupName) throws PluginInitializationException {
55 final ReceptionHandlerParameters receptionHandlerParameters = ParameterService.get(parameterGroupName);
56 pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName());
57 initializeReception(receptionHandlerParameters.getReceptionHandlerConfigurationName());
61 * Sub classes must implement this method to perform the specific initialization required to receive inputs, for
62 * example setting up subscriptions.
64 * @param parameterGroupName the parameter group name
65 * @throws PluginInitializationException if initialization of reception handler fails
67 protected abstract void initializeReception(String parameterGroupName) throws PluginInitializationException;
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
74 * @param policyInput the input that has been received
75 * @throws PolicyDecodingException if an error occurs in decoding a policy from the received input
77 protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
79 final Collection<ToscaEntity> policies = new ArrayList<>();
80 for (final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder : getRelevantPolicyDecoders(policyInput)) {
81 policies.addAll(policyDecoder.decode(policyInput));
84 for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) {
86 policyForwarder.forward(policies);
87 } catch (final PolicyForwardingException policyForwardingException) {
88 LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException);
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);
101 if (relevantPolicyDecoders.isEmpty()) {
102 throw new PolicyDecodingException("No decoder available matching requirements");
104 return relevantPolicyDecoders;