diff options
author | ramverma <ram.krishna.verma@ericsson.com> | 2018-06-11 11:27:47 +0100 |
---|---|---|
committer | ramverma <ram.krishna.verma@ericsson.com> | 2018-06-11 17:17:24 +0100 |
commit | d48fd41c995cca495a945da4c183a70bff765dea (patch) | |
tree | 38b803637a13c5af6afc16ff7e2b7a2c6b168fc3 /plugins/plugins-executor/plugins-executor-javascript | |
parent | d0746b062ca6b85eae756db6eb6c4dead037a35a (diff) |
Adding plugin-executor module to apex-pdp
Change-Id: I711eaaff3707aa6398c26f1a124fedeb1d0e11f4
Issue-ID: POLICY-862
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'plugins/plugins-executor/plugins-executor-javascript')
6 files changed, 518 insertions, 0 deletions
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; |