2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5 * Modifications Copyright (C) 2021 Nordix Foundation.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.event.impl.enevent;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.onap.policy.apex.core.engine.engine.ApexEngine;
28 import org.onap.policy.apex.core.engine.event.EnEvent;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.model.basicmodel.service.ModelService;
31 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
32 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
33 import org.onap.policy.apex.service.engine.event.ApexEvent;
34 import org.onap.policy.apex.service.engine.event.ApexEventConverter;
35 import org.onap.policy.apex.service.engine.event.ApexEventException;
36 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
39 * The Class ApexEvent2EnEventConverter converts externally facing {@link ApexEvent} instances to
40 * and from instances of {@link EnEvent} that are used internally in the Apex engine core.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
46 // The Apex engine with its event definitions
47 private final ApexEngine apexEngine;
50 * Set up the event converter.
52 * @param apexEngine The engine to use to create events to be converted
54 public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
55 this.apexEngine = apexEngine;
62 public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
63 // Check the Engine event
65 throw new ApexEventException("event processing failed, engine event is null");
68 // Cast the event to an Engine event event, if our conversion is correctly configured, this
69 // cast should always work
70 EnEvent enEvent = null;
72 enEvent = (EnEvent) event;
73 } catch (final Exception e) {
74 final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
75 throw new ApexEventRuntimeException(errorMessage, e);
78 // Create the Apex event
79 final var axEvent = enEvent.getAxEvent();
80 final var apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
81 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
83 apexEvent.setExecutionId(enEvent.getExecutionId());
84 apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
86 // Copy he exception message to the Apex event if it is set
87 if (enEvent.getExceptionMessage() != null) {
88 apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
91 // Set the data on the apex event
92 apexEvent.putAll(enEvent);
94 // Return the event in a single element
95 final ArrayList<ApexEvent> eventList = new ArrayList<>();
96 eventList.add(apexEvent);
104 public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
105 // Check the Apex model
106 if (apexEngine == null) {
107 throw new ApexEventException("event processing failed, apex engine is null");
110 // Get the event definition
111 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
112 if (eventDefinition == null) {
113 throw new ApexEventException(
114 "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
117 // Create the internal engine event
118 final var enEvent = apexEngine.createEvent(eventDefinition.getKey());
120 // Set the data on the engine event
121 enEvent.putAll(apexEvent);
123 enEvent.setExecutionId(apexEvent.getExecutionId());
124 enEvent.setExecutionProperties(apexEvent.getExecutionProperties());