-
Notifications
You must be signed in to change notification settings - Fork 188
/
SynchronizationServiceImpl.java
1109 lines (959 loc) · 46 KB
/
SynchronizationServiceImpl.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-2016 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.sync;
import com.evolveum.midpoint.common.InternalsConfig;
import com.evolveum.midpoint.common.refinery.RefinedResourceSchema;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.model.api.PolicyViolationException;
import com.evolveum.midpoint.model.common.expression.ExpressionFactory;
import com.evolveum.midpoint.model.common.expression.ExpressionUtil;
import com.evolveum.midpoint.model.common.expression.ExpressionVariables;
import com.evolveum.midpoint.model.impl.lens.*;
import com.evolveum.midpoint.model.impl.util.Utils;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.PrismPropertyValue;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.provisioning.api.ResourceObjectShadowChangeDescription;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.processor.ObjectClassComplexTypeDefinition;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.schema.statistics.StatisticsUtil;
import com.evolveum.midpoint.schema.statistics.SynchronizationInformation;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.schema.util.ShadowUtil;
import com.evolveum.midpoint.schema.util.SynchronizationSituationUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.xml.namespace.QName;
import java.util.*;
import static com.evolveum.midpoint.common.InternalsConfig.consistencyChecks;
/**
* Synchronization service receives change notifications from provisioning. It decides which synchronization
* policy to use and evaluates it (correlation, confirmation, situations, reaction, ...)
*
* @author lazyman
* @author Radovan Semancik
*
* Note: don't autowire this bean by implementing class, as it is proxied by Spring AOP.
* Use the interface instead.
*/
@Service(value = "synchronizationService")
public class SynchronizationServiceImpl implements SynchronizationService {
private static final Trace LOGGER = TraceManager.getTrace(SynchronizationServiceImpl.class);
@Autowired(required = true)
private ActionManager<Action> actionManager;
@Autowired
private CorrelationConfirmationEvaluator correlationConfirmationEvaluator;
@Autowired(required = true)
private PrismContext prismContext;
@Autowired(required = true)
@Qualifier("cacheRepositoryService")
private RepositoryService repositoryService;
@Autowired(required = true)
private ProvisioningService provisioningService;
@Autowired(required = true)
private ContextFactory contextFactory;
@Autowired(required = true)
private Clockwork clockwork;
@Autowired(required = true)
private ExpressionFactory expressionFactory;
@Override
public void notifyChange(ResourceObjectShadowChangeDescription change, Task task, OperationResult parentResult) {
validate(change);
Validate.notNull(parentResult, "Parent operation result must not be null.");
boolean logDebug = isLogDebug(change);
if (logDebug) {
LOGGER.debug("SYNCHRONIZATION: received change notification {}", change);
} else {
LOGGER.trace("SYNCHRONIZATION: received change notification {}", change);
}
OperationResult subResult = parentResult.createSubresult(NOTIFY_CHANGE);
PrismObject<? extends ShadowType> currentShadow = change.getCurrentShadow();
PrismObject<? extends ShadowType> applicableShadow = currentShadow;
if (applicableShadow == null) {
// We need this e.g. in case of delete
applicableShadow = change.getOldShadow();
}
SynchronizationEventInformation eventInfo = new SynchronizationEventInformation(applicableShadow, change.getSourceChannel(), task);
try {
ResourceType resourceType = change.getResource().asObjectable();
PrismObject<SystemConfigurationType> configuration = Utils.getSystemConfiguration(repositoryService, subResult);
ObjectSynchronizationType synchronizationPolicy = determineSynchronizationPolicy(resourceType,
applicableShadow, configuration, task, subResult);
if (LOGGER.isTraceEnabled()) {
String policyDesc = null;
if (synchronizationPolicy != null) {
if (synchronizationPolicy.getName() == null) {
policyDesc = "(kind=" + synchronizationPolicy.getKind() + ", intent=" +
synchronizationPolicy.getIntent() + ", objectclass=" + synchronizationPolicy.getObjectClass() +
")";
} else {
policyDesc = synchronizationPolicy.getName();
}
}
LOGGER.trace("SYNCHRONIZATION determined policy: {}", policyDesc);
}
if (synchronizationPolicy == null) {
String message = "SYNCHRONIZATION no matching policy for " + applicableShadow + " (" + applicableShadow.asObjectable().getObjectClass() + ") "
+ " on " + resourceType + ", ignoring change from channel " + change.getSourceChannel();
LOGGER.debug(message);
subResult.recordStatus(OperationResultStatus.NOT_APPLICABLE, message);
eventInfo.setNoSynchronizationPolicy();
eventInfo.record(task);
return;
}
if (!isSynchronizationEnabled(synchronizationPolicy)) {
String message = "SYNCHRONIZATION is not enabled for " + resourceType
+ " ignoring change from channel " + change.getSourceChannel();
LOGGER.debug(message);
subResult.recordStatus(OperationResultStatus.NOT_APPLICABLE, message);
eventInfo.setSynchronizationNotEnabled();
eventInfo.record(task);
return;
}
//check if the kind/intent in the syncPolicy satisfy constraints defined in task
if (!satisfyTaskConstraints(synchronizationPolicy, task)){
LOGGER.trace("SYNCHRONIZATION skipping {} because it does not match kind/intent defined in task",new Object[] {
applicableShadow});
subResult.recordStatus(OperationResultStatus.NOT_APPLICABLE, "Skipped because it does not match objectClass/kind/intent");
eventInfo.setDoesNotMatchTaskSpecification();
eventInfo.record(task);
return;
}
Class<? extends FocusType> focusType = determineFocusClass(synchronizationPolicy, resourceType);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Synchronization is enabled, focus class: {}, found applicable policy: {}", focusType, Utils.getPolicyDesc(synchronizationPolicy));
}
SynchronizationSituation situation = determineSituation(focusType, change, synchronizationPolicy, configuration.asObjectable(), task, subResult);
if (logDebug) {
LOGGER.debug("SYNCHRONIZATION: SITUATION: '{}', currentOwner={}, correlatedOwner={}", situation.getSituation().value(),
situation.getCurrentOwner(), situation.getCorrelatedOwner());
} else {
LOGGER.trace("SYNCHRONIZATION: SITUATION: '{}', currentOwner={}, correlatedOwner={}", situation.getSituation().value(),
situation.getCurrentOwner(), situation.getCorrelatedOwner());
}
eventInfo.setOriginalSituation(situation.getSituation());
eventInfo.setNewSituation(situation.getSituation()); // overwritten later (TODO fix this!)
if (change.isUnrelatedChange() || Utils.isDryRun(task) || isProtected((PrismObject<ShadowType>) currentShadow)){
PrismObject object = null;
if (change.getCurrentShadow() != null){
object = change.getCurrentShadow();
} else if (change.getOldShadow() != null){
object = change.getOldShadow();
}
Collection modifications = SynchronizationSituationUtil
.createSynchronizationSituationAndDescriptionDelta(object,
situation.getSituation(), task.getChannel(), false);
if (StringUtils.isNotBlank(synchronizationPolicy.getIntent())){
modifications.add(PropertyDelta.createModificationReplaceProperty(ShadowType.F_INTENT, object.getDefinition(), synchronizationPolicy.getIntent()));
}
try {
repositoryService.modifyObject(ShadowType.class, object.getOid(), modifications, subResult);
task.recordObjectActionExecuted(object, ChangeType.MODIFY, null);
} catch (Throwable t) {
task.recordObjectActionExecuted(object, ChangeType.MODIFY, t);
} finally {
task.markObjectActionExecutedBoundary();
}
subResult.recordSuccess();
eventInfo.record(task);
LOGGER.debug("SYNCHRONIZATION: DONE (dry run) for {}", object);
return;
}
//must be here, because when the reaction has no action, the situation will be not set.
PrismObject<ShadowType> newCurrentShadow = saveSyncMetadata((PrismObject<ShadowType>) currentShadow,
situation, change, synchronizationPolicy, task, parentResult);
if (newCurrentShadow != null) {
change.setCurrentShadow(newCurrentShadow);
}
SynchronizationSituationType newSituation =
reactToChange(focusType, change, synchronizationPolicy, situation, resourceType, logDebug, configuration, task, subResult);
eventInfo.setNewSituation(newSituation);
eventInfo.record(task);
subResult.computeStatus();
} catch (Exception ex) {
eventInfo.setException(ex);
eventInfo.record(task);
subResult.recordFatalError(ex);
throw new SystemException(ex);
} finally {
task.markObjectActionExecutedBoundary();
// if (LOGGER.isTraceEnabled()) {
// LOGGER.trace(subResult.dump());
// }
}
LOGGER.debug("SYNCHRONIZATION: DONE for {}", currentShadow);
}
private boolean satisfyTaskConstraints(ObjectSynchronizationType synchronizationPolicy, Task task) {
PrismProperty<ShadowKindType> kind = task.getExtensionProperty(SchemaConstants.MODEL_EXTENSION_KIND);
if (kind != null && !kind.isEmpty()){
ShadowKindType kindValue = kind.getRealValue();
ShadowKindType policyKind = synchronizationPolicy.getKind();
if (policyKind == null) {
policyKind = ShadowKindType.ACCOUNT; // TODO is this ok? [med]
}
if (!policyKind.equals(kindValue)){
return false;
}
}
PrismProperty<String> intent = task.getExtensionProperty(SchemaConstants.MODEL_EXTENSION_INTENT);
if (intent != null && !intent.isEmpty()){
String intentValue = intent.getRealValue();
if (StringUtils.isEmpty(synchronizationPolicy.getIntent())){
return false;
}
if (!synchronizationPolicy.getIntent().equals(intentValue)){
return false;
}
}
return true;
}
private boolean isProtected(PrismObject<ShadowType> shadow){
if (shadow == null){
return false;
}
ShadowType currentShadowType = shadow.asObjectable();
if (currentShadowType.isProtectedObject() == null){
return false;
}
return currentShadowType.isProtectedObject();
}
private <F extends FocusType> Class<F> determineFocusClass(ObjectSynchronizationType synchronizationPolicy, ResourceType resource) throws ConfigurationException {
if (synchronizationPolicy == null) {
throw new IllegalStateException("synchronizationPolicy is null");
}
QName focusTypeQName = synchronizationPolicy.getFocusType();
if (focusTypeQName == null) {
return (Class<F>) UserType.class;
}
ObjectTypes objectType = ObjectTypes.getObjectTypeFromTypeQName(focusTypeQName);
if (objectType == null) {
throw new ConfigurationException("Unknown focus type "+focusTypeQName+" in synchronization policy in "+resource);
}
return (Class<F>) objectType.getClassDefinition();
}
@Override
public ObjectSynchronizationType determineSynchronizationPolicy(ResourceType resourceType,
PrismObject<? extends ShadowType> currentShadow, PrismObject<SystemConfigurationType> configuration, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
SynchronizationType synchronization = resourceType.getSynchronization();
if (synchronization == null) {
return null;
}
for (ObjectSynchronizationType objectSynchronization: synchronization.getObjectSynchronization()) {
if (isPolicyApplicable(currentShadow, objectSynchronization, resourceType.asPrismObject(), configuration, task, result)) {
return objectSynchronization;
}
}
return null;
}
private boolean isPolicyApplicable(PrismObject<? extends ShadowType> currentShadow,
ObjectSynchronizationType synchronizationPolicy, PrismObject<ResourceType> resource, PrismObject<SystemConfigurationType> configuration,
Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
ShadowType currentShadowType = currentShadow.asObjectable();
// objectClass
QName shadowObjectClass = currentShadowType.getObjectClass();
Validate.notNull(shadowObjectClass, "No objectClass in currentShadow");
List<QName> policyObjectClasses = synchronizationPolicy.getObjectClass();
if (policyObjectClasses == null || policyObjectClasses.isEmpty()) {
String policyIntent = synchronizationPolicy.getIntent();
ShadowKindType policyKind = synchronizationPolicy.getKind();
ObjectClassComplexTypeDefinition policyObjectClass = null;
RefinedResourceSchema schema = RefinedResourceSchema.getRefinedSchema(resource);
if (policyKind == null && policyIntent == null) {
policyObjectClass = schema.findDefaultObjectClassDefinition(policyKind);
}
if (policyKind != null){
if (StringUtils.isEmpty(policyIntent)) {
policyObjectClass = schema.findDefaultObjectClassDefinition(policyKind);
} else {
policyObjectClass = schema.findObjectClassDefinition(policyKind, policyIntent);
}
}
if (policyObjectClass != null && policyObjectClass.getTypeName().equals(shadowObjectClass)){
return true;
}
}
if (policyObjectClasses != null && !policyObjectClasses.isEmpty()) {
if (!policyObjectClasses.contains(shadowObjectClass)) { // TODO relaxed QName match [med]
return false;
}
}
// kind
ShadowKindType shadowKind = currentShadowType.getKind();
ShadowKindType policyKind = synchronizationPolicy.getKind();
if (policyKind != null && shadowKind != null && !policyKind.equals(shadowKind)) {
return false;
}
// intent
// TODO is the intent always present in shadow at this time? [med]
String shadowIntent = currentShadowType.getIntent();
String policyIntent = synchronizationPolicy.getIntent();
if (policyIntent != null && shadowIntent != null && !MiscSchemaUtil.equalsIntent(shadowIntent, policyIntent)) {
return false;
}
if (synchronizationPolicy.getCondition() != null) {
ExpressionType conditionExpressionType = synchronizationPolicy.getCondition();
String desc = "condition in object synchronization "+synchronizationPolicy.getName();
ExpressionVariables variables = Utils.getDefaultExpressionVariables(null, currentShadow, null, resource, configuration);
PrismPropertyValue<Boolean> evaluateCondition = ExpressionUtil.evaluateCondition(variables, conditionExpressionType, expressionFactory, desc, task, result);
return evaluateCondition.getValue();
}
return true;
}
private boolean isLogDebug(ResourceObjectShadowChangeDescription change) {
// Reconciliation changes are routine. Do not let it polute the logfiles.
return !SchemaConstants.CHANGE_CHANNEL_RECON_URI.equals(change.getSourceChannel());
}
private void validate(ResourceObjectShadowChangeDescription change) {
Validate.notNull(change, "Resource object shadow change description must not be null.");
Validate.isTrue(change.getCurrentShadow() != null || change.getObjectDelta() != null,
"Object delta and current shadow are null. At least one must be provided.");
Validate.notNull(change.getResource(), "Resource in change must not be null.");
if (consistencyChecks) {
if (change.getCurrentShadow() != null) {
change.getCurrentShadow().checkConsistence();
ShadowUtil.checkConsistence(change.getCurrentShadow(), "current shadow in change description");
}
if (change.getObjectDelta() != null) {
change.getObjectDelta().checkConsistence();
}
}
}
// @Override
// public void notifyFailure(ResourceOperationFailureDescription failureDescription,
// Task task, OperationResult parentResult) {
// Validate.notNull(failureDescription, "Resource object shadow failure description must not be null.");
// Validate.notNull(failureDescription.getCurrentShadow(), "Current shadow in resource object shadow failure description must not be null.");
// Validate.notNull(failureDescription.getObjectDelta(), "Delta in resource object shadow failure description must not be null.");
// Validate.notNull(failureDescription.getResource(), "Resource in failure must not be null.");
// Validate.notNull(failureDescription.getResult(), "Result in failure description must not be null.");
// Validate.notNull(parentResult, "Parent operation result must not be null.");
//
// LOGGER.debug("SYNCHRONIZATION: received failure notifiation {}", failureDescription);
//
// LOGGER.error("Provisioning error: {}", failureDescription.getResult().getMessage());
//
// // TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
// }
private boolean isSynchronizationEnabled(ObjectSynchronizationType synchronization) {
if (synchronization == null || synchronization.isEnabled() == null) {
return false;
}
return synchronization.isEnabled();
}
/**
* XXX: in situation when one account belongs to two different idm users
* (repository returns only first user, method
* {@link com.evolveum.midpoint.model.api.ModelService#findShadowOwner(String, Task, OperationResult)} (String, com.evolveum.midpoint.schema.result.OperationResult)}
* ). It should be changed because otherwise we can't find
* {@link SynchronizationSituationType#DISPUTED} situation
*/
private <F extends FocusType> SynchronizationSituation determineSituation(Class<F> focusType,
ResourceObjectShadowChangeDescription change, ObjectSynchronizationType synchronizationPolicy,
SystemConfigurationType configurationType, Task task, OperationResult result) {
OperationResult subResult = result.createSubresult(CHECK_SITUATION);
LOGGER.trace("Determining situation for resource object shadow.");
SynchronizationSituation situation = null;
try {
String shadowOid = getOidFromChange(change);
Validate.notEmpty(shadowOid, "Couldn't get resource object shadow oid from change.");
PrismObject<F> owner = repositoryService.searchShadowOwner(shadowOid, SelectorOptions.createCollection(GetOperationOptions.createAllowNotFound()), subResult);
if (owner != null) {
F ownerType = owner.asObjectable();
LOGGER.trace("Shadow OID {} does have owner: {}", shadowOid, ownerType.getName());
SynchronizationSituationType state = null;
switch (getModificationType(change)) {
case ADD:
case MODIFY:
// if user is found it means account/group is linked to
// resource
state = SynchronizationSituationType.LINKED;
break;
case DELETE:
state = SynchronizationSituationType.DELETED;
}
situation = new SynchronizationSituation<>(ownerType, null, state);
} else {
LOGGER.trace("Resource object shadow doesn't have owner.");
situation = determineSituationWithCorrelation(focusType, change, synchronizationPolicy, owner, configurationType, task, result);
}
} catch (Exception ex) {
LOGGER.error("Error occurred during resource object shadow owner lookup.");
throw new SystemException("Error occurred during resource object shadow owner lookup, reason: "
+ ex.getMessage(), ex);
} finally {
subResult.computeStatus();
}
return situation;
}
private String getOidFromChange(ResourceObjectShadowChangeDescription change) {
if (change.getCurrentShadow() != null && StringUtils.isNotEmpty(change.getCurrentShadow().getOid())) {
return change.getCurrentShadow().getOid();
}
if (change.getOldShadow() != null && StringUtils.isNotEmpty(change.getOldShadow().getOid())) {
return change.getOldShadow().getOid();
}
if (change.getObjectDelta() == null || StringUtils.isEmpty(change.getObjectDelta().getOid())) {
throw new IllegalArgumentException("Oid was not defined in change (not in current, old shadow, delta).");
}
return change.getObjectDelta().getOid();
}
/**
* Tries to match specified focus and shadow. Return true if it matches, false otherwise.
*/
@Override
public <F extends FocusType> boolean matchUserCorrelationRule(PrismObject<ShadowType> shadow, PrismObject<F> focus,
ResourceType resourceType, PrismObject<SystemConfigurationType> configuration, Task task, OperationResult result) throws ConfigurationException, SchemaException, ObjectNotFoundException, ExpressionEvaluationException{
ObjectSynchronizationType synchronizationPolicy = determineSynchronizationPolicy(resourceType, shadow, configuration, task, result);
Class<F> focusClass;
// TODO is this correct? The problem is that synchronizationPolicy can be null...
if (synchronizationPolicy != null) {
focusClass = determineFocusClass(synchronizationPolicy, resourceType);
} else {
focusClass = (Class) focus.asObjectable().getClass();
}
return correlationConfirmationEvaluator.matchUserCorrelationRule(focusClass, shadow, focus, synchronizationPolicy, resourceType,
configuration==null?null:configuration.asObjectable(), task, result);
}
/**
* account is not linked to user. you have to use correlation and
* confirmation rule to be sure user for this account doesn't exists
* resourceShadow only contains the data that were in the repository before
* the change. But the correlation/confirmation should work on the updated
* data. Therefore let's apply the changes before running
* correlation/confirmation
*/
private <F extends FocusType> SynchronizationSituation determineSituationWithCorrelation(
Class<F> focusType, ResourceObjectShadowChangeDescription change, ObjectSynchronizationType synchronizationPolicy,
PrismObject<F> owner, SystemConfigurationType configurationType, Task task, OperationResult result)
throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
if (ChangeType.DELETE.equals(getModificationType(change))) {
// account was deleted and we know it didn't have owner
return new SynchronizationSituation<>(owner == null ? null : owner.asObjectable(),
null, SynchronizationSituationType.DELETED);
}
PrismObject<? extends ShadowType> resourceShadow = change.getCurrentShadow();
ObjectDelta syncDelta = change.getObjectDelta();
if (resourceShadow == null && syncDelta != null && ChangeType.ADD.equals(syncDelta.getChangeType())) {
LOGGER.trace("Trying to compute current shadow from change delta add.");
PrismObject<? extends ShadowType> shadow = syncDelta.computeChangedObject(syncDelta
.getObjectToAdd());
resourceShadow = shadow;
change.setCurrentShadow(shadow);
}
Validate.notNull(resourceShadow, "Current shadow must not be null.");
ResourceType resource = change.getResource().asObjectable();
validateResourceInShadow(resourceShadow.asObjectable(), resource);
SynchronizationSituationType state = null;
LOGGER.trace("SYNCHRONIZATION: CORRELATION: Looking for list of {} objects based on correlation rule.", focusType.getSimpleName());
List<PrismObject<F>> users = correlationConfirmationEvaluator.findFocusesByCorrelationRule(
focusType, resourceShadow.asObjectable(), synchronizationPolicy.getCorrelation(), resource, configurationType, task, result);
if (users == null) {
users = new ArrayList<>();
}
if (users.size() > 1) {
if (synchronizationPolicy.getConfirmation() == null) {
LOGGER.trace("SYNCHRONIZATION: CONFIRMATION: no confirmation defined.");
} else {
LOGGER.debug("SYNCHRONIZATION: CONFIRMATION: Checking objects from correlation with confirmation rule.");
users = correlationConfirmationEvaluator.findUserByConfirmationRule(focusType, users, resourceShadow.asObjectable(),
resource, configurationType, synchronizationPolicy.getConfirmation(), task, result);
}
}
F user = null;
switch (users.size()) {
case 0:
state = SynchronizationSituationType.UNMATCHED;
break;
case 1:
switch (getModificationType(change)) {
case ADD:
case MODIFY:
state = SynchronizationSituationType.UNLINKED;
break;
case DELETE:
state = SynchronizationSituationType.DELETED;
break;
}
user = users.get(0).asObjectable();
break;
default:
state = SynchronizationSituationType.DISPUTED;
}
return new SynchronizationSituation(null, user, state);
}
private void validateResourceInShadow(ShadowType shadow, ResourceType resource) {
if (shadow.getResource() != null || shadow.getResourceRef() != null) {
return;
}
ObjectReferenceType reference = new ObjectReferenceType();
reference.setOid(resource.getOid());
reference.setType(ObjectTypes.RESOURCE.getTypeQName());
shadow.setResourceRef(reference);
}
/**
* @param change
* @return method checks change type in object delta if available, otherwise
* returns {@link ChangeType#ADD}
*/
private ChangeType getModificationType(ResourceObjectShadowChangeDescription change) {
if (change.getObjectDelta() != null) {
return change.getObjectDelta().getChangeType();
}
return ChangeType.ADD;
}
private <F extends FocusType> SynchronizationSituationType reactToChange(Class<F> focusClass, ResourceObjectShadowChangeDescription change,
ObjectSynchronizationType synchronizationPolicy, SynchronizationSituation<F> situation,
ResourceType resource, boolean logDebug, PrismObject<SystemConfigurationType> configuration, Task task, OperationResult parentResult) throws ConfigurationException, ObjectNotFoundException, SchemaException, PolicyViolationException, ExpressionEvaluationException, ObjectAlreadyExistsException, CommunicationException, SecurityViolationException {
SynchronizationSituationType newSituation = situation.getSituation();
SynchronizationReactionType reactionDefinition = findReactionDefinition(synchronizationPolicy, situation,
change.getSourceChannel(), resource);
if (reactionDefinition == null) {
LOGGER.trace("No reaction is defined for situation {} in {}", situation.getSituation(), resource);
return newSituation;
}
// seems to be unused so commented it out [med]
// PrismObject<? extends ObjectType> shadow = null;
// if (change.getCurrentShadow() != null) {
// shadow = change.getCurrentShadow();
// } else if (change.getOldShadow() != null) {
// shadow = change.getOldShadow();
// }
Boolean doReconciliation = determineReconciliation(synchronizationPolicy, reactionDefinition);
if (doReconciliation == null) {
// We have to do reconciliation if we have got a full shadow and no delta.
// There is no other good way how to reflect the changes from the shadow.
if (change.getObjectDelta() == null) {
doReconciliation = true;
}
}
Boolean limitPropagation = determinePropagationLimitation(synchronizationPolicy, reactionDefinition, change.getSourceChannel());
ModelExecuteOptions options = new ModelExecuteOptions();
options.setReconcile(doReconciliation);
options.setLimitPropagation(limitPropagation);
final boolean willSynchronize = isSynchronize(reactionDefinition);
LensContext<F> lensContext = null;
if (willSynchronize) {
lensContext = createLensContext(focusClass, change, reactionDefinition, synchronizationPolicy, situation,
options, configuration, parentResult);
}
if (LOGGER.isTraceEnabled() && lensContext != null) {
LOGGER.trace("---[ SYNCHRONIZATION context before action execution ]-------------------------\n"
+ "{}\n------------------------------------------",
lensContext.debugDump());
}
if (willSynchronize) {
// there's no point in calling executeAction without context - so the actions are executed only if synchronize == true
executeActions(reactionDefinition, lensContext, situation, BeforeAfterType.BEFORE, resource,
logDebug, task, parentResult);
Iterator<LensProjectionContext> iterator = lensContext.getProjectionContextsIterator();
LensProjectionContext originalProjectionContext = iterator.hasNext() ? iterator.next() : null;
clockwork.run(lensContext, task, parentResult);
// note: actions "AFTER" seem to be useless here (basically they modify lens context - which is relevant only if followed by clockwork run)
executeActions(reactionDefinition, lensContext, situation, BeforeAfterType.AFTER, resource,
logDebug, task, parentResult);
if (originalProjectionContext != null) {
newSituation = originalProjectionContext.getSynchronizationSituationResolved();
}
} else {
LOGGER.trace("Skipping clockwork run on {} for situation {}, synchronize is set to false.",
new Object[] { resource, situation.getSituation() });
}
return newSituation;
}
private Boolean determineReconciliation(ObjectSynchronizationType synchronizationPolicy,
SynchronizationReactionType reactionDefinition) {
if (reactionDefinition.isReconcile() != null) {
return reactionDefinition.isReconcile();
}
if (synchronizationPolicy.isReconcile() != null) {
return synchronizationPolicy.isReconcile();
}
return null;
}
private Boolean determinePropagationLimitation(ObjectSynchronizationType synchronizationPolicy,
SynchronizationReactionType reactionDefinition, String channel) {
if (StringUtils.isNotBlank(channel)){
QName channelQName = QNameUtil.uriToQName(channel);
// Discovery channel is used when compensating some inconsistent
// state. Therefore we do not want to propagate changes to other
// resources. We only want to resolve the problem and continue in
// previous provisioning/synchronization during which this
// compensation was triggered.
if (SchemaConstants.CHANGE_CHANNEL_DISCOVERY.equals(channelQName) && SynchronizationSituationType.DELETED != reactionDefinition.getSituation()){
return true;
}
}
if (reactionDefinition.isLimitPropagation() != null) {
return reactionDefinition.isLimitPropagation();
}
if (synchronizationPolicy.isLimitPropagation() != null) {
return synchronizationPolicy.isLimitPropagation();
}
return null;
}
@NotNull
private <F extends FocusType> LensContext<F> createLensContext(Class<F> focusClass, ResourceObjectShadowChangeDescription change,
SynchronizationReactionType reactionDefinition, ObjectSynchronizationType synchronizationPolicy,
SynchronizationSituation<F> situation, ModelExecuteOptions options, PrismObject<SystemConfigurationType> configuration,
OperationResult parentResult) throws ObjectNotFoundException, SchemaException {
LensContext<F> context = contextFactory.createSyncContext(focusClass, change);
context.setLazyAuditRequest(true);
context.setSystemConfiguration(configuration);
context.setOptions(options);
ResourceType resource = change.getResource().asObjectable();
if (ModelExecuteOptions.isLimitPropagation(options)){
context.setTriggeredResource(resource);
}
context.rememberResource(resource);
PrismObject<ShadowType> shadow = getShadowFromChange(change);
if (InternalsConfig.consistencyChecks) shadow.checkConsistence();
// Projection context
ShadowKindType kind = getKind(shadow, synchronizationPolicy);
String intent = getIntent(shadow, synchronizationPolicy);
boolean thombstone = isThombstone(change);
ResourceShadowDiscriminator descr = new ResourceShadowDiscriminator(resource.getOid(), kind,
intent, thombstone);
LensProjectionContext projectionContext = context.createProjectionContext(descr);
projectionContext.setResource(resource);
projectionContext.setOid(getOidFromChange(change));
projectionContext.setSynchronizationSituationDetected(situation.getSituation());
//insert object delta if available in change
ObjectDelta<? extends ShadowType> delta = change.getObjectDelta();
if (delta != null) {
projectionContext.setSyncDelta((ObjectDelta<ShadowType>) delta);
} else {
projectionContext.setSyncAbsoluteTrigger(true);
}
//we insert account if available in change
PrismObject<ShadowType> currentAccount = shadow;
if (currentAccount != null) {
projectionContext.setLoadedObject(currentAccount);
if (!thombstone) {
projectionContext.setFullShadow(true);
}
projectionContext.setFresh(true);
}
if (delta != null && delta.isDelete()) {
projectionContext.setExists(false);
} else {
projectionContext.setExists(true);
}
projectionContext.setDoReconciliation(ModelExecuteOptions.isReconcile(options));
// Focus context
if (situation.getCurrentOwner() != null) {
F focusType = situation.getCurrentOwner();
LensFocusContext<F> focusContext = context.createFocusContext();
PrismObject<F> focusOld = focusType.asPrismObject();
focusContext.setLoadedObject(focusOld);
}
// Global stuff
ObjectReferenceType objectTemplateRef = null;
if (reactionDefinition.getObjectTemplateRef() != null) {
objectTemplateRef = reactionDefinition.getObjectTemplateRef();
} else if (synchronizationPolicy.getObjectTemplateRef() != null) {
objectTemplateRef = synchronizationPolicy.getObjectTemplateRef();
}
if (objectTemplateRef != null) {
ObjectTemplateType objectTemplate = repositoryService.getObject(ObjectTemplateType.class, objectTemplateRef.getOid(), null, parentResult).asObjectable();
context.setFocusTemplate(objectTemplate);
}
return context;
}
protected PrismObject<ShadowType> getShadowFromChange(ResourceObjectShadowChangeDescription change) {
if (change.getCurrentShadow() != null) {
return (PrismObject<ShadowType>) change.getCurrentShadow();
}
if (change.getOldShadow() != null) {
return (PrismObject<ShadowType>) change.getOldShadow();
}
return null;
}
private ShadowKindType getKind(PrismObject<ShadowType> shadow, ObjectSynchronizationType synchronizationPolicy) {
ShadowKindType shadowKind = shadow.asObjectable().getKind();
if (shadowKind != null) {
return shadowKind;
}
if (synchronizationPolicy.getKind() != null) {
return synchronizationPolicy.getKind();
}
return ShadowKindType.ACCOUNT;
}
private String getIntent(PrismObject<ShadowType> shadow, ObjectSynchronizationType synchronizationPolicy) {
String shadowIntent = shadow.asObjectable().getIntent();
if (shadowIntent != null) {
return shadowIntent;
}
return synchronizationPolicy.getIntent();
}
private boolean isThombstone(ResourceObjectShadowChangeDescription change) {
PrismObject<? extends ShadowType> shadow = null;
if (change.getOldShadow() != null){
shadow = change.getOldShadow();
} else if (change.getCurrentShadow() != null){
shadow = change.getCurrentShadow();
}
if (shadow != null){
if (shadow.asObjectable().isDead() != null){
return shadow.asObjectable().isDead().booleanValue();
}
}
ObjectDelta<? extends ShadowType> objectDelta = change.getObjectDelta();
if (objectDelta == null) {
return false;
}
return objectDelta.isDelete();
}
private boolean isSynchronize(SynchronizationReactionType reactionDefinition) {
if (reactionDefinition.isSynchronize() != null) {
return reactionDefinition.isSynchronize();
}
return !reactionDefinition.getAction().isEmpty();
}
private SynchronizationReactionType findReactionDefinition(ObjectSynchronizationType synchronizationPolicy, SynchronizationSituation situation,
String channel, ResourceType resource) throws ConfigurationException {
SynchronizationReactionType defaultReaction = null;
for (SynchronizationReactionType reaction: synchronizationPolicy.getReaction()) {
SynchronizationSituationType reactionSituation = reaction.getSituation();
if (reactionSituation == null) {
throw new ConfigurationException("No situation defined for a reaction in "+resource);
}
if (reactionSituation.equals(situation.getSituation())) {
if (reaction.getChannel() != null && !reaction.getChannel().isEmpty()) {
if (reaction.getChannel().contains("") || reaction.getChannel().contains(null)) {
defaultReaction = reaction;
}
if (reaction.getChannel().contains(channel)) {
return reaction;
} else {
LOGGER.trace("Skipping reaction {} because the channel does not match {}", reaction, channel);
continue;
}
} else {
defaultReaction = reaction;
}
}
}
LOGGER.trace("Using default reaction {}", defaultReaction);
return defaultReaction;
}
/**
* Saves situation, timestamps, kind and intent (if needed)
*/
private PrismObject<ShadowType> saveSyncMetadata(PrismObject<ShadowType> shadow,
SynchronizationSituation situation, ResourceObjectShadowChangeDescription change, ObjectSynchronizationType synchronizationPolicy,
Task task, OperationResult parentResult) {
if (shadow == null) {
return null;
}
ShadowType shadowType = shadow.asObjectable();
// new situation description
List<PropertyDelta<?>> deltas = SynchronizationSituationUtil
.createSynchronizationSituationAndDescriptionDelta(shadow, situation.getSituation(), change.getSourceChannel(), true);
// refresh situation
// PropertyDelta<SynchronizationSituationType> syncSituationDelta = SynchronizationSituationUtil.createSynchronizationSituationDelta(object,
// situation.getSituation());
// if (syncSituationDelta != null){
// syncSituationDeltas.add(syncSituationDelta);
// }
// syncSituationDeltas.add(SynchronizationSituationUtil.createSynchronizationTimestampDelta(object, timestamp));
//
if (shadowType.getKind() == null) {
ShadowKindType kind = synchronizationPolicy.getKind();
if (kind == null) {
kind = ShadowKindType.ACCOUNT;
}
PropertyDelta<ShadowKindType> kindDelta = PropertyDelta.createReplaceDelta(shadow.getDefinition(),
ShadowType.F_KIND, kind);
deltas.add(kindDelta);
}
if (shadowType.getIntent() == null) {
String intent = synchronizationPolicy.getIntent();
if (intent == null) {
intent = SchemaConstants.INTENT_DEFAULT;
}
PropertyDelta<String> intentDelta = PropertyDelta.createReplaceDelta(shadow.getDefinition(),
ShadowType.F_INTENT, intent);
deltas.add(intentDelta);
}
try {
repositoryService.modifyObject(shadowType.getClass(), shadow.getOid(), deltas, parentResult);
ItemDelta.applyTo(deltas, shadow);
task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, null);
return shadow;
} catch (ObjectNotFoundException ex) {
task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, ex);
// This may happen e.g. during some recon-livesync interactions.
// If the shadow is gone then it is gone. No point in recording the situation any more.
LOGGER.debug("Could not update situation in account, because shadow {} does not exist any more (this may be harmless)", shadow.getOid());
parentResult.getLastSubresult().setStatus(OperationResultStatus.HANDLED_ERROR);
} catch (ObjectAlreadyExistsException|SchemaException ex) {
task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, ex);
LoggingUtils.logException(LOGGER,
"### SYNCHRONIZATION # notifyChange(..): Save of synchronization situation failed: could not modify shadow "
+ shadow.getOid() + ": "+ex.getMessage(), ex);
parentResult.recordFatalError("Save of synchronization situation failed: could not modify shadow "
+ shadow.getOid() + ": "+ex.getMessage(), ex);
throw new SystemException("Save of synchronization situation failed: could not modify shadow "
+ shadow.getOid() + ": "+ex.getMessage(), ex);
} catch (Throwable t) {
task.recordObjectActionExecuted(shadow, ChangeType.MODIFY, t);
throw t;
}
return null;
}
private <F extends FocusType> void executeActions(SynchronizationReactionType reactionDef,
LensContext<F> context, SynchronizationSituation<F> situation, BeforeAfterType order, ResourceType resource,
boolean logDebug, Task task, OperationResult parentResult) throws ConfigurationException {
for (SynchronizationActionType actionDef : reactionDef.getAction()) {
if ((actionDef.getOrder() == null && order == BeforeAfterType.BEFORE) ||
(actionDef.getOrder() != null && actionDef.getOrder() == order)) {
String handlerUri = actionDef.getHandlerUri();
if (handlerUri == null) {
handlerUri = actionDef.getRef();
}
if (handlerUri == null) {
LOGGER.error("Action definition in resource {} doesn't contain handler URI",resource);
throw new ConfigurationException("Action definition in resource "+resource+" doesn't contain handler URI");
}
Action action = actionManager.getActionInstance(handlerUri);
if (action == null) {
LOGGER.warn("Couldn't create action with uri '{}' in resource {}, skipping action.", new Object[] {
handlerUri, resource });
continue;
}
// TODO: legacy userTemplate
Map<QName,Object> parameters = null;
if (actionDef.getParameters() != null) {
// TODO: process parameters
// parameters = actionDef.getParameters().getAny();