b73aeb567f38e6f3a3dc5f5edb84d3ff8614cd90
[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.apexprotocolplugin;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.onap.policy.apex.service.engine.event.ApexEvent;
27 import org.onap.policy.apex.service.engine.event.ApexEventException;
28 import org.onap.policy.apex.service.engine.event.ApexEventList;
29 import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter;
30 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
31 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * The Class Apex2ApexEventConverter passes through {@link ApexEvent} instances. It is used for
37  * transferring Apex events directly as POJOs between APEX producers and consumers.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class Apex2ApexEventConverter implements ApexEventProtocolConverter {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2ApexEventConverter.class);
43
44     /*
45      * (non-Javadoc)
46      * 
47      * @see
48      * org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter#init(org.onap.policy.
49      * apex. service.parameters.eventprotocol.EventProtocolParameters)
50      */
51     @Override
52     public void init(final EventProtocolParameters parameters) {
53         // Check and get the APEX parameters
54         if (!(parameters instanceof ApexEventProtocolParameters)) {
55             final String errorMessage = "specified consumer properties are not applicable to the APEX event protocol";
56             LOGGER.warn(errorMessage);
57             throw new ApexEventRuntimeException(errorMessage);
58         }
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see
65      * org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String,
66      * java.lang.Object)
67      */
68     @Override
69     public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
70         // Check the event eventObject
71         if (eventObject == null) {
72             LOGGER.warn("event processing failed, event is null");
73             throw new ApexEventException("event processing failed, event is null");
74         }
75
76         // The list of events we will return
77         final List<ApexEvent> eventList = new ArrayList<>();
78
79         try {
80             // Check if its a single APEX event
81             if (!(eventObject instanceof ApexEvent)) {
82                 throw new ApexEventException("incoming event (" + eventObject + ") is not an ApexEvent");
83             }
84
85             final ApexEvent event = (ApexEvent) eventObject;
86
87             // Check whether we have any ApexEventList fields, if so this is an event of events and
88             // all fields should be of type ApexEventList
89             boolean foundEventListFields = false;
90             boolean foundOtherFields = false;
91             for (final Object fieldObject : event.values()) {
92                 if (fieldObject instanceof ApexEventList) {
93                     foundEventListFields = true;
94
95                     // Add the events to the event list
96                     eventList.addAll((ApexEventList) fieldObject);
97                 } else {
98                     foundOtherFields = true;
99                 }
100             }
101
102             // If we found both event list fields and other fields we're in trouble
103             if (foundEventListFields && foundOtherFields) {
104                 throw new ApexEventException("incoming event (" + eventObject
105                         + ") has both event list fields and other fields, it cannot be processed");
106             }
107
108             // Check if the incoming event just has other fields, if so it's just a regular event
109             // and we add it to the event list as the only event there
110             if (foundOtherFields) {
111                 eventList.add(event);
112             }
113         } catch (final Exception e) {
114             final String errorString = "Failed to unmarshal APEX event: " + e.getMessage() + ", event=" + eventObject;
115             LOGGER.warn(errorString, e);
116             throw new ApexEventException(errorString, e);
117         }
118
119         // Return the list of events we have unmarshalled
120         return eventList;
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see
127      * org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.
128      * apex.service.engine.event.ApexEvent)
129      */
130     @Override
131     public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
132         // Check the Apex event
133         if (apexEvent == null) {
134             LOGGER.warn("event processing failed, Apex event is null");
135             throw new ApexEventException("event processing failed, Apex event is null");
136         }
137
138         return apexEvent;
139     }
140 }