1ba79ceec5d02113a540318def8bc3de5bf63357
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-mvel / src / main / java / org / onap / policy / apex / plugins / executor / mvel / MvelTaskSelectExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
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.plugins.executor.mvel;
23
24 import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull;
25
26 import java.io.Serializable;
27 import java.util.HashMap;
28 import java.util.Properties;
29
30 import org.mvel2.MVEL;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.core.engine.event.EnEvent;
33 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
34 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
38
39 /**
40  * The Class MvelTaskSelectExecutor is the task selection executor for task selection logic written in MVEL.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class MvelTaskSelectExecutor extends TaskSelectExecutor {
45     // Logger for this class
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelTaskSelectExecutor.class);
47
48     // The MVEL code
49     private Serializable compiled = null;
50
51     /**
52      * Prepares the task for processing.
53      *
54      * @throws StateMachineException thrown when a state machine execution error occurs
55      */
56     @Override
57     public void prepare() throws StateMachineException {
58         // Call generic prepare logic
59         super.prepare();
60
61         // Compile the MVEL code
62         try {
63             compiled = MVEL.compileExpression(getSubject().getTaskSelectionLogic().getLogic());
64         } catch (final Exception e) {
65             LOGGER.warn("failed to compile MVEL code for state " + getSubject().getKey().getId(), e);
66             throw new StateMachineException("failed to compile MVEL code for state " + getSubject().getKey().getId(),
67                     e);
68         }
69     }
70
71     /**
72      * Executes the executor for the task in a sequential manner.
73      *
74      * @param executionId the execution ID for the current APEX policy execution
75      * @param executionProperties properties for the current APEX policy execution
76      * @param incomingEvent the incoming event
77      * @return The outgoing event
78      * @throws StateMachineException on an execution error
79      * @throws ContextException on context errors
80      */
81     @Override
82     public AxArtifactKey execute(final long executionId, final Properties executionProperties,
83             final EnEvent incomingEvent) throws StateMachineException, ContextException {
84         // Do execution pre work
85         executePre(executionId, executionProperties, incomingEvent);
86
87         // Check and execute the MVEL logic
88         argumentNotNull(compiled, "MVEL task not compiled.");
89
90         boolean returnValue = false;
91         try {
92             // Execute the MVEL code
93             returnValue =
94                     (boolean) MVEL.executeExpression(compiled, getExecutionContext(), new HashMap<String, Object>());
95         } catch (final Exception e) {
96             LOGGER.warn("failed to execute MVEL code for state " + getSubject().getKey().getId(), e);
97             throw new StateMachineException("failed to execute MVEL code for state " + getSubject().getKey().getId(),
98                     e);
99         }
100
101         // Do the execution post work
102         executePost(returnValue);
103
104         // Send back the return event
105         return getOutgoing();
106     }
107
108     /**
109      * Cleans up the task after processing.
110      *
111      * @throws StateMachineException thrown when a state machine execution error occurs
112      */
113     @Override
114     public void cleanUp() throws StateMachineException {
115         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
116                 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
117                 + getSubject().getTaskSelectionLogic().getLogic());
118     }
119 }