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.argumentOfClassNotNull;
27 import java.util.Properties;
28 import lombok.NonNull;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.core.engine.ExecutorParameters;
31 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
32 import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
35 import org.onap.policy.apex.model.policymodel.concepts.AxState;
36 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * This abstract class executes state finalizer logic in a state of an Apex policy and is specialized by classes that
42 * implement execution of state finalizer 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 StateFinalizerExecutor
48 implements Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> {
49 // Logger for this class
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(StateFinalizerExecutor.class);
52 // Repeated string constants
53 private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
55 // Hold the state and context definitions
56 private Executor<?, ?, ?, ?> parent = null;
57 private AxState axState = null;
58 private AxStateFinalizerLogic finalizerLogic = null;
59 private ApexInternalContext internalContext = null;
61 // Holds the incoming and outgoing fields
62 private Map<String, Object> incomingFields = null;
64 // The next state finalizer executor
65 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
67 // The execution context; contains the facades for events and context to be used by tasks
68 // executed by this task
70 private StateFinalizerExecutionContext executionContext = null;
73 * Gets the execution internalContext.
75 * @return the execution context
77 protected StateFinalizerExecutionContext getExecutionContext() {
78 return executionContext;
85 public void setContext(final Executor<?, ?, ?, ?> incomingParent,
86 final AxStateFinalizerLogic incomingFinalizerLogic, final ApexInternalContext incomingInternalContext) {
87 this.parent = incomingParent;
88 axState = (AxState) parent.getSubject();
89 this.finalizerLogic = incomingFinalizerLogic;
90 this.internalContext = incomingInternalContext;
97 public void prepare() throws StateMachineException {
98 LOGGER.debug("prepare:" + finalizerLogic.getId() + "," + finalizerLogic.getLogicFlavour() + ","
99 + finalizerLogic.getLogic());
100 argumentOfClassNotNull(finalizerLogic.getLogic(), StateMachineException.class,
101 "state finalizer logic cannot be null.");
108 public String execute(final long executionId, final Properties executionProperties,
109 final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException {
110 throw new StateMachineException("execute() not implemented on abstract StateFinalizerExecutionContext class, "
111 + "only on its subclasses");
118 public final void executePre(final long executionId, @NonNull final Properties executionProperties,
119 final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException {
120 LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getId() + ","
121 + finalizerLogic.getLogic());
123 // Record the incoming fields
124 this.incomingFields = newIncomingFields;
126 // Get state finalizer context object
127 executionContext = new StateFinalizerExecutionContext(this, executionId, executionProperties, axState,
128 getIncoming(), axState.getStateOutputs().keySet(), getContext());
135 public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
137 String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getId()
138 + "\" on finalizer logic " + finalizerLogic.getId();
139 if (executionContext.getMessage() != null) {
140 errorMessage += ", user message: " + executionContext.getMessage();
142 LOGGER.warn(errorMessage);
143 throw new StateMachineException(errorMessage);
146 // Check a state output has been selected
147 if (getOutgoing() == null) {
148 String message = EXECUTE_POST_SFL + finalizerLogic.getId() + "\" did not select an output state";
149 LOGGER.warn(message);
150 throw new StateMachineException(message);
153 if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
154 LOGGER.warn(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \"" + getOutgoing()
155 + "\" that does not exsist on state \"" + axState.getId() + "\"");
156 throw new StateMachineException(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \""
157 + getOutgoing() + "\" that does not exsist on state \"" + axState.getId() + "\"");
160 LOGGER.debug("execute-post:{}, returning state output \"{}\" and fields {}", finalizerLogic.getId(),
161 getOutgoing(), incomingFields);
168 public void cleanUp() throws StateMachineException {
169 throw new StateMachineException("cleanUp() not implemented on class");
176 public AxReferenceKey getKey() {
177 return finalizerLogic.getKey();
184 public Executor<?, ?, ?, ?> getParent() {
192 public AxStateFinalizerLogic getSubject() {
193 return finalizerLogic;
200 public ApexInternalContext getContext() {
201 return internalContext;
208 public Map<String, Object> getIncoming() {
209 return incomingFields;
216 public String getOutgoing() {
217 if (executionContext != null) {
218 return executionContext.getSelectedStateOutputName();
229 final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
230 this.nextExecutor = inNextEx;
237 public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
245 public void setParameters(final ExecutorParameters parameters) {