/
DependencyProcessor.java
625 lines (573 loc) · 30 KB
/
DependencyProcessor.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
/*
* Copyright (c) 2010-2017 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.Iterator;
import java.util.List;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.model.api.context.SynchronizationPolicyDecision;
import com.evolveum.midpoint.model.impl.lens.LensContext;
import com.evolveum.midpoint.model.impl.lens.LensObjectDeltaOperation;
import com.evolveum.midpoint.model.impl.lens.LensProjectionContext;
import com.evolveum.midpoint.model.impl.lens.LensUtil;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.provisioning.api.ProvisioningService;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
import com.evolveum.midpoint.schema.util.ResourceTypeUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.PolicyViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* @author Radovan Semancik
*
*/
@Component
public class DependencyProcessor {
private static final Trace LOGGER = TraceManager.getTrace(DependencyProcessor.class);
@Autowired
private ProvisioningService provisioningService;
public <F extends ObjectType> void resetWaves(LensContext<F> context) throws PolicyViolationException {
}
public <F extends ObjectType> void sortProjectionsToWaves(LensContext<F> context) throws PolicyViolationException {
// Create a snapshot of the projection collection at the beginning of computation.
// The collection may be changed during computation (projections may be added). We do not want to process
// these added projections. They are already processed inside the computation.
// This also avoids ConcurrentModificationException
LensProjectionContext[] projectionArray = context.getProjectionContexts().toArray(new LensProjectionContext[0]);
// Reset incomplete flag for those contexts that are not yet computed
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
if (projectionContext.getWave() < 0) {
projectionContext.setWaveIncomplete(true);
}
}
for (LensProjectionContext projectionContext: projectionArray) {
determineProjectionWave(context, projectionContext, null, null);
projectionContext.setWaveIncomplete(false);
}
if (LOGGER.isTraceEnabled()) {
StringBuilder sb = new StringBuilder();
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
sb.append("\n");
sb.append(projectionContext.getResourceShadowDiscriminator());
sb.append(": ");
sb.append(projectionContext.getWave());
}
LOGGER.trace("Projections sorted to waves (projection wave {}, execution wave {}):{}",
context.getProjectionWave(), context.getExecutionWave(), sb.toString());
}
}
public <F extends ObjectType> int computeMaxWaves(LensContext<F> context) {
if (context.getPartialProcessingOptions().getInbound() != PartialProcessingTypeType.SKIP) {
// Let's do one extra wave with no accounts in it. This time we expect to get the results of the execution to the user
// via inbound, e.g. identifiers generated by the resource, DNs and similar things. Hence the +2 instead of +1
return context.getMaxWave() + 2;
} else {
return context.getMaxWave() + 1;
}
}
private <F extends ObjectType> LensProjectionContext determineProjectionWave(LensContext<F> context,
LensProjectionContext projectionContext, ResourceObjectTypeDependencyType inDependency, List<ResourceObjectTypeDependencyType> depPath) throws PolicyViolationException {
if (!projectionContext.isWaveIncomplete()) {
// This was already processed
return projectionContext;
}
if (projectionContext.isDelete()) {
// When deprovisioning (deleting) the dependencies needs to be processed in reverse
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Determining wave for (deprovision): {}", projectionContext.getHumanReadableName());
}
return determineProjectionWaveDeprovision(context, projectionContext, inDependency, depPath);
} else {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Determining wave for (provision): {}", projectionContext.getHumanReadableName());
}
return determineProjectionWaveProvision(context, projectionContext, inDependency, depPath);
}
}
private <F extends ObjectType> LensProjectionContext determineProjectionWaveProvision(LensContext<F> context,
LensProjectionContext projectionContext, ResourceObjectTypeDependencyType inDependency, List<ResourceObjectTypeDependencyType> depPath) throws PolicyViolationException {
if (depPath == null) {
depPath = new ArrayList<>();
}
int determinedWave = 0;
int determinedOrder = 0;
for (ResourceObjectTypeDependencyType outDependency: projectionContext.getDependencies()) {
if (inDependency != null && isHigerOrder(outDependency, inDependency)) {
// There is incomming dependency. Deal only with dependencies of this order and lower
// otherwise we can end up in endless loop even for legal dependencies.
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing dependency: {}: ignore (higher order)", PrettyPrinter.prettyPrint(outDependency));
}
continue;
}
checkForCircular(depPath, outDependency, projectionContext);
depPath.add(outDependency);
ResourceShadowDiscriminator refDiscr = new ResourceShadowDiscriminator(outDependency,
projectionContext.getResource().getOid(), projectionContext.getKind());
LensProjectionContext dependencyProjectionContext = findDependencyTargetContext(context, projectionContext, outDependency);
if (dependencyProjectionContext == null || dependencyProjectionContext.isDelete()) {
ResourceObjectTypeDependencyStrictnessType outDependencyStrictness = ResourceTypeUtil.getDependencyStrictness(outDependency);
if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.STRICT) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing dependency: {}: unsatisfied strict dependency", PrettyPrinter.prettyPrint(outDependency));
}
throw new PolicyViolationException("Unsatisfied strict dependency of account "+projectionContext.getResourceShadowDiscriminator()+
" dependent on "+refDiscr+": Account not provisioned");
} else if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.LAX) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing dependency: {}: unsatisfied lax dependency", PrettyPrinter.prettyPrint(outDependency));
}
// independent object not in the context, just ignore it
LOGGER.debug("Unsatisfied lax dependency of account "+projectionContext.getResourceShadowDiscriminator()+
" dependent on "+refDiscr+"; dependency skipped");
} else if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.RELAXED) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing dependency: {}: unsatisfied relaxed dependency", PrettyPrinter.prettyPrint(outDependency));
}
// independent object not in the context, just ignore it
LOGGER.debug("Unsatisfied relaxed dependency of account "+projectionContext.getResourceShadowDiscriminator()+
" dependent on "+refDiscr+"; dependency skipped");
} else {
throw new IllegalArgumentException("Unknown dependency strictness "+outDependency.getStrictness()+" in "+refDiscr);
}
} else {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing dependency: {}: satisfied dependency", PrettyPrinter.prettyPrint(outDependency));
}
dependencyProjectionContext = determineProjectionWave(context, dependencyProjectionContext, outDependency, depPath);
LOGGER.trace(" dependency projection wave: {}", dependencyProjectionContext.getWave());
if (dependencyProjectionContext.getWave() + 1 > determinedWave) {
determinedWave = dependencyProjectionContext.getWave() + 1;
if (outDependency.getOrder() == null) {
determinedOrder = 0;
} else {
determinedOrder = outDependency.getOrder();
}
}
LOGGER.trace(" determined dependency wave: {} (order={})", determinedWave, determinedOrder);
}
depPath.remove(outDependency);
}
LensProjectionContext resultAccountContext = projectionContext;
if (projectionContext.getWave() >=0 && projectionContext.getWave() != determinedWave) {
// Wave for this context was set during the run of this method (it was not set when we
// started, we checked at the beginning). Therefore this context must have been visited again.
// therefore there is a circular dependency. Therefore we need to create another context to split it.
ResourceShadowDiscriminator origDiscr = projectionContext.getResourceShadowDiscriminator();
ResourceShadowDiscriminator discr = new ResourceShadowDiscriminator(origDiscr.getResourceOid(), origDiscr.getKind(), origDiscr.getIntent(), origDiscr.isThombstone());
discr.setOrder(determinedOrder);
if (!projectionContext.compareResourceShadowDiscriminator(discr, true)){
resultAccountContext = createAnotherContext(context, projectionContext, discr);
}
}
// LOGGER.trace("Wave for {}: {}", resultAccountContext.getResourceAccountType(), wave);
resultAccountContext.setWave(determinedWave);
return resultAccountContext;
}
private <F extends ObjectType> LensProjectionContext determineProjectionWaveDeprovision(LensContext<F> context,
LensProjectionContext projectionContext, ResourceObjectTypeDependencyType inDependency, List<ResourceObjectTypeDependencyType> depPath) throws PolicyViolationException {
if (depPath == null) {
depPath = new ArrayList<>();
}
int determinedWave = 0;
int determinedOrder = 0;
// This needs to go in the reverse. We need to figure out who depends on us.
for (DependencyAndSource ds: findReverseDependecies(context, projectionContext)) {
LensProjectionContext dependencySourceContext = ds.sourceProjectionContext;
ResourceObjectTypeDependencyType outDependency = ds.dependency;
if (inDependency != null && isHigerOrder(outDependency, inDependency)) {
// There is incomming dependency. Deal only with dependencies of this order and lower
// otherwise we can end up in endless loop even for legal dependencies.
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing (reversed) dependency: {}: ignore (higher order)", PrettyPrinter.prettyPrint(outDependency));
}
continue;
}
if (!dependencySourceContext.isDelete()) {
ResourceObjectTypeDependencyStrictnessType outDependencyStrictness = ResourceTypeUtil.getDependencyStrictness(outDependency);
if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.STRICT) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing (reversed) dependency: {}: unsatisfied strict dependency", PrettyPrinter.prettyPrint(outDependency));
}
throw new PolicyViolationException("Unsatisfied strict reverse dependency of account " + dependencySourceContext.getResourceShadowDiscriminator()+
" dependent on " + projectionContext.getResourceShadowDiscriminator() + ": Account is provisioned, but the account that it depends on is going to be deprovisioned");
} else if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.LAX) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing (reversed) dependency: {}: unsatisfied lax dependency", PrettyPrinter.prettyPrint(outDependency));
}
// independent object not in the context, just ignore it
LOGGER.debug("Unsatisfied lax reversed dependency of account " + dependencySourceContext.getResourceShadowDiscriminator()+
" dependent on " + projectionContext.getResourceShadowDiscriminator() + "; dependency skipped");
} else if (outDependencyStrictness == ResourceObjectTypeDependencyStrictnessType.RELAXED) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing (reversed) dependency: {}: unsatisfied relaxed dependency", PrettyPrinter.prettyPrint(outDependency));
}
// independent object not in the context, just ignore it
LOGGER.debug("Unsatisfied relaxed dependency of account " + dependencySourceContext.getResourceShadowDiscriminator()+
" dependent on " + projectionContext.getResourceShadowDiscriminator() + "; dependency skipped");
} else {
throw new IllegalArgumentException("Unknown dependency strictness "+outDependency.getStrictness()+" in "+dependencySourceContext.getResourceShadowDiscriminator());
}
} else {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(" processing (reversed) dependency: {}: satisfied", PrettyPrinter.prettyPrint(outDependency));
}
checkForCircular(depPath, outDependency, projectionContext);
depPath.add(outDependency);
ResourceShadowDiscriminator refDiscr = new ResourceShadowDiscriminator(outDependency,
projectionContext.getResource().getOid(), projectionContext.getKind());
dependencySourceContext = determineProjectionWave(context, dependencySourceContext, outDependency, depPath);
LOGGER.trace(" dependency projection wave: {}", dependencySourceContext.getWave());
if (dependencySourceContext.getWave() + 1 > determinedWave) {
determinedWave = dependencySourceContext.getWave() + 1;
if (outDependency.getOrder() == null) {
determinedOrder = 0;
} else {
determinedOrder = outDependency.getOrder();
}
}
LOGGER.trace(" determined dependency wave: {} (order={})", determinedWave, determinedOrder);
depPath.remove(outDependency);
}
}
LensProjectionContext resultAccountContext = projectionContext;
if (projectionContext.getWave() >=0 && projectionContext.getWave() != determinedWave) {
// Wave for this context was set during the run of this method (it was not set when we
// started, we checked at the beginning). Therefore this context must have been visited again.
// therefore there is a circular dependency. Therefore we need to create another context to split it.
if (!projectionContext.isDelete()){
resultAccountContext = createAnotherContext(context, projectionContext, determinedOrder);
}
}
// LOGGER.trace("Wave for {}: {}", resultAccountContext.getResourceAccountType(), wave);
resultAccountContext.setWave(determinedWave);
return resultAccountContext;
}
private <F extends ObjectType> Collection<DependencyAndSource> findReverseDependecies(LensContext<F> context,
LensProjectionContext targetProjectionContext) throws PolicyViolationException {
Collection<DependencyAndSource> deps = new ArrayList<>();
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
for (ResourceObjectTypeDependencyType dependency: projectionContext.getDependencies()) {
if (LensUtil.isDependencyTargetContext(projectionContext, targetProjectionContext, dependency)) {
DependencyAndSource ds = new DependencyAndSource();
ds.dependency = dependency;
ds.sourceProjectionContext = projectionContext;
deps.add(ds);
}
}
}
return deps;
}
private void checkForCircular(List<ResourceObjectTypeDependencyType> depPath,
ResourceObjectTypeDependencyType outDependency, LensProjectionContext projectionContext) throws PolicyViolationException {
for (ResourceObjectTypeDependencyType pathElement: depPath) {
if (pathElement.equals(outDependency)) {
StringBuilder sb = new StringBuilder();
Iterator<ResourceObjectTypeDependencyType> iterator = depPath.iterator();
while (iterator.hasNext()) {
ResourceObjectTypeDependencyType el = iterator.next();
ObjectReferenceType resourceRef = el.getResourceRef();
if (resourceRef != null) {
sb.append(resourceRef.getOid());
}
sb.append("(").append(el.getKind()).append("/");
sb.append(el.getIntent()).append(")");
if (iterator.hasNext()) {
sb.append("->");
}
}
throw new PolicyViolationException("Circular dependency in "+projectionContext.getHumanReadableName()+", path: "+sb.toString());
}
}
}
private boolean isHigerOrder(ResourceObjectTypeDependencyType a,
ResourceObjectTypeDependencyType b) {
Integer ao = a.getOrder();
Integer bo = b.getOrder();
if (ao == null) {
ao = 0;
}
if (bo == null) {
bo = 0;
}
return ao > bo;
}
/**
* Find context that has the closest order to the dependency.
*/
private <F extends ObjectType> LensProjectionContext findDependencyTargetContext(
LensContext<F> context, LensProjectionContext sourceProjContext, ResourceObjectTypeDependencyType dependency) {
ResourceShadowDiscriminator refDiscr = new ResourceShadowDiscriminator(dependency,
sourceProjContext.getResource().getOid(), sourceProjContext.getKind());
LensProjectionContext selected = null;
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
if (!projectionContext.compareResourceShadowDiscriminator(refDiscr, false)) {
continue;
}
int ctxOrder = projectionContext.getResourceShadowDiscriminator().getOrder();
if (ctxOrder > refDiscr.getOrder()) {
continue;
}
if (selected == null) {
selected = projectionContext;
} else {
if (ctxOrder > selected.getResourceShadowDiscriminator().getOrder()) {
selected = projectionContext;
}
}
}
return selected;
}
// private <F extends ObjectType> boolean isDependencyTargetContext(LensProjectionContext sourceProjContext, LensProjectionContext targetProjectionContext, ResourceObjectTypeDependencyType dependency) {
// ResourceShadowDiscriminator refDiscr = new ResourceShadowDiscriminator(dependency,
// sourceProjContext.getResource().getOid(), sourceProjContext.getKind());
// return targetProjectionContext.compareResourceShadowDiscriminator(refDiscr, false);
// }
private <F extends ObjectType> LensProjectionContext createAnotherContext(LensContext<F> context, LensProjectionContext origProjectionContext,
ResourceShadowDiscriminator discr) throws PolicyViolationException {
LensProjectionContext otherCtx = context.createProjectionContext(discr);
otherCtx.setResource(origProjectionContext.getResource());
// Force recon for the new context. This is a primitive way how to avoid phantom changes.
otherCtx.setDoReconciliation(true);
return otherCtx;
}
private <F extends ObjectType> LensProjectionContext createAnotherContext(LensContext<F> context, LensProjectionContext origProjectionContext,
int determinedOrder) throws PolicyViolationException {
ResourceShadowDiscriminator origDiscr = origProjectionContext.getResourceShadowDiscriminator();
ResourceShadowDiscriminator discr = new ResourceShadowDiscriminator(origDiscr.getResourceOid(), origDiscr.getKind(), origDiscr.getIntent(), origDiscr.isThombstone());
discr.setOrder(determinedOrder);
LensProjectionContext otherCtx = createAnotherContext(context, origProjectionContext, discr);
return otherCtx;
}
/**
* Check that the dependencies are still satisfied. Also check for high-ordes vs low-order operation consistency
* and stuff like that.
*/
public <F extends ObjectType> boolean checkDependencies(LensContext<F> context,
LensProjectionContext projContext, OperationResult result) throws PolicyViolationException {
if (projContext.isDelete()) {
// It is OK if we depend on something that is not there if we are being removed ... for now
return true;
}
if (projContext.getOid() == null || projContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.ADD) {
// Check for lower-order contexts
LensProjectionContext lowerOrderContext = null;
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
if (projContext == projectionContext) {
continue;
}
if (projectionContext.compareResourceShadowDiscriminator(projContext.getResourceShadowDiscriminator(), false) &&
projectionContext.getResourceShadowDiscriminator().getOrder() < projContext.getResourceShadowDiscriminator().getOrder()) {
if (projectionContext.getOid() != null) {
lowerOrderContext = projectionContext;
break;
}
}
}
if (lowerOrderContext != null) {
if (lowerOrderContext.getOid() != null) {
if (projContext.getOid() == null) {
projContext.setOid(lowerOrderContext.getOid());
}
if (projContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.ADD) {
// This context cannot be ADD. There is a lower-order context with an OID
// it means that the lower-order projection exists, we cannot add it twice
projContext.setSynchronizationPolicyDecision(SynchronizationPolicyDecision.KEEP);
}
}
if (lowerOrderContext.isDelete()) {
projContext.setSynchronizationPolicyDecision(SynchronizationPolicyDecision.DELETE);
}
}
}
for (ResourceObjectTypeDependencyType dependency: projContext.getDependencies()) {
ResourceShadowDiscriminator refRat = new ResourceShadowDiscriminator(dependency,
projContext.getResource().getOid(), projContext.getKind());
LOGGER.trace("LOOKING FOR {}", refRat);
LensProjectionContext dependencyAccountContext = context.findProjectionContext(refRat);
ResourceObjectTypeDependencyStrictnessType strictness = ResourceTypeUtil.getDependencyStrictness(dependency);
if (dependencyAccountContext == null) {
if (strictness == ResourceObjectTypeDependencyStrictnessType.STRICT) {
// This should not happen, it is checked before projection
throw new PolicyViolationException("Unsatisfied strict dependency of "
+ projContext.getResourceShadowDiscriminator().toHumanReadableDescription() +
" dependent on " + refRat.toHumanReadableDescription() + ": No context in dependency check");
} else if (strictness == ResourceObjectTypeDependencyStrictnessType.LAX) {
// independent object not in the context, just ignore it
LOGGER.trace("Unsatisfied lax dependency of account " +
projContext.getResourceShadowDiscriminator().toHumanReadableDescription() +
" dependent on " + refRat.toHumanReadableDescription() + "; dependency skipped");
} else if (strictness == ResourceObjectTypeDependencyStrictnessType.RELAXED) {
// independent object not in the context, just ignore it
LOGGER.trace("Unsatisfied relaxed dependency of account "
+ projContext.getResourceShadowDiscriminator().toHumanReadableDescription() +
" dependent on " + refRat.toHumanReadableDescription() + "; dependency skipped");
} else {
throw new IllegalArgumentException("Unknown dependency strictness "+dependency.getStrictness()+" in "+refRat);
}
} else {
// We have the context of the object that we depend on. We need to check if it was provisioned.
if (strictness == ResourceObjectTypeDependencyStrictnessType.STRICT
|| strictness == ResourceObjectTypeDependencyStrictnessType.RELAXED) {
if (wasProvisioned(dependencyAccountContext, context.getExecutionWave())) {
// everything OK
} else {
// We do not want to throw exception here. That will stop entire projection.
// Let's just mark the projection as broken and skip it.
LOGGER.warn("Unsatisfied dependency of account "+projContext.getResourceShadowDiscriminator()+
" dependent on "+refRat+": Account not provisioned in dependency check (execution wave "+context.getExecutionWave()+", account wave "+projContext.getWave() + ", dependency account wave "+dependencyAccountContext.getWave()+")");
projContext.setSynchronizationPolicyDecision(SynchronizationPolicyDecision.BROKEN);
return false;
}
} else if (strictness == ResourceObjectTypeDependencyStrictnessType.LAX) {
// we don't care what happened, just go on
return true; // TODO why return here? shouldn't we check other dependencies as well? [med]
} else {
throw new IllegalArgumentException("Unknown dependency strictness "+dependency.getStrictness()+" in "+refRat);
}
}
}
return true;
}
public <F extends ObjectType> void preprocessDependencies(LensContext<F> context){
//in the first wave we do not have enough information to preprocess contexts
if (context.getExecutionWave() == 0){
return;
}
for (LensProjectionContext projContext : context.getProjectionContexts()){
if (!projContext.isCanProject()){
continue;
}
for (ResourceObjectTypeDependencyType dependency: projContext.getDependencies()) {
ResourceShadowDiscriminator refRat = new ResourceShadowDiscriminator(dependency,
projContext.getResource().getOid(), projContext.getKind());
LOGGER.trace("LOOKING FOR {}", refRat);
LensProjectionContext dependencyAccountContext = context.findProjectionContext(refRat);
ResourceObjectTypeDependencyStrictnessType strictness = ResourceTypeUtil.getDependencyStrictness(dependency);
if (dependencyAccountContext != null){
if (!dependencyAccountContext.isCanProject()){
continue;
}
// We have the context of the object that we depend on. We need to check if it was provisioned.
if (strictness == ResourceObjectTypeDependencyStrictnessType.STRICT
|| strictness == ResourceObjectTypeDependencyStrictnessType.RELAXED) {
if (wasExecuted(dependencyAccountContext)) {
// everything OK
if (ResourceTypeUtil.isForceLoadDependentShadow(dependency) && !dependencyAccountContext.isDelete()){
dependencyAccountContext.setDoReconciliation(true);
projContext.setDoReconciliation(true);
}
}
}
}
}
}
}
/**
* Finally checks for all the dependencies. Some dependencies cannot be checked during wave computations as
* we might not have all activation decisions yet.
*/
public <F extends ObjectType> void checkDependenciesFinal(LensContext<F> context, OperationResult result) throws PolicyViolationException {
for (LensProjectionContext accountContext: context.getProjectionContexts()) {
checkDependencies(context, accountContext, result);
}
for (LensProjectionContext accountContext: context.getProjectionContexts()) {
if (accountContext.isDelete()
|| accountContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.UNLINK) {
// It is OK if we depend on something that is not there if we are being removed
// but we still need to check if others depends on me
for (LensProjectionContext projectionContext: context.getProjectionContexts()) {
if (projectionContext.isDelete()
|| projectionContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.UNLINK
|| projectionContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.BROKEN
|| projectionContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.IGNORE) {
// If someone who is being deleted depends on us then it does not really matter
continue;
}
for (ResourceObjectTypeDependencyType dependency: projectionContext.getDependencies()) {
String dependencyResourceOid = dependency.getResourceRef() != null ?
dependency.getResourceRef().getOid() : projectionContext.getResource().getOid();
// TODO what to do if dependencyResourceOid or accountContext.getResource() is null?
if (dependencyResourceOid != null && accountContext.getResource() != null &&
dependencyResourceOid.equals(accountContext.getResource().getOid()) &&
MiscSchemaUtil.equalsIntent(dependency.getIntent(), projectionContext.getResourceShadowDiscriminator().getIntent())) {
// Someone depends on us
if (ResourceTypeUtil.getDependencyStrictness(dependency) == ResourceObjectTypeDependencyStrictnessType.STRICT) {
throw new PolicyViolationException("Cannot remove "+accountContext.getHumanReadableName()
+" because "+projectionContext.getHumanReadableName()+" depends on it");
}
}
}
}
}
}
}
private <F extends ObjectType> boolean wasProvisioned(LensProjectionContext accountContext, int executionWave) {
int accountWave = accountContext.getWave();
if (accountWave >= executionWave) {
// This had no chance to be provisioned yet, so we assume it will be provisioned
return true;
}
if (accountContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.BROKEN
|| accountContext.getSynchronizationPolicyDecision() == SynchronizationPolicyDecision.IGNORE) {
return false;
}
PrismObject<ShadowType> objectCurrent = accountContext.getObjectCurrent();
if (objectCurrent != null && objectCurrent.asObjectable().getFailedOperationType() != null) {
// There is unfinished operation in the shadow. We cannot continue.
return false;
}
if (accountContext.isExists()) {
return true;
}
if (accountContext.isAdd()) {
List<LensObjectDeltaOperation<ShadowType>> executedDeltas = accountContext.getExecutedDeltas();
if (executedDeltas == null || executedDeltas.isEmpty()) {
return false;
}
for (LensObjectDeltaOperation<ShadowType> executedDelta: executedDeltas) {
OperationResult executionResult = executedDelta.getExecutionResult();
if (executionResult == null || !executionResult.isSuccess()) {
return false;
}
}
return true;
}
return false;
}
private boolean wasExecuted(LensProjectionContext accountContext){
if (accountContext.isAdd()) {
if (accountContext.getOid() == null){
return false;
}
List<LensObjectDeltaOperation<ShadowType>> executedDeltas = accountContext.getExecutedDeltas();
if (executedDeltas == null || executedDeltas.isEmpty()) {
return false;
}
}
return true;
}
static class DependencyAndSource {
ResourceObjectTypeDependencyType dependency;
LensProjectionContext sourceProjectionContext;
}
}