2e6d96143c3cce76bb495158d71bfda1dbdb5d46
[policy/apex-pdp.git] /
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
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;
38
39 /**
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.
42  *
43  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
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);
50
51     // Repeated string constants
52     private static final String EXECUTE_POST_SFL = "execute-post: state finalizer logic \"";
53
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;
59
60     // Holds the incoming and outgoing fields
61     private Map<String, Object> incomingFields = null;
62
63     // The next state finalizer executor
64     private Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> nextExecutor = null;
65
66     // The execution context; contains the facades for events and context to be used by tasks
67     // executed by this task
68     // executor
69     private StateFinalizerExecutionContext executionContext = null;
70
71     /**
72      * Gets the execution internalContext.
73      *
74      * @return the execution context
75      */
76     protected StateFinalizerExecutionContext getExecutionContext() {
77         return executionContext;
78     }
79
80     /*
81      * (non-Javadoc)
82      *
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)
85      */
86     @Override
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;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
100      */
101     @Override
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.");
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long, java.lang.Object)
113      */
114     @Override
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");
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long, java.lang.Object)
125      */
126     @Override
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());
131
132         // Record the incoming fields
133         this.incomingFields = newIncomingFields;
134
135         // Get state finalizer context object
136         executionContext = new StateFinalizerExecutionContext(this, executionId, axState, getIncoming(),
137                         axState.getStateOutputs().keySet(), getContext());
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
144      */
145     @Override
146     public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
147         if (!returnValue) {
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();
152             }
153             LOGGER.warn(errorMessage);
154             throw new StateMachineException(errorMessage);
155         }
156
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);
162         }
163
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() + "\"");
169         }
170
171         LOGGER.debug("execute-post:{}, returning  state output \"{}\" and fields {}", finalizerLogic.getId(),
172                         getOutgoing(), incomingFields);
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see org.onap.policy.apex.core.engine.executor.Executor#cleanUp()
179      */
180     @Override
181     public void cleanUp() throws StateMachineException {
182         throw new StateMachineException("cleanUp() not implemented on class");
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.onap.policy.apex.core.engine.executor.Executor#getKey()
189      */
190     @Override
191     public AxReferenceKey getKey() {
192         return finalizerLogic.getKey();
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.onap.policy.apex.core.engine.executor.Executor#getParent()
199      */
200     @Override
201     public Executor<?, ?, ?, ?> getParent() {
202         return parent;
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.onap.policy.apex.core.engine.executor.Executor#getSubject()
209      */
210     @Override
211     public AxStateFinalizerLogic getSubject() {
212         return finalizerLogic;
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.onap.policy.apex.core.engine.executor.Executor#getContext()
219      */
220     @Override
221     public ApexInternalContext getContext() {
222         return internalContext;
223     }
224
225     /*
226      * (non-Javadoc)
227      *
228      * @see org.onap.policy.apex.core.engine.executor.Executor#getIncoming()
229      */
230     @Override
231     public Map<String, Object> getIncoming() {
232         return incomingFields;
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see org.onap.policy.apex.core.engine.executor.Executor#getOutgoing()
239      */
240     @Override
241     public String getOutgoing() {
242         if (executionContext != null) {
243             return executionContext.getSelectedStateOutputName();
244         } else {
245             return null;
246         }
247     }
248
249     /*
250      * (non-Javadoc)
251      *
252      * @see org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.
253      * executor.Executor)
254      */
255     @Override
256     public void setNext(
257                     final Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> inNextEx) {
258         this.nextExecutor = inNextEx;
259     }
260
261     /*
262      * (non-Javadoc)
263      *
264      * @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
265      */
266     @Override
267     public Executor<Map<String, Object>, String, AxStateFinalizerLogic, ApexInternalContext> getNext() {
268         return nextExecutor;
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core. engine.
275      * ExecutorParameters)
276      */
277     @Override
278     public void setParameters(final ExecutorParameters parameters) {
279     }
280 }