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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin;
23 import java.util.ArrayList;
24 import java.util.List;
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;
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.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class Apex2ApexEventConverter implements ApexEventProtocolConverter {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Apex2ApexEventConverter.class);
48 public void init(final EventProtocolParameters parameters) {
49 // Check and get the APEX parameters
50 if (!(parameters instanceof ApexEventProtocolParameters)) {
51 final String errorMessage = "specified consumer properties are not applicable to the APEX event protocol";
52 LOGGER.warn(errorMessage);
53 throw new ApexEventRuntimeException(errorMessage);
61 public List<ApexEvent> toApexEvent(final String eventName, final Object eventObject) throws ApexEventException {
62 // Check the event eventObject
63 if (eventObject == null) {
64 LOGGER.warn("event processing failed, event is null");
65 throw new ApexEventException("event processing failed, event is null");
68 // The list of events we will return
69 final List<ApexEvent> eventList = new ArrayList<>();
72 // Check if its a single APEX event
73 if (!(eventObject instanceof ApexEvent)) {
74 throw new ApexEventException("incoming event (" + eventObject + ") is not an ApexEvent");
77 final ApexEvent event = (ApexEvent) eventObject;
79 // Check whether we have any ApexEventList fields, if so this is an event of events and
80 // all fields should be of type ApexEventList
81 boolean foundEventListFields = false;
82 boolean foundOtherFields = false;
83 for (final Object fieldObject : event.values()) {
84 if (fieldObject instanceof ApexEventList) {
85 foundEventListFields = true;
87 // Add the events to the event list
88 eventList.addAll((ApexEventList) fieldObject);
90 foundOtherFields = true;
94 // If we found both event list fields and other fields we're in trouble
95 if (foundEventListFields && foundOtherFields) {
96 throw new ApexEventException("incoming event (" + eventObject
97 + ") has both event list fields and other fields, it cannot be processed");
100 // Check if the incoming event just has other fields, if so it's just a regular event
101 // and we add it to the event list as the only event there
102 if (foundOtherFields) {
103 eventList.add(event);
105 } catch (final Exception e) {
106 final String errorString = "Failed to unmarshal APEX event: " + e.getMessage() + ", event=" + eventObject;
107 LOGGER.warn(errorString, e);
108 throw new ApexEventException(errorString, e);
111 // Return the list of events we have unmarshalled
119 public Object fromApexEvent(final ApexEvent apexEvent) throws ApexEventException {
120 // Check the Apex event
121 if (apexEvent == null) {
122 LOGGER.warn("event processing failed, Apex event is null");
123 throw new ApexEventException("event processing failed, Apex event is null");