add SDC client and reception handle
[multicloud/framework.git] / artifactbroker / reception / src / main / java / org / onap / policy / distribution / reception / handling / PluginHandler.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Map;
26
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;
34
35 /**
36  * Handles the plugins to policy distribution.
37  */
38 public class PluginHandler {
39
40     private static final Logger LOGGER = FlexLogger.getLogger(PluginHandler.class);
41
42     private Collection<ArtifactForwarder> artifactForwarders;
43
44     /**
45      * Create an instance to instantiate plugins based on the given parameter group.
46      *
47      * @param parameterGroupName the name of the parameter group
48      * @throws PluginInitializationException exception if it occurs
49      */
50     public PluginHandler(final String parameterGroupName) throws PluginInitializationException {
51         final PluginHandlerParameters params = ParameterService.get(parameterGroupName);
52         initArtifactForwarders(params.getArtifactForwarders());
53     }
54
55     /**
56      * Get the policy forwarders.
57      *
58      * @return the policy forwarders
59      */
60     public Collection<ArtifactForwarder> getArtifactForwarders() {
61         return artifactForwarders;
62     }
63
64     /**
65      * Initialize policy forwarders.
66      *
67      * @param artifactForwarderParameters exception if it occurs
68      * @throws PluginInitializationException exception if it occurs
69      */
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()) {
75             try {
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());
84             }
85         }
86     }
87
88 }