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