c906a9ca303387803b8d31f578714d0ffcda597a
[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.javascript;
23
24 import java.util.Map;
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 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 JavascriptStateFinalizerExecutor is the state finalizer executor for state finalizer
38  * logic written in Javascript It is unlikely that this is thread safe.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptStateFinalizerExecutor.class);
45
46     // Javascript engine
47     private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
48     private CompiledScript compiled = null;
49
50     /**
51      * Prepares the state finalizer for processing.
52      *
53      * @throws StateMachineException thrown when a state machine execution error occurs
54      */
55     @Override
56     public void prepare() throws StateMachineException {
57         // Call generic prepare logic
58         super.prepare();
59         try {
60             compiled = ((Compilable) engine).compile(getSubject().getLogic());
61         } catch (final ScriptException e) {
62             LOGGER.error("execute: state finalizer logic failed to compile for state finalizer  \""
63                     + getSubject().getKey().getId() + "\"");
64             throw new StateMachineException("state finalizer logic failed to compile for state finalizer  \""
65                     + getSubject().getKey().getId() + "\"", 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         // Set up the Javascript engine
85         engine.put("executor", getExecutionContext());
86
87         // Check and execute the Javascript logic
88         try {
89             if (compiled == null) {
90                 engine.eval(getSubject().getLogic());
91             } else {
92                 compiled.eval(engine.getContext());
93             }
94         } catch (final ScriptException e) {
95             LOGGER.error("execute: state finalizer logic failed to run for state finalizer  \""
96                     + getSubject().getKey().getId() + "\"");
97             throw new StateMachineException("state finalizer logic failed to run for state finalizer  \""
98                     + getSubject().getKey().getId() + "\"", e);
99         }
100
101         // Do the execution post work
102         executePost((boolean) engine.get("returnValue"));
103
104         return getOutgoing();
105     }
106
107     /**
108      * Cleans up the state finalizer after processing.
109      *
110      * @throws StateMachineException thrown when a state machine execution error occurs
111      */
112     @Override
113     public void cleanUp() throws StateMachineException {
114         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
115                 + getSubject().getLogic());
116         engine = null;
117     }
118 }