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;
29 import lombok.NonNull;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.core.engine.ExecutorParameters;
33 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
34 import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext;
35 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.policymodel.concepts.AxState;
38 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
43 * This abstract class executes state finalizer logic in a state of an Apex policy and is specialized by classes that
44 * implement execution of state finalizer logic.
46 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
47 * @author Liam Fallon (liam.fallon@ericsson.com)
49 public abstract class StateFinalizerExecutor
50 implements Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> {
51 // Logger for this class
52 private static final XLogger LOGGER = XLoggerFactory.getXLogger(StateFinalizerExecutor.class);
54 // Repeated string constants
55 private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
57 // Hold the state and context definitions
58 private Executor<?, ?, ?, ?> parent = null;
59 private AxState axState = null;
60 private AxStateFinalizerLogic finalizerLogic = null;
61 private ApexInternalContext internalContext = null;
63 // Holds the incoming and outgoing fields
64 private Map<String, Object> incomingFields = null;
66 // The next state finalizer executor
67 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
69 // The execution context; contains the facades for events and context to be used by tasks
70 // executed by this task
72 private StateFinalizerExecutionContext executionContext = null;
75 * Gets the execution internalContext.
77 * @return the execution context
79 protected StateFinalizerExecutionContext getExecutionContext() {
80 return executionContext;
87 public void setContext(final Executor<?, ?, ?, ?> incomingParent,
88 final AxStateFinalizerLogic incomingFinalizerLogic, final ApexInternalContext incomingInternalContext) {
89 this.parent = incomingParent;
90 axState = (AxState) parent.getSubject();
91 this.finalizerLogic = incomingFinalizerLogic;
92 this.internalContext = incomingInternalContext;
99 public void prepare() throws StateMachineException {
100 LOGGER.debug("prepare:" + finalizerLogic.getId() + "," + finalizerLogic.getLogicFlavour() + ","
101 + finalizerLogic.getLogic());
102 argumentOfClassNotNull(finalizerLogic.getLogic(), StateMachineException.class,
103 "state finalizer logic cannot be null.");
110 public String execute(final long executionId, final Properties executionProperties,
111 final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException {
112 throw new StateMachineException("execute() not implemented on abstract StateFinalizerExecutionContext class, "
113 + "only on its subclasses");
120 public final void executePre(final long executionId, @NonNull final Properties executionProperties,
121 final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException {
122 LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getId() + ","
123 + finalizerLogic.getLogic());
125 // Record the incoming fields
126 this.incomingFields = newIncomingFields;
128 // Get state finalizer context object
129 executionContext = new StateFinalizerExecutionContext(this, executionId, executionProperties, axState,
130 getIncoming(), axState.getStateOutputs().keySet(), getContext());
137 public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
139 String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getId()
140 + "\" on finalizer logic " + finalizerLogic.getId();
141 if (executionContext.getMessage() != null) {
142 errorMessage += ", user message: " + executionContext.getMessage();
144 LOGGER.warn(errorMessage);
145 throw new StateMachineException(errorMessage);
148 // Check a state output has been selected
149 if (getOutgoing() == null) {
150 String message = EXECUTE_POST_SFL + finalizerLogic.getId() + "\" did not select an output state";
151 LOGGER.warn(message);
152 throw new StateMachineException(message);
155 if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
156 LOGGER.warn(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \"" + getOutgoing()
157 + "\" that does not exsist on state \"" + axState.getId() + "\"");
158 throw new StateMachineException(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \""
159 + getOutgoing() + "\" that does not exsist on state \"" + axState.getId() + "\"");
162 LOGGER.debug("execute-post:{}, returning state output \"{}\" and fields {}", finalizerLogic.getId(),
163 getOutgoing(), incomingFields);
170 public void cleanUp() throws StateMachineException {
171 throw new StateMachineException("cleanUp() not implemented on class");
178 public AxReferenceKey getKey() {
179 return finalizerLogic.getKey();
186 public Executor<?, ?, ?, ?> getParent() {
194 public AxStateFinalizerLogic getSubject() {
195 return finalizerLogic;
202 public ApexInternalContext getContext() {
203 return internalContext;
210 public Map<String, Object> getIncoming() {
211 return incomingFields;
218 public String getOutgoing() {
219 if (executionContext != null) {
220 return executionContext.getSelectedStateOutputName();
231 final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
232 this.nextExecutor = inNextEx;
239 public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
247 public void setParameters(final ExecutorParameters parameters) {