727f77995043eee2b2adb7c00c84d2bc688da5e7
[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      * Create an event producer of the required type for the specified producer technology.
41      *
42      * @param name the name of the producer
43      * @param producerParameters The Apex parameters containing the configuration for the producer
44      * @return the event producer
45      * @throws ApexEventException on errors creating the Apex event producer
46      */
47     public ApexEventProducer createProducer(final String name, final EventHandlerParameters producerParameters)
48             throws ApexEventException {
49         // Get the carrier technology parameters
50         final CarrierTechnologyParameters technologyParameters = producerParameters.getCarrierTechnologyParameters();
51
52         // Get the class for the event producer using reflection
53         final String producerPluginClass = technologyParameters.getEventProducerPluginClass();
54         Object producerPluginObject = null;
55         try {
56             producerPluginObject = Class.forName(producerPluginClass).newInstance();
57         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
58             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
59                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
60                     + "\", specified event producer plugin class \"" + producerPluginClass + "\" not found";
61             LOGGER.error(errorMessage, e);
62             throw new ApexEventException(errorMessage, e);
63         }
64
65         // Check the class is an event producer
66         if (!(producerPluginObject instanceof ApexEventProducer)) {
67             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
68                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
69                     + "\", specified event producer plugin class \"" + producerPluginClass
70                     + "\" is not an instance of \"" + ApexEventProducer.class.getCanonicalName() + "\"";
71             LOGGER.error(errorMessage);
72             throw new ApexEventException(errorMessage);
73         }
74
75         return (ApexEventProducer) producerPluginObject;
76     }
77 }