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