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