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