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