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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.event.protocol.jms;
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;
37 * The Class Apex2JMSObjectEventConverter converts {@link ApexEvent} instances into string instances of object message
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public final class Apex2JmsObjectEventConverter implements ApexEventProtocolConverter {
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2JmsObjectEventConverter.class);
45 // JMS event protocol parameters on the consumer (JMS->Apex) sides
46 private JmsObjectEventProtocolParameters eventProtocolParameters = null;
49 * Constructor to create the Apex to JMS Object converter.
51 * @throws ApexEventException the apex event exception
53 public Apex2JmsObjectEventConverter() throws ApexEventException {
54 // Nothing specific to initiate for this plugin
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);
68 this.eventProtocolParameters = (JmsObjectEventProtocolParameters) parameters;
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
80 Method getObjectMethod;
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);
89 Object jmsIncomingObject;
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);
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);
105 // Create the Apex event
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());
115 // Set the data on the apex event as the incoming object
116 apexEvent.put(jmsIncomingObject.getClass().getSimpleName(), jmsIncomingObject);
118 // Return the event in a single element
119 final ArrayList<ApexEvent> eventList = new ArrayList<>();
120 eventList.add(apexEvent);
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");
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);
141 // Return the single object from the Apex event message
142 return apexEvent.values().iterator().next();
146 * Returns eventProtocolParameters field. Getter used only for testing
148 * @return eventProtocolParameters
150 JmsObjectEventProtocolParameters getEventProtocolParameters() {
151 return eventProtocolParameters;