8d2ccffe1a639855eeb859f508254d134b0d6701
[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.jruby;
22
23 import java.util.Map;
24
25 import org.jruby.embed.EmbedEvalUnit;
26 import org.jruby.embed.LocalContextScope;
27 import org.jruby.embed.LocalVariableBehavior;
28 import org.jruby.embed.ScriptingContainer;
29 import org.jruby.runtime.builtin.IRubyObject;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
32 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * The Class JrubyTaskExecutor is the task executor for task logic written in JRuby.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class JrubyTaskExecutor extends TaskExecutor {
42     // Logger for this class
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskExecutor.class);
44
45     // Jruby container
46     private ScriptingContainer container =
47             new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true);
48     private EmbedEvalUnit parsedjruby = null;
49
50     /**
51      * Prepares the task for processing.
52      *
53      * @throws StateMachineException thrown when a state machine execution error occurs
54      */
55     @Override
56     public void prepare() throws StateMachineException {
57         // Call generic prepare logic
58         super.prepare();
59
60         // Set up the JRuby engine
61         container = (container == null)
62                 ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true)
63                 : container;
64         container.setError(System.err);
65         container.setOutput(System.out);
66         container.put("executor", getExecutionContext()); // needed for the compile
67         parsedjruby = container.parse(getSubject().getTaskLogic().getLogic());
68     }
69
70     /**
71      * Executes the executor for the task in a sequential manner.
72      *
73      * @param executionId the execution ID for the current APEX policy execution
74      * @param incomingFields the incoming fields
75      * @return The outgoing fields
76      * @throws StateMachineException on an execution error
77      * @throws ContextException on context errors
78      */
79     @Override
80     public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
81             throws StateMachineException, ContextException {
82         // Do execution pre work
83         executePre(executionId, incomingFields);
84
85         // Check and execute the JRuby logic
86         container.put("executor", getExecutionContext());
87
88         /* Precompiled version */
89         boolean returnValue = false;
90         final IRubyObject ret = parsedjruby.run();
91         if (ret != null) {
92             final Boolean retbool = ret.toJava(java.lang.Boolean.class);
93             if (retbool != null) {
94                 returnValue = true;
95             }
96         }
97
98         // Do the execution post work
99         executePost(returnValue);
100
101         // Send back the return event
102         return getOutgoing();
103     }
104
105     /**
106      * Cleans up the task after processing.
107      *
108      * @throws StateMachineException thrown when a state machine execution error occurs
109      */
110     @Override
111     public void cleanUp() throws StateMachineException {
112         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
113                 + "," + getSubject().getTaskLogic().getLogic());
114         container.terminate();
115         container = null;
116     }
117 }