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.argumentOfClassNotNull;
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 specialized by classes that
40 * 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 // Repeated string constants
51 private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
53 // Hold the state and context definitions
54 private Executor<?, ?, ?, ?> parent = null;
55 private AxState axState = null;
56 private AxStateFinalizerLogic finalizerLogic = null;
57 private ApexInternalContext internalContext = null;
59 // Holds the incoming and outgoing fields
60 private Map<String, Object> incomingFields = null;
62 // The next state finalizer executor
63 private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
65 // The execution context; contains the facades for events and context to be used by tasks
66 // executed by this task
68 private StateFinalizerExecutionContext executionContext = null;
71 * Gets the execution internalContext.
73 * @return the execution context
75 protected StateFinalizerExecutionContext getExecutionContext() {
76 return executionContext;
82 * @see org.onap.policy.apex.core.engine.executor.Executor#setContext(org.onap.policy.apex.core.
83 * engine.executor.Executor, java.lang.Object, java.lang.Object)
86 public void setContext(final Executor<?, ?, ?, ?> incomingParent,
87 final AxStateFinalizerLogic incomingFinalizerLogic,
88 final ApexInternalContext incomingInternalContext) {
89 this.parent = incomingParent;
90 axState = (AxState) parent.getSubject();
91 this.finalizerLogic = incomingFinalizerLogic;
92 this.internalContext = incomingInternalContext;
98 * @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
101 public void prepare() throws StateMachineException {
102 LOGGER.debug("prepare:" + finalizerLogic.getId() + "," + finalizerLogic.getLogicFlavour() + ","
103 + finalizerLogic.getLogic());
104 argumentOfClassNotNull(finalizerLogic.getLogic(), StateMachineException.class,
105 "state finalizer logic cannot be null.");
111 * @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long, java.lang.Object)
114 public String execute(final long executionId, final Map<String, Object> newIncomingFields)
115 throws StateMachineException, ContextException {
116 throw new StateMachineException("execute() not implemented on abstract StateFinalizerExecutionContext class, "
117 + "only on its subclasses");
123 * @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long, java.lang.Object)
126 public final void executePre(final long executionId, final Map<String, Object> newIncomingFields)
127 throws StateMachineException, ContextException {
128 LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getId() + ","
129 + finalizerLogic.getLogic());
131 // Record the incoming fields
132 this.incomingFields = newIncomingFields;
134 // Get state finalizer context object
135 executionContext = new StateFinalizerExecutionContext(this, executionId, axState, getIncoming(),
136 axState.getStateOutputs().keySet(), getContext());
142 * @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
145 public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
147 String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getId()
148 + "\" on finalizer logic " + finalizerLogic.getId();
149 if (executionContext.getMessage() != null) {
150 errorMessage += ", user message: " + executionContext.getMessage();
152 LOGGER.warn(errorMessage);
153 throw new StateMachineException(errorMessage);
156 // Check a state output has been selected
157 if (getOutgoing() == null) {
158 String message = EXECUTE_POST_SFL + finalizerLogic.getId() + "\" did not select an output state";
159 LOGGER.warn(message);
160 throw new StateMachineException(message);
163 if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
164 LOGGER.warn(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \"" + getOutgoing()
165 + "\" that does not exsist on state \"" + axState.getId() + "\"");
166 throw new StateMachineException(EXECUTE_POST_SFL + finalizerLogic.getId() + "\" selected output state \""
167 + getOutgoing() + "\" that does not exsist on state \"" + axState.getId() + "\"");
170 LOGGER.debug("execute-post:{}, returning state output \"{}\" and fields {}", finalizerLogic.getId(),
171 getOutgoing(), 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 if (executionContext != null) {
242 return executionContext.getSelectedStateOutputName();
251 * @see org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.
256 final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
257 this.nextExecutor = inNextEx;
263 * @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
266 public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
273 * @see org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core. engine.
274 * ExecutorParameters)
277 public void setParameters(final ExecutorParameters parameters) {