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 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.apex.service.engine.event.impl.enevent;
26 import java.util.ArrayList;
27 import java.util.List;
28 import lombok.AllArgsConstructor;
29 import org.onap.policy.apex.core.engine.engine.ApexEngine;
30 import org.onap.policy.apex.core.engine.event.EnEvent;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.basicmodel.service.ModelService;
33 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
34 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
35 import org.onap.policy.apex.service.engine.event.ApexEvent;
36 import org.onap.policy.apex.service.engine.event.ApexEventConverter;
37 import org.onap.policy.apex.service.engine.event.ApexEventException;
38 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
41 * The Class ApexEvent2EnEventConverter converts externally facing {@link ApexEvent} instances to
42 * and from instances of {@link EnEvent} that are used internally in the Apex engine core.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
49 // The Apex engine with its event definitions
50 private final ApexEngine apexEngine;
56 public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
57 // Check the Engine event
59 throw new ApexEventException("event processing failed, engine event is null");
62 // Cast the event to an Engine event event, if our conversion is correctly configured, this
63 // cast should always work
64 EnEvent enEvent = null;
66 enEvent = (EnEvent) event;
67 } catch (final Exception e) {
68 final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
69 throw new ApexEventRuntimeException(errorMessage, e);
72 // Create the Apex event
73 final var axEvent = enEvent.getAxEvent();
74 final var apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
75 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
77 apexEvent.setExecutionId(enEvent.getExecutionId());
78 apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
80 // Copy he exception message to the Apex event if it is set
81 if (enEvent.getExceptionMessage() != null) {
82 apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
85 // Set the data on the apex event
86 apexEvent.putAll(enEvent);
88 // Return the event in a single element
89 final ArrayList<ApexEvent> eventList = new ArrayList<>();
90 eventList.add(apexEvent);
98 public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
99 // Check the Apex model
100 if (apexEngine == null) {
101 throw new ApexEventException("event processing failed, apex engine is null");
104 // Get the event definition
105 final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
106 if (eventDefinition == null) {
107 throw new ApexEventException(
108 "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
111 // Create the internal engine event
112 final var enEvent = apexEngine.createEvent(eventDefinition.getKey());
114 // Set the data on the engine event
115 enEvent.putAll(apexEvent);
117 enEvent.setExecutionId(apexEvent.getExecutionId());
118 enEvent.setExecutionProperties(apexEvent.getExecutionProperties());