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