-
Notifications
You must be signed in to change notification settings - Fork 188
/
MappingEvaluator.java
420 lines (343 loc) · 17.6 KB
/
MappingEvaluator.java
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) 2013-2015 Evolveum
*
* 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.
*/
package com.evolveum.midpoint.model.impl.lens.projector;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.model.common.mapping.Mapping;
import com.evolveum.midpoint.model.common.mapping.MappingFactory;
import com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder;
import com.evolveum.midpoint.model.impl.lens.LensContext;
import com.evolveum.midpoint.model.impl.lens.LensElementContext;
import com.evolveum.midpoint.model.impl.lens.LensUtil;
import com.evolveum.midpoint.model.impl.trigger.RecomputeTriggerHandler;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.ItemDefinition;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismObjectDefinition;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ContainerDelta;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingStrengthType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TriggerType;
/**
* @author Radovan Semancik
*
*/
@Component
public class MappingEvaluator {
private static final Trace LOGGER = TraceManager.getTrace(MappingEvaluator.class);
@Autowired
private PrismContext prismContext;
@Autowired
private MappingFactory mappingFactory;
public <V extends PrismValue, D extends ItemDefinition, F extends ObjectType> void evaluateMapping(
Mapping<V,D> mapping, LensContext<F> lensContext, Task task, OperationResult parentResult) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
ModelExpressionThreadLocalHolder.pushLensContext(lensContext);
ModelExpressionThreadLocalHolder.pushCurrentResult(parentResult);
ModelExpressionThreadLocalHolder.pushCurrentTask(task);
ObjectType originObject = mapping.getOriginObject();
String objectOid, objectName, objectTypeName;
if (originObject != null) {
objectOid = originObject.getOid();
objectName = String.valueOf(originObject.getName());
objectTypeName = originObject.getClass().getSimpleName();
} else {
objectOid = objectName = objectTypeName = null;
}
String mappingName = mapping.getItemName() != null ? mapping.getItemName().getLocalPart() : null;
long start = System.currentTimeMillis();
try {
task.recordState("Started evaluation of mapping " + mapping.getMappingContextDescription() + ".");
mapping.evaluate(task, parentResult);
task.recordState("Successfully finished evaluation of mapping " + mapping.getMappingContextDescription() + " in " + (System.currentTimeMillis()-start) + " ms.");
} catch (IllegalArgumentException e) {
task.recordState("Evaluation of mapping " + mapping.getMappingContextDescription() + " finished with error in " + (System.currentTimeMillis()-start) + " ms.");
throw new IllegalArgumentException(e.getMessage()+" in "+mapping.getContextDescription(), e);
} finally {
task.recordMappingOperation(objectOid, objectName, objectTypeName, mappingName, System.currentTimeMillis() - start);
ModelExpressionThreadLocalHolder.popLensContext();
ModelExpressionThreadLocalHolder.popCurrentResult();
ModelExpressionThreadLocalHolder.popCurrentTask();
if (lensContext.getDebugListener() != null) {
lensContext.getDebugListener().afterMappingEvaluation(lensContext, mapping);
}
}
}
public <V extends PrismValue, D extends ItemDefinition, T extends ObjectType, F extends FocusType> void evaluateMappingSetProjection(
MappingEvaluatorParams<V,D,T,F> params,
Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
String mappingDesc = params.getMappingDesc();
LensElementContext<T> targetContext = params.getTargetContext();
PrismObjectDefinition<T> targetObjectDefinition = targetContext.getObjectDefinition();
ItemPath defaultTargetItemPath = params.getDefaultTargetItemPath();
Map<ItemPath,MappingOutputStruct<V>> outputTripleMap = new HashMap<>();
XMLGregorianCalendar nextRecomputeTime = null;
Collection<MappingType> mappingTypes = params.getMappingTypes();
Collection<Mapping<V,D>> mappings = new ArrayList<>(mappingTypes.size());
for (MappingType mappingType: mappingTypes) {
Mapping.Builder<V,D> mappingBuilder = mappingFactory.createMappingBuilder(mappingType, mappingDesc);
if (!mappingBuilder.isApplicableToChannel(params.getContext().getChannel())) {
continue;
}
mappingBuilder.setNow(params.getNow());
if (defaultTargetItemPath != null && targetObjectDefinition != null) {
D defaultTargetItemDef = targetObjectDefinition.findItemDefinition(defaultTargetItemPath);
mappingBuilder.setDefaultTargetDefinition(defaultTargetItemDef);
mappingBuilder.setDefaultTargetPath(defaultTargetItemPath);
} else {
mappingBuilder.setDefaultTargetDefinition(params.getTargetItemDefinition());
mappingBuilder.setDefaultTargetPath(defaultTargetItemPath);
}
mappingBuilder.setTargetContext(targetObjectDefinition);
// Initialize mapping (using Inversion of Control)
mappingBuilder = params.getInitializer().initialize(mappingBuilder);
Mapping<V,D> mapping = mappingBuilder.build();
Boolean timeConstraintValid = mapping.evaluateTimeConstraintValid(task, result);
if (params.getEvaluateCurrent() != null) {
if (params.getEvaluateCurrent() && !timeConstraintValid) {
continue;
}
if (!params.getEvaluateCurrent() && timeConstraintValid) {
continue;
}
}
mappings.add(mapping);
}
for (Mapping<V,D> mapping: mappings) {
if (mapping.getStrength() == MappingStrengthType.WEAK) {
// Evaluate weak mappings in a second run.
continue;
}
ItemPath mappingOutputPath = mapping.getOutputPath();
if (params.isFixTarget() && mappingOutputPath != null && defaultTargetItemPath != null && !mappingOutputPath.equivalent(defaultTargetItemPath)) {
throw new ExpressionEvaluationException("Target cannot be overridden in "+mappingDesc);
}
if (params.getAPrioriTargetDelta() != null && mappingOutputPath != null) {
ItemDelta<?,?> aPrioriItemDelta = params.getAPrioriTargetDelta().findItemDelta(mappingOutputPath);
if (mapping.getStrength() != MappingStrengthType.STRONG) {
if (aPrioriItemDelta != null && !aPrioriItemDelta.isEmpty()) {
continue;
}
}
}
evaluateMapping(mapping, params.getContext(), task, result);
PrismValueDeltaSetTriple<V> mappingOutputTriple = mapping.getOutputTriple();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Output triple of mapping {}\n{}", mapping.getContextDescription(),
mappingOutputTriple==null?null:mappingOutputTriple.debugDump(1));
}
if (mappingOutputTriple != null) {
MappingOutputStruct<V> mappingOutputStruct = outputTripleMap.get(mappingOutputPath);
if (mappingOutputStruct == null) {
mappingOutputStruct = new MappingOutputStruct<>();
outputTripleMap.put(mappingOutputPath, mappingOutputStruct);
}
if (mapping.getStrength() == MappingStrengthType.STRONG) {
mappingOutputStruct.setStrongMappingWasUsed(true);
}
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
if (outputTriple == null) {
mappingOutputStruct.setOutputTriple(mappingOutputTriple);
} else {
outputTriple.merge(mappingOutputTriple);
}
}
}
// Second pass, evaluate only weak mappings
for (Mapping<V,D> mapping: mappings) {
ItemPath mappingOutputPath = mapping.getOutputPath();
if (params.isFixTarget() && mappingOutputPath != null && defaultTargetItemPath != null && !mappingOutputPath.equivalent(defaultTargetItemPath)) {
throw new ExpressionEvaluationException("Target cannot be overridden in "+mappingDesc);
}
MappingOutputStruct<V> mappingOutputStruct = outputTripleMap.get(mappingOutputPath);
if (mappingOutputStruct == null) {
mappingOutputStruct = new MappingOutputStruct<>();
outputTripleMap.put(mappingOutputPath, mappingOutputStruct);
}
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
Item<V,D> aPrioriTargetItem = null;
PrismObject<T> aPrioriTargetObject = params.getAPrioriTargetObject();
if (aPrioriTargetObject != null && mappingOutputPath != null) {
aPrioriTargetItem = aPrioriTargetObject.findItem(mappingOutputPath);
}
if ((aPrioriTargetItem == null || aPrioriTargetItem.isEmpty()) && outputTriple == null) {
if (mapping.getStrength() != MappingStrengthType.WEAK) {
continue;
}
evaluateMapping(mapping, params.getContext(), task, result);
PrismValueDeltaSetTriple<V> mappingOutputTriple = mapping.getOutputTriple();
if (mappingOutputTriple != null) {
if (outputTriple == null) {
mappingOutputStruct.setOutputTriple(mappingOutputTriple);
} else {
outputTriple.merge(mappingOutputTriple);
}
}
}
}
MappingOutputProcessor<V> processor = params.getProcessor();
for (Entry<ItemPath, MappingOutputStruct<V>> outputTripleMapEntry: outputTripleMap.entrySet()) {
ItemPath mappingOutputPath = outputTripleMapEntry.getKey();
MappingOutputStruct<V> mappingOutputStruct = outputTripleMapEntry.getValue();
PrismValueDeltaSetTriple<V> outputTriple = mappingOutputStruct.getOutputTriple();
if (processor != null) {
processor.process(mappingOutputPath, outputTriple);
} else {
if (outputTriple == null) {
LOGGER.trace("{} expression resulted in null triple for {}, skipping", mappingDesc, targetContext);
continue;
}
ItemDefinition targetItemDefinition = null;
if (mappingOutputPath != null) {
targetItemDefinition = targetObjectDefinition.findItemDefinition(mappingOutputPath);
if (targetItemDefinition == null) {
throw new SchemaException("No definition for item "+mappingOutputPath+" in "+targetObjectDefinition);
}
} else {
targetItemDefinition = params.getTargetItemDefinition();
}
ItemDelta<V,D> targetItemDelta = targetItemDefinition.createEmptyDelta(mappingOutputPath);
Item<V,D> aPrioriTargetItem = null;
PrismObject<T> aPrioriTargetObject = params.getAPrioriTargetObject();
if (aPrioriTargetObject != null) {
aPrioriTargetItem = aPrioriTargetObject.findItem(mappingOutputPath);
}
// WARNING
// Following code seems to be wrong. It is not very relativisic. It seems to always
// go for replace.
// It seems that it is only used for activation mappings (outbout and inbound). As
// these are quite special single-value properties then it seems to work fine
// (with the exception of MID-3418). Todo: make it more relativistic: MID-3419
if (targetContext.isAdd()) {
Collection<V> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
LOGGER.trace("{} resulted in null or empty value for {}, skipping", mappingDesc, targetContext);
continue;
}
targetItemDelta.setValuesToReplace(PrismValue.cloneCollection(nonNegativeValues));
} else {
// if we have fresh information (full shadow) AND the mapping used to derive the information was strong,
// we will consider all values (zero & plus sets) -- otherwise, we take only the "plus" (i.e. changed) set
// the first case is necessary, because in some situations (e.g. when mapping is changed)
// the evaluator sees no differences w.r.t. real state, even if there is a difference
// - and we must have a way to push new information onto the resource
Collection<V> valuesToReplace;
if (params.hasFullTargetObject() && mappingOutputStruct.isStrongMappingWasUsed()) {
valuesToReplace = outputTriple.getNonNegativeValues();
} else {
valuesToReplace = outputTriple.getPlusSet();
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("{}: hasFullTargetObject={}, isStrongMappingWasUsed={}, valuesToReplace={}",
new Object[]{mappingDesc, params.hasFullTargetObject(),
mappingOutputStruct.isStrongMappingWasUsed(), valuesToReplace});
}
if (valuesToReplace != null && !valuesToReplace.isEmpty()) {
// if what we want to set is the same as is already in the shadow, we skip that
// (we insist on having full shadow, to be sure we work with current data)
if (params.hasFullTargetObject() && targetContext.isFresh() && aPrioriTargetItem != null) {
Collection<V> valuesPresent = aPrioriTargetItem.getValues();
if (PrismValue.equalsRealValues(valuesPresent, valuesToReplace)) {
LOGGER.trace("{} resulted in existing values for {}, skipping creation of a delta", mappingDesc, targetContext);
continue;
}
}
targetItemDelta.setValuesToReplace(PrismValue.cloneCollection(valuesToReplace));
} else if (outputTriple.hasMinusSet()) {
LOGGER.trace("{} resulted in null or empty value for {} and there is a minus set, resetting it (replace with empty)", mappingDesc, targetContext);
targetItemDelta.setValueToReplace();
} else {
LOGGER.trace("{} resulted in null or empty value for {}, skipping", mappingDesc, targetContext);
}
}
if (targetItemDelta.isEmpty()) {
continue;
}
LOGGER.trace("{} adding new delta for {}: {}", mappingDesc, targetContext, targetItemDelta);
targetContext.swallowToSecondaryDelta(targetItemDelta);
}
}
// Figure out recompute time
for (Mapping<V,D> mapping: mappings) {
XMLGregorianCalendar mappingNextRecomputeTime = mapping.getNextRecomputeTime();
if (mappingNextRecomputeTime != null) {
if (nextRecomputeTime == null || nextRecomputeTime.compare(mappingNextRecomputeTime) == DatatypeConstants.GREATER) {
nextRecomputeTime = mappingNextRecomputeTime;
}
}
}
if (nextRecomputeTime != null) {
boolean alreadyHasTrigger = false;
if (params.getAPrioriTargetObject() != null) {
for (TriggerType trigger: params.getAPrioriTargetObject().asObjectable().getTrigger()) {
if (RecomputeTriggerHandler.HANDLER_URI.equals(trigger.getHandlerUri()) &&
nextRecomputeTime.equals(trigger.getTimestamp())) {
alreadyHasTrigger = true;
break;
}
}
}
if (!alreadyHasTrigger) {
PrismContainerDefinition<TriggerType> triggerContDef = targetObjectDefinition.findContainerDefinition(ObjectType.F_TRIGGER);
ContainerDelta<TriggerType> triggerDelta = triggerContDef.createEmptyDelta(new ItemPath(ObjectType.F_TRIGGER));
PrismContainerValue<TriggerType> triggerCVal = triggerContDef.createValue();
triggerDelta.addValueToAdd(triggerCVal);
TriggerType triggerType = triggerCVal.asContainerable();
triggerType.setTimestamp(nextRecomputeTime);
triggerType.setHandlerUri(RecomputeTriggerHandler.HANDLER_URI);
targetContext.swallowToSecondaryDelta(triggerDelta);
}
}
}
private class MappingOutputStruct<V extends PrismValue> {
private PrismValueDeltaSetTriple<V> outputTriple = null;
private boolean strongMappingWasUsed = false;
public PrismValueDeltaSetTriple<V> getOutputTriple() {
return outputTriple;
}
public void setOutputTriple(PrismValueDeltaSetTriple<V> outputTriple) {
this.outputTriple = outputTriple;
}
public boolean isStrongMappingWasUsed() {
return strongMappingWasUsed;
}
public void setStrongMappingWasUsed(boolean strongMappingWasUsed) {
this.strongMappingWasUsed = strongMappingWasUsed;
}
}
}