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