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.enevent;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.onap.policy.apex.core.engine.engine.ApexEngine;
27 import org.onap.policy.apex.core.engine.event.EnEvent;
28 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
29 import org.onap.policy.apex.model.basicmodel.service.ModelService;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
32 import org.onap.policy.apex.service.engine.event.ApexEvent;
33 import org.onap.policy.apex.service.engine.event.ApexEventConverter;
34 import org.onap.policy.apex.service.engine.event.ApexEventException;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * The Class ApexEvent2EnEventConverter converts externally facing {@link ApexEvent} instances to
41 * and from instances of {@link EnEvent} that are used internally in the Apex engine core.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEvent2EnEventConverter.class);
48 // The Apex engine with its event definitions
49 private final ApexEngine apexEngine;
52 * Set up the event converter.
54 * @param apexEngine The engine to use to create events to be converted
56 public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
57 this.apexEngine = apexEngine;
64 public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
65 // Check the Engine event
67 LOGGER.warn("event processing failed, engine event is null");
68 throw new ApexEventException("event processing failed, engine event is null");
71 // Cast the event to an Engine event event, if our conversion is correctly configured, this
72 // cast should always work
73 EnEvent enEvent = null;
75 enEvent = (EnEvent) event;
76 } catch (final Exception e) {
77 final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
78 LOGGER.debug(errorMessage, e);
79 throw new ApexEventRuntimeException(errorMessage, e);
82 // Create the Apex event
83 final AxEvent axEvent = enEvent.getAxEvent();
84 final ApexEvent apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
85 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
87 apexEvent.setExecutionId(enEvent.getExecutionId());
88 apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
90 // Copy he exception message to the Apex event if it is set
91 if (enEvent.getExceptionMessage() != null) {
92 apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
95 // Set the data on the apex event
96 apexEvent.putAll(enEvent);
98 // Return the event in a single element
99 final ArrayList<ApexEvent> eventList = new ArrayList<>();
100 eventList.add(apexEvent);
108 public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
109 // Check the Apex model
110 if (apexEngine == null) {
111 LOGGER.warn("event processing failed, apex engine is null");
112 throw new ApexEventException("event processing failed, apex engine is null");
115 // Get the event definition
116 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
117 if (eventDefinition == null) {
118 LOGGER.warn("event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
119 throw new ApexEventException(
120 "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
123 // Create the internal engine event
124 final EnEvent enEvent = apexEngine.createEvent(eventDefinition.getKey());
126 // Set the data on the engine event
127 enEvent.putAll(apexEvent);
129 enEvent.setExecutionId(apexEvent.getExecutionId());
130 enEvent.setExecutionProperties(apexEvent.getExecutionProperties());