85c5bf03f227c4b73ef3a94a099ec2e4c77ddd2d
[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.ApexEventProtocolConverter;
24 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
25 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
28
29 /**
30  * This factory class uses the Apex event protocol parameters to create and return an instance of
31  * the correct Apex event protocol converter plugin for the specified event protocol.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class EventProtocolFactory {
36     // The logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProtocolFactory.class);
38
39     /**
40      * Create an event converter that converts between an
41      * {@link org.onap.policy.apex.service.engine.event.ApexEvent} and the specified event protocol.
42      *
43      * @param name the name of the event protocol
44      * @param eventProtocolParameters the event protocol parameters defining what to convert from
45      *        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.getCanonicalName() + "\"";
70             LOGGER.error(errorMessage);
71             throw new ApexEventRuntimeException(errorMessage);
72         }
73         ((ApexEventProtocolConverter) eventProtocolPluginObject).init(eventProtocolParameters);
74         return (ApexEventProtocolConverter) eventProtocolPluginObject;
75     }
76 }