2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor;
23 import static org.onap.policy.apex.model.utilities.Assertions.argumentNotNull;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.ExecutorParameters;
29 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
30 import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext;
31 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.policymodel.concepts.AxState;
34 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * This abstract class executes state finalizer logic in a state of an Apex policy and is
40 * specialized by classes that implement execution of state finalizer logic.
42 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public abstract class StateFinalizerExecutor
46 implements Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> {
47 // Logger for this class
48 private static final XLogger LOGGER = XLoggerFactory.getXLogger(StateFinalizerExecutor.class);
50 // Hold the state and context definitions
51 private Executor<?, ?, ?, ?> parent = null;
52 private AxState axState = null;
53 private AxStateFinalizerLogic finalizerLogic = null;
54 private ApexInternalContext internalContext = null;
56 // Holds the incoming and outgoing fields
57 private Map<String, Object> incomingFields = null;
59 // The next state finalizer executor
60 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
62 // The execution context; contains the facades for events and context to be used by tasks
63 // executed by this task
65 private StateFinalizerExecutionContext executionContext = null;
68 * Gets the execution internalContext.
70 * @return the execution context
72 protected StateFinalizerExecutionContext getExecutionContext() {
73 return executionContext;
79 * @see org.onap.policy.apex.core.engine.executor.Executor#setContext(org.onap.policy.apex.core.
80 * engine.executor.Executor, java.lang.Object, java.lang.Object)
83 public void setContext(final Executor<?, ?, ?, ?> incomingParent,
84 final AxStateFinalizerLogic incomingFinalizerLogic, final ApexInternalContext incomingInternalContext) {
85 this.parent = incomingParent;
86 axState = (AxState) parent.getSubject();
87 this.finalizerLogic = incomingFinalizerLogic;
88 this.internalContext = incomingInternalContext;
94 * @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
97 public void prepare() throws StateMachineException {
98 LOGGER.debug("prepare:" + finalizerLogic.getID() + "," + finalizerLogic.getLogicFlavour() + ","
99 + finalizerLogic.getLogic());
100 argumentNotNull(finalizerLogic.getLogic(), StateMachineException.class, "task logic cannot be null.");
106 * @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long,
110 public String execute(final long executionID, final Map<String, Object> newIncomingFields)
111 throws StateMachineException, ContextException {
112 throw new StateMachineException(
113 "execute() not implemented on abstract StateFinalizerExecutionContext class, only on its subclasses");
119 * @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long,
123 public final void executePre(final long executionID, final Map<String, Object> newIncomingFields)
124 throws StateMachineException, ContextException {
125 LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getID() + ","
126 + finalizerLogic.getLogic());
128 // Record the incoming fields
129 this.incomingFields = newIncomingFields;
131 // Get state finalizer context object
132 executionContext = new StateFinalizerExecutionContext(this, executionID, axState, getIncoming(),
133 axState.getStateOutputs().keySet(), getContext());
139 * @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
142 public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
144 String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getID()
145 + "\" on finalizer logic " + finalizerLogic.getID();
146 if (executionContext.getMessage() != null) {
147 errorMessage += ", user message: " + executionContext.getMessage();
149 LOGGER.warn(errorMessage);
150 throw new StateMachineException(errorMessage);
153 // Check a state output has been selected
154 if (getOutgoing() == null) {
155 LOGGER.warn("execute-post: state finalizer logic \"" + finalizerLogic.getID()
156 + "\" did not select an output state");
157 throw new StateMachineException("execute-post: state finalizer logic \"" + finalizerLogic.getID()
158 + "\" did not select an output state");
161 if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
163 "execute-post: state finalizer logic \"" + finalizerLogic.getID() + "\" selected output state \""
164 + getOutgoing() + "\" that does not exsist on state \"" + axState.getID() + "\"");
165 throw new StateMachineException(
166 "execute-post: state finalizer logic \"" + finalizerLogic.getID() + "\" selected output state \""
167 + getOutgoing() + "\" that does not exsist on state \"" + axState.getID() + "\"");
170 LOGGER.debug("execute-post:" + finalizerLogic.getID() + ", returning state output \"" + getOutgoing()
171 + " and fields " + incomingFields);
177 * @see org.onap.policy.apex.core.engine.executor.Executor#cleanUp()
180 public void cleanUp() throws StateMachineException {
181 throw new StateMachineException("cleanUp() not implemented on class");
187 * @see org.onap.policy.apex.core.engine.executor.Executor#getKey()
190 public AxReferenceKey getKey() {
191 return finalizerLogic.getKey();
197 * @see org.onap.policy.apex.core.engine.executor.Executor#getParent()
200 public Executor<?, ?, ?, ?> getParent() {
207 * @see org.onap.policy.apex.core.engine.executor.Executor#getSubject()
210 public AxStateFinalizerLogic getSubject() {
211 return finalizerLogic;
217 * @see org.onap.policy.apex.core.engine.executor.Executor#getContext()
220 public ApexInternalContext getContext() {
221 return internalContext;
227 * @see org.onap.policy.apex.core.engine.executor.Executor#getIncoming()
230 public Map<String, Object> getIncoming() {
231 return incomingFields;
237 * @see org.onap.policy.apex.core.engine.executor.Executor#getOutgoing()
240 public String getOutgoing() {
241 return executionContext.getSelectedStateOutputName();
248 * org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.
252 public void setNext(final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext>
253 incomingNextExecutor) {
254 this.nextExecutor = incomingNextExecutor;
260 * @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
263 public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
271 * org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core.
272 * engine. ExecutorParameters)
275 public void setParameters(final ExecutorParameters parameters) {}