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