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