e87e2fd78d450dbf4d1632b7adad029cc5949205
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.event.impl;
23
24 import org.onap.policy.apex.service.engine.event.ApexEventException;
25 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
26 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
27 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This factory class creates event producers for the defined technology type for Apex engines.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class EventProducerFactory {
37     // The logger for this class
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProducerFactory.class);
39
40     /**
41      * Create an event producer of the required type for the specified producer technology.
42      *
43      * @param name the name of the producer
44      * @param producerParameters The Apex parameters containing the configuration for the producer
45      * @return the event producer
46      * @throws ApexEventException on errors creating the Apex event producer
47      */
48     public ApexEventProducer createProducer(final String name, final EventHandlerParameters producerParameters)
49             throws ApexEventException {
50         // Get the carrier technology parameters
51         final CarrierTechnologyParameters technologyParameters = producerParameters.getCarrierTechnologyParameters();
52
53         // Get the class for the event producer using reflection
54         final String producerPluginClass = technologyParameters.getEventProducerPluginClass();
55         Object producerPluginObject = null;
56         try {
57             producerPluginObject = Class.forName(producerPluginClass).getDeclaredConstructor().newInstance();
58         } catch (final Exception e) {
59             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
60                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
61                     + "\", specified event producer plugin class \"" + producerPluginClass + "\" not found";
62             LOGGER.error(errorMessage, e);
63             throw new ApexEventException(errorMessage, e);
64         }
65
66         // Check the class is an event producer
67         if (!(producerPluginObject instanceof ApexEventProducer)) {
68             final String errorMessage = "could not create an Apex event producer for Producer \"" + name
69                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
70                     + "\", specified event producer plugin class \"" + producerPluginClass
71                     + "\" is not an instance of \"" + ApexEventProducer.class.getName() + "\"";
72             LOGGER.error(errorMessage);
73             throw new ApexEventException(errorMessage);
74         }
75
76         return (ApexEventProducer) producerPluginObject;
77     }
78 }