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