b2a6f47545e290a04cb404845d216315a0abfe11
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.ApexEventProtocolConverter;
25 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This factory class uses the Apex event protocol parameters to create and return an instance of the correct Apex event
32  * protocol converter plugin for the specified event protocol.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class EventProtocolFactory {
37     // The logger for this class
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProtocolFactory.class);
39
40     /**
41      * Create an event converter that converts between an {@link org.onap.policy.apex.service.engine.event.ApexEvent}
42      * and the specified event protocol.
43      *
44      * @param name the name of the event protocol
45      * @param eventProtocolParameters the event protocol parameters defining what to convert from and to
46      * @return The event converter for converting events to and from Apex format
47      */
48     public ApexEventProtocolConverter createConverter(final String name,
49                     final EventProtocolParameters eventProtocolParameters) {
50         // Get the class for the event protocol plugin using reflection
51         final String eventProtocolPluginClass = eventProtocolParameters.getEventProtocolPluginClass();
52         Object eventProtocolPluginObject = null;
53         try {
54             eventProtocolPluginObject = Class.forName(eventProtocolPluginClass).newInstance();
55         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
56             final String errorMessage = "could not create an Apex event protocol converter for \"" + name
57                             + "\" for the protocol \"" + eventProtocolParameters.getLabel()
58                             + "\", specified event protocol converter plugin class \"" + eventProtocolPluginClass
59                             + "\" not found";
60             LOGGER.error(errorMessage, e);
61             throw new ApexEventRuntimeException(errorMessage, e);
62         }
63
64         // Check the class is an event consumer
65         if (!(eventProtocolPluginObject instanceof ApexEventProtocolConverter)) {
66             final String errorMessage = "could not create an Apex event protocol converter for \"" + name
67                             + "\" for the protocol \"" + eventProtocolParameters.getLabel()
68                             + "\", specified event protocol converter plugin class \"" + eventProtocolPluginClass
69                             + "\" is not an instance of \"" + ApexEventProtocolConverter.class.getName() + "\"";
70             LOGGER.error(errorMessage);
71             throw new ApexEventRuntimeException(errorMessage);
72         }
73         ((ApexEventProtocolConverter) eventProtocolPluginObject).init(eventProtocolParameters);
74         return (ApexEventProtocolConverter) eventProtocolPluginObject;
75     }
76 }