1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.core.engine.executor;
import static org.onap.policy.apex.model.utilities.Assertions.argumentNotNull;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.core.engine.ExecutorParameters;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
import org.onap.policy.apex.model.policymodel.concepts.AxTask;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* This abstract class executes a task in a state of an Apex policy and is specialized by classes that implement
* execution of task logic.
*
* @author Sven van der Meer (sven.van.der.meer@ericsson.com)
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public abstract class TaskExecutor
implements Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> {
// Logger for this class
private static final XLogger LOGGER = XLoggerFactory.getXLogger(TaskExecutor.class);
// Hold the task and context definitions for this task
private Executor<?, ?, ?, ?> parent = null;
private AxTask axTask = null;
private ApexInternalContext internalContext = null;
// Holds the incoming and outgoing fields
private Map<String, Object> incomingFields = null;
private Map<String, Object> outgoingFields = null;
// The next task executor
private Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> nextExecutor = null;
// The task execution context; contains the facades for events and context to be used by tasks executed by this task
// executor
private TaskExecutionContext executionContext = null;
/**
* Gets the execution internalContext.
*
* @return the execution context
*/
protected TaskExecutionContext getExecutionContext() {
return executionContext;
}
/*
* (non-Javadoc)
*
* @see
* org.onap.policy.apex.core.engine.executor.Executor#setContext(org.onap.policy.apex.core.engine.executor.Executor,
* java.lang.Object, java.lang.Object)
*/
@Override
public void setContext(final Executor<?, ?, ?, ?> newParent, final AxTask newAxTask,
final ApexInternalContext newInternalContext) {
this.parent = newParent;
this.axTask = newAxTask;
this.internalContext = newInternalContext;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#prepare()
*/
@Override
public void prepare() throws StateMachineException {
LOGGER.debug("prepare:" + axTask.getKey().getID() + "," + axTask.getTaskLogic().getLogicFlavour() + ","
+ axTask.getTaskLogic().getLogic());
argumentNotNull(axTask.getTaskLogic().getLogic(), StateMachineException.class, "task logic cannot be null.");
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#execute(java.lang.long, java.lang.Object)
*/
@Override
public Map<String, Object> execute(final long executionID, final Map<String, Object> newIncomingFields)
throws StateMachineException, ContextException {
throw new StateMachineException(
"execute() not implemented on abstract TaskExecutor class, only on its subclasses");
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#executePre(java.lang.long, java.lang.Object)
*/
@Override
public final void executePre(final long executionID, final Map<String, Object> newIncomingFields)
throws StateMachineException, ContextException {
LOGGER.debug("execute-pre:" + getSubject().getTaskLogic().getLogicFlavour() + ","
+ getSubject().getKey().getID() + "," + getSubject().getTaskLogic().getLogic());
// Check that the incoming event has all the input fields for this state
final Set<String> missingTaskInputFields = new TreeSet<>(axTask.getInputFields().keySet());
missingTaskInputFields.removeAll(newIncomingFields.keySet());
// Remove fields from the set that are optional
for (final Iterator<String> missingFieldIterator = missingTaskInputFields.iterator(); missingFieldIterator
.hasNext();) {
final String missingField = missingFieldIterator.next();
if (axTask.getInputFields().get(missingField).getOptional()) {
missingTaskInputFields.remove(missingField);
}
}
if (!missingTaskInputFields.isEmpty()) {
throw new StateMachineException("task input fields \"" + missingTaskInputFields
+ "\" are missing for task \"" + axTask.getKey().getID() + "\"");
}
// Record the incoming fields
this.incomingFields = newIncomingFields;
// Initiate the outgoing fields
outgoingFields = new TreeMap<>();
for (final String outputFieldName : getSubject().getOutputFields().keySet()) {
outgoingFields.put(outputFieldName, null);
}
// Get task context object
executionContext =
new TaskExecutionContext(this, executionID, getSubject(), getIncoming(), getOutgoing(), getContext());
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#executePost(boolean)
*/
@Override
public final void executePost(final boolean returnValue) throws StateMachineException, ContextException {
if (!returnValue) {
String errorMessage = "execute-post: task logic execution failure on task \"" + axTask.getKey().getName()
+ "\" in model " + internalContext.getKey().getID();
if (executionContext.getMessage() != null) {
errorMessage += ", user message: " + executionContext.getMessage();
}
LOGGER.warn(errorMessage);
throw new StateMachineException(errorMessage);
}
// Copy any unset fields from the input to the output if their data type and names are identical
for (final String field : axTask.getOutputFields().keySet()) {
// Check if the field exists and is not set on the output
if (!getOutgoing().containsKey(field) || getOutgoing().get(field) != null) {
continue;
}
// This field is not in the output, check if it's on the input and is the same type (Note here, the output
// field definition has to exist so it's not
// null checked)
final AxInputField inputFieldDef = axTask.getInputFields().get(field);
final AxOutputField outputFieldDef = axTask.getOutputFields().get(field);
if (inputFieldDef == null || !inputFieldDef.getSchema().equals(outputFieldDef.getSchema())) {
continue;
}
// We have an input field that matches our output field, copy the value across
getOutgoing().put(field, getIncoming().get(field));
}
// Finally, check that the outgoing fields have all the output fields defined for this state and, if not, output
// a list of missing fields
final Set<String> missingTaskOutputFields = new TreeSet<>(axTask.getOutputFields().keySet());
missingTaskOutputFields.removeAll(outgoingFields.keySet());
// Remove fields from the set that are optional
for (final Iterator<String> missingFieldIterator = missingTaskOutputFields.iterator(); missingFieldIterator
.hasNext();) {
final String missingField = missingFieldIterator.next();
if (axTask.getInputFields().get(missingField).getOptional()) {
missingTaskOutputFields.remove(missingField);
}
}
if (!missingTaskOutputFields.isEmpty()) {
throw new StateMachineException("task output fields \"" + missingTaskOutputFields
+ "\" are missing for task \"" + axTask.getKey().getID() + "\"");
}
// Finally, check that the outgoing field map don't have any extra fields, if present, raise exception with the
// list of extra fields
final Set<String> extraTaskOutputFields = new TreeSet<>(outgoingFields.keySet());
extraTaskOutputFields.removeAll(axTask.getOutputFields().keySet());
if (!extraTaskOutputFields.isEmpty()) {
throw new StateMachineException("task output fields \"" + extraTaskOutputFields
+ "\" are unwanted for task \"" + axTask.getKey().getID() + "\"");
}
LOGGER.debug("execute-post:" + axTask.getKey().getID() + ", returning fields " + outgoingFields.toString());
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#cleanUp()
*/
@Override
public void cleanUp() throws StateMachineException {
throw new StateMachineException("cleanUp() not implemented on class");
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getKey()
*/
@Override
public AxArtifactKey getKey() {
return axTask.getKey();
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getParent()
*/
@Override
public Executor<?, ?, ?, ?> getParent() {
return parent;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getSubject()
*/
@Override
public AxTask getSubject() {
return axTask;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getContext()
*/
@Override
public ApexInternalContext getContext() {
return internalContext;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getIncoming()
*/
@Override
public Map<String, Object> getIncoming() {
return incomingFields;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getOutgoing()
*/
@Override
public Map<String, Object> getOutgoing() {
return outgoingFields;
}
/*
* (non-Javadoc)
*
* @see
* org.onap.policy.apex.core.engine.executor.Executor#setNext(org.onap.policy.apex.core.engine.executor.Executor)
*/
@Override
public void setNext(
final Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> newNextExecutor) {
this.nextExecutor = newNextExecutor;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#getNext()
*/
@Override
public Executor<Map<String, Object>, Map<String, Object>, AxTask, ApexInternalContext> getNext() {
return nextExecutor;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.core.engine.executor.Executor#setParameters(org.onap.policy.apex.core.engine.
* ExecutorParameters)
*/
@Override
public void setParameters(final ExecutorParameters parameters) {}
}
|