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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.handling;
23 import java.util.ArrayList;
24 import java.util.Collection;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
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.Policy;
32 import org.onap.policy.distribution.model.PolicyInput;
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;
38 * Base implementation of {@link ReceptionHandler}. All reception handlers should extend this base class by implementing
39 * the {@link #initializeReception(String)} method to perform the specific initialization required to receive inputs and
40 * by invoking {@link #inputReceived(PolicyInput)} when the reception handler receives input
42 public abstract class AbstractReceptionHandler implements ReceptionHandler {
44 private static final Logger LOGGER = FlexLogger.getLogger(AbstractReceptionHandler.class);
46 private PluginHandler pluginHandler;
49 public void initialize(final String parameterGroupName) throws PolicyDecodingException, PolicyForwardingException {
50 final ReceptionHandlerParameters receptionHandlerParameters =
51 (ReceptionHandlerParameters) ParameterService.get(parameterGroupName);
52 pluginHandler = new PluginHandler(receptionHandlerParameters.getPluginHandlerParameters().getName());
53 initializeReception(parameterGroupName);
57 * Sub classes must implement this method to perform the specific initialization required to receive inputs, for
58 * example setting up subscriptions
60 * @param parameterGroupName the parameter group name
62 protected abstract void initializeReception(String parameterGroupName);
65 * Handle input that has been received. The given input shall be decoded using the {@link PolicyDecoder}s configured
66 * for this reception handler and forwarded using the {@link PolicyForwarder}s configured for this reception
69 * @param policyInput the input that has been received
70 * @throws PolicyDecodingException if an error occurs in decoding a policy from the received input
72 protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
74 final Collection<Policy> policies = new ArrayList<>();
75 for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : getRelevantPolicyDecoders(policyInput)) {
76 policies.addAll(policyDecoder.decode(policyInput));
79 for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) {
81 policyForwarder.forward(policies);
82 } catch (final PolicyForwardingException policyForwardingException) {
83 LOGGER.error("Error when forwarding policies to " + policyForwarder, policyForwardingException);
88 private Collection<PolicyDecoder<PolicyInput, Policy>> getRelevantPolicyDecoders(final PolicyInput policyInput)
89 throws PolicyDecodingException {
90 final Collection<PolicyDecoder<PolicyInput, Policy>> relevantPolicyDecoders = new ArrayList<>();
91 for (final PolicyDecoder<PolicyInput, Policy> policyDecoder : pluginHandler.getPolicyDecoders()) {
92 if (policyDecoder.canHandle(policyInput)) {
93 relevantPolicyDecoders.add(policyDecoder);
96 if (relevantPolicyDecoders.isEmpty()) {
97 throw new PolicyDecodingException("No decoder available matching requirements");
99 return relevantPolicyDecoders;