Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-javascript / src / main / java / org / onap / policy / apex / plugins / executor / javascript / JavascriptTaskSelectExecutor.java
1 /*-
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
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.Properties;
25 import org.onap.policy.apex.context.ContextException;
26 import org.onap.policy.apex.core.engine.event.EnEvent;
27 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
28 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * The Class JavascriptTaskSelectExecutor is the task selection executor for task selection logic written in Javascript
35  * It is unlikely that this is thread safe.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class JavascriptTaskSelectExecutor extends TaskSelectExecutor {
40     // Logger for this class
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskSelectExecutor.class);
42
43     private JavascriptExecutor javascriptExecutor;
44
45     /**
46      * Prepares the task selection logic for processing.
47      *
48      * @throws StateMachineException thrown when a state machine execution error occurs
49      */
50     @Override
51     public void prepare() throws StateMachineException {
52         // Call generic prepare logic
53         super.prepare();
54
55         // Create the executor
56         if (javascriptExecutor == null) {
57             javascriptExecutor = new JavascriptExecutor(getSubject().getKey());
58         }
59
60         // Initialize and cleanup the executor to check the Javascript code
61         javascriptExecutor.init(getSubject().getTaskSelectionLogic().getLogic());
62     }
63
64     /**
65      * Executes the executor for the task in a sequential manner.
66      *
67      * @param executionId the execution ID for the current APEX policy execution
68      * @param executionProperties properties for the current APEX policy execution
69      * @param incomingEvent the incoming event
70      * @return The outgoing event
71      * @throws StateMachineException on an execution error
72      * @throws ContextException on context errors
73      */
74     @Override
75     public AxArtifactKey execute(final long executionId, final Properties executionProperties,
76         final EnEvent incomingEvent) throws StateMachineException, ContextException {
77         // Do execution pre work
78         executePre(executionId, executionProperties, incomingEvent);
79
80         // Execute the Javascript executor
81         boolean result = javascriptExecutor.execute(getExecutionContext());
82
83         // Execute the Javascript
84         executePost(result);
85
86         return getOutgoing();
87     }
88
89     /**
90      * Cleans up the task after processing.
91      *
92      * @throws StateMachineException thrown when a state machine execution error occurs
93      */
94     @Override
95     public void cleanUp() throws StateMachineException {
96         LOGGER.debug(
97             "cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskSelectionLogic().getLogicFlavour()
98                 + "," + getSubject().getTaskSelectionLogic().getLogic());
99
100         javascriptExecutor.cleanUp();
101     }
102 }