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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.javascript;
25 import java.util.Properties;
27 import javax.script.Compilable;
28 import javax.script.CompiledScript;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import javax.script.ScriptException;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
34 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * The Class JavascriptStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in
40 * Javascript It is unlikely that this is thread safe.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor {
45 // Logger for this class
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptStateFinalizerExecutor.class);
49 private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
50 private CompiledScript compiled = null;
53 * Prepares the state finalizer for processing.
55 * @throws StateMachineException thrown when a state machine execution error occurs
58 public void prepare() throws StateMachineException {
59 // Call generic prepare logic
62 compiled = ((Compilable) engine).compile(getSubject().getLogic());
63 } catch (final ScriptException e) {
64 LOGGER.error("execute: state finalizer logic failed to compile for state finalizer \""
65 + getSubject().getKey().getId() + "\"");
66 throw new StateMachineException("state finalizer logic failed to compile for state finalizer \""
67 + getSubject().getKey().getId() + "\"", e);
72 * Executes the executor for the state finalizer logic in a sequential manner.
74 * @param executionId the execution ID for the current APEX policy execution
75 * @param executionProperties properties for the current APEX policy execution
76 * @param incomingFields the incoming fields for finalisation
77 * @return The state output for the state
78 * @throws StateMachineException on an execution error
79 * @throws ContextException on context errors
82 public String execute(final long executionId, final Properties executionProperties,
83 final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
84 // Do execution pre work
85 executePre(executionId, executionProperties, incomingFields);
87 // Set up the Javascript engine
88 engine.put("executor", getExecutionContext());
90 // Check and execute the Javascript logic
92 if (compiled == null) {
93 engine.eval(getSubject().getLogic());
95 compiled.eval(engine.getContext());
97 } catch (final ScriptException e) {
98 LOGGER.error("execute: state finalizer logic failed to run for state finalizer \""
99 + getSubject().getKey().getId() + "\"");
100 throw new StateMachineException("state finalizer logic failed to run for state finalizer \""
101 + getSubject().getKey().getId() + "\"", e);
104 // Do the execution post work
105 executePost((boolean) engine.get("returnValue"));
107 return getOutgoing();
111 * Cleans up the state finalizer after processing.
113 * @throws StateMachineException thrown when a state machine execution error occurs
116 public void cleanUp() throws StateMachineException {
117 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
118 + getSubject().getLogic());