18a6ef58ac438945e6d3c14544ea661f473319d6
[policy/apex-pdp.git] /
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
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
29 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * The Class JavascriptStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in
35  * Javascript It is unlikely that this is thread safe.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor {
40     // Logger for this class
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptStateFinalizerExecutor.class);
42
43     private JavascriptExecutor javascriptExecutor;
44
45     /**
46      * Prepares the state finalizer 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         javascriptExecutor = new JavascriptExecutor(getSubject().getKey());
56     }
57
58     /**
59      * Executes the executor for the state finalizer logic in a sequential manner.
60      *
61      * @param executionId the execution ID for the current APEX policy execution
62      * @param executionProperties properties for the current APEX policy execution
63      * @param incomingFields the incoming fields for finalisation
64      * @return The state output for the state
65      * @throws StateMachineException on an execution error
66      * @throws ContextException on context errors
67      */
68     @Override
69     public String execute(final long executionId, final Properties executionProperties,
70             final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
71         // Do execution pre work
72         executePre(executionId, executionProperties, incomingFields);
73
74         // Execute the Javascript and do post processing
75         executePost(javascriptExecutor.execute(getExecutionContext(), getSubject().getLogic()));
76
77         return getOutgoing();
78     }
79
80     /**
81      * Cleans up the state finalizer after processing.
82      *
83      * @throws StateMachineException thrown when a state machine execution error occurs
84      */
85     @Override
86     public void cleanUp() throws StateMachineException {
87         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
88                 + getSubject().getLogic());
89
90         javascriptExecutor.cleanUp();
91     }
92 }