3636d811a53e9364d6a1bd9b85bb73e43b9d0290
[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.jython;
22
23 import java.util.Map;
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
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;
35
36 /**
37  * The Class JythonStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Jython It
38  * is unlikely that this is thread safe.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class JythonStateFinalizerExecutor extends StateFinalizerExecutor {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonStateFinalizerExecutor.class);
45
46     // The Jython interpreter
47     private final PythonInterpreter interpreter = new PythonInterpreter();
48     private PyCode 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         interpreter.setErr(System.err);
58         interpreter.setOut(System.out);
59
60         // Call generic prepare logic
61         super.prepare();
62         try {
63             synchronized (Py.class) {
64                 final String logic = getSubject().getLogic();
65                 final String filename = "<" + getSubject().getKey().toString() + ">";
66                 compiled = Py.compile_flags(logic, filename, CompileMode.exec, new CompilerFlags());
67             }
68         } catch (final PyException e) {
69             LOGGER.warn("failed to compile Jython code for state finalizer " + getSubject().getKey(), e);
70             throw new StateMachineException(
71                     "failed to compile Jython code for state finalizer " + getSubject().getKey(), e);
72         }
73
74     }
75
76     /**
77      * Executes the executor for the state finalizer logic in a sequential manner.
78      *
79      * @param executionId the execution ID for the current APEX policy execution
80      * @param incomingFields the incoming fields for finalisation
81      * @return The state output for the state
82      * @throws StateMachineException on an execution error
83      * @throws ContextException on context errors
84      */
85     @Override
86     public String execute(final long executionId, final Map<String, Object> incomingFields)
87             throws StateMachineException, ContextException {
88
89         boolean returnValue = false;
90
91         // Do execution pre work
92         executePre(executionId, incomingFields);
93
94         try {
95
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 = interpreter.get("returnValue", java.lang.Boolean.class);
103             }
104             /* */
105         } catch (final Exception e) {
106             LOGGER.warn("failed to execute Jython code for state finalizer " + getSubject().getKey(), e);
107             throw new StateMachineException(
108                     "failed to execute Jython code for state finalizer " + getSubject().getKey(), e);
109         }
110
111         // Do the execution post work
112         executePost(returnValue);
113
114         // Send back the return event
115         return getOutgoing();
116     }
117
118     /**
119      * Cleans up the state finalizer after processing.
120      *
121      * @throws StateMachineException thrown when a state machine execution error occurs
122      */
123     @Override
124     public void cleanUp() throws StateMachineException {
125         interpreter.cleanup();
126         LOGGER.debug("cleanUp:" + getSubject().getKey() + "," + getSubject().getLogicFlavour() + ","
127                 + getSubject().getLogic());
128     }
129 }