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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.mvel;
24 import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull;
26 import java.io.Serializable;
27 import java.util.HashMap;
30 import org.mvel2.MVEL;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * The Class MvelTaskExecutor is the task executor for task logic written in MVEL.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class MvelTaskExecutor extends TaskExecutor {
43 // Logger for this class
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelTaskExecutor.class);
47 private Serializable compiled = null;
50 * Prepares the task for processing.
52 * @throws StateMachineException thrown when a state machine execution error occurs
55 public void prepare() throws StateMachineException {
56 // Call generic prepare logic
59 // Compile the MVEL code
61 compiled = MVEL.compileExpression(getSubject().getTaskLogic().getLogic());
62 } catch (final Exception e) {
63 LOGGER.warn("failed to compile MVEL code for task " + getSubject().getKey().getId(), e);
64 throw new StateMachineException("failed to compile MVEL code for task " + getSubject().getKey().getId(), e);
66 argumentNotNull(compiled, "MVEL task not compiled.");
70 * Executes the executor for the task in a sequential manner.
72 * @param executionId the execution ID for the current APEX policy execution
73 * @param incomingFields the incoming fields
74 * @return The outgoing fields
75 * @throws StateMachineException on an execution error
76 * @throws ContextException on context errors
79 public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
80 throws StateMachineException, ContextException {
81 // Do execution pre work
82 executePre(executionId, incomingFields);
84 // Check and execute the MVEL logic
85 argumentNotNull(compiled, "MVEL task not compiled.");
86 boolean returnValue = false;
89 // Execute the MVEL code
91 (boolean) MVEL.executeExpression(compiled, getExecutionContext(), new HashMap<String, Object>());
92 } catch (final Exception e) {
93 LOGGER.warn("failed to execute MVEL code for task " + getSubject().getKey().getId(), e);
94 throw new StateMachineException("failed to execute MVEL code for task " + getSubject().getKey().getId(), e);
97 // Do the execution post work
98 executePost(returnValue);
100 // Send back the return event
101 return getOutgoing();
105 * Cleans up the task after processing.
107 * @throws StateMachineException thrown when a state machine execution error occurs
110 public void cleanUp() throws StateMachineException {
111 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
112 + "," + getSubject().getTaskLogic().getLogic());