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