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;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.common.parameters.ParameterService;
30 import org.onap.policy.distribution.forwarding.ArtifactForwarder;
31 import org.onap.policy.distribution.forwarding.parameters.ArtifactForwarderParameters;
32 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
33 import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters;
36 * Handles the plugins to policy distribution.
38 public class PluginHandler {
40 private static final Logger LOGGER = FlexLogger.getLogger(PluginHandler.class);
42 private Collection<ArtifactForwarder> artifactForwarders;
45 * Create an instance to instantiate plugins based on the given parameter group.
47 * @param parameterGroupName the name of the parameter group
48 * @throws PluginInitializationException exception if it occurs
50 public PluginHandler(final String parameterGroupName) throws PluginInitializationException {
51 final PluginHandlerParameters params = ParameterService.get(parameterGroupName);
52 initArtifactForwarders(params.getArtifactForwarders());
56 * Get the policy forwarders.
58 * @return the policy forwarders
60 public Collection<ArtifactForwarder> getArtifactForwarders() {
61 return artifactForwarders;
65 * Initialize policy forwarders.
67 * @param artifactForwarderParameters exception if it occurs
68 * @throws PluginInitializationException exception if it occurs
70 @SuppressWarnings("unchecked")
71 private void initArtifactForwarders(final Map<String, ArtifactForwarderParameters> artifactForwarderParameters)
72 throws PluginInitializationException {
73 artifactForwarders = new ArrayList<>();
74 for (final ArtifactForwarderParameters forwarderParameters : artifactForwarderParameters.values()) {
76 final Class<ArtifactForwarder> artifactForwarderClass =
77 (Class<ArtifactForwarder>) Class.forName(forwarderParameters.getForwarderClassName());
78 final ArtifactForwarder artifactForwarder = artifactForwarderClass.newInstance();
79 artifactForwarder.configure(forwarderParameters.getForwarderConfigurationName());
80 artifactForwarders.add(artifactForwarder);
81 } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException exp) {
82 LOGGER.error("exception occured while initializing forwarders", exp);
83 throw new PluginInitializationException(exp.getMessage(), exp.getCause());