1ceeba3b109a60de471c9d76c34222d5f08ea030
[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 import java.util.Properties;
26
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
29 import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
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 JavaTaskExecutor is the task executor for task logic written in Java.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class JavaTaskExecutor extends TaskExecutor {
40     // Logger for this class
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskExecutor.class);
42
43     // The Java Task executor class
44     private Object taskLogicObject = null;
45
46     /**
47      * Prepares the task 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 task execution
57         try {
58             // Create the task logic object from the byte code of the class
59             taskLogicObject = Class.forName(getSubject().getTaskLogic().getLogic()).newInstance();
60         } catch (final Exception e) {
61             LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
62             throw new StateMachineException(
63                     "instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
64         }
65     }
66
67     /**
68      * Executes the executor for the task 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
73      * @return The outgoing fields
74      * @throws StateMachineException on an execution error
75      * @throws ContextException on context errors
76      */
77     @Override
78     public Map<String, Object> 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 getEvent(final TaskExecutionContext executor)
87             // throws ApexException" to invoke the
88             // task logic in the Java class
89             final Method method = taskLogicObject.getClass().getDeclaredMethod("getEvent",
90                     new Class[] { TaskExecutionContext.class });
91             returnValue = (boolean) method.invoke(taskLogicObject, getExecutionContext());
92         } catch (final Exception e) {
93             LOGGER.error("execute: task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"");
94             throw new StateMachineException(
95                     "task logic failed to run for task  \"" + getSubject().getKey().getId() + "\"", e);
96         }
97
98         // Do the execution post work
99         executePost(returnValue);
100
101         // Send back the return event
102         return getOutgoing();
103     }
104
105     /**
106      * Cleans up the task after processing.
107      *
108      * @throws StateMachineException thrown when a state machine execution error occurs
109      */
110     @Override
111     public void cleanUp() throws StateMachineException {
112         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
113                 + "," + getSubject().getTaskLogic().getLogic());
114     }
115 }