ef0a8d17810a4a78bb00b8b76955271b534f8ea5
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.executor.java;
22
23 import java.lang.reflect.Method;
24 import java.util.Map;
25
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.context.StateFinalizerExecutionContext;
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 JavaStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Java.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class JavaStateFinalizerExecutor extends StateFinalizerExecutor {
39     // Logger for this class
40     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaStateFinalizerExecutor.class);
41
42     // The Java State Finalizer executor class
43     private Object stateFinalizerLogicObject = null;
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         // Get the class for state finalizer execution
56         try {
57             // Create the state finalizer logic object from the byte code of the class
58             stateFinalizerLogicObject = Class.forName(getSubject().getLogic()).newInstance();
59         } catch (final Exception e) {
60             LOGGER.error("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
61             throw new StateMachineException("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
62         }
63     }
64
65     /**
66      * Executes the executor for the state finalizer logic in a sequential manner.
67      *
68      * @param executionId the execution ID for the current APEX policy execution
69      * @param incomingFields the incoming fields for finalisation
70      * @return The state output for the state
71      * @throws StateMachineException on an execution error
72      * @throws ContextException on context errors
73      */
74     @Override
75     public String execute(final long executionId, final Map<String, Object> incomingFields)
76             throws StateMachineException, ContextException {
77         // Do execution pre work
78         executePre(executionId, incomingFields);
79
80         // Check and execute the Java logic
81         boolean returnValue = false;
82         try {
83             // Find and call the method with the signature "public boolean getStateOutput(final
84             // StateFinalizerExecutionContext executor) throws ApexException"
85             // to invoke the
86             // task logic in the Java class
87             final Method method = stateFinalizerLogicObject.getClass().getDeclaredMethod("getStateOutput", (Class[])
88                     new Class[] { StateFinalizerExecutionContext.class });
89             returnValue = (boolean) method.invoke(stateFinalizerLogicObject, getExecutionContext());
90         } catch (final Exception e) {
91             LOGGER.error("execute: state finalizer logic failed to run for state finalizer  \"" + getSubject().getId()
92                     + "\"");
93             throw new StateMachineException(
94                     "state finalizer logic failed to run for state finalizer  \"" + getSubject().getId() + "\"", e);
95         }
96
97         // Do the execution post work
98         executePost(returnValue);
99
100         // Send back the return event
101         return getOutgoing();
102     }
103
104     /**
105      * Cleans up the state finalizer after processing.
106      *
107      * @throws StateMachineException thrown when a state machine execution error occurs
108      */
109     @Override
110     public void cleanUp() throws StateMachineException {
111         LOGGER.debug("cleanUp:" + getSubject().getId() + "," + getSubject().getLogicFlavour() + ","
112                 + getSubject().getLogic());
113     }
114 }