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;
29 import org.mvel2.MVEL;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.core.engine.event.EnEvent;
32 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * The Class MvelTaskSelectExecutor is the task selection executor for task selection logic written in MVEL.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class MvelTaskSelectExecutor extends TaskSelectExecutor {
44 // Logger for this class
45 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelTaskSelectExecutor.class);
48 private Serializable compiled = null;
51 * Prepares the task for processing.
53 * @throws StateMachineException thrown when a state machine execution error occurs
56 public void prepare() throws StateMachineException {
57 // Call generic prepare logic
60 // Compile the MVEL code
62 compiled = MVEL.compileExpression(getSubject().getTaskSelectionLogic().getLogic());
63 } catch (final Exception e) {
64 LOGGER.warn("failed to compile MVEL code for state " + getSubject().getKey().getId(), e);
65 throw new StateMachineException("failed to compile MVEL code for state " + getSubject().getKey().getId(),
71 * Executes the executor for the task in a sequential manner.
73 * @param executionId the execution ID for the current APEX policy execution
74 * @param incomingEvent the incoming event
75 * @return The outgoing event
76 * @throws StateMachineException on an execution error
77 * @throws ContextException on context errors
80 public AxArtifactKey execute(final long executionId, final EnEvent incomingEvent)
81 throws StateMachineException, ContextException {
82 // Do execution pre work
83 executePre(executionId, incomingEvent);
85 // Check and execute the MVEL logic
86 argumentNotNull(compiled, "MVEL task not compiled.");
88 boolean returnValue = false;
90 // Execute the MVEL code
92 (boolean) MVEL.executeExpression(compiled, getExecutionContext(), new HashMap<String, Object>());
93 } catch (final Exception e) {
94 LOGGER.warn("failed to execute MVEL code for state " + getSubject().getKey().getId(), e);
95 throw new StateMachineException("failed to execute MVEL code for state " + getSubject().getKey().getId(),
99 // Do the execution post work
100 executePost(returnValue);
102 // Send back the return event
103 return getOutgoing();
107 * Cleans up the task after processing.
109 * @throws StateMachineException thrown when a state machine execution error occurs
112 public void cleanUp() throws StateMachineException {
113 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
114 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
115 + getSubject().getTaskSelectionLogic().getLogic());