7eaaa041585e21762fb42b5b49e157a3a05ae5e1
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-java / src / main / java / org / onap / policy / apex / plugins / executor / java / JavaStateFinalizerExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 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.java;
23
24 import java.lang.reflect.Method;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
30 import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext;
31 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * The Class JavaStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Java.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class JavaStateFinalizerExecutor extends StateFinalizerExecutor {
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaStateFinalizerExecutor.class);
43
44     // The Java State Finalizer executor class
45     private Object stateFinalizerLogicObject = null;
46
47     /**
48      * Prepares the state finalizer for processing.
49      *
50      * @throws StateMachineException thrown when a state machine execution error occurs
51      */
52     @Override
53     public void prepare() throws StateMachineException {
54         // Call generic prepare logic
55         super.prepare();
56
57         // Get the class for state finalizer execution
58         try {
59             // Create the state finalizer logic object from the byte code of the class
60             stateFinalizerLogicObject = Class.forName(getSubject().getLogic()).getDeclaredConstructor().newInstance();
61         } catch (final Exception e) {
62             LOGGER.error("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
63             throw new StateMachineException("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
64         }
65     }
66
67     /**
68      * Executes the executor for the state finalizer logic in a sequential manner.
69      *
70      * @param executionId the execution ID for the current APEX policy execution
71      * @param executionProperties properties for the current APEX policy execution
72      * @param incomingFields the incoming fields for finalisation
73      * @return The state output for the state
74      * @throws StateMachineException on an execution error
75      * @throws ContextException on context errors
76      */
77     @Override
78     public String execute(final long executionId, final Properties executionProperties,
79             final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
80         // Do execution pre work
81         executePre(executionId, executionProperties, incomingFields);
82
83         // Check and execute the Java logic
84         boolean returnValue = false;
85         try {
86             // Find and call the method with the signature "public boolean getStateOutput(final
87             // StateFinalizerExecutionContext executor) throws ApexException"
88             // to invoke the
89             // task logic in the Java class
90             final Method method = stateFinalizerLogicObject.getClass().getDeclaredMethod("getStateOutput",
91                     new Class[] {StateFinalizerExecutionContext.class});
92             returnValue = (boolean) method.invoke(stateFinalizerLogicObject, getExecutionContext());
93         } catch (final Exception e) {
94             LOGGER.error("execute: state finalizer logic failed to run for state finalizer  \"" + getSubject().getId()
95                     + "\"");
96             throw new StateMachineException(
97                     "state finalizer logic failed to run for state finalizer  \"" + getSubject().getId() + "\"", e);
98         }
99
100         // Do the execution post work
101         executePost(returnValue);
102
103         // Send back the return event
104         return getOutgoing();
105     }
106
107     /**
108      * Cleans up the state finalizer after processing.
109      *
110      * @throws StateMachineException thrown when a state machine execution error occurs
111      */
112     @Override
113     public void cleanUp() throws StateMachineException {
114         LOGGER.debug("cleanUp:" + getSubject().getId() + "," + getSubject().getLogicFlavour() + ","
115                 + getSubject().getLogic());
116     }
117 }