cf94793ff09e2149aab92933e8c8eec3a3cb5a3d
[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 org.onap.policy.apex.context.ContextException;
24 import org.onap.policy.apex.core.engine.event.EnEvent;
25 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
26 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
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 JythonTaskSelectExecutor is the task selection executor for task selection logic written in Jython It is
37  * unlikely that this is thread safe.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class JythonTaskSelectExecutor extends TaskSelectExecutor {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonTaskSelectExecutor.class);
43
44     // The Jython interpreter
45     private final PythonInterpreter interpreter = new PythonInterpreter();
46     private PyCode compiled = null;
47
48     /**
49      * Prepares the task for processing.
50      *
51      * @throws StateMachineException thrown when a state machine execution error occurs
52      */
53     @Override
54     public void prepare() throws StateMachineException {
55         interpreter.setErr(System.err);
56         interpreter.setOut(System.out);
57
58         // Call generic prepare logic
59         super.prepare();
60         try {
61             synchronized (Py.class) {
62                 compiled = Py.compile_flags(getSubject().getTaskSelectionLogic().getLogic(),
63                         "<" + getSubject().getKey().toString() + ">", "exec", null);
64             }
65         } catch (final PyException e) {
66             LOGGER.warn("failed to compile Jython code for task selection logic in " + getSubject().getKey().getID(),
67                     e);
68             throw new StateMachineException(
69                     "failed to compile Jython code for task selection logic in " + getSubject().getKey().getID(), e);
70         }
71
72     }
73
74     /**
75      * Executes the executor for the task in a sequential manner.
76      *
77      * @param executionID the execution ID for the current APEX policy execution
78      * @param incomingEvent the incoming event
79      * @return The outgoing event
80      * @throws StateMachineException on an execution error
81      * @throws ContextException on context errors
82      */
83     @Override
84     public AxArtifactKey execute(final long executionID, final EnEvent incomingEvent)
85             throws StateMachineException, ContextException {
86
87         boolean returnValue = false;
88
89         // Do execution pre work
90         executePre(executionID, incomingEvent);
91
92         try {
93             // Check and execute the Jython logic
94             /* Precompiled Version */
95             synchronized (Py.class) {
96                 // Set up the Jython engine
97                 interpreter.set("executor", getExecutionContext());
98                 interpreter.exec(compiled);
99
100                 try {
101                     final Object ret = interpreter.get("returnValue", java.lang.Boolean.class);
102                     if (ret == null) {
103                         LOGGER.error("execute: task selection logic failed to set a return value for state  \""
104                                 + getSubject().getKey().getID() + "\"");
105                         throw new StateMachineException(
106                                 "execute: task selection logic failed to set a return value for state  \""
107                                         + getSubject().getKey().getID() + "\"");
108                     }
109                     returnValue = (Boolean) ret;
110                 } catch (NullPointerException | ClassCastException e) {
111                     LOGGER.error("execute: task selection logic failed to set a correct return value for state  \""
112                             + getSubject().getKey().getID() + "\"", e);
113                     throw new StateMachineException(
114                             "execute: task selection logic failed to set a return value for state  \""
115                                     + getSubject().getKey().getID() + "\"",
116                             e);
117                 }
118             }
119             /* */
120         } catch (final Exception e) {
121             LOGGER.warn("failed to execute Jython code for task selection logic in " + getSubject().getKey().getID(),
122                     e);
123             throw new StateMachineException(
124                     "failed to execute Jython code for task selection logic in " + getSubject().getKey().getID(), e);
125         }
126
127         // Do the execution post work
128         executePost(returnValue);
129
130         // Send back the return event
131         if (returnValue) {
132             return getOutgoing();
133         } else {
134             return null;
135         }
136     }
137
138     /**
139      * Cleans up the task after processing.
140      *
141      * @throws StateMachineException thrown when a state machine execution error occurs
142      */
143     @Override
144     public void cleanUp() throws StateMachineException {
145         interpreter.cleanup();
146         LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + ","
147                 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
148                 + getSubject().getTaskSelectionLogic().getLogic());
149     }
150 }