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