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