Changes for checkstyle 8.32
[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 import org.onap.policy.apex.core.engine.engine.ApexEngine;
26 import org.onap.policy.apex.core.engine.event.EnEvent;
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
28 import org.onap.policy.apex.model.basicmodel.service.ModelService;
29 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
30 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
31 import org.onap.policy.apex.service.engine.event.ApexEvent;
32 import org.onap.policy.apex.service.engine.event.ApexEventConverter;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
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     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEvent2EnEventConverter.class);
46
47     // The Apex engine with its event definitions
48     private final ApexEngine apexEngine;
49
50     /**
51      * Set up the event converter.
52      *
53      * @param apexEngine The engine to use to create events to be converted
54      */
55     public ApexEvent2EnEventConverter(final ApexEngine apexEngine) {
56         this.apexEngine = apexEngine;
57     }
58
59     /**
60      * {@inheritDoc}.
61      */
62     @Override
63     public List<ApexEvent> toApexEvent(final String eventName, final Object event) throws ApexException {
64         // Check the Engine event
65         if (event == null) {
66             LOGGER.warn("event processing failed, engine event is null");
67             throw new ApexEventException("event processing failed, engine event is null");
68         }
69
70         // Cast the event to an Engine event event, if our conversion is correctly configured, this
71         // cast should always work
72         EnEvent enEvent = null;
73         try {
74             enEvent = (EnEvent) event;
75         } catch (final Exception e) {
76             final String errorMessage = "error transferring event \"" + event + "\" to the Apex engine";
77             LOGGER.debug(errorMessage, e);
78             throw new ApexEventRuntimeException(errorMessage, e);
79         }
80
81         // Create the Apex event
82         final AxEvent axEvent = enEvent.getAxEvent();
83         final ApexEvent apexEvent = new ApexEvent(axEvent.getKey().getName(), axEvent.getKey().getVersion(),
84                 axEvent.getNameSpace(), axEvent.getSource(), axEvent.getTarget());
85
86         apexEvent.setExecutionId(enEvent.getExecutionId());
87         apexEvent.setExecutionProperties(enEvent.getExecutionProperties());
88
89         // Copy he exception message to the Apex event if it is set
90         if (enEvent.getExceptionMessage() != null) {
91             apexEvent.setExceptionMessage(enEvent.getExceptionMessage());
92         }
93
94         // Set the data on the apex event
95         apexEvent.putAll(enEvent);
96
97         // Return the event in a single element
98         final ArrayList<ApexEvent> eventList = new ArrayList<>();
99         eventList.add(apexEvent);
100         return eventList;
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
107     public EnEvent fromApexEvent(final ApexEvent apexEvent) throws ApexException {
108         // Check the Apex model
109         if (apexEngine == null) {
110             LOGGER.warn("event processing failed, apex engine is null");
111             throw new ApexEventException("event processing failed, apex engine is null");
112         }
113
114         // Get the event definition
115         final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(apexEvent.getName());
116         if (eventDefinition == null) {
117             LOGGER.warn("event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
118             throw new ApexEventException(
119                     "event processing failed, event \"" + apexEvent.getName() + "\" not found in apex model");
120         }
121
122         // Create the internal engine event
123         final EnEvent enEvent = apexEngine.createEvent(eventDefinition.getKey());
124
125         // Set the data on the engine event
126         enEvent.putAll(apexEvent);
127
128         enEvent.setExecutionId(apexEvent.getExecutionId());
129         enEvent.setExecutionProperties(apexEvent.getExecutionProperties());
130
131         return enEvent;
132     }
133 }