Merge "Add period after inheritDoc for Sonar"
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / enevent / ApexEvent2EnEventConverter.java
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.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 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
38
39 /**
40  * The Class ApexEvent2EnEventConverter converts externally facing {@link ApexEvent} instances to
41  * and from instances of {@link EnEvent} that are used internally in the Apex engine core.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public final class ApexEvent2EnEventConverter implements ApexEventConverter {
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEvent2EnEventConverter.class);
47
48     // The Apex engine with its event definitions
49     private final ApexEngine apexEngine;
50
51     /**
52      * Set up the event converter.
53      *
54      * @param apexEngine The engine to use to create events to be converted
55      */
56     public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
57         this.apexEngine = apexEngine;
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see
64      * org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String,
65      * java.lang.Object)
66      */
67     @Override
68     public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
69         // Check the Engine event
70         if (event == null) {
71             LOGGER.warn("event processing failed, engine event is null");
72             throw new ApexEventException("event processing failed, engine event is null");
73         }
74
75         // Cast the event to an Engine event event, if our conversion is correctly configured, this
76         // cast should always work
77         EnEvent enEvent = null;
78         try {
79             enEvent = (EnEvent) event;
80         } catch (final Exception e) {
81             final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
82             LOGGER.debug(errorMessage, e);
83             throw new ApexEventRuntimeException(errorMessage, e);
84         }
85
86         // Create the Apex event
87         final AxEvent axEvent = enEvent.getAxEvent();
88         final ApexEvent apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
89                 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
90
91         // Copy the ExecutionID from the EnEvent into the ApexEvent
92         apexEvent.setExecutionId(enEvent.getExecutionId());
93
94         // Copy he exception message to the Apex event if it is set
95         if (enEvent.getExceptionMessage() != null) {
96             apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
97         }
98
99         // Set the data on the apex event
100         apexEvent.putAll(enEvent);
101
102         // Return the event in a single element
103         final ArrayList<ApexEvent> eventList = new ArrayList<>();
104         eventList.add(apexEvent);
105         return eventList;
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see
112      * org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy.
113      * apex.service.engine.event.ApexEvent)
114      */
115     @Override
116     public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
117         // Check the Apex model
118         if (apexEngine == null) {
119             LOGGER.warn("event processing failed, apex engine is null");
120             throw new ApexEventException("event processing failed, apex engine is null");
121         }
122
123         // Get the event definition
124         final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
125         if (eventDefinition == null) {
126             LOGGER.warn("event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
127             throw new ApexEventException(
128                     "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
129         }
130
131         // Create the internal engine event
132         final EnEvent enEvent = apexEngine.createEvent(eventDefinition.getKey());
133
134         // Set the data on the engine event
135         enEvent.putAll(apexEvent);
136
137         // copy the ExecutionID from the ApexEvent into the EnEvent
138         enEvent.setExecutionId(apexEvent.getExecutionId());
139
140         return enEvent;
141     }
142 }