8f54c049bfbc7150a8f9b9735ecd51cf430c7510
[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      * Empty constructor with no generic overloading.
41      */
42     public EventConsumerFactory() {}
43
44     /**
45      * Create an event consumer of the required type for the specified consumer technology.
46      *
47      * @param name the name of the consumer
48      * @param consumerParameters The parameters for the Apex engine, we use the technology type of
49      *        the required consumer
50      * @return the event consumer
51      * @throws ApexEventException on errors creating the Apex event consumer
52      */
53     public ApexEventConsumer createConsumer(final String name, final EventHandlerParameters consumerParameters)
54             throws ApexEventException {
55         // Get the carrier technology parameters
56         final CarrierTechnologyParameters technologyParameters = consumerParameters.getCarrierTechnologyParameters();
57
58         // Get the class for the event consumer using reflection
59         final String consumerPluginClass = technologyParameters.getEventConsumerPluginClass();
60         Object consumerPluginObject = null;
61         try {
62             consumerPluginObject = Class.forName(consumerPluginClass).newInstance();
63         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
64             final String errorMessage = "could not create an Apex event consumer for \"" + name
65                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
66                     + "\", specified event consumer plugin class \"" + consumerPluginClass + "\" not found";
67             LOGGER.error(errorMessage, e);
68             throw new ApexEventException(errorMessage, e);
69         }
70
71         // Check the class is an event consumer
72         if (!(consumerPluginObject instanceof ApexEventConsumer)) {
73             final String errorMessage = "could not create an Apex event consumer \"" + name
74                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
75                     + "\", specified event consumer plugin class \"" + consumerPluginClass
76                     + "\" is not an instance of \"" + ApexEventConsumer.class.getCanonicalName() + "\"";
77             LOGGER.error(errorMessage);
78             throw new ApexEventException(errorMessage);
79         }
80
81         return (ApexEventConsumer) consumerPluginObject;
82     }
83 }