Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / executor / StateFinalizerExecutor.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor;
23
24 import static org.onap.policy.common.utils.validation.Assertions.argumentOfClassNotNull;
25
26 import java.util.Map;
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;
39
40 /**
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.
43  *
44  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
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);
51
52     // Repeated string constants
53     private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
54
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;
60
61     // Holds the incoming and outgoing fields
62     private Map<String, Object> incomingFields = null;
63
64     // The next state finalizer executor
65     private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
66
67     // The execution context; contains the facades for events and context to be used by tasks
68     // executed by this task
69     // executor
70     private StateFinalizerExecutionContext executionContext = null;
71
72     /**
73      * Gets the execution internalContext.
74      *
75      * @return the execution context
76      */
77     protected StateFinalizerExecutionContext getExecutionContext() {
78         return executionContext;
79     }
80
81     /**
82      * {@inheritDoc}.
83      */
84     @Override
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;
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
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.");
102     }
103
104     /**
105      * {@inheritDoc}.
106      */
107     @Override
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");
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
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());
122
123         // Record the incoming fields
124         this.incomingFields = newIncomingFields;
125
126         // Get state finalizer context object
127         executionContext = new StateFinalizerExecutionContext(this, executionId, executionProperties, axState,
128                 getIncoming(), axState.getStateOutputs().keySet(), getContext());
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
135     public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
136         if (!returnValue) {
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();
141             }
142             LOGGER.warn(errorMessage);
143             throw new StateMachineException(errorMessage);
144         }
145
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);
151         }
152
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() + "\"");
158         }
159
160         LOGGER.debug("execute-post:{}, returning  state output \"{}\" and fields {}", finalizerLogic.getId(),
161                 getOutgoing(), incomingFields);
162     }
163
164     /**
165      * {@inheritDoc}.
166      */
167     @Override
168     public void cleanUp() throws StateMachineException {
169         throw new StateMachineException("cleanUp() not implemented on class");
170     }
171
172     /**
173      * {@inheritDoc}.
174      */
175     @Override
176     public AxReferenceKey getKey() {
177         return finalizerLogic.getKey();
178     }
179
180     /**
181      * {@inheritDoc}.
182      */
183     @Override
184     public Executor<?, ?, ?, ?> getParent() {
185         return parent;
186     }
187
188     /**
189      * {@inheritDoc}.
190      */
191     @Override
192     public AxStateFinalizerLogic getSubject() {
193         return finalizerLogic;
194     }
195
196     /**
197      * {@inheritDoc}.
198      */
199     @Override
200     public ApexInternalContext getContext() {
201         return internalContext;
202     }
203
204     /**
205      * {@inheritDoc}.
206      */
207     @Override
208     public Map<String, Object> getIncoming() {
209         return incomingFields;
210     }
211
212     /**
213      * {@inheritDoc}.
214      */
215     @Override
216     public String getOutgoing() {
217         if (executionContext != null) {
218             return executionContext.getSelectedStateOutputName();
219         } else {
220             return null;
221         }
222     }
223
224     /**
225      * {@inheritDoc}.
226      */
227     @Override
228     public void setNext(
229             final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
230         this.nextExecutor = inNextEx;
231     }
232
233     /**
234      * {@inheritDoc}.
235      */
236     @Override
237     public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
238         return nextExecutor;
239     }
240
241     /**
242      * {@inheritDoc}.
243      */
244     @Override
245     public void setParameters(final ExecutorParameters parameters) {
246         // Not used
247     }
248 }