diff options
author | Liam Fallon <liam.fallon@ericsson.com> | 2018-06-12 10:08:51 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-06-12 10:08:51 +0000 |
commit | cea4732c49f8d03f8078c802d8a032607e5936d2 (patch) | |
tree | fb356fdd9ab8ac87d7679fb8cd26d070b90c2ddd /plugins | |
parent | 40f1df1cb571959d1d089ffe1d64b39041e3d634 (diff) | |
parent | d48fd41c995cca495a945da4c183a70bff765dea (diff) |
Merge "Adding plugin-executor module to apex-pdp"
Diffstat (limited to 'plugins')
25 files changed, 2022 insertions, 33 deletions
diff --git a/plugins/plugins-executor/plugins-executor-java/pom.xml b/plugins/plugins-executor/plugins-executor-java/pom.xml new file mode 100644 index 000000000..159e19333 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/pom.xml @@ -0,0 +1,36 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2018 Ericsson. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>plugins-executor-java</artifactId> + <name>${project.artifactId}</name> + <description>[${project.parent.artifactId}] Plugin for execution of Java logic in Apex</description> + + <properties> + <apex-plugins-executor-java-dir>${project.basedir}/src</apex-plugins-executor-java-dir> + </properties> +</project>
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParameters.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParameters.java new file mode 100644 index 000000000..64abeac47 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaExecutorParameters.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.java; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * This class provides executor parameters for the Java Executor plugin. It specifies the classes that provide the Java + * implementations of the abstract classes {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaExecutorParameters extends ExecutorParameters { + /** + * Constructor that sets the abstract implementation classes. + */ + public JavaExecutorParameters() { + this.setTaskExecutorPluginClass(JavaTaskExecutor.class.getCanonicalName()); + this.setTaskSelectionExecutorPluginClass(JavaTaskSelectExecutor.class.getCanonicalName()); + this.setStateFinalizerExecutorPluginClass(JavaStateFinalizerExecutor.class.getCanonicalName()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java new file mode 100644 index 000000000..52b78733a --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaStateFinalizerExecutor.java @@ -0,0 +1,118 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.java; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavaStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Java. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaStateFinalizerExecutor extends StateFinalizerExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaStateFinalizerExecutor.class); + + // The Java State Finalizer executor class + private Object stateFinalizerLogicObject = null; + + /** + * Prepares the state finalizer for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Get the class for state finalizer execution + try { + // Create the state finalizer logic object from the byte code of the class + stateFinalizerLogicObject = Class.forName(getSubject().getLogic()).newInstance(); + } catch (final Exception e) { + LOGGER.error("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e); + throw new StateMachineException("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e); + } + } + + /** + * Executes the executor for the state finalizer logic in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields for finalisation + * @return The state output for the state + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public String execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Check and execute the Java logic + boolean returnValue = false; + try { + // Find and call the method with the signature "public boolean getStateOutput(final + // StateFinalizerExecutionContext executor) throws ApexException" + // to invoke the + // task logic in the Java class + final Method method = stateFinalizerLogicObject.getClass().getDeclaredMethod("getStateOutput", + new Class[] { StateFinalizerExecutionContext.class }); + returnValue = (boolean) method.invoke(stateFinalizerLogicObject, getExecutionContext()); + } catch (final Exception e) { + LOGGER.error("execute: state finalizer logic failed to run for state finalizer \"" + getSubject().getID() + + "\""); + throw new StateMachineException( + "state finalizer logic failed to run for state finalizer \"" + getSubject().getID() + "\"", e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the state finalizer after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getID() + "," + getSubject().getLogicFlavour() + "," + + getSubject().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java new file mode 100644 index 000000000..6343f1a9f --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskExecutor.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.java; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavaTaskExecutor is the task executor for task logic written in Java. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaTaskExecutor extends TaskExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskExecutor.class); + + // The Java Task executor class + private Object taskLogicObject = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Get the class for task execution + try { + // Create the task logic object from the byte code of the class + taskLogicObject = Class.forName(getSubject().getTaskLogic().getLogic()).newInstance(); + } catch (final Exception e) { + LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e); + throw new StateMachineException( + "instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e); + } + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields + * @return The outgoing fields + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public Map<String, Object> execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Check and execute the Java logic + boolean returnValue = false; + try { + // Find and call the method with the signature "public boolean getEvent(final TaskExecutionContext executor) + // throws ApexException" to invoke the + // task logic in the Java class + final Method method = taskLogicObject.getClass().getDeclaredMethod("getEvent", + new Class[] { TaskExecutionContext.class }); + returnValue = (boolean) method.invoke(taskLogicObject, getExecutionContext()); + } catch (final Exception e) { + LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "task logic failed to run for task \"" + getSubject().getKey().getID() + "\"", e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getTaskLogic().getLogicFlavour() + + "," + getSubject().getTaskLogic().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java new file mode 100644 index 000000000..ee33c52d1 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/JavaTaskSelectExecutor.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.java; + +import java.lang.reflect.Method; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavaTaskSelectExecutor is the task selection executor for task selection logic written in Java. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaTaskSelectExecutor extends TaskSelectExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskSelectExecutor.class); + + // The Java Task Selection executor class + private Object taskSelectionLogicObject = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Get the class for task selection + try { + // Create the task logic object from the byte code of the class + taskSelectionLogicObject = Class.forName(getSubject().getTaskSelectionLogic().getLogic()).newInstance(); + } catch (final Exception e) { + LOGGER.error("instantiation error on Java class" + taskSelectionLogicObject, e); + throw new StateMachineException("instantiation error on Java class" + taskSelectionLogicObject, e); + } + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingEvent the incoming event + * @return The outgoing event + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public AxArtifactKey execute(final long executionID, final EnEvent incomingEvent) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingEvent); + + // Check and execute the Java logic + boolean returnValue = false; + try { + // Find and call the method with the signature "public boolean getTask(final TaskSelectionExecutionContext + // executor)" to invoke the task selection + // logic in the Java class + final Method method = taskSelectionLogicObject.getClass().getDeclaredMethod("getTask", + new Class[] { TaskSelectionExecutionContext.class }); + returnValue = (boolean) method.invoke(taskSelectionLogicObject, getExecutionContext()); + } catch (final Exception e) { + LOGGER.error( + "execute: task selection logic failed to run for state \"" + getSubject().getKey().getID() + "\"", + e); + throw new StateMachineException( + "task selection logic failed to run for state \"" + getSubject().getKey().getID() + "\"", e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + + getSubject().getTaskSelectionLogic().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/package-info.java b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/package-info.java new file mode 100644 index 000000000..21f37b0e2 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-java/src/main/java/org/onap/policy/apex/plugins/executor/java/package-info.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Implements the Java executor plugin for Apex, providing extensions of the abstract classes + * {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.executor.java; diff --git a/plugins/plugins-executor/plugins-executor-javascript/pom.xml b/plugins/plugins-executor/plugins-executor-javascript/pom.xml new file mode 100644 index 000000000..51297018a --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/pom.xml @@ -0,0 +1,36 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2018 Ericsson. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>plugins-executor-javascript</artifactId> + <name>${project.artifactId}</name> + <description>[${project.parent.artifactId}] Plugin for execution of Javascript logic in Apex</description> + + <properties> + <apex-plugins-executor-javascript-dir>${project.basedir}/src</apex-plugins-executor-javascript-dir> + </properties> +</project>
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorParameters.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorParameters.java new file mode 100644 index 000000000..bbbea9308 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorParameters.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.javascript; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * This class provides executor parameters for the Javascipt Executor plugin. It specifies the classes that provide the + * Javascript implementations of the abstract classes {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavascriptExecutorParameters extends ExecutorParameters { + /** + * Constructor that sets the abstract implementation classes. + */ + public JavascriptExecutorParameters() { + this.setTaskExecutorPluginClass(JavascriptTaskExecutor.class.getCanonicalName()); + this.setTaskSelectionExecutorPluginClass(JavascriptTaskSelectExecutor.class.getCanonicalName()); + this.setStateFinalizerExecutorPluginClass(JavascriptStateFinalizerExecutor.class.getCanonicalName()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java new file mode 100644 index 000000000..66c036375 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.javascript; + +import java.util.Map; + +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavascriptStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in + * Javascript It is unlikely that this is thread safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptStateFinalizerExecutor.class); + + // Javascript engine + private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); + private CompiledScript compiled = null; + + /** + * Prepares the state finalizer for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + try { + compiled = ((Compilable) engine).compile(getSubject().getLogic()); + } catch (final ScriptException e) { + LOGGER.error("execute: state finalizer logic failed to compile for state finalizer \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException("state finalizer logic failed to compile for state finalizer \"" + + getSubject().getKey().getID() + "\"", e); + } + } + + /** + * Executes the executor for the state finalizer logic in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields for finalisation + * @return The state output for the state + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public String execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Set up the Javascript engine + engine.put("executor", getExecutionContext()); + + // Check and execute the Javascript logic + boolean returnValue = false; + try { + if (compiled == null) { + engine.eval(getSubject().getLogic()); + } else { + compiled.eval(engine.getContext()); + } + } catch (final ScriptException e) { + LOGGER.error("execute: state finalizer logic failed to run for state finalizer \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException("state finalizer logic failed to run for state finalizer \"" + + getSubject().getKey().getID() + "\"", e); + } + + returnValue = (boolean) engine.get("returnValue"); + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the state finalizer after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getLogicFlavour() + "," + + getSubject().getLogic()); + engine = null; + } +} diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java new file mode 100644 index 000000000..80f744b5d --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.javascript; + +import java.util.Map; + +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavascriptTaskExecutor is the task executor for task logic written in Javascript It is unlikely that this + * is thread safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavascriptTaskExecutor extends TaskExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskExecutor.class); + + // Javascript engine + private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); + private CompiledScript compiled = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + try { + compiled = ((Compilable) engine).compile(getSubject().getTaskLogic().getLogic()); + } catch (final ScriptException e) { + LOGGER.error("execute: task logic failed to compile for task \"" + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "task logic failed to compile for task \"" + getSubject().getKey().getID() + "\"", e); + } + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields + * @return The outgoing fields + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public Map<String, Object> execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Set up the Javascript engine + engine.put("executor", getExecutionContext()); + + // Check and execute the Javascript logic + boolean returnValue = false; + try { + if (compiled == null) { + engine.eval(getSubject().getTaskLogic().getLogic()); + } else { + compiled.eval(engine.getContext()); + } + } catch (final ScriptException e) { + LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "task logic failed to run for task \"" + getSubject().getKey().getID() + "\"", e); + } + + try { + final Object ret = engine.get("returnValue"); + if (ret == null) { + LOGGER.error("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getID() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getID() + "\"", e); + throw new StateMachineException("execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\"", e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getTaskLogic().getLogicFlavour() + + "," + getSubject().getTaskLogic().getLogic()); + engine = null; + } +} diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java new file mode 100644 index 000000000..aa9c6650f --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java @@ -0,0 +1,144 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.javascript; + +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JavascriptTaskSelectExecutor is the task selection executor for task selection logic written in Javascript + * It is unlikely that this is thread safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavascriptTaskSelectExecutor extends TaskSelectExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskSelectExecutor.class); + + // Javascript engine + private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); + private CompiledScript compiled = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + try { + compiled = ((Compilable) engine).compile(getSubject().getTaskSelectionLogic().getLogic()); + } catch (final ScriptException e) { + LOGGER.error("execute: task selection logic failed to compile for state \"" + getSubject().getKey().getID() + + "\""); + throw new StateMachineException( + "task selection logic failed to compile for state \"" + getSubject().getKey().getID() + "\"", e); + } + + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingEvent the incoming event + * @return The outgoing event + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public AxArtifactKey execute(final long executionID, final EnEvent incomingEvent) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingEvent); + + // Set up the Javascript engine + engine.put("executor", getExecutionContext()); + + // Check and execute the Javascript logic + boolean returnValue = false; + try { + if (compiled == null) { + engine.eval(getSubject().getTaskSelectionLogic().getLogic()); + } else { + compiled.eval(engine.getContext()); + } + } catch (final ScriptException e) { + LOGGER.error( + "execute: task selection logic failed to run for state \"" + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "task selection logic failed to run for state \"" + getSubject().getKey().getID() + "\"", e); + } + + try { + final Object ret = engine.get("returnValue"); + if (ret == null) { + LOGGER.error("execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getID() + "\"", e); + throw new StateMachineException("execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\"", e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + + getSubject().getTaskSelectionLogic().getLogic()); + engine = null; + } +} diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/package-info.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/package-info.java new file mode 100644 index 000000000..7f7ba3918 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/package-info.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Implements the Javascript executor plugin for APEX, providing extensions of the abstract classes + * {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.executor.javascript; diff --git a/plugins/plugins-executor/plugins-executor-jruby/pom.xml b/plugins/plugins-executor/plugins-executor-jruby/pom.xml new file mode 100644 index 000000000..c9284a20d --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/pom.xml @@ -0,0 +1,44 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2018 Ericsson. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>plugins-executor-jruby</artifactId> + <name>${project.artifactId}</name> + <description>[${project.parent.artifactId}] Plugin for execution of JRuby logic in Apex</description> + + <properties> + <apex-plugins-executor-jruby-dir>${project.basedir}/src</apex-plugins-executor-jruby-dir> + </properties> + + <dependencies> + <dependency> + <groupId>org.jruby</groupId> + <artifactId>jruby-complete</artifactId> + <version>1.7.26</version> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParameters.java b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParameters.java new file mode 100644 index 000000000..0245cd80d --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParameters.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jruby; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * This class provides executor parameters for the JRuby Executor plugin. It specifies the classes that provide the Juby + * implementations of the abstract classes {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JrubyExecutorParameters extends ExecutorParameters { + /** + * Constructor that sets the abstract implementation classes. + */ + public JrubyExecutorParameters() { + this.setTaskExecutorPluginClass(JrubyTaskExecutor.class.getCanonicalName()); + this.setTaskSelectionExecutorPluginClass(JrubyTaskSelectExecutor.class.getCanonicalName()); + this.setStateFinalizerExecutorPluginClass(JrubyStateFinalizerExecutor.class.getCanonicalName()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutor.java new file mode 100644 index 000000000..41ad359eb --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutor.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jruby; + +import java.util.Map; + +import org.jruby.embed.EmbedEvalUnit; +import org.jruby.embed.LocalContextScope; +import org.jruby.embed.LocalVariableBehavior; +import org.jruby.embed.ScriptingContainer; +import org.jruby.runtime.builtin.IRubyObject; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JrubyStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in JRuby. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JrubyStateFinalizerExecutor extends StateFinalizerExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyStateFinalizerExecutor.class); + + // Jruby container + private ScriptingContainer container = + new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true); + private EmbedEvalUnit parsedjruby = null; + + /** + * Prepares the state finalizer for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Set up the JRuby engine + container = (container == null) + ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true) + : container; + container.setError(System.err); + container.setOutput(System.out); + container.put("executor", getExecutionContext()); // needed for the compile + parsedjruby = container.parse(getSubject().getLogic()); + } + + /** + * Executes the executor for the state finalizer logic in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields for finalisation + * @return The state output for the state + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public String execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Check and execute the JRuby logic + container.put("executor", getExecutionContext()); + + /* Precompiled version */ + boolean returnValue = false; + final IRubyObject ret = parsedjruby.run(); + if (ret != null) { + final Boolean retbool = (Boolean) ret.toJava(java.lang.Boolean.class); + if (retbool != null) { + returnValue = true; + } + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the state finalizer after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getLogicFlavour() + "," + + getSubject().getLogic()); + container.terminate(); + container = null; + } +} diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutor.java b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutor.java new file mode 100644 index 000000000..8db650fa1 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutor.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jruby; + +import java.util.Map; + +import org.jruby.embed.EmbedEvalUnit; +import org.jruby.embed.LocalContextScope; +import org.jruby.embed.LocalVariableBehavior; +import org.jruby.embed.ScriptingContainer; +import org.jruby.runtime.builtin.IRubyObject; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JrubyTaskExecutor is the task executor for task logic written in JRuby. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JrubyTaskExecutor extends TaskExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskExecutor.class); + + // Jruby container + private ScriptingContainer container = + new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true); + private EmbedEvalUnit parsedjruby = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Set up the JRuby engine + container = (container == null) + ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true) + : container; + container.setError(System.err); + container.setOutput(System.out); + container.put("executor", getExecutionContext()); // needed for the compile + parsedjruby = container.parse(getSubject().getTaskLogic().getLogic()); + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields + * @return The outgoing fields + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public Map<String, Object> execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingFields); + + // Check and execute the JRuby logic + container.put("executor", getExecutionContext()); + + /* Precompiled version */ + boolean returnValue = false; + final IRubyObject ret = parsedjruby.run(); + if (ret != null) { + final Boolean retbool = (Boolean) ret.toJava(java.lang.Boolean.class); + if (retbool != null) { + returnValue = true; + } + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getTaskLogic().getLogicFlavour() + + "," + getSubject().getTaskLogic().getLogic()); + container.terminate(); + container = null; + } +} diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutor.java new file mode 100644 index 000000000..85c7ba018 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutor.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jruby; + +import java.util.Set; +import java.util.TreeSet; + +import org.jruby.embed.EmbedEvalUnit; +import org.jruby.embed.LocalContextScope; +import org.jruby.embed.LocalVariableBehavior; +import org.jruby.embed.ScriptingContainer; +import org.jruby.runtime.builtin.IRubyObject; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JrubyTaskSelectExecutor is the task selection executor for task selection logic written in JRuby. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JrubyTaskSelectExecutor extends TaskSelectExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskSelectExecutor.class); + + // Jruby container + private ScriptingContainer container = + new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true); + private EmbedEvalUnit parsedjruby = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + // Call generic prepare logic + super.prepare(); + + // Set up the JRuby engine + container = (container == null) + ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true) + : container; + container.put("executor", getExecutionContext()); // needed for compile as a placeholder + parsedjruby = container.parse(getSubject().getTaskSelectionLogic().getLogic()); + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingEvent the incoming event + * @return The outgoing event + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public AxArtifactKey execute(final long executionID, final EnEvent incomingEvent) + throws StateMachineException, ContextException { + // Do execution pre work + executePre(executionID, incomingEvent); + + // Check and execute the JRuby logic + container.put("executor", getExecutionContext()); + + /* Precompiled version */ + boolean returnValue = false; + final IRubyObject ret = parsedjruby.run(); + if (ret != null) { + final Boolean retbool = (Boolean) ret.toJava(java.lang.Boolean.class); + if (retbool != null) { + returnValue = true; + } + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + + getSubject().getTaskSelectionLogic().getLogic()); + container.terminate(); + container = null; + } + + /** + * Gets the output event set. + * + * @return the output event set + */ + public Set<AxArtifactKey> getOutputEventSet() { + return new TreeSet<>(); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/package-info.java b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/package-info.java new file mode 100644 index 000000000..9eee0ee78 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/package-info.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Implements the JRuby executor plugin for APEX, providing extensions of the abstract classes + * {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.executor.jruby; diff --git a/plugins/plugins-executor/plugins-executor-jython/pom.xml b/plugins/plugins-executor/plugins-executor-jython/pom.xml new file mode 100644 index 000000000..8b246561b --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/pom.xml @@ -0,0 +1,44 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2018 Ericsson. All rights reserved. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> + <artifactId>plugins-executor</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + + <artifactId>plugins-executor-jython</artifactId> + <name>${project.artifactId}</name> + <description>[${project.parent.artifactId}] Plugin for execution of Jython logic in Apex</description> + + <properties> + <apex-plugins-executor-jython-dir>${project.basedir}/src</apex-plugins-executor-jython-dir> + </properties> + + <dependencies> + <dependency> + <groupId>org.python</groupId> + <artifactId>jython</artifactId> + <version>2.2.1</version> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonExecutorParameters.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonExecutorParameters.java new file mode 100644 index 000000000..7a22013f1 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonExecutorParameters.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jython; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * This class provides executor parameters for the Jython Executor plugin. It specifies the classes that provide the + * Jython implementations of the abstract classes {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JythonExecutorParameters extends ExecutorParameters { + /** + * Constructor that sets the abstract implementation classes. + */ + public JythonExecutorParameters() { + this.setTaskExecutorPluginClass(JythonTaskExecutor.class.getCanonicalName()); + this.setTaskSelectionExecutorPluginClass(JythonTaskSelectExecutor.class.getCanonicalName()); + this.setStateFinalizerExecutorPluginClass(JythonStateFinalizerExecutor.class.getCanonicalName()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonStateFinalizerExecutor.java new file mode 100644 index 000000000..26512b52d --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonStateFinalizerExecutor.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jython; + +import java.util.Map; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.python.core.Py; +import org.python.core.PyCode; +import org.python.core.PyException; +import org.python.util.PythonInterpreter; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JythonStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Jython It + * is unlikely that this is thread safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JythonStateFinalizerExecutor extends StateFinalizerExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonStateFinalizerExecutor.class); + + // The Jython interpreter + private final PythonInterpreter interpreter = new PythonInterpreter(); + private PyCode compiled = null; + + /** + * Prepares the state finalizer for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + interpreter.setErr(System.err); + interpreter.setOut(System.out); + + // Call generic prepare logic + super.prepare(); + try { + synchronized (Py.class) { + compiled = Py.compile_flags(getSubject().getLogic(), "<" + getSubject().getKey().toString() + ">", + "exec", null); + } + } catch (final PyException e) { + LOGGER.warn("failed to compile Jython code for state finalizer " + getSubject().getKey(), e); + throw new StateMachineException( + "failed to compile Jython code for state finalizer " + getSubject().getKey(), e); + } + + } + + /** + * Executes the executor for the state finalizer logic in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields for finalisation + * @return The state output for the state + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public String execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + + boolean returnValue = false; + + // Do execution pre work + executePre(executionID, incomingFields); + + try { + + // Check and execute the Jython logic + /* Precompiled Version */ + synchronized (Py.class) { + // Set up the Jython engine + interpreter.set("executor", getExecutionContext()); + interpreter.exec(compiled); + returnValue = (boolean) interpreter.get("returnValue", java.lang.Boolean.class); + } + /* */ + } catch (final Exception e) { + LOGGER.warn("failed to execute Jython code for state finalizer " + getSubject().getKey(), e); + throw new StateMachineException( + "failed to execute Jython code for state finalizer " + getSubject().getKey(), e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the state finalizer after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + interpreter.cleanup(); + LOGGER.debug("cleanUp:" + getSubject().getKey() + "," + getSubject().getLogicFlavour() + "," + + getSubject().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java new file mode 100644 index 000000000..4387a5df7 --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jython; + +import java.util.Map; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.python.core.Py; +import org.python.core.PyCode; +import org.python.core.PyException; +import org.python.util.PythonInterpreter; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JythonTaskExecutor is the task executor for task logic written in Jython It is unlikely that this is thread + * safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JythonTaskExecutor extends TaskExecutor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonTaskExecutor.class); + + // The Jython interpreter + private final PythonInterpreter interpreter = new PythonInterpreter(); + private PyCode compiled = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + interpreter.setErr(System.err); + interpreter.setOut(System.out); + + // Call generic prepare logic + super.prepare(); + try { + synchronized (Py.class) { + compiled = Py.compile_flags(getSubject().getTaskLogic().getLogic(), + "<" + getSubject().getKey().toString() + ">", "exec", null); + } + } catch (final PyException e) { + LOGGER.warn("failed to compile Jython code for task " + getSubject().getKey().getID(), e); + throw new StateMachineException("failed to compile Jython code for task " + getSubject().getKey().getID(), + e); + } + + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingFields the incoming fields + * @return The outgoing fields + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public Map<String, Object> execute(final long executionID, final Map<String, Object> incomingFields) + throws StateMachineException, ContextException { + + boolean returnValue = false; + + // Do execution pre work + executePre(executionID, incomingFields); + + try { + + // Check and execute the Jython logic + /* Precompiled Version */ + synchronized (Py.class) { + // Set up the Jython engine + interpreter.set("executor", getExecutionContext()); + interpreter.exec(compiled); + try { + final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); + if (ret == null) { + LOGGER.error("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getID() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getID() + "\"", e); + throw new StateMachineException( + "execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\"", + e); + } + } + /* */ + } catch (final Exception e) { + LOGGER.warn("failed to execute Jython code for task " + getSubject().getKey().getID(), e); + throw new StateMachineException("failed to execute Jython code for task " + getSubject().getKey().getID(), + e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + interpreter.cleanup(); + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + getSubject().getTaskLogic().getLogicFlavour() + + "," + getSubject().getTaskLogic().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java new file mode 100644 index 000000000..cf94793ff --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java @@ -0,0 +1,150 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.executor.jython; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.python.core.Py; +import org.python.core.PyCode; +import org.python.core.PyException; +import org.python.util.PythonInterpreter; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class JythonTaskSelectExecutor is the task selection executor for task selection logic written in Jython It is + * unlikely that this is thread safe. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JythonTaskSelectExecutor extends TaskSelectExecutor { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JythonTaskSelectExecutor.class); + + // The Jython interpreter + private final PythonInterpreter interpreter = new PythonInterpreter(); + private PyCode compiled = null; + + /** + * Prepares the task for processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void prepare() throws StateMachineException { + interpreter.setErr(System.err); + interpreter.setOut(System.out); + + // Call generic prepare logic + super.prepare(); + try { + synchronized (Py.class) { + compiled = Py.compile_flags(getSubject().getTaskSelectionLogic().getLogic(), + "<" + getSubject().getKey().toString() + ">", "exec", null); + } + } catch (final PyException e) { + LOGGER.warn("failed to compile Jython code for task selection logic in " + getSubject().getKey().getID(), + e); + throw new StateMachineException( + "failed to compile Jython code for task selection logic in " + getSubject().getKey().getID(), e); + } + + } + + /** + * Executes the executor for the task in a sequential manner. + * + * @param executionID the execution ID for the current APEX policy execution + * @param incomingEvent the incoming event + * @return The outgoing event + * @throws StateMachineException on an execution error + * @throws ContextException on context errors + */ + @Override + public AxArtifactKey execute(final long executionID, final EnEvent incomingEvent) + throws StateMachineException, ContextException { + + boolean returnValue = false; + + // Do execution pre work + executePre(executionID, incomingEvent); + + try { + // Check and execute the Jython logic + /* Precompiled Version */ + synchronized (Py.class) { + // Set up the Jython engine + interpreter.set("executor", getExecutionContext()); + interpreter.exec(compiled); + + try { + final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); + if (ret == null) { + LOGGER.error("execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\""); + throw new StateMachineException( + "execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getID() + "\"", e); + throw new StateMachineException( + "execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getID() + "\"", + e); + } + } + /* */ + } catch (final Exception e) { + LOGGER.warn("failed to execute Jython code for task selection logic in " + getSubject().getKey().getID(), + e); + throw new StateMachineException( + "failed to execute Jython code for task selection logic in " + getSubject().getKey().getID(), e); + } + + // Do the execution post work + executePost(returnValue); + + // Send back the return event + if (returnValue) { + return getOutgoing(); + } else { + return null; + } + } + + /** + * Cleans up the task after processing. + * + * @throws StateMachineException thrown when a state machine execution error occurs + */ + @Override + public void cleanUp() throws StateMachineException { + interpreter.cleanup(); + LOGGER.debug("cleanUp:" + getSubject().getKey().getID() + "," + + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + + getSubject().getTaskSelectionLogic().getLogic()); + } +} diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/package-info.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/package-info.java new file mode 100644 index 000000000..f88ff75ef --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/package-info.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Implements the Jython executor plugin for APEX, providing extensions of the abstract classes + * {@link org.onap.policy.apex.core.engine.executor.TaskExecutor}, + * {@link org.onap.policy.apex.core.engine.executor.TaskSelectExecutor}, and + * {@link org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor}. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.executor.jython; diff --git a/plugins/plugins-executor/pom.xml b/plugins/plugins-executor/pom.xml index e18fd17e8..3125d6532 100644 --- a/plugins/plugins-executor/pom.xml +++ b/plugins/plugins-executor/pom.xml @@ -45,37 +45,11 @@ </dependency> </dependencies> - <profiles> - <profile> - <id>apexDefault</id> - <activation> - <activeByDefault>true</activeByDefault> - </activation> - <modules> - <module>plugins-executor-mvel</module> - </modules> - </profile> - <profile> - <id>apexTests</id> - <activation> - <property> - <name>apexTests</name> - </property> - </activation> - <modules> - <module>plugins-executor-mvel</module> - </modules> - </profile> - <profile> - <id>apexAll</id> - <activation> - <property> - <name>apexAll</name> - </property> - </activation> - <modules> - <module>plugins-executor-mvel</module> - </modules> - </profile> - </profiles> + <modules> + <module>plugins-executor-java</module> + <module>plugins-executor-javascript</module> + <module>plugins-executor-jruby</module> + <module>plugins-executor-jython</module> + <module>plugins-executor-mvel</module> + </modules> </project>
\ No newline at end of file |