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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.java;
24 import java.lang.reflect.Method;
25 import java.util.Properties;
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;
37 * The Class JavaTaskSelectExecutor is the task selection executor for task selection logic written in Java.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class JavaTaskSelectExecutor extends TaskSelectExecutor {
42 // Logger for this class
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskSelectExecutor.class);
45 // The Java Task Selection executor class
46 private Object taskSelectionLogicObject = null;
49 * Prepares the task for processing.
51 * @throws StateMachineException thrown when a state machine execution error occurs
54 public void prepare() throws StateMachineException {
55 // Call generic prepare logic
58 // Get the class for task selection
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) {
65 "instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic() + "\"", e);
66 throw new StateMachineException(
67 "instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic() + "\"", e);
72 * Executes the executor for the task in a sequential manner.
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
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);
87 // Check and execute the Java logic
88 boolean returnValue = false;
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) {
98 "execute: task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"",
100 throw new StateMachineException(
101 "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", e);
104 // Do the execution post work
105 executePost(returnValue);
107 // Send back the return event
108 return getOutgoing();
112 * Cleans up the task after processing.
114 * @throws StateMachineException thrown when a state machine execution error occurs
117 public void cleanUp() throws StateMachineException {
118 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
119 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
120 + getSubject().getTaskSelectionLogic().getLogic());