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