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;
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.executor.context.StateFinalizerExecutionContext;
32 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.policymodel.concepts.AxState;
35 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * This abstract class executes state finalizer logic in a state of an Apex policy and is specialized by classes that
41 * implement execution of state finalizer logic.
43 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public abstract class StateFinalizerExecutor
47 implements Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> {
48 // Logger for this class
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(StateFinalizerExecutor.class);
51 // Repeated string constants
52 private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
54 // Hold the state and context definitions
55 private Executor<?, ?, ?, ?> parent = null;
56 private AxState axState = null;
57 private AxStateFinalizerLogic finalizerLogic = null;
58 private ApexInternalContext internalContext = null;
60 // Holds the incoming and outgoing fields
61 private Map<String, Object> incomingFields = null;
63 // The next state finalizer executor
64 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
66 // The execution context; contains the facades for events and context to be used by tasks
67 // executed by this task
69 private StateFinalizerExecutionContext executionContext = null;
72 * Gets the execution internalContext.
74 * @return the execution context
76 protected StateFinalizerExecutionContext getExecutionContext() {
77 return executionContext;
83 * @see org.onap.policy.apex.core.engine.executor.Executor#setContext(org.onap.policy.apex.core.
84 * engine.executor.Executor, java.lang.Object, java.lang.Object)
87 public void setContext(final Executor<?, ?, ?, ?> incomingParent,
88 final AxStateFinalizerLogic incomingFinalizerLogic,
89 final ApexInternalContext incomingInternalContext) {
90 this.parent = incomingParent;
91 axState = (AxState) parent.getSubject();
92 this.finalizerLogic = incomingFinalizerLogic;
93 this.internalContext = incomingInternalContext;
99 * @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
102 public void prepare() throws StateMachineException {
103 LOGGER.debug("prepare:" + finalizerLogic.getId() + "," + finalizerLogic.getLogicFlavour() + ","
104 + finalizerLogic.getLogic());
105 argumentOfClassNotNull(finalizerLogic.getLogic(), StateMachineException.class,
106 "state finalizer logic cannot be null.");
112 * @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long, java.lang.Object)
115 public String execute(final long executionId, final Map<String, Object> newIncomingFields)
116 throws StateMachineException, ContextException {
117 throw new StateMachineException("execute() not implemented on abstract StateFinalizerExecutionContext class, "
118 + "only on its subclasses");
124 * @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long, java.lang.Object)
127 public final void executePre(final long executionId, final Map<String, Object> newIncomingFields)
128 throws StateMachineException, ContextException {
129 LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getId() + ","
130 + finalizerLogic.getLogic());
132 // Record the incoming fields
133 this.incomingFields = newIncomingFields;
135 // Get state finalizer context object
136 executionContext = new StateFinalizerExecutionContext(this, executionId, axState, getIncoming(),
137 axState.getStateOutputs().keySet(), getContext());
143 * @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
146 public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
148 String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getId()
149 + "\" on finalizer logic " + finalizerLogic.getId();
150 if (executionContext.getMessage() != null) {
151 errorMessage += ", user message: " + executionContext.getMessage();
153 LOGGER.warn(errorMessage);
154 throw new StateMachineException(errorMessage);
157 // Check a state output has been selected
158 if (getOutgoing() == null) {
159 String message = EXECUTE_POST_SFL + finalizerLogic.getId() + "\" did not select an output state";
160 LOGGER.warn(message);
161 throw new StateMachineException(message);
164 if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
165 LOGGER.warn(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \"" + getOutgoing()
166 + "\" that does not exsist on state \"" + axState.getId() + "\"");
167 throw new StateMachineException(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \""
168 + getOutgoing() + "\" that does not exsist on state \"" + axState.getId() + "\"");
171 LOGGER.debug("execute-post:{}, returning state output \"{}\" and fields {}", finalizerLogic.getId(),
172 getOutgoing(), incomingFields);
178 * @see org.onap.policy.apex.core.engine.executor.Executor#cleanUp()
181 public void cleanUp() throws StateMachineException {
182 throw new StateMachineException("cleanUp() not implemented on class");
188 * @see org.onap.policy.apex.core.engine.executor.Executor#getKey()
191 public AxReferenceKey getKey() {
192 return finalizerLogic.getKey();
198 * @see org.onap.policy.apex.core.engine.executor.Executor#getParent()
201 public Executor<?, ?, ?, ?> getParent() {
208 * @see org.onap.policy.apex.core.engine.executor.Executor#getSubject()
211 public AxStateFinalizerLogic getSubject() {
212 return finalizerLogic;
218 * @see org.onap.policy.apex.core.engine.executor.Executor#getContext()
221 public ApexInternalContext getContext() {
222 return internalContext;
228 * @see org.onap.policy.apex.core.engine.executor.Executor#getIncoming()
231 public Map<String, Object> getIncoming() {
232 return incomingFields;
238 * @see org.onap.policy.apex.core.engine.executor.Executor#getOutgoing()
241 public String getOutgoing() {
242 if (executionContext != null) {
243 return executionContext.getSelectedStateOutputName();
252 * @see org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.
257 final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
258 this.nextExecutor = inNextEx;
264 * @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
267 public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
274 * @see org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core. engine.
275 * ExecutorParameters)
278 public void setParameters(final ExecutorParameters parameters) {