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