2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.javascript;
25 import java.util.Properties;
26 import org.onap.policy.apex.context.ContextException;
27 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
28 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
33 * The Class JavascriptTaskExecutor is the task executor for task logic written in Javascript It is unlikely that this
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class JavascriptTaskExecutor extends TaskExecutor {
39 // Logger for this class
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskExecutor.class);
42 private JavascriptExecutor javascriptExecutor;
45 * Prepares the task for processing.
47 * @throws StateMachineException thrown when a state machine execution error occurs
50 public void prepare() throws StateMachineException {
51 // Call generic prepare logic
54 // Create the executor
55 if (javascriptExecutor == null) {
56 javascriptExecutor = new JavascriptExecutor(getSubject().getKey());
59 // Initialize and cleanup the executor to check the Javascript code
60 javascriptExecutor.init(getSubject().getTaskLogic().getLogic());
64 * Executes the executor for the task in a sequential manner.
66 * @param executionId the execution ID for the current APEX policy execution
67 * @param executionProperties properties for the current APEX policy execution
68 * @param incomingFields the incoming fields
69 * @return The outgoing fields
70 * @throws StateMachineException on an execution error
71 * @throws ContextException on context errors
74 public Map<String, Object> execute(final long executionId, final Properties executionProperties,
75 final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
77 // Do execution pre work
78 executePre(executionId, executionProperties, incomingFields);
80 // Execute the Javascript executor
81 boolean result = javascriptExecutor.execute(getExecutionContext());
83 // Execute the Javascript
90 * Cleans up the task after processing.
92 * @throws StateMachineException thrown when a state machine execution error occurs
95 public void cleanUp() throws StateMachineException {
96 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
97 + "," + getSubject().getTaskLogic().getLogic());
99 if (javascriptExecutor != null) {
100 javascriptExecutor.cleanUp();
103 javascriptExecutor = null;