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