2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.event.impl.enevent;
24 import java.util.ArrayList;
25 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;
38 * The Class ApexEvent2EnEventConverter converts externally facing {@link ApexEvent} instances to
39 * and from instances of {@link EnEvent} that are used internally in the Apex engine core.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
45 // The Apex engine with its event definitions
46 private final ApexEngine apexEngine;
49 * Set up the event converter.
51 * @param apexEngine The engine to use to create events to be converted
53 public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
54 this.apexEngine = apexEngine;
61 public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
62 // Check the Engine event
64 throw new ApexEventException("event processing failed, engine event is null");
67 // Cast the event to an Engine event event, if our conversion is correctly configured, this
68 // cast should always work
69 EnEvent enEvent = null;
71 enEvent = (EnEvent) event;
72 } catch (final Exception e) {
73 final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
74 throw new ApexEventRuntimeException(errorMessage, e);
77 // Create the Apex event
78 final AxEvent axEvent = enEvent.getAxEvent();
79 final ApexEvent apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
80 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
82 apexEvent.setExecutionId(enEvent.getExecutionId());
83 apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
85 // Copy he exception message to the Apex event if it is set
86 if (enEvent.getExceptionMessage() != null) {
87 apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
90 // Set the data on the apex event
91 apexEvent.putAll(enEvent);
93 // Return the event in a single element
94 final ArrayList<ApexEvent> eventList = new ArrayList<>();
95 eventList.add(apexEvent);
103 public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
104 // Check the Apex model
105 if (apexEngine == null) {
106 throw new ApexEventException("event processing failed, apex engine is null");
109 // Get the event definition
110 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
111 if (eventDefinition == null) {
112 throw new ApexEventException(
113 "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
116 // Create the internal engine event
117 final EnEvent enEvent = apexEngine.createEvent(eventDefinition.getKey());
119 // Set the data on the engine event
120 enEvent.putAll(apexEvent);
122 enEvent.setExecutionId(apexEvent.getExecutionId());
123 enEvent.setExecutionProperties(apexEvent.getExecutionProperties());