ab54d426c20613f08d8e71ffa5e01deebca2ac53
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.service.engine.event.impl.enevent;
24
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;
37
38 /**
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.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
45
46     // The Apex engine with its event definitions
47     private final ApexEngine apexEngine;
48
49     /**
50      * Set up the event converter.
51      *
52      * @param apexEngine The engine to use to create events to be converted
53      */
54     public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
55         this.apexEngine = apexEngine;
56     }
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
63         // Check the Engine event
64         if (event == null) {
65             throw new ApexEventException("event processing failed, engine event is null");
66         }
67
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;
71         try {
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);
76         }
77
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());
82
83         apexEvent.setExecutionId(enEvent.getExecutionId());
84         apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
85
86         // Copy he exception message to the Apex event if it is set
87         if (enEvent.getExceptionMessage() != null) {
88             apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
89         }
90
91         // Set the data on the apex event
92         apexEvent.putAll(enEvent);
93
94         // Return the event in a single element
95         final ArrayList<ApexEvent> eventList = new ArrayList<>();
96         eventList.add(apexEvent);
97         return eventList;
98     }
99
100     /**
101      * {@inheritDoc}.
102      */
103     @Override
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");
108         }
109
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");
115         }
116
117         // Create the internal engine event
118         final var enEvent = apexEngine.createEvent(eventDefinition.getKey());
119
120         // Set the data on the engine event
121         enEvent.putAll(apexEvent);
122
123         enEvent.setExecutionId(apexEvent.getExecutionId());
124         enEvent.setExecutionProperties(apexEvent.getExecutionProperties());
125
126         return enEvent;
127     }
128 }