9bbbad3625fdba13aad661508bce39c35313caa2
[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.ApexEventException;
24 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
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 producers for the defined technology type for Apex engines.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class EventProducerFactory {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProducerFactory.class);
38
39     /**
40      * Empty constructor with no generic overloading.
41      */
42     public EventProducerFactory() {}
43
44     /**
45      * Create an event producer of the required type for the specified producer technology.
46      *
47      * @param name the name of the producer
48      * @param producerParameters The Apex parameters containing the configuration for the producer
49      * @return the event producer
50      * @throws ApexEventException on errors creating the Apex event producer
51      */
52     public ApexEventProducer createProducer(final String name, final EventHandlerParameters producerParameters)
53             throws ApexEventException {
54         // Get the carrier technology parameters
55         final CarrierTechnologyParameters technologyParameters = producerParameters.getCarrierTechnologyParameters();
56
57         // Get the class for the event producer using reflection
58         final String producerPluginClass = technologyParameters.getEventProducerPluginClass();
59         Object producerPluginObject = null;
60         try {
61             producerPluginObject = Class.forName(producerPluginClass).newInstance();
62         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
63             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
64                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
65                     + "\", specified event producer plugin class \"" + producerPluginClass + "\" not found";
66             LOGGER.error(errorMessage, e);
67             throw new ApexEventException(errorMessage, e);
68         }
69
70         // Check the class is an event producer
71         if (!(producerPluginObject instanceof ApexEventProducer)) {
72             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
73                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
74                     + "\", specified event producer plugin class \"" + producerPluginClass
75                     + "\" is not an instance of \"" + ApexEventProducer.class.getCanonicalName() + "\"";
76             LOGGER.error(errorMessage);
77             throw new ApexEventException(errorMessage);
78         }
79
80         return (ApexEventProducer) producerPluginObject;
81     }
82 }