17691011f119f7aac595016fcc445289297dd083
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.engine.executor;
22
23 import static org.onap.policy.apex.model.utilities.Assertions.argumentNotNull;
24
25 import java.util.Map;
26
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;
37
38 /**
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.
41  *
42  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
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);
49
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;
55
56     // Holds the incoming and outgoing fields
57     private Map<String, Object> incomingFields = null;
58
59     // The next state finalizer executor
60     private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
61
62     // The execution context; contains the facades for events and context to be used by tasks executed by this task
63     // executor
64     private StateFinalizerExecutionContext executionContext = null;
65
66     /**
67      * Gets the execution internalContext.
68      *
69      * @return the execution context
70      */
71     protected StateFinalizerExecutionContext getExecutionContext() {
72         return executionContext;
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see
79      * org.onap.policy.apex.core.engine.executor.Executor#setContext(org.onap.policy.apex.core.engine.executor.Executor,
80      * java.lang.Object, java.lang.Object)
81      */
82     @Override
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;
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
95      */
96     @Override
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.");
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long, java.lang.Object)
107      */
108     @Override
109     public String execute(final long executionID, final Map<String, Object> newIncomingFields)
110             throws StateMachineException, ContextException {
111         throw new StateMachineException(
112                 "execute() not implemented on abstract StateFinalizerExecutionContext class, only on its subclasses");
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long, java.lang.Object)
119      */
120     @Override
121     public final void executePre(final long executionID, final Map<String, Object> newIncomingFields)
122             throws StateMachineException, ContextException {
123         LOGGER.debug("execute-pre:" + finalizerLogic.getLogicFlavour() + "," + getSubject().getID() + ","
124                 + finalizerLogic.getLogic());
125
126         // Record the incoming fields
127         this.incomingFields = newIncomingFields;
128
129         // Get state finalizer context object
130         executionContext = new StateFinalizerExecutionContext(this, executionID, axState, getIncoming(),
131                 axState.getStateOutputs().keySet(), getContext());
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
138      */
139     @Override
140     public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
141         if (!returnValue) {
142             String errorMessage = "execute-post: state finalizer logic execution failure on state \"" + axState.getID()
143                     + "\" on finalizer logic " + finalizerLogic.getID();
144             if (executionContext.getMessage() != null) {
145                 errorMessage += ", user message: " + executionContext.getMessage();
146             }
147             LOGGER.warn(errorMessage);
148             throw new StateMachineException(errorMessage);
149         }
150
151         // Check a state output has been selected
152         if (getOutgoing() == null) {
153             LOGGER.warn("execute-post: state finalizer logic \"" + finalizerLogic.getID()
154                     + "\" did not select an output state");
155             throw new StateMachineException("execute-post: state finalizer logic \"" + finalizerLogic.getID()
156                     + "\" did not select an output state");
157         }
158
159         if (!axState.getStateOutputs().keySet().contains(getOutgoing())) {
160             LOGGER.warn(
161                     "execute-post: state finalizer logic \"" + finalizerLogic.getID() + "\" selected output state \""
162                             + getOutgoing() + "\" that does not exsist on state \"" + axState.getID() + "\"");
163             throw new StateMachineException(
164                     "execute-post: state finalizer logic \"" + finalizerLogic.getID() + "\" selected output state \""
165                             + getOutgoing() + "\" that does not exsist on state \"" + axState.getID() + "\"");
166         }
167
168         LOGGER.debug("execute-post:" + finalizerLogic.getID() + ", returning  state output \"" + getOutgoing()
169                 + " and fields " + incomingFields);
170     }
171
172     /*
173      * (non-Javadoc)
174      *
175      * @see org.onap.policy.apex.core.engine.executor.Executor#cleanUp()
176      */
177     @Override
178     public void cleanUp() throws StateMachineException {
179         throw new StateMachineException("cleanUp() not implemented on class");
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see org.onap.policy.apex.core.engine.executor.Executor#getKey()
186      */
187     @Override
188     public AxReferenceKey getKey() {
189         return finalizerLogic.getKey();
190     }
191
192     /*
193      * (non-Javadoc)
194      *
195      * @see org.onap.policy.apex.core.engine.executor.Executor#getParent()
196      */
197     @Override
198     public Executor<?, ?, ?, ?> getParent() {
199         return parent;
200     }
201
202     /*
203      * (non-Javadoc)
204      *
205      * @see org.onap.policy.apex.core.engine.executor.Executor#getSubject()
206      */
207     @Override
208     public AxStateFinalizerLogic getSubject() {
209         return finalizerLogic;
210     }
211
212     /*
213      * (non-Javadoc)
214      *
215      * @see org.onap.policy.apex.core.engine.executor.Executor#getContext()
216      */
217     @Override
218     public ApexInternalContext getContext() {
219         return internalContext;
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.onap.policy.apex.core.engine.executor.Executor#getIncoming()
226      */
227     @Override
228     public Map<String, Object> getIncoming() {
229         return incomingFields;
230     }
231
232     /*
233      * (non-Javadoc)
234      *
235      * @see org.onap.policy.apex.core.engine.executor.Executor#getOutgoing()
236      */
237     @Override
238     public String getOutgoing() {
239         return executionContext.getSelectedStateOutputName();
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see
246      * org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.executor.Executor)
247      */
248     @Override
249     public void setNext(
250             final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> incomingNextExecutor) {
251         this.nextExecutor = incomingNextExecutor;
252     }
253
254     /*
255      * (non-Javadoc)
256      *
257      * @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
258      */
259     @Override
260     public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
261         return nextExecutor;
262     }
263
264     /*
265      * (non-Javadoc)
266      *
267      * @see org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core.engine.
268      * ExecutorParameters)
269      */
270     @Override
271     public void setParameters(final ExecutorParameters parameters) {}
272 }