-
Notifications
You must be signed in to change notification settings - Fork 188
/
IvwoConsolidator.java
1200 lines (1031 loc) · 55 KB
/
IvwoConsolidator.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2017 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.impl.lens;
import static com.evolveum.midpoint.prism.PrismContainerValue.asPrismContainerValues;
import static com.evolveum.midpoint.util.MiscUtil.emptyIfNull;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.model.common.mapping.PrismValueDeltaSetTripleProducer;
import com.evolveum.midpoint.model.impl.lens.projector.ValueMatcher;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.DeltaSetTriple;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.repo.common.expression.ConsolidationValueMetadataComputer;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.metadata.MidpointProvenanceEquivalenceStrategy;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.Holder;
import com.evolveum.midpoint.util.annotation.Experimental;
import com.evolveum.midpoint.util.exception.*;
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.ItemConsolidationTraceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MappingStrengthType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ValueMetadataType;
import com.evolveum.prism.xml.ns._public.types_3.DeltaSetTripleType;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.evolveum.prism.xml.ns._public.types_3.ItemType;
/**
* Consolidate the output of mappings for a single item to a delta. It takes the convenient structure of ItemValueWithOrigin triple.
* It produces the delta considering the mapping exclusion, authoritativeness and strength. See {@link #consolidateTriples()}.
*
* @author semancik
*/
public class IvwoConsolidator<V extends PrismValue, D extends ItemDefinition, I extends ItemValueWithOrigin<V,D>> implements AutoCloseable {
private static final Trace LOGGER = TraceManager.getTrace(IvwoConsolidator.class);
private static final String OP_CONSOLIDATE_TO_DELTA = IvwoConsolidator.class.getName() + ".consolidateToDelta";
/**
* Path of item being consolidated.
*/
private final ItemPath itemPath;
/**
* Categorization of all values (from existing item as well as from delta set triple)
* into equivalence classes.
*/
private final List<EquivalenceClass> equivalenceClasses = new ArrayList<>();
/**
* Is the item an assignment (i.e. is path = c:assignment)?
* This information is needed when preparing an item to be added into the delta.
*/
private final boolean isAssignment;
/**
* Delta set triple of values-with-origin. This is the primary input for the consolidation.
*/
private final DeltaSetTriple<I> ivwoTriple;
/**
* Existing value(s) of the item. This is the secondary input for the consolidation.
*/
private final Item<V,D> existingItem;
/**
* If true, we know the values of existing item so we can filter values using this knowledge.
*
* (Setting this to false is important e.g. when we do not have full projection information.)
*/
private final boolean existingItemKnown;
/**
* Is existing item empty?
*/
private final boolean existingItemIsEmpty;
/**
* A priori (i.e. explicitly specified or previously computed) delta for the item. This is the tertiary
* input for the consolidation.
*/
private final ItemDelta<V,D> aprioriItemDelta;
/**
* Apriori delta is not empty.
*/
private final boolean itemDeltaExists;
/**
* Value matcher used to compare values (for the purpose of consolidation).
*/
private final ValueMatcher valueMatcher;
/**
* Comparator used to compare values. Used if valueMatcher is null or cannot be used (because item is not a property).
*/
private final Comparator<V> comparator;
/**
* What mappings are to be considered?
*/
private final StrengthSelector strengthSelector;
/**
* Whether we want to consider values from zero set as being added. (Typically during object ADD operations.)
*/
private final boolean addUnchangedValues;
/**
* Whether we want to consider values from zero set as being added, except for normal, source-ful mappings.
*/
private final boolean addUnchangedValuesExceptForNormalMappings;
/**
* This is a property of resource item:
*
* When set to false then both strong and normal mapping values are merged to produce the final set of values.
* When set to true only strong values are used if there is at least one strong mapping. Normal values are
* used if there is no strong mapping.
*/
private final boolean itemIsExclusiveStrong;
/**
* If true, deletes all existing values that have no origin. (Non-tolerant behavior.)
*/
@Experimental
private final boolean deleteExistingValues;
/**
* If true, "mapping is not strong and the item X already has a delta" exclusion rule is skipped.
* Currently to be used for inbound mappings, because in the original consolidation algorithm
* for these mappings there was no such rule.
*
* (The rule is a bit dubious anyway.)
*/
@Experimental
private final boolean skipNormalMappingAPrioriDeltaCheck;
/**
* Whether we should ignore values produced by normal-strength mappings.
* This is the case if exclusiveStrong is true and there is at least one value produced by strong mapping.
* TODO Currently checked for ADD values. Should we use the same approach for DELETE or unchanged values?
*/
private final boolean ignoreNormalMappings;
/**
* Computer for value metadata.
*/
private final ConsolidationValueMetadataComputer valueMetadataComputer;
/**
* Description of the context.
*/
private final String contextDescription;
/**
* Operation result (currently needed for value metadata computation).
* Experimentally we make this consolidator auto-closeable so the result is marked as closed automatically.
*
* TODO reconsider if we should not record processing in the caller (e.g. because ConsolidationProcessor
* already creates a result for item consolidation).
*/
private final OperationResult result;
@NotNull private final PrismContext prismContext;
/**
* The output.
*/
@NotNull private final ItemDelta<V,D> itemDelta;
IvwoConsolidator(IvwoConsolidatorBuilder<V, D, I> builder) {
itemPath = builder.itemPath;
isAssignment = FocusType.F_ASSIGNMENT.equivalent(itemPath);
ivwoTriple = builder.ivwoTriple;
if (builder.existingItem != null) {
existingItem = builder.existingItem;
} else if (builder.itemContainer != null) {
existingItem = builder.itemContainer.findItem(itemPath);
} else {
existingItem = null;
}
existingItemIsEmpty = existingItem == null || existingItem.isEmpty(); // should be hasNoValues, perhaps!
aprioriItemDelta = builder.aprioriItemDelta;
itemDeltaExists = builder.itemDeltaExists;
valueMatcher = builder.valueMatcher;
comparator = builder.comparator;
addUnchangedValues = builder.addUnchangedValues;
addUnchangedValuesExceptForNormalMappings = builder.addUnchangedValuesExceptForNormalMappings;
existingItemKnown = builder.existingItemKnown;
strengthSelector = builder.strengthSelector;
itemIsExclusiveStrong = builder.isExclusiveStrong;
deleteExistingValues = builder.deleteExistingValues;
skipNormalMappingAPrioriDeltaCheck = builder.skipNormalMappingAPrioriDeltaCheck;
ignoreNormalMappings = computeIgnoreNormalMappings();
valueMetadataComputer = builder.valueMetadataComputer;
contextDescription = builder.contextDescription;
result = builder.result.createMinorSubresult(OP_CONSOLIDATE_TO_DELTA)
.addArbitraryObjectAsParam("itemPath", itemPath);
prismContext = builder.prismContext;
//noinspection unchecked
itemDelta = builder.itemDefinition.createEmptyDelta(itemPath);
}
/**
* Simplified signature (exceptions thrown), assuming no value metadata computation is to be done.
*/
@NotNull
public ItemDelta<V,D> consolidateToDeltaNoMetadata() throws ExpressionEvaluationException, SchemaException {
try {
assert valueMetadataComputer == null;
return consolidateTriples();
} catch (ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException e) {
throw new IllegalStateException("Unexpected exception: " + e.getMessage(), e);
}
}
@NotNull
public ItemDelta<V,D> consolidateTriples() throws ExpressionEvaluationException, SchemaException,
ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException {
try {
logStart();
if (strengthSelector.isNone()) {
LOGGER.trace("Consolidation of {} skipped as strength selector is 'none'", itemPath);
} else {
categorizeValues();
for (EquivalenceClass equivalenceClass : equivalenceClasses) {
new ValueConsolidation(equivalenceClass).consolidate();
}
// Experimental. Should be part of consolidate() method.
if (deleteExistingValues && existingItem != null) {
for (EquivalenceClass equivalenceClass : equivalenceClasses) {
consolidateExistingValue(equivalenceClass);
}
}
if (!newItemWillHaveAnyValue()) {
// The application of computed delta results in no value, apply weak mappings
applyWeakMappings();
} else {
LOGGER.trace("Item {} will have some values in {}, weak mapping processing skipped", itemPath, contextDescription);
}
}
setEstimatedOldValues();
logEnd();
return itemDelta;
} catch (Throwable t) {
result.recordFatalError(t);
throw t;
}
}
@Experimental
private void consolidateExistingValue(EquivalenceClass equivalenceClass) {
if (equivalenceClass.presentInExistingItem() && equivalenceClass.plusOrigins.isEmpty() &&
equivalenceClass.zeroOrigins.isEmpty() && equivalenceClass.minusOrigins.isEmpty()) {
LOGGER.trace("No origins -- removing the value");
//noinspection unchecked
itemDelta.addValueToDelete((V) equivalenceClass.getRepresentative().clone());
}
}
private void setEstimatedOldValues() {
if (existingItem != null) {
List<V> existingValues = existingItem.getValues();
itemDelta.setEstimatedOldValues(PrismValueCollectionsUtil.cloneCollection(existingValues));
}
}
private void applyWeakMappings() throws SchemaException, ConfigurationException, ObjectNotFoundException,
CommunicationException, SecurityViolationException, ExpressionEvaluationException {
Collection<I> valuesToAdd = selectWeakNonNegativeValues();
LOGGER.trace("No value for item {} in {}, weak mapping processing yielded values: {}",
itemPath, contextDescription, valuesToAdd);
ValueCategorization categorization = new ValueCategorization(valuesToAdd);
for (ValueCategorization.EquivalenceClassSimple equivalenceClass : categorization.equivalenceClasses) {
V value = equivalenceClass.getRepresentative().getItemValue();
// MD#5 - but metadata for weak mappings are not supported yet
// computeValueMetadataOnDeltaValue(value, equivalenceClass::getMemberValues);
itemDelta.addValueToAdd(LensUtil.cloneAndApplyAssignmentOrigin(value, isAssignment, equivalenceClass.members));
}
}
private Collection<I> selectWeakNonNegativeValues() {
Collection<I> nonNegativeIvwos = ivwoTriple.getNonNegativeValues();
// The distinction here is necessary when dealing with outbound mappings; these are reconciled
// at the end, where all mappings were processed. So, if a value was given by assignment-based construction
// then we should use that one. If not, we need to check resource-based outbound mappings.
Collection<I> weakFromAssignments = selectWeakValues(nonNegativeIvwos, OriginType.ASSIGNMENTS);
if (!weakFromAssignments.isEmpty()) {
return weakFromAssignments;
}
Collection<I> weakFromOutbounds = selectWeakValues(nonNegativeIvwos, OriginType.OUTBOUND);
if (!weakFromOutbounds.isEmpty()) {
return weakFromOutbounds;
}
return selectWeakValues(nonNegativeIvwos, null);
}
private Collection<I> selectWeakValues(Collection<I> ivwos, OriginType origin) {
Collection<I> values = new ArrayList<>();
if (strengthSelector.isWeak()) {
for (I ivwo : ivwos) {
if (ivwo.getMapping().getStrength() == MappingStrengthType.WEAK &&
(origin == null || origin == ivwo.getItemValue().getOriginType())) {
values.add(ivwo);
}
}
}
return values;
}
private class ValueConsolidation {
/**
* Value being consolidated.
*/
private final EquivalenceClass equivalenceClass;
/**
* IVwOs (origins) that indicate the need to add the value to the item:
* computed as plusOrigins, optionally combined with (all or selected) zeroOrigins.
*/
private final Collection<I> addingOrigins;
/**
* In cases we check for exclusiveness we need to store exclusive mapping we have found here.
* See {@link #classifyMappings(Collection, boolean)}.
*/
private PrismValueDeltaSetTripleProducer<V, D> exclusiveMapping;
/**
* In the set of relevant mappings (can be different for different situations),
* do we have at least one strong mapping? See {@link #classifyMappings(Collection, boolean)}.
*/
private boolean hasAtLeastOneStrongMapping;
private ValueConsolidation(EquivalenceClass equivalenceClass) {
this.equivalenceClass = equivalenceClass;
LOGGER.trace("Consolidating value equivalence class ({} of {}):\n{}",
equivalenceClass.id, equivalenceClasses.size(),
equivalenceClass.debugDumpLazily());
addingOrigins = findAddingOrigins();
LOGGER.trace("Add-indicating IVwOs = {}", addingOrigins);
}
private void consolidate() throws ExpressionEvaluationException, SchemaException,
ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException {
// This division is quite simplistic in the presence of metadata (yields).
// But let's keep it for the time being.
if (!addingOrigins.isEmpty()) {
consolidateToAddSet();
} else {
consolidateToDeleteSet();
}
}
private Collection<I> findAddingOrigins() {
Collection<I> addingOrigins = new HashSet<>(equivalenceClass.plusOrigins);
if (addUnchangedValues) {
addingOrigins.addAll(equivalenceClass.zeroOrigins);
} else if (addUnchangedValuesExceptForNormalMappings) {
for (I zeroIvwo : equivalenceClass.zeroOrigins) {
if (zeroIvwo.isStrong() || zeroIvwo.isNormal() && (zeroIvwo.isSourceless() || zeroIvwo.isPushChanges()) || zeroIvwo.isWeak()) {
addingOrigins.add(zeroIvwo);
}
}
} else {
// only plus origins cause adding values here
}
return addingOrigins;
}
/**
* We know we have a reason to add the value: either a mapping that returned this value in its plus set,
* or a mapping that returned it in zero set with "addUnchangedValues" being true. (Typically during object ADD
* operation.) Or addUnchangedValues is false but addUnchangedValuesExceptForNormalMappings is true, and
* the mapping is strong or weak or sourceless normal.
*
* So let us sort out the mappings to learn about the presence of weak-normal-strong ones and decide
* on adding/not-adding the value using this information.
*/
private void consolidateToAddSet() throws ExpressionEvaluationException, SchemaException, ConfigurationException,
ObjectNotFoundException, CommunicationException, SecurityViolationException {
classifyMappings(addingOrigins, true);
addValueIfNeeded();
}
private void addCurrentValueToDeltaAddSet() throws SchemaException {
itemDelta.addValueToAdd(LensUtil.cloneAndApplyAssignmentOrigin(equivalenceClass.getRepresentative(), isAssignment, addingOrigins));
}
private void addCurrentValueToDeltaDeleteSet() {
//noinspection unchecked
itemDelta.addValueToDelete((V) equivalenceClass.getRepresentative().clone());
}
private void addValueIfNeeded() throws CommunicationException, ObjectNotFoundException, SchemaException,
SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
// Checking for presence of value0 in existing item (real or assumed)
if (existingItemKnown) {
if (equivalenceClass.presentInExistingItem()) {
LOGGER.trace("Value {} NOT added to delta for item {} because the item already has that value. In: {}",
equivalenceClass, itemPath, contextDescription);
if (valueMetadataComputer != null) {
decideAccordingToMetadata("adding-but-present-in-existing (#2)");
}
return;
}
} else {
if (hasZeroNonWeakMapping()) {
// We use this approximate check only if we don't know current item value - we can only assume it
LOGGER.trace("Value {} in item {} has zero mapping, so we assume it already exists, skipping processing in {}",
equivalenceClass, itemPath, contextDescription);
// We cannot compute metadata because we do not have the target value to compute them for.
return;
}
}
// Normal mappings are ignored on some occasions
if (!hasAtLeastOneStrongMapping) {
if (ignoreNormalMappings) {
LOGGER.trace("Value {} mapping is normal in item {} and we have exclusiveStrong, skipping processing in {}",
equivalenceClass, itemPath, contextDescription);
// We are not sure what to do with metadata now. But, fortunately, this case occurs only with
// resource object items mappings, and we currently do not implement metadata computation for them.
return;
}
if (itemDeltaExists && !skipNormalMappingAPrioriDeltaCheck) {
LOGGER.trace("Value {} mapping is not strong and the item {} already has a delta; " +
"skipping adding in {} (also skipping value metadata computation)",
equivalenceClass, itemPath, contextDescription);
return;
}
}
LOGGER.trace("Decided to ADD value {} to delta for item {} in {}", equivalenceClass, itemPath, contextDescription);
assert equivalenceClass.getPresenceInExistingItem().isEmpty();
if (valueMetadataComputer != null) {
decideAccordingToMetadata("adding-new (#1)");
} else {
addCurrentValueToDeltaAddSet();
}
}
/**
* We have no reason to add the value based on the mappings. Let's check the other options.
*/
private void consolidateToDeleteSet() throws ExpressionEvaluationException, ConfigurationException,
ObjectNotFoundException, SchemaException, CommunicationException, SecurityViolationException {
assert equivalenceClass.plusOrigins.isEmpty() : "Non-empty plus origin set is treated in consolidateToAddSet";
if (!equivalenceClass.zeroOrigins.isEmpty() /* && !selectNonWeakValuesFrom(equivalenceClass.zeroOrigins).isEmpty() */) {
LOGGER.trace("Keeping the value {} for {} because there are some (non-weak) zero origins. In: {}",
equivalenceClass, itemPath, contextDescription);
// This means there are some non-weak mappings that think the value should be kept. These mappings are
// normal (not strong), because otherwise the value would be in plus set (as addUnchangedValuesExceptForNormalMappings
// is now universally true). However, although they are normal, we take them into account. Why? In the world of yields
// it is obvious: they provide yields that are distinct from yield(s) present in the minus origins. So the most natural
// behavior is to keep the value, and remove only the respective yields (if provenance metadata processing is enabled).
if (valueMetadataComputer != null) {
decideAccordingToMetadata("not-adding-present-in-zero (#3)");
} else {
// just keep the value
}
} else if (!equivalenceClass.minusOrigins.isEmpty()) {
classifyMappings(equivalenceClass.minusOrigins, false);
deleteValueIfNeeded();
} else {
LOGGER.trace("Value being consolidated ({} for {}) has no visible plus, minus, nor zero origins. " +
"So we just keep it as is. In: {}",
equivalenceClass, itemPath, contextDescription);
}
}
private void deleteValueIfNeeded() throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
if (existingItemKnown && !equivalenceClass.presentInExistingItem()) {
LOGGER.trace("Value {} NOT add to delta as DELETE because item {} the item does not have that value in {} (matcher: {})",
equivalenceClass, itemPath, contextDescription, valueMatcher);
assert equivalenceClass.zeroOrigins.isEmpty();
// No metadata to be computed here, because no existing value and no zero nor plus origins
return;
}
if (!hasAtLeastOneStrongMapping && itemDeltaExists && !skipNormalMappingAPrioriDeltaCheck) {
LOGGER.trace("Value {} mapping is not strong and the item {} already has a delta, skipping deletion in {}",
equivalenceClass, itemPath, contextDescription);
// TODO treat metadata somehow here
return;
}
if (valueMetadataComputer != null && existingItemKnown) {
assert !equivalenceClass.presenceInExistingItem.isEmpty();
assert !equivalenceClass.minusOrigins.isEmpty();
assert equivalenceClass.plusOrigins.isEmpty();
assert equivalenceClass.zeroOrigins.isEmpty();
decideAccordingToMetadata("deleting (#4)");
} else {
LOGGER.trace("Value {} added to delta as DELETE for item {} in {}", equivalenceClass, itemPath, contextDescription);
addCurrentValueToDeltaDeleteSet();
}
}
/**
* Metadata support is enabled so we will analyze the situation more precisely.
*/
private void decideAccordingToMetadata(String situation) throws CommunicationException, ObjectNotFoundException,
SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
MetadataBasedConsolidation metadataBasedConsolidation = new MetadataBasedConsolidation(situation);
metadataBasedConsolidation.consolidate();
}
private void classifyMappings(Collection<I> origins, boolean checkExclusiveness)
throws ExpressionEvaluationException {
hasAtLeastOneStrongMapping = false;
exclusiveMapping = null;
for (ItemValueWithOrigin<V,D> origin : origins) {
PrismValueDeltaSetTripleProducer<V,D> mapping = origin.getMapping();
if (mapping.getStrength() == MappingStrengthType.STRONG) {
hasAtLeastOneStrongMapping = true;
}
if (checkExclusiveness && mapping.isExclusive()) {
checkMappingExclusiveness(mapping);
}
}
}
private void checkMappingExclusiveness(PrismValueDeltaSetTripleProducer<V, D> mapping) throws ExpressionEvaluationException {
if (exclusiveMapping == null) {
exclusiveMapping = mapping;
} else {
String message = "Exclusion conflict in " + contextDescription + ", item " + itemPath +
", conflicting constructions: " + exclusiveMapping + " and " + mapping;
LOGGER.error(message);
throw new ExpressionEvaluationException(message);
}
}
private boolean hasZeroNonWeakMapping() {
return equivalenceClass.zeroOrigins.stream()
.anyMatch(origin -> !origin.isWeak());
}
// private List<V> selectNonWeakValuesFrom(Collection<I> origins) {
// return origins.stream()
// .filter(origin -> !origin.isWeak())
// .map(ItemValueWithOrigin::getItemValue)
// .collect(Collectors.toList());
// }
private List<V> valuesIn(Collection<I> origins) {
return origins.stream()
.map(ItemValueWithOrigin::getItemValue)
.collect(Collectors.toList());
}
private class MetadataBasedConsolidation {
private final String situation;
private final List<YieldPresence> yieldPresences = new ArrayList<>();
private final List<ValueMetadataType> toAdd = new ArrayList<>();
private final List<ValueMetadataType> toDelete = new ArrayList<>();
private MetadataBasedConsolidation(String situation) {
this.situation = situation;
createYieldPresences();
}
private void createYieldPresences() {
addYields(equivalenceClass.presenceInExistingItem, YieldPresence::getExisting);
addYields(valuesIn(equivalenceClass.plusOrigins), YieldPresence::getPlus);
addYields(valuesIn(equivalenceClass.zeroOrigins), YieldPresence::getZero);
addYields(valuesIn(equivalenceClass.minusOrigins), YieldPresence::getMinus);
}
private void addYields(List<V> values, Function<YieldPresence, List<ValueMetadataType>> collection) {
for (V value : values) {
for (PrismContainerValue<Containerable> yieldPcv : value.getValueMetadataAsContainer().getValues()) {
ValueMetadataType yield = (ValueMetadataType) yieldPcv.asContainerable();
YieldPresence yieldPresence = createOrFindYieldPresence(yield);
collection.apply(yieldPresence).add(yield);
}
}
}
private @NotNull YieldPresence createOrFindYieldPresence(ValueMetadataType yield) {
for (YieldPresence yieldPresence : yieldPresences) {
if (yieldPresence.matches(yield)) {
return yieldPresence;
}
}
YieldPresence newPresence = new YieldPresence(yield);
yieldPresences.add(newPresence);
return newPresence;
}
public void consolidate() throws CommunicationException, ObjectNotFoundException,
SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
logStart();
for (YieldPresence yieldPresence : yieldPresences) {
yieldPresence.consolidate();
}
logEnd();
if (!toAdd.isEmpty()) {
PrismValue valueToAdd = equivalenceClass.getRepresentative().clone();
valueToAdd.getValueMetadataAsContainer().clear();
for (ValueMetadataType metadataToAdd : toAdd) {
//noinspection unchecked
valueToAdd.getValueMetadataAsContainer().add(metadataToAdd.clone().asPrismContainerValue());
}
//noinspection unchecked
itemDelta.addValueToAdd((V) valueToAdd);
}
if (!toDelete.isEmpty()) {
PrismValue valueToDelete = equivalenceClass.getRepresentative().clone();
valueToDelete.getValueMetadataAsContainer().clear();
for (ValueMetadataType metadataToDelete : toDelete) {
//noinspection unchecked
valueToDelete.getValueMetadataAsContainer().add(metadataToDelete.clone().asPrismContainerValue());
}
//noinspection unchecked
itemDelta.addValueToDelete((V) valueToDelete);
}
}
private void logStart() {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Starting metadata-based consolidation for situation: {}\nYield presences:\n{}", situation,
yieldPresences.stream()
.map(presence -> " -- Yield presence:\n" + presence.debugDump(2))
.collect(Collectors.joining("\n")));
}
}
private void logEnd() {
LOGGER.trace("Result of metadata-based consolidation:\nTo add:\n{}\nTo delete:\n{}",
DebugUtil.debugDumpLazily(toAdd, 1),
DebugUtil.debugDumpLazily(toDelete, 1));
}
private class YieldPresence implements DebugDumpable {
@NotNull private final ValueMetadataType yield;
private final List<ValueMetadataType> existing = new ArrayList<>();
private final List<ValueMetadataType> plus = new ArrayList<>();
private final List<ValueMetadataType> zero = new ArrayList<>();
private final List<ValueMetadataType> minus = new ArrayList<>();
private YieldPresence(@NotNull ValueMetadataType yield) {
this.yield = yield;
}
public List<ValueMetadataType> getExisting() {
return existing;
}
public List<ValueMetadataType> getPlus() {
return plus;
}
public List<ValueMetadataType> getZero() {
return zero;
}
public List<ValueMetadataType> getMinus() {
return minus;
}
public boolean matches(ValueMetadataType yield) {
return MidpointProvenanceEquivalenceStrategy.INSTANCE.equals(yield, this.yield);
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.debugDumpWithLabelLn(sb, "Existing", asPrismContainerValues(existing), indent);
DebugUtil.debugDumpWithLabelLn(sb, "Plus", asPrismContainerValues(plus), indent);
DebugUtil.debugDumpWithLabelLn(sb, "Zero", asPrismContainerValues(zero), indent);
DebugUtil.debugDumpWithLabel(sb, "Minus", asPrismContainerValues(minus), indent);
return sb.toString();
}
public void consolidate() throws CommunicationException, ObjectNotFoundException, SchemaException,
SecurityViolationException, ConfigurationException, ExpressionEvaluationException {
if (!zero.isEmpty() || !plus.isEmpty()) {
@NotNull ValueMetadataType computedMetadata =
valueMetadataComputer.compute(getNonNegativeValues(), existing, result);
// This is to avoid phantom adds. It is sufficient to check this on the level
// of a yield because we assume that the metadata computer never changes the yield
// of the input metadata. (I.e. provenance of the output is equivalent with the provenance
// of the inputs. And provenance of all the inputs is equivalent.)
//
// TODO What if the existing metadata are also in the minus set?
// It is no harm if we ignore this fact but at least we should think about it.
// See e.g. TestValueMetadata.test320ReinforceGivenNameByManualEntry.
if (isPresent(computedMetadata)) {
LOGGER.trace("Computed metadata is already present, not adding them:\n{}",
DebugUtil.debugDumpLazily(computedMetadata));
} else {
LOGGER.trace("Adding computed metadata:\n{}",
DebugUtil.debugDumpLazily(computedMetadata));
toAdd.add(computedMetadata);
}
} else {
if (!existing.isEmpty() && !minus.isEmpty()) {
LOGGER.trace("Yield {} has no plus/zero presence but it is present in existing item and "
+ "in minus set -- so we need to delete it", yield);
toDelete.add(yield);
}
}
}
private boolean isPresent(ValueMetadataType toBeAdded) {
// TODO Consider the strategy. REAL_VALUE (instead of DATA) prevents uninteresting changes to be applied.
// But what about timestamps, channels, actors, etc?
return existing.stream()
.anyMatch(e -> e.asPrismContainerValue().equals(toBeAdded.asPrismContainerValue(), EquivalenceStrategy.REAL_VALUE));
}
private List<ValueMetadataType> getNonNegativeValues() {
List<ValueMetadataType> rv = new ArrayList<>(plus);
rv.addAll(zero);
return rv;
}
}
}
}
private boolean computeIgnoreNormalMappings() {
if (!itemIsExclusiveStrong) {
return false;
} else if (strengthSelector.isNone()) {
return true; // just an optimization
} else {
Holder<Boolean> resultHolder = new Holder<>(false);
SimpleVisitor<I> visitor = pvwo -> {
if (pvwo.getMapping().getStrength() == MappingStrengthType.STRONG) {
resultHolder.setValue(true);
}
};
ivwoTriple.simpleAccept(visitor);
return resultHolder.getValue();
}
}
private void logStart() {
LOGGER.trace("Consolidating {} IVwO triple:\n{}\n Apriori Delta (exists: {}):\n{}\n Existing item (known: {}, isEmpty: {}):\n{}\n Parameters:\n"
+ " - valueMatcher: {}\n"
+ " - comparator: {}\n"
+ " - strengthSelector: {}\n"
+ " - addUnchangedValues: {}\n"
+ " - addUnchangedValuesExceptForNormalMappings: {}\n"
+ " - itemIsExclusiveStrong: {}\n"
+ " - deleteExistingValues (experimental): {}\n"
+ " - skipNormalMappingAPrioriDeltaCheck (experimental): {}\n"
+ " - ignoreNormalMappings: {}\n"
+ " - valueMetadataComputer: {}\n"
+ " Context: {}\n",
itemPath, ivwoTriple.debugDumpLazily(1),
itemDeltaExists, DebugUtil.debugDumpLazily(aprioriItemDelta, 2),
existingItemKnown, existingItemIsEmpty, DebugUtil.debugDumpLazily(existingItem, 2),
valueMatcher, comparator, strengthSelector,
addUnchangedValues, addUnchangedValuesExceptForNormalMappings,
itemIsExclusiveStrong,
deleteExistingValues, skipNormalMappingAPrioriDeltaCheck,
ignoreNormalMappings, valueMetadataComputer,
contextDescription);
}
private void logEnd() {
LOGGER.trace("Consolidated {} IVwO triple to delta:\n{}", itemPath, itemDelta.debugDumpLazily(1));
if (result.isTraced()) {
ItemConsolidationTraceType trace = new ItemConsolidationTraceType(prismContext);
trace.setItemPath(new ItemPathType(itemPath));
try {
if (ivwoTriple != null) {
PrismValueDeltaSetTriple<PrismValue> prismValueDeltaSetTriple = prismContext.deltaFactory().createPrismValueDeltaSetTriple();
ivwoTriple.transform(prismValueDeltaSetTriple, ItemValueWithOrigin::getItemValue);
trace.setDeltaSetTriple(DeltaSetTripleType.fromDeltaSetTriple(prismValueDeltaSetTriple, prismContext));
}
if (existingItem != null) {
trace.setExistingItem(ItemType.fromItem(existingItem, prismContext));
}
if (aprioriItemDelta != null) {
trace.getAprioriDelta().addAll(DeltaConvertor.toItemDeltaTypes(aprioriItemDelta));
}
trace.setEquivalenceClassCount(equivalenceClasses.size());
trace.getResultingDelta().addAll(DeltaConvertor.toItemDeltaTypes(itemDelta));
} catch (SchemaException e) {
LOGGER.warn("Couldn't convert itemDelta to the trace", e);
}
result.getTraces().add(trace);
}
}
private void categorizeValues() throws SchemaException {
categorizeFromTriple(ivwoTriple.getPlusSet(), EquivalenceClass::getPlusOrigins, false);
categorizeFromTriple(ivwoTriple.getZeroSet(), EquivalenceClass::getZeroOrigins, false);
categorizeFromTriple(ivwoTriple.getMinusSet(), EquivalenceClass::getMinusOrigins, true);
if (existingItem != null) {
categorizeFromCollection(existingItem.getValues(), EquivalenceClass::getPresenceInExistingItem);
}
if (aprioriItemDelta != null) {
categorizeFromCollection(aprioriItemDelta.getValuesToAdd(), EquivalenceClass::getPresenceInAprioriPlus);
categorizeFromCollection(aprioriItemDelta.getValuesToReplace(), EquivalenceClass::getPresenceInAprioriPlus);
categorizeFromCollection(aprioriItemDelta.getValuesToDelete(), EquivalenceClass::getPresenceInAprioriMinus);
}
}
private void categorizeFromCollection(Collection<V> values, Function<EquivalenceClass, List<V>> targetSet) throws SchemaException {
for (V value : emptyIfNull(values)) {
EquivalenceClass equivalenceClass = findOrCreateEquivalenceClass(value);
targetSet.apply(equivalenceClass).add(value);
}
}
private void categorizeFromTriple(Collection<I> ivwoSet, Function<EquivalenceClass, List<I>> targetSet,
boolean takeValidInvalid) throws SchemaException {
for (I ivwo : ivwoSet) {
categorizeFromTriple(ivwo, targetSet, takeValidInvalid);
}
}
private void categorizeFromTriple(I ivwo, Function<EquivalenceClass, List<I>> targetSet, boolean takeValidInvalid) throws SchemaException {
if (shouldCategorize(ivwo, takeValidInvalid)) {
EquivalenceClass equivalenceClass = findOrCreateEquivalenceClass(ivwo.getItemValue());
targetSet.apply(equivalenceClass).add(ivwo);
} else {
LOGGER.trace("Not categorizing {} (to {})", ivwo, targetSet);
}
}
private boolean shouldCategorize(I ivwo, boolean takeValidInvalid) {
return !ivwo.isWeak() && // experimental
!shouldSkipMapping(ivwo.getMapping().getStrength()) &&
(ivwo.isValid() || takeValidInvalid && ivwo.wasValid());
}
private EquivalenceClass findOrCreateEquivalenceClass(V value) throws SchemaException {
for (EquivalenceClass equivalenceClass : equivalenceClasses) {
if (equivalenceClass.covers(value)) {
return equivalenceClass;
}
}
EquivalenceClass equivalenceClass = new EquivalenceClass(value);
equivalenceClasses.add(equivalenceClass);
return equivalenceClass;
}
private boolean shouldSkipMapping(MappingStrengthType mappingStrength) {
return mappingStrength == MappingStrengthType.STRONG && !strengthSelector.isStrong() ||
mappingStrength == MappingStrengthType.NORMAL && !strengthSelector.isNormal() ||
mappingStrength == MappingStrengthType.WEAK && !strengthSelector.isWeak();
}
/**
* After application of the computed delta, will the item have any value?
*/
private boolean newItemWillHaveAnyValue() throws SchemaException {
if (existingItem == null || existingItem.isEmpty()) {
return itemDelta.addsAnyValue();
} else if (itemDelta.isEmpty()) {
return true;
} else {
//noinspection unchecked
Item<V,D> clonedItem = existingItem.clone();
itemDelta.applyToMatchingPath(clonedItem);
return !clonedItem.isEmpty();
}
}
// private void computeMetadataOnExistingValue(EquivalenceClass equivalenceClass, Supplier<List<V>> additionalSourcesSupplier)
// throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException,
// ConfigurationException, ExpressionEvaluationException {
// if (valueMetadataComputer == null) {
// LOGGER.trace("Skipping value metadata computation (for existing value) because computer is null");
// } else if (!equivalenceClass.presentInExistingItem()) {
// LOGGER.trace("Skipping value metadata computation for existing value because the value is not present in existing item: {}", equivalenceClass);
// } else {
// V existingValue = equivalenceClass.presenceInExistingItem.get(0);
// ValueMetadata metadata = computeValueMetadata(existingValue, additionalSourcesSupplier, equivalenceClass::getPresenceInAprioriPlus); // TODO
// LOGGER.trace("Computed value metadata for existing value {} ({}):\n{}", existingValue, itemPath,
// DebugUtil.debugDumpLazily(metadata));
// applyMetadataIfChanged(existingValue, metadata);
// }
// }
// @SuppressWarnings("unchecked")
// private void applyMetadataIfChanged(V existingValue, ValueMetadata metadata) {
// ValueMetadata existingMetadata = existingValue.getValueMetadata();
// if (metadata.isEmpty() && existingMetadata.isEmpty()) {
// // Maybe we should generalize this comparison
// LOGGER.trace("Both computed and existing value metadata is empty, not doing anything");
// } else if (metadata.equals(existingMetadata)) {
// LOGGER.trace("Computed value metadata is the same as existing, not doing anything");
// } else {
// LOGGER.trace("Computed value metadata is different from existing, deleting old value and adding updated one");
//
// V oldValue = (V) existingValue.clone();
// itemDelta.addValueToDelete(oldValue);
//
// V newValue = (V) existingValue.clone();
// newValue.setValueMetadata(metadata);
// itemDelta.addValuesToAdd(newValue);
// }
// }
// @SafeVarargs
// private final void computeValueMetadataOnDeltaValue(EquivalenceClass equivalenceClass, Supplier<List<V>>... additionalSourcesSuppliers)
// throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException,
// ConfigurationException, ExpressionEvaluationException {
// if (valueMetadataComputer != null) {
// ValueMetadata metadata = computeValueMetadata(equivalenceClass.getExistingValue(), additionalSourcesSuppliers);
// LOGGER.trace("Computed value metadata for delta value {} ({}):\n{}", equivalenceClass, itemPath, DebugUtil.debugDumpLazily(metadata));
// equivalenceClass.getRepresentative().setValueMetadata(metadata);
// } else {
// LOGGER.trace("Skipping value metadata computation (for delta value) because computer is null");
// }
// }
// // TEMPORARY
// private void computeValueMetadataOnDeltaValue(V deltaValue, Supplier<List<V>> additionalSourcesSupplier)
// throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException,
// ConfigurationException, ExpressionEvaluationException {
// if (valueMetadataComputer != null) {
// ValueMetadata metadata = computeValueMetadata(null, additionalSourcesSupplier);
// LOGGER.trace("Computed value metadata for delta value {} ({}):\n{}", deltaValue, itemPath, DebugUtil.debugDumpLazily(metadata));
// deltaValue.setValueMetadata(metadata);
// } else {
// LOGGER.trace("Skipping value metadata computation (for delta value) because computer is null");
// }
// }
// @SafeVarargs
// private final ValueMetadata computeValueMetadata(V existingValue, Supplier<List<V>>... additionalSourcesSuppliers)
// throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException,
// ConfigurationException, ExpressionEvaluationException {
// List<PrismValue> inputValues = new ArrayList<>();
// if (existingValue != null) {
// inputValues.add(existingValue);
// }
// for (Supplier<List<V>> additionalSourcesSupplier : additionalSourcesSuppliers) {
// inputValues.addAll(additionalSourcesSupplier.get());
// }
// return valueMetadataComputer.compute(inputValues, result);
// }
private class ValueCategorization {
// TEMPORARY: TODO rework!
private class EquivalenceClassSimple {