924765dc55034830b03dcda208c6b25cf1992394
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.protocol.jms;
24
25 import java.lang.reflect.Method;
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.onap.policy.apex.service.engine.event.ApexEvent;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter;
31 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
32 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * The Class Apex2JMSObjectEventConverter converts {@link ApexEvent} instances into string instances of object message
38  * events for JMS.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public final class Apex2JmsObjectEventConverter implements ApexEventProtocolConverter {
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JmsObjectEventConverter.class);
44
45     // JMS event protocol parameters on the consumer (JMS->Apex) sides
46     private JmsObjectEventProtocolParameters eventProtocolParameters = null;
47
48     /**
49      * Constructor to create the Apex to JMS Object converter.
50      *
51      * @throws ApexEventException the apex event exception
52      */
53     public Apex2JmsObjectEventConverter() throws ApexEventException {
54         // Nothing specific to initiate for this plugin
55     }
56
57     @Override
58     public void init(final EventProtocolParameters parameters) {
59         // Check if properties have been set for JMS object event conversion as a consumer. They may not be set because
60         // JMS may not be in use
61         // on both sides of Apex
62         if (!(parameters instanceof JmsObjectEventProtocolParameters)) {
63             final String errormessage = "specified Event Protocol Parameters properties of type \""
64                             + parameters.getClass().getName() + "\" are not applicable to a "
65                             + Apex2JmsObjectEventConverter.class.getName() + " converter";
66             LOGGER.error(errormessage);
67         } else {
68             this.eventProtocolParameters = (JmsObjectEventProtocolParameters) parameters;
69         }
70
71     }
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
78         // Look for a "getObject()" method on the incoming object, if there is no such method, then we cannot fetch the
79         // object from JMS
80         Method getObjectMethod;
81         try {
82             getObjectMethod = eventObject.getClass().getMethod("getObject", (Class<?>[]) null);
83         } catch (Exception exp) {
84             final String errorMessage = "message \"" + eventObject
85                             + "\" received from JMS does not have a \"getObject()\" method";
86             throw new ApexEventRuntimeException(errorMessage, exp);
87         }
88
89         Object jmsIncomingObject;
90         try {
91             jmsIncomingObject = getObjectMethod.invoke(eventObject, (Object[]) null);
92         } catch (final Exception exp) {
93             final String errorMessage = "object contained in message \"" + eventObject
94                             + "\" received from JMS could not be retrieved as a Java object";
95             throw new ApexEventRuntimeException(errorMessage, exp);
96         }
97
98         // Check that the consumer parameters for JMS->Apex messaging have been set
99         if (eventProtocolParameters == null) {
100             final String errorMessage = "consumer parameters for JMS events consumed by "
101                             + "Apex are not set in the Apex configuration for this engine";
102             throw new ApexEventRuntimeException(errorMessage);
103         }
104
105         // Create the Apex event
106         // @formatter:off
107         final ApexEvent apexEvent = new ApexEvent(
108                         jmsIncomingObject.getClass().getSimpleName() + eventProtocolParameters.getIncomingEventSuffix(),
109                         eventProtocolParameters.getIncomingEventVersion(),
110                         jmsIncomingObject.toString().getClass().getPackage().getName(),
111                         eventProtocolParameters.getIncomingEventSource(),
112                         eventProtocolParameters.getIncomingEventTarget());
113         // @formatter:on
114
115         // Set the data on the apex event as the incoming object
116         apexEvent.put(jmsIncomingObject.getClass().getSimpleName(), jmsIncomingObject);
117
118         // Return the event in a single element
119         final ArrayList<ApexEvent> eventList = new ArrayList<>();
120         eventList.add(apexEvent);
121         return eventList;
122     }
123
124     /**
125      * {@inheritDoc}.
126      */
127     @Override
128     public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
129         // Check the Apex event
130         if (apexEvent == null) {
131             throw new ApexEventException("event processing failed, Apex event is null");
132         }
133
134         // Check that the Apex event has a single parameter
135         if (apexEvent.size() != 1) {
136             final String errorMessage = "event processing failed, "
137                             + "Apex event must have one and only one parameter for JMS Object handling";
138             throw new ApexEventException(errorMessage);
139         }
140
141         // Return the single object from the Apex event message
142         return apexEvent.values().iterator().next();
143     }
144
145     /**
146      * Returns eventProtocolParameters field. Getter used only for testing
147      *
148      * @return eventProtocolParameters
149      */
150     JmsObjectEventProtocolParameters getEventProtocolParameters() {
151         return eventProtocolParameters;
152     }
153 }