bc3062d133b8db5e6e503b2b0481c2b8711f0d36
[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.javascript;
22
23 import java.util.Map;
24
25 import javax.script.Compilable;
26 import javax.script.CompiledScript;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30
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 JavascriptStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in
39  * Javascript It is unlikely that this is thread safe.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor {
44     // Logger for this class
45     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptStateFinalizerExecutor.class);
46
47     // Javascript engine
48     private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
49     private CompiledScript compiled = null;
50
51     /**
52      * Prepares the state finalizer for processing.
53      *
54      * @throws StateMachineException thrown when a state machine execution error occurs
55      */
56     @Override
57     public void prepare() throws StateMachineException {
58         // Call generic prepare logic
59         super.prepare();
60         try {
61             compiled = ((Compilable) engine).compile(getSubject().getLogic());
62         } catch (final ScriptException e) {
63             LOGGER.error("execute: state finalizer logic failed to compile for state finalizer  \""
64                     + getSubject().getKey().getId() + "\"");
65             throw new StateMachineException("state finalizer logic failed to compile for state finalizer  \""
66                     + getSubject().getKey().getId() + "\"", e);
67         }
68     }
69
70     /**
71      * Executes the executor for the state finalizer logic in a sequential manner.
72      *
73      * @param executionId the execution ID for the current APEX policy execution
74      * @param incomingFields the incoming fields for finalisation
75      * @return The state output for the state
76      * @throws StateMachineException on an execution error
77      * @throws ContextException on context errors
78      */
79     @Override
80     public String execute(final long executionId, final Map<String, Object> incomingFields)
81             throws StateMachineException, ContextException {
82         // Do execution pre work
83         executePre(executionId, incomingFields);
84
85         // Set up the Javascript engine
86         engine.put("executor", getExecutionContext());
87
88         // Check and execute the Javascript logic
89         boolean returnValue = false;
90         try {
91             if (compiled == null) {
92                 engine.eval(getSubject().getLogic());
93             } else {
94                 compiled.eval(engine.getContext());
95             }
96         } catch (final ScriptException e) {
97             LOGGER.error("execute: state finalizer logic failed to run for state finalizer  \""
98                     + getSubject().getKey().getId() + "\"");
99             throw new StateMachineException("state finalizer logic failed to run for state finalizer  \""
100                     + getSubject().getKey().getId() + "\"", e);
101         }
102
103         returnValue = (boolean) engine.get("returnValue");
104
105         // Do the execution post work
106         executePost(returnValue);
107
108         // Send back the return event
109         if (returnValue) {
110             return getOutgoing();
111         } else {
112             return null;
113         }
114     }
115
116     /**
117      * Cleans up the state finalizer after processing.
118      *
119      * @throws StateMachineException thrown when a state machine execution error occurs
120      */
121     @Override
122     public void cleanUp() throws StateMachineException {
123         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
124                 + getSubject().getLogic());
125         engine = null;
126     }
127 }