4b6ff023282bc625aeb599ff0313675af00824a8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.executor.javascript;
23
24 import java.util.Map;
25 import javax.script.Compilable;
26 import javax.script.CompiledScript;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
32 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * The Class JavascriptTaskExecutor is the task executor for task logic written in Javascript It is
38  * unlikely that this is thread safe.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class JavascriptTaskExecutor extends TaskExecutor {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskExecutor.class);
45
46     // Javascript engine
47     private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
48     private CompiledScript compiled = null;
49
50     /**
51      * Prepares the task for processing.
52      *
53      * @throws StateMachineException thrown when a state machine execution error occurs
54      */
55     @Override
56     public void prepare() throws StateMachineException {
57         // Call generic prepare logic
58         super.prepare();
59         try {
60             compiled = ((Compilable) engine).compile(getSubject().getTaskLogic().getLogic());
61         } catch (final ScriptException e) {
62             LOGGER.error("execute: task logic failed to compile for task  \"" + getSubject().getKey().getId() + "\"");
63             throw new StateMachineException(
64                     "task logic failed to compile for task  \"" + getSubject().getKey().getId() + "\"", e);
65         }
66     }
67
68     /**
69      * Executes the executor for the task in a sequential manner.
70      *
71      * @param executionId the execution ID for the current APEX policy execution
72      * @param incomingFields the incoming fields
73      * @return The outgoing fields
74      * @throws StateMachineException on an execution error
75      * @throws ContextException on context errors
76      */
77     @Override
78     public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
79             throws StateMachineException, ContextException {
80         // Do execution pre work
81         executePre(executionId, incomingFields);
82
83         // Set up the Javascript engine
84         engine.put("executor", getExecutionContext());
85
86         // Check and execute the Javascript logic
87         try {
88             if (compiled == null) {
89                 engine.eval(getSubject().getTaskLogic().getLogic());
90             } else {
91                 compiled.eval(engine.getContext());
92             }
93         } catch (final ScriptException e) {
94             LOGGER.error("execute: task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"");
95             throw new StateMachineException(
96                     "task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"", e);
97         }
98
99         final Object ret = engine.get("returnValue");
100         if (ret == null) {
101             LOGGER.error("execute: task logic failed to set a return value for task  \"" + getSubject().getKey().getId()
102                     + "\"");
103             throw new StateMachineException("execute: task logic failed to set a return value for task  \""
104                     + getSubject().getKey().getId() + "\"");
105         }
106
107         // Do the execution post work
108         executePost((Boolean) ret);
109
110         return getOutgoing();
111     }
112
113     /**
114      * Cleans up the task after processing.
115      *
116      * @throws StateMachineException thrown when a state machine execution error occurs
117      */
118     @Override
119     public void cleanUp() throws StateMachineException {
120         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
121                 + "," + getSubject().getTaskLogic().getLogic());
122         engine = null;
123     }
124 }