9b941a39a269ca1a8375f656ea2f9eb25b3d034c
[policy/apex-pdp.git] /
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
30 import org.mvel2.MVEL;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class MvelStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in MVEL.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class MvelStateFinalizerExecutor extends StateFinalizerExecutor {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelStateFinalizerExecutor.class);
45
46     // The MVEL code
47     private Serializable compiled = null;
48
49     /**
50      * Prepares the state finalizer 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().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 state finalizer logic in a sequential manner.
71      *
72      * @param executionId the execution ID for the current APEX policy execution
73      * @param incomingFields the incoming fields for finalisation
74      * @return The state output for the state
75      * @throws StateMachineException on an execution error
76      * @throws ContextException on context errors
77      */
78     @Override
79     public String execute(final long executionId, final Map<String, Object> incomingFields)
80             throws StateMachineException, ContextException {
81         // Do execution pre work
82         executePre(executionId, incomingFields);
83
84         // Check and execute the MVEL logic
85         argumentNotNull(compiled, "MVEL state finalizer logic 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         return getOutgoing();
103     }
104
105     /**
106      * Cleans up the state finalizer after processing.
107      *
108      * @throws StateMachineException thrown when a state machine execution error occurs
109      */
110     @Override
111     public void cleanUp() throws StateMachineException {
112         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
113                 + getSubject().getLogic());
114     }
115 }