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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  19  * SPDX-License-Identifier: Apache-2.0
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.policy.distribution.reception.handling;
 
  25 import java.util.ArrayList;
 
  26 import java.util.Collection;
 
  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;
 
  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.
 
  45 public abstract class AbstractReceptionHandler implements ReceptionHandler {
 
  47     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReceptionHandler.class);
 
  49     private PluginHandler pluginHandler;
 
  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());
 
  62      * Sub classes must implement this method to perform the specific initialization required to receive inputs, for
 
  63      * example setting up subscriptions.
 
  65      * @param parameterGroupName the parameter group name
 
  66      * @throws PluginInitializationException if initialization of reception handler fails
 
  68     protected abstract void initializeReception(String parameterGroupName) throws PluginInitializationException;
 
  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
 
  75      * @param policyInput the input that has been received
 
  76      * @throws PolicyDecodingException if an error occurs in decoding a policy from the received input
 
  78     protected void inputReceived(final PolicyInput policyInput) throws PolicyDecodingException {
 
  80         final Collection<ToscaEntity> policies = new ArrayList<>();
 
  81         for (final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder : getRelevantPolicyDecoders(policyInput)) {
 
  82             policies.addAll(policyDecoder.decode(policyInput));
 
  85         for (final PolicyForwarder policyForwarder : pluginHandler.getPolicyForwarders()) {
 
  87                 policyForwarder.forward(policies);
 
  88             } catch (final PolicyForwardingException policyForwardingException) {
 
  89                 LOGGER.error("Error when forwarding policies to {}", policyForwarder, policyForwardingException);
 
  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);
 
 102         if (relevantPolicyDecoders.isEmpty()) {
 
 103             throw new PolicyDecodingException("No decoder available matching requirements");
 
 105         return relevantPolicyDecoders;