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;
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;
36 * The Class JavaTaskSelectExecutor is the task selection executor for task selection logic written in Java.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class JavaTaskSelectExecutor extends TaskSelectExecutor {
41 // Logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskSelectExecutor.class);
44 // The Java Task Selection executor class
45 private Object taskSelectionLogicObject = null;
48 * Prepares the task for processing.
50 * @throws StateMachineException thrown when a state machine execution error occurs
53 public void prepare() throws StateMachineException {
54 // Call generic prepare logic
57 // Get the class for task selection
59 // Create the task logic object from the byte code of the class
60 taskSelectionLogicObject = Class.forName(getSubject().getTaskSelectionLogic().getLogic())
61 .getDeclaredConstructor().newInstance();
62 } catch (final Exception e) {
64 "instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic() + "\"", e);
65 throw new StateMachineException(
66 "instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic() + "\"", e);
71 * Executes the executor for the task in a sequential manner.
73 * @param executionId the execution ID for the current APEX policy execution
74 * @param executionProperties properties for the current APEX policy execution
75 * @param incomingEvent the incoming event
76 * @return The outgoing event
77 * @throws StateMachineException on an execution error
78 * @throws ContextException on context errors
81 public AxArtifactKey execute(final long executionId, final Properties executionProperties,
82 final EnEvent incomingEvent) throws StateMachineException, ContextException {
83 // Do execution pre work
84 executePre(executionId, executionProperties, incomingEvent);
86 // Check and execute the Java logic
87 boolean returnValue = false;
89 // Find and call the method with the signature "public boolean getTask(final TaskSelectionExecutionContext
90 // executor)" to invoke the task selection
91 // logic in the Java class
92 final Method method = taskSelectionLogicObject.getClass().getDeclaredMethod("getTask",
93 new Class[] {TaskSelectionExecutionContext.class});
94 returnValue = (boolean) method.invoke(taskSelectionLogicObject, getExecutionContext());
95 } catch (final Exception e) {
97 "execute: task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"",
99 throw new StateMachineException(
100 "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"", e);
103 // Do the execution post work
104 executePost(returnValue);
106 // Send back the return event
107 return getOutgoing();
111 * Cleans up the task after processing.
113 * @throws StateMachineException thrown when a state machine execution error occurs
116 public void cleanUp() throws StateMachineException {
117 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
118 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
119 + getSubject().getTaskSelectionLogic().getLogic());