3944aab500ca4ce620ddabf9acf59e4486fc41ab
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-jruby / src / main / java / org / onap / policy / apex / plugins / executor / jruby / JrubyTaskExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.executor.jruby;
23
24 import java.util.Map;
25 import java.util.Properties;
26
27 import org.jruby.embed.EmbedEvalUnit;
28 import org.jruby.embed.LocalContextScope;
29 import org.jruby.embed.LocalVariableBehavior;
30 import org.jruby.embed.ScriptingContainer;
31 import org.jruby.runtime.builtin.IRubyObject;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
34 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
37
38 /**
39  * The Class JrubyTaskExecutor is the task executor for task logic written in JRuby.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class JrubyTaskExecutor extends TaskExecutor {
44     // Logger for this class
45     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskExecutor.class);
46
47     // Jruby container
48     private ScriptingContainer container = new ScriptingContainer(LocalContextScope.CONCURRENT,
49                     LocalVariableBehavior.TRANSIENT, true);
50     private EmbedEvalUnit parsedjruby = null;
51
52     /**
53      * Prepares the task for processing.
54      *
55      * @throws StateMachineException thrown when a state machine execution error occurs
56      */
57     @Override
58     public void prepare() throws StateMachineException {
59         // Call generic prepare logic
60         super.prepare();
61
62         // Set up the JRuby engine
63         container = (container == null)
64                         ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true)
65                         : container;
66
67         // Use the container.setError(System.err) and container.setOutput(System.out) method calls to redirect output
68         // and error to standard output and error for debugging
69         container.put("executor", getExecutionContext()); // needed for the compile
70         parsedjruby = container.parse(getSubject().getTaskLogic().getLogic());
71     }
72
73     /**
74      * Executes the executor for the task in a sequential manner.
75      *
76      * @param executionId the execution ID for the current APEX policy execution
77      * @param executionProperties properties for the current APEX policy execution
78      * @param incomingFields the incoming fields
79      * @return The outgoing fields
80      * @throws StateMachineException on an execution error
81      * @throws ContextException on context errors
82      */
83     @Override
84     public Map<String, Object> execute(final long executionId, final Properties executionProperties,
85                     final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
86         // Do execution pre work
87         executePre(executionId, executionProperties, incomingFields);
88
89         // Check and execute the JRuby logic
90         container.put("executor", getExecutionContext());
91
92         /* Precompiled version */
93         boolean returnValue = false;
94         final IRubyObject ret = parsedjruby.run();
95         if (ret != null) {
96             final Boolean retbool = ret.toJava(java.lang.Boolean.class);
97             if (retbool != null) {
98                 returnValue = true;
99             }
100         }
101
102         // Do the execution post work
103         executePost(returnValue);
104
105         // Send back the return event
106         return getOutgoing();
107     }
108
109     /**
110      * Cleans up the task after processing.
111      *
112      * @throws StateMachineException thrown when a state machine execution error occurs
113      */
114     @Override
115     public void cleanUp() throws StateMachineException {
116         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
117                         + "," + getSubject().getTaskLogic().getLogic());
118         container.terminate();
119         container = null;
120     }
121 }