9769f42db3876a2ea342c553cec560e5a0947a5d
[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 java.util.Properties;
26
27 import javax.script.Compilable;
28 import javax.script.CompiledScript;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import javax.script.ScriptException;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
34 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
37
38 /**
39  * The Class JavascriptTaskExecutor is the task executor for task logic written in Javascript It is unlikely that this
40  * is thread safe.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class JavascriptTaskExecutor extends TaskExecutor {
45     // Logger for this class
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskExecutor.class);
47
48     // Javascript engine
49     private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
50     private CompiledScript compiled = null;
51
52     /**
53      * Prepares the task for processing.
54      *
55      * @throws StateMachineException thrown when a state machine execution error occurs
56      */
57     @Override
58     public void prepare() throws StateMachineException {
59         // Call generic prepare logic
60         super.prepare();
61         try {
62             compiled = ((Compilable) engine).compile(getSubject().getTaskLogic().getLogic());
63         } catch (final ScriptException e) {
64             LOGGER.error("execute: task logic failed to compile for task  \"" + getSubject().getKey().getId() + "\"");
65             throw new StateMachineException(
66                     "task logic failed to compile for task  \"" + getSubject().getKey().getId() + "\"", e);
67         }
68     }
69
70     /**
71      * Executes the executor for the task in a sequential manner.
72      *
73      * @param executionId the execution ID for the current APEX policy execution
74      * @param executionProperties properties for the current APEX policy execution
75      * @param incomingFields the incoming fields
76      * @return The outgoing fields
77      * @throws StateMachineException on an execution error
78      * @throws ContextException on context errors
79      */
80     @Override
81     public Map<String, Object> execute(final long executionId, final Properties executionProperties,
82             final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
83         // Do execution pre work
84         executePre(executionId, executionProperties, incomingFields);
85
86         // Set up the Javascript engine
87         engine.put("executor", getExecutionContext());
88
89         // Check and execute the Javascript logic
90         try {
91             if (compiled == null) {
92                 engine.eval(getSubject().getTaskLogic().getLogic());
93             } else {
94                 compiled.eval(engine.getContext());
95             }
96         } catch (final ScriptException e) {
97             LOGGER.error("execute: task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"");
98             throw new StateMachineException(
99                     "task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"", e);
100         }
101
102         final Object ret = engine.get("returnValue");
103         if (ret == null) {
104             LOGGER.error("execute: task logic failed to set a return value for task  \"" + getSubject().getKey().getId()
105                     + "\"");
106             throw new StateMachineException("execute: task logic failed to set a return value for task  \""
107                     + getSubject().getKey().getId() + "\"");
108         }
109
110         // Do the execution post work
111         executePost((Boolean) ret);
112
113         return getOutgoing();
114     }
115
116     /**
117      * Cleans up the task after processing.
118      *
119      * @throws StateMachineException thrown when a state machine execution error occurs
120      */
121     @Override
122     public void cleanUp() throws StateMachineException {
123         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
124                 + "," + getSubject().getTaskLogic().getLogic());
125         engine = null;
126     }
127 }