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