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