-
Notifications
You must be signed in to change notification settings - Fork 188
/
PolicyRuleSuspendTaskExecutor.java
80 lines (68 loc) · 3.46 KB
/
PolicyRuleSuspendTaskExecutor.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
/*
* Copyright (c) 2010-2019 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.projector.policy;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule;
import com.evolveum.midpoint.model.api.context.ModelContext;
import com.evolveum.midpoint.model.api.context.ModelElementContext;
import com.evolveum.midpoint.repo.api.CounterManager;
import com.evolveum.midpoint.repo.api.CounterSpecification;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.ThresholdPolicyViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
/**
* @author katka
*
*/
@Component
public class PolicyRuleSuspendTaskExecutor {
private static final Trace LOGGER = TraceManager.getTrace(PolicyRuleSuspendTaskExecutor.class);
@Autowired private CounterManager counterManager;
public <O extends ObjectType> void execute(@NotNull ModelContext<O> context, Task task, OperationResult result) throws ThresholdPolicyViolationException, ObjectNotFoundException, SchemaException {
ModelElementContext<O> focusCtx = context.getFocusContext();
if (focusCtx == null) {
return;
}
String id = context.getTaskTreeOid(task, result);
if (id == null) {
LOGGER.trace("No persistent task context, no counting!");
return;
}
for (EvaluatedPolicyRule policyRule : focusCtx.getPolicyRules()) {
// In theory we could count events also for other kinds of actions (not only SuspendTask)
if (policyRule.containsEnabledAction(SuspendTaskPolicyActionType.class)) {
CounterSpecification counterSpec = counterManager.getCounterSpec(id, policyRule.getPolicyRuleIdentifier(), policyRule.getPolicyRule());
LOGGER.trace("Created/found counter specification {} for:\n{}", counterSpec, DebugUtil.debugDumpLazily(policyRule));
int countAfter = counterSpec.incrementAndGet();
if (isOverThreshold(policyRule.getPolicyThreshold(), countAfter)) {
throw new ThresholdPolicyViolationException("Policy rule violation: " + policyRule.getPolicyRule());
}
}
}
}
private boolean isOverThreshold(PolicyThresholdType thresholdSettings, int counter) throws SchemaException {
// TODO: better implementation that takes high water mark into account
WaterMarkType lowWaterMark = thresholdSettings != null ? thresholdSettings.getLowWaterMark() : null;
if (lowWaterMark == null) {
LOGGER.trace("No low water mark defined.");
return true;
}
Integer lowWaterCount = lowWaterMark.getCount();
if (lowWaterCount == null) {
throw new SchemaException("No count in low water mark in a policy rule");
}
return counter >= lowWaterCount;
}
}