34690cc7d7c8d638123023abbaf58b3b1a2871b0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.enevent;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.onap.policy.apex.service.engine.event.ApexEvent;
27 import org.onap.policy.apex.service.engine.event.ApexEventConverter;
28 import org.onap.policy.apex.service.engine.event.ApexEventException;
29 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 import org.onap.policy.apex.core.engine.engine.ApexEngine;
34 import org.onap.policy.apex.core.engine.event.EnEvent;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.service.ModelService;
37 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
38 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
39
40 /**
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.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEvent2EnEventConverter.class);
48
49     // The Apex engine with its event definitions
50     private final ApexEngine apexEngine;
51
52     /**
53      * Set up the event converter.
54      *
55      * @param apexEngine The engine to use to create events to be converted
56      */
57     public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
58         this.apexEngine = apexEngine;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see
65      * org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String,
66      * java.lang.Object)
67      */
68     @Override
69     public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
70         // Check the Engine event
71         if (event == null) {
72             LOGGER.warn("event processing failed, engine event is null");
73             throw new ApexEventException("event processing failed, engine event is null");
74         }
75
76         // Cast the event to an Engine event event, if our conversion is correctly configured, this
77         // cast should always work
78         EnEvent enEvent = null;
79         try {
80             enEvent = (EnEvent) event;
81         } catch (final Exception e) {
82             final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
83             LOGGER.debug(errorMessage, e);
84             throw new ApexEventRuntimeException(errorMessage, e);
85         }
86
87         // Create the Apex event
88         final AxEvent axEvent = enEvent.getAxEvent();
89         final ApexEvent apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
90                 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
91
92         // Copy the ExecutionID from the EnEvent into the ApexEvent
93         apexEvent.setExecutionID(enEvent.getExecutionId());
94
95         // Copy he exception message to the Apex event if it is set
96         if (enEvent.getExceptionMessage() != null) {
97             apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
98         }
99
100         // Set the data on the apex event
101         apexEvent.putAll(enEvent);
102
103         // Return the event in a single element
104         final ArrayList<ApexEvent> eventList = new ArrayList<ApexEvent>();
105         eventList.add(apexEvent);
106         return eventList;
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see
113      * org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.
114      * apex.service.engine.event.ApexEvent)
115      */
116     @Override
117     public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
118         // Check the Apex model
119         if (apexEngine == null) {
120             LOGGER.warn("event processing failed, apex engine is null");
121             throw new ApexEventException("event processing failed, apex engine is null");
122         }
123
124         // Get the event definition
125         final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
126         if (eventDefinition == null) {
127             LOGGER.warn("event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
128             throw new ApexEventException(
129                     "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
130         }
131
132         // Create the internal engine event
133         final EnEvent enEvent = apexEngine.createEvent(eventDefinition.getKey());
134
135         // Set the data on the engine event
136         enEvent.putAll(apexEvent);
137
138         // copy the ExecutionID from the ApexEvent into the EnEvent
139         enEvent.setExecutionId(apexEvent.getExecutionID());
140
141         return enEvent;
142     }
143 }