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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.jython;
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
26 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
27 import org.python.core.CompileMode;
28 import org.python.core.CompilerFlags;
29 import org.python.core.Py;
30 import org.python.core.PyCode;
31 import org.python.core.PyException;
32 import org.python.util.PythonInterpreter;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * The Class JythonTaskExecutor is the task executor for task logic written in Jython It is unlikely
38 * that this is thread safe.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class JythonTaskExecutor extends TaskExecutor {
43 // Logger for this class
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonTaskExecutor.class);
46 // The Jython interpreter
47 private final PythonInterpreter interpreter = new PythonInterpreter();
48 private PyCode compiled = null;
51 * Prepares the task for processing.
53 * @throws StateMachineException thrown when a state machine execution error occurs
56 public void prepare() throws StateMachineException {
57 interpreter.setErr(System.err);
58 interpreter.setOut(System.out);
60 // Call generic prepare logic
63 synchronized (Py.class) {
64 final String logic = getSubject().getTaskLogic().getLogic();
65 final String filename = "<" + getSubject().getKey().toString() + ">";
66 compiled = Py.compile_flags(logic, filename, CompileMode.exec, new CompilerFlags());
68 } catch (final PyException e) {
69 LOGGER.warn("failed to compile Jython code for task " + getSubject().getKey().getId(), e);
70 throw new StateMachineException("failed to compile Jython code for task " + getSubject().getKey().getId(),
77 * Executes the executor for the task in a sequential manner.
79 * @param executionId the execution ID for the current APEX policy execution
80 * @param incomingFields the incoming fields
81 * @return The outgoing fields
82 * @throws StateMachineException on an execution error
83 * @throws ContextException on context errors
86 public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
87 throws StateMachineException, ContextException {
89 boolean returnValue = false;
91 // Do execution pre work
92 executePre(executionId, incomingFields);
96 // Check and execute the Jython logic
97 /* Precompiled Version */
98 synchronized (Py.class) {
99 // Set up the Jython engine
100 interpreter.set("executor", getExecutionContext());
101 interpreter.exec(compiled);
102 returnValue = handleInterpreterResult();
105 } catch (final Exception e) {
106 LOGGER.warn("failed to execute Jython code for task " + getSubject().getKey().getId(), e);
107 throw new StateMachineException("failed to execute Jython code for task " + getSubject().getKey().getId(),
111 // Do the execution post work
112 executePost(returnValue);
114 // Send back the return event
115 return getOutgoing();
120 * Handle the result returned by the interpreter.
122 * @return true if the result was successful
123 * @throws StateMachineException on interpreter failures
125 private boolean handleInterpreterResult() throws StateMachineException {
126 final Object ret = interpreter.get("returnValue", java.lang.Boolean.class);
128 LOGGER.error("execute: task logic failed to set a return value for task \"" + getSubject().getKey().getId()
130 throw new StateMachineException("execute: task logic failed to set a return value for task \""
131 + getSubject().getKey().getId() + "\"");
133 return (Boolean) ret;
138 * Cleans up the task after processing.
140 * @throws StateMachineException thrown when a state machine execution error occurs
143 public void cleanUp() throws StateMachineException {
144 interpreter.cleanup();
145 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
146 + "," + getSubject().getTaskLogic().getLogic());