5c44f2d7de84e12dff9cfc2113a668443e13e05d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.apex.service.engine.event.impl;
22
23 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
24 import org.onap.policy.apex.service.engine.event.ApexEventException;
25 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
26 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This factory class creates event consumers of various technology types for Apex engines.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class EventConsumerFactory {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventConsumerFactory.class);
38
39     /**
40      * Create an event consumer of the required type for the specified consumer technology.
41      *
42      * @param name the name of the consumer
43      * @param consumerParameters The parameters for the Apex engine, we use the technology type of
44      *        the required consumer
45      * @return the event consumer
46      * @throws ApexEventException on errors creating the Apex event consumer
47      */
48     public ApexEventConsumer createConsumer(final String name, final EventHandlerParameters consumerParameters)
49             throws ApexEventException {
50         // Get the carrier technology parameters
51         final CarrierTechnologyParameters technologyParameters = consumerParameters.getCarrierTechnologyParameters();
52
53         // Get the class for the event consumer using reflection
54         final String consumerPluginClass = technologyParameters.getEventConsumerPluginClass();
55         Object consumerPluginObject = null;
56         try {
57             consumerPluginObject = Class.forName(consumerPluginClass).newInstance();
58         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
59             final String errorMessage = "could not create an Apex event consumer for \"" + name
60                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
61                     + "\", specified event consumer plugin class \"" + consumerPluginClass + "\" not found";
62             LOGGER.error(errorMessage, e);
63             throw new ApexEventException(errorMessage, e);
64         }
65
66         // Check the class is an event consumer
67         if (!(consumerPluginObject instanceof ApexEventConsumer)) {
68             final String errorMessage = "could not create an Apex event consumer \"" + name
69                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
70                     + "\", specified event consumer plugin class \"" + consumerPluginClass
71                     + "\" is not an instance of \"" + ApexEventConsumer.class.getCanonicalName() + "\"";
72             LOGGER.error(errorMessage);
73             throw new ApexEventException(errorMessage);
74         }
75
76         return (ApexEventConsumer) consumerPluginObject;
77     }
78 }