2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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.core.engine.executor;
24 import static org.onap.policy.common.utils.validation.Assertions.argumentNotNull;
26 import java.util.Properties;
27 import lombok.NonNull;
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.core.engine.ExecutorParameters;
30 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
31 import org.onap.policy.apex.core.engine.event.EnEvent;
32 import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.policymodel.concepts.AxState;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * This abstract class executes a the task selection logic of a state of an Apex policy and is specialized by classes
42 * that implement execution of task selection logic.
44 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public abstract class TaskSelectExecutor implements Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> {
48 // Logger for this class
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(TaskSelectExecutor.class);
51 // Hold the state and context definitions for this task selector
52 private Executor<?, ?, ?, ?> parent = null;
53 private AxState axState = null;
54 private ApexInternalContext context = null;
56 // Holds the incoming event and outgoing task keys
57 private EnEvent incomingEvent = null;
58 private AxArtifactKey outgoingTaskKey = null;
60 // The next task selection executor
61 private Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> nextExecutor = null;
63 // The task selection execution context; contains the facades for events and context to be used
64 // by tasks executed by
65 // this task selection executor
66 private TaskSelectionExecutionContext executionContext;
69 * Gets the execution context.
71 * @return the execution context
73 protected TaskSelectionExecutionContext getExecutionContext() {
74 return executionContext;
81 public void setContext(final Executor<?, ?, ?, ?> newParent, final AxState newAxState,
82 final ApexInternalContext newContext) {
83 this.parent = newParent;
84 this.axState = newAxState;
85 this.context = newContext;
92 public void prepare() throws StateMachineException {
93 LOGGER.debug("prepare:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
94 + "," + axState.getTaskSelectionLogic().getLogic());
95 argumentNotNull(axState.getTaskSelectionLogic().getLogic(), "task selection logic cannot be null.");
102 public AxArtifactKey execute(final long executionId, final Properties executionProperties,
103 final EnEvent newIncomingEvent) throws StateMachineException, ContextException {
104 throw new StateMachineException("execute() not implemented on class");
111 public final void executePre(final long executionId, @NonNull final Properties executionProperties,
112 final EnEvent newIncomingEvent) throws StateMachineException {
113 LOGGER.debug("execute-pre:" + axState.getKey().getId() + "," + axState.getTaskSelectionLogic().getLogicFlavour()
114 + "," + axState.getTaskSelectionLogic().getLogic());
116 this.incomingEvent = newIncomingEvent;
118 // Initialize the returned task object so it can be set
119 outgoingTaskKey = new AxArtifactKey();
121 // Get task selection context object
122 executionContext = new TaskSelectionExecutionContext(this, executionId, getSubject(), getIncoming(),
123 getOutgoing(), getContext());
130 public final void executePost(final boolean returnValue) throws StateMachineException {
132 String errorMessage = "execute-post: task selection logic failed on state \"" + axState.getKey().getId()
134 if (executionContext.getMessage() != null) {
135 errorMessage += ", user message: " + executionContext.getMessage();
137 LOGGER.warn(errorMessage);
138 throw new StateMachineException(errorMessage);
141 if (outgoingTaskKey == null || AxArtifactKey.getNullKey().getName().equals(outgoingTaskKey.getName())) {
142 outgoingTaskKey = axState.getDefaultTask();
143 LOGGER.debug("execute-post:" + axState.getKey().getId() + ", returning default task");
147 if (!axState.getTaskReferences().containsKey(outgoingTaskKey)) {
148 LOGGER.error("execute-post: task \"" + outgoingTaskKey.getId()
149 + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
150 throw new StateMachineException("task \"" + outgoingTaskKey.getId()
151 + "\" returned by task selection logic not defined on state \"" + axState.getKey().getId() + "\"");
154 LOGGER.debug("execute-post:" + axState.getKey().getId() + "," + ", returning task " + outgoingTaskKey.getId());
161 public void cleanUp() throws StateMachineException {
162 throw new StateMachineException("cleanUp() not implemented on class");
169 public AxReferenceKey getKey() {
170 return axState.getKey();
177 public Executor<?, ?, ?, ?> getParent() {
185 public AxState getSubject() {
193 public ApexInternalContext getContext() {
201 public void setNext(final Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> newNextExecutor) {
202 this.nextExecutor = newNextExecutor;
209 public Executor<EnEvent, AxArtifactKey, AxState, ApexInternalContext> getNext() {
217 public EnEvent getIncoming() {
218 return incomingEvent;
225 public AxArtifactKey getOutgoing() {
226 return outgoingTaskKey;
233 public void setParameters(final ExecutorParameters parameters) {