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;
24 import javax.script.Compilable;
25 import javax.script.CompiledScript;
26 import javax.script.ScriptEngine;
27 import javax.script.ScriptEngineManager;
28 import javax.script.ScriptException;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.core.engine.event.EnEvent;
31 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
32 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * The Class JavascriptTaskSelectExecutor is the task selection executor for task selection logic
39 * written in Javascript It is unlikely that this is thread safe.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class JavascriptTaskSelectExecutor extends TaskSelectExecutor {
44 // Logger for this class
45 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskSelectExecutor.class);
47 // Recurring string constants
48 private static final String TSL_FAILED_PREFIX =
49 "execute: task selection logic failed to set a return value for state \"";
52 private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
53 private CompiledScript compiled = null;
56 * Prepares the task for processing.
58 * @throws StateMachineException thrown when a state machine execution error occurs
61 public void prepare() throws StateMachineException {
62 // Call generic prepare logic
65 compiled = ((Compilable) engine).compile(getSubject().getTaskSelectionLogic().getLogic());
66 } catch (final ScriptException e) {
67 LOGGER.error("execute: task selection logic failed to compile for state \"" + getSubject().getKey().getId()
69 throw new StateMachineException(
70 "task selection logic failed to compile for state \"" + getSubject().getKey().getId() + "\"", e);
76 * Executes the executor for the task in a sequential manner.
78 * @param executionId the execution ID for the current APEX policy execution
79 * @param incomingEvent the incoming event
80 * @return The outgoing event
81 * @throws StateMachineException on an execution error
82 * @throws ContextException on context errors
85 public AxArtifactKey execute(final long executionId, final EnEvent incomingEvent)
86 throws StateMachineException, ContextException {
87 // Do execution pre work
88 executePre(executionId, incomingEvent);
90 // Set up the Javascript engine
91 engine.put("executor", getExecutionContext());
93 // Check and execute the Javascript logic
95 if (compiled == null) {
96 engine.eval(getSubject().getTaskSelectionLogic().getLogic());
98 compiled.eval(engine.getContext());
100 } catch (final ScriptException e) {
102 "execute: task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"");
103 throw new StateMachineException(
104 "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", e);
107 final Object ret = engine.get("returnValue");
109 LOGGER.error(TSL_FAILED_PREFIX + getSubject().getKey().getId() + "\"");
110 throw new StateMachineException(TSL_FAILED_PREFIX + getSubject().getKey().getId() + "\"");
113 // Do the execution post work
114 executePost((Boolean) ret);
116 return getOutgoing();
120 * Cleans up the task after processing.
122 * @throws StateMachineException thrown when a state machine execution error occurs
125 public void cleanUp() throws StateMachineException {
126 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
127 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
128 + getSubject().getTaskSelectionLogic().getLogic());