Skip to content

Commit

Permalink
Minor cleanup (focus->object at a couple of places)
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Sep 4, 2017
1 parent d990fec commit de74933
Show file tree
Hide file tree
Showing 27 changed files with 459 additions and 96 deletions.
Expand Up @@ -316,8 +316,8 @@ public abstract class SchemaConstants {
QNameUtil.qNameToUri(new QName(NS_MODEL_POLICY_SITUATION, "hasAssignment")); // TODO
public static final String MODEL_POLICY_SITUATION_HAS_NO_ASSIGNMENT =
QNameUtil.qNameToUri(new QName(NS_MODEL_POLICY_SITUATION, "hasNoAssignment")); // TODO
public static final String MODEL_POLICY_SITUATION_FOCUS_STATE =
QNameUtil.qNameToUri(new QName(NS_MODEL_POLICY_SITUATION, "focusState")); // TODO
public static final String MODEL_POLICY_SITUATION_OBJECT_STATE =
QNameUtil.qNameToUri(new QName(NS_MODEL_POLICY_SITUATION, "objectState")); // TODO
public static final String MODEL_POLICY_SITUATION_ASSIGNMENT_STATE =
QNameUtil.qNameToUri(new QName(NS_MODEL_POLICY_SITUATION, "assignmentState")); // TODO
public static final String MODEL_POLICY_SITUATION_TIME_VALIDITY =
Expand Down
Expand Up @@ -279,7 +279,7 @@ public static boolean hasMatchingPrimitiveConstraint(PolicyConstraintsType pc, P
|| hasMatchingPrimitiveConstraint(pc.getModification(), F_MODIFICATION, matcher)
|| hasMatchingPrimitiveConstraint(pc.getTimeValidity(), F_TIME_VALIDITY, matcher)
|| hasMatchingPrimitiveConstraint(pc.getAssignmentState(), F_ASSIGNMENT_STATE, matcher)
|| hasMatchingPrimitiveConstraint(pc.getFocusState(), F_FOCUS_STATE, matcher)
|| hasMatchingPrimitiveConstraint(pc.getObjectState(), F_OBJECT_STATE, matcher)
|| hasMatchingPrimitiveConstraint(pc.getSituation(), F_SITUATION, matcher);
if (recursive) {
rv = rv
Expand Down Expand Up @@ -320,11 +320,11 @@ public static boolean isApplicableToAssignment(PolicyRuleType rule) {
if (rule.getEvaluationTarget() != null) {
return rule.getEvaluationTarget() == PolicyRuleEvaluationTargetType.ASSIGNMENT;
} else {
return hasAssignmentOnlyConstraint(rule) || !hasFocusRelatedConstraint(rule);
return hasAssignmentOnlyConstraint(rule) || !hasObjectRelatedConstraint(rule);
}
}

public static boolean isApplicableToFocus(PolicyRuleType rule) {
public static boolean isApplicableToObject(PolicyRuleType rule) {
if (rule.getEvaluationTarget() != null) {
return rule.getEvaluationTarget() == PolicyRuleEvaluationTargetType.FOCUS;
} else {
Expand All @@ -336,9 +336,9 @@ private static boolean hasAssignmentOnlyConstraint(PolicyRuleType rule) {
return hasMatchingPrimitiveConstraint(rule.getPolicyConstraints(), PolicyRuleTypeUtil::isAssignmentOnly, true);
}

// do we have a constraint that indicates a use against focus?
private static boolean hasFocusRelatedConstraint(PolicyRuleType rule) {
return hasMatchingPrimitiveConstraint(rule.getPolicyConstraints(), PolicyRuleTypeUtil::isFocusRelated, true);
// do we have a constraint that indicates a use against object?
private static boolean hasObjectRelatedConstraint(PolicyRuleType rule) {
return hasMatchingPrimitiveConstraint(rule.getPolicyConstraints(), PolicyRuleTypeUtil::isObjectRelated, true);
}

private static final Set<Class<? extends AbstractPolicyConstraintType>> ASSIGNMENTS_ONLY_CONSTRAINTS_CLASSES =
Expand All @@ -353,9 +353,9 @@ private static boolean isAssignmentOnly(QName name, AbstractPolicyConstraintType
private static final Set<Class<? extends AbstractPolicyConstraintType>> FOCUS_RELATED_CONSTRAINTS_CLASSES =
new HashSet<>(Arrays.asList(HasAssignmentPolicyConstraintType.class, ModificationPolicyConstraintType.class));

private static boolean isFocusRelated(QName name, AbstractPolicyConstraintType c) {
private static boolean isObjectRelated(QName name, AbstractPolicyConstraintType c) {
return FOCUS_RELATED_CONSTRAINTS_CLASSES.contains(c.getClass())
|| QNameUtil.match(name, PolicyConstraintsType.F_FOCUS_STATE)
|| QNameUtil.match(name, PolicyConstraintsType.F_OBJECT_STATE)
|| c instanceof TimeValidityPolicyConstraintType && !Boolean.TRUE.equals(((TimeValidityPolicyConstraintType) c).isAssignment());
}
}
Expand Up @@ -12788,10 +12788,10 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="focusState">
<xsd:enumeration value="objectState">
<xsd:annotation>
<xsd:appinfo>
<jaxb:typesafeEnumMember name="FOCUS_STATE"/>
<jaxb:typesafeEnumMember name="OBJECT_STATE"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:enumeration>
Expand Down
Expand Up @@ -98,10 +98,10 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="focusState" type="tns:StatePolicyConstraintType" minOccurs="0" maxOccurs="unbounded">
<xsd:element name="objectState" type="tns:StatePolicyConstraintType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Particular state of the focus, described by a filter or an expression.
Particular state of the object, described by a filter or an expression.
</xsd:documentation>
<xsd:appinfo>
<a:since>3.7</a:since>
Expand Down Expand Up @@ -519,7 +519,7 @@
<xsd:complexType name="StatePolicyConstraintType">
<xsd:annotation>
<xsd:documentation>
A constraint that triggers when a focus or assignment is in a given state.
A constraint that triggers when a object or assignment is in a given state.
If both filter and expressions are specified, both must be true in order for constraint to be triggered.
</xsd:documentation>
<xsd:appinfo>
Expand All @@ -545,7 +545,7 @@
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="checkInState" type="tns:FocusStateType" minOccurs="0" maxOccurs="unbounded">
<xsd:element name="checkInState" type="tns:ObjectStateType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
On what state(s) of the focal object this constraint is to be checked.
Expand All @@ -557,10 +557,10 @@
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="focusStatePolicyConstraintType" type="tns:StatePolicyConstraintType" substitutionGroup="tns:abstractPolicyConstraint" />
<xsd:element name="objectStatePolicyConstraintType" type="tns:StatePolicyConstraintType" substitutionGroup="tns:abstractPolicyConstraint" />
<xsd:element name="assignmentStatePolicyConstraintType" type="tns:StatePolicyConstraintType" substitutionGroup="tns:abstractPolicyConstraint" />

<xsd:simpleType name="FocusStateType">
<xsd:simpleType name="ObjectStateType">
<xsd:annotation>
<xsd:documentation>
On which state of the object a constraint is to be evaluated.
Expand Down
Expand Up @@ -106,7 +106,7 @@ private Collection<CertificationPolicyActionType> getAssignmentCertificationActi

private Collection<CertificationPolicyActionType> getCertificationActions(Collection<EvaluatedPolicyRule> policyRules) {
return policyRules.stream()
.filter(r -> !r.getTriggers().isEmpty() && r.getActions() != null && r.getActions().getCertification() != null)
.filter(r -> r.isTriggered() && r.getActions() != null && r.getActions().getCertification() != null)
.map(r -> r.getActions().getCertification())
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -32,6 +32,10 @@ public interface EvaluatedPolicyRule extends DebugDumpable, Serializable {
@NotNull
Collection<EvaluatedPolicyRuleTrigger<?>> getTriggers();

default boolean isTriggered() {
return !getTriggers().isEmpty();
}

/**
* Returns all triggers, even those that were indirectly "collected" via situation policy rules.
*/
Expand Down
Expand Up @@ -38,7 +38,7 @@ public enum PredefinedPolicySituation {

HAS_NO_ASSIGNMENT(SchemaConstants.MODEL_POLICY_SITUATION_HAS_NO_ASSIGNMENT, PolicyConstraintKindType.HAS_NO_ASSIGNMENT),

FOCUS_STATE(SchemaConstants.MODEL_POLICY_SITUATION_FOCUS_STATE, PolicyConstraintKindType.FOCUS_STATE),
OBJECT_STATE(SchemaConstants.MODEL_POLICY_SITUATION_OBJECT_STATE, PolicyConstraintKindType.OBJECT_STATE),

ASSIGNMENT_STATE(SchemaConstants.MODEL_POLICY_SITUATION_ASSIGNMENT_STATE, PolicyConstraintKindType.ASSIGNMENT_STATE),

Expand Down
Expand Up @@ -174,8 +174,8 @@ private String getSituationFromConstraints(PolicyConstraintsType policyConstrain
return PredefinedPolicySituation.HAS_ASSIGNMENT.getUrl();
} else if (policyConstraints.getHasNoAssignment() != null) {
return PredefinedPolicySituation.HAS_NO_ASSIGNMENT.getUrl();
} else if (policyConstraints.getFocusState() != null) {
return PredefinedPolicySituation.FOCUS_STATE.getUrl();
} else if (policyConstraints.getObjectState() != null) {
return PredefinedPolicySituation.OBJECT_STATE.getUrl();
} else if (policyConstraints.getAssignmentState() != null) {
return PredefinedPolicySituation.ASSIGNMENT_STATE.getUrl();
}
Expand Down
Expand Up @@ -1046,7 +1046,7 @@ private void dumpRules(StringBuilder sb, String label, int indent, Collection<Ev
}

static int getTriggeredRulesCount(Collection<EvaluatedPolicyRule> policyRules) {
return (int) policyRules.stream().filter(r -> !r.getTriggers().isEmpty()).count();
return (int) policyRules.stream().filter(EvaluatedPolicyRule::isTriggered).count();
}

public LensContextType toLensContextType() throws SchemaException {
Expand Down
Expand Up @@ -305,7 +305,7 @@ private <F extends FocusType> void processFocusFocus(LensContext<F> context, Str
// focus deltas so we can properly trigger the rules.

LensUtil.partialExecute("focusPolicyRules",
() -> policyRuleProcessor.evaluateFocusPolicyRules(context, activityDescription, now, task, result),
() -> policyRuleProcessor.evaluateObjectPolicyRules(context, activityDescription, now, task, result),
partialProcessingOptions::getFocusPolicyRules);

// Processing done, check for success
Expand Down
Expand Up @@ -25,9 +25,9 @@
/**
* @author mederly
*/
public class FocusPolicyRuleEvaluationContext<F extends FocusType> extends PolicyRuleEvaluationContext<F> {
public class ObjectPolicyRuleEvaluationContext<F extends FocusType> extends PolicyRuleEvaluationContext<F> {

public FocusPolicyRuleEvaluationContext(@NotNull EvaluatedPolicyRule policyRule, LensContext<F> context, Task task) {
public ObjectPolicyRuleEvaluationContext(@NotNull EvaluatedPolicyRule policyRule, LensContext<F> context, Task task) {
super(policyRule, context, task);
}

Expand Down
Expand Up @@ -139,7 +139,7 @@ public <F extends FocusType> void evaluateAssignmentPolicyRules(LensContext<F> c
//endregion

//region ------------------------------------------------------------------ Focus policy rules
public <F extends FocusType> void evaluateFocusPolicyRules(LensContext<F> context, String activityDescription,
public <F extends FocusType> void evaluateObjectPolicyRules(LensContext<F> context, String activityDescription,
XMLGregorianCalendar now, Task task, OperationResult result)
throws PolicyViolationException, SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
LensFocusContext<F> focusContext = context.getFocusContext();
Expand All @@ -149,15 +149,15 @@ public <F extends FocusType> void evaluateFocusPolicyRules(LensContext<F> contex

List<EvaluatedPolicyRule> rules = new ArrayList<>();
collectFocusRulesFromAssignments(rules, context);
collectGlobalFocusRules(rules, context, task, result);
collectGlobalObjectRules(rules, context, task, result);

for (EvaluatedPolicyRule rule : rules) {
if (!hasSituationConstraint(rule) && isApplicableToFocus(rule)) {
if (!hasSituationConstraint(rule) && isApplicableToObject(rule)) {
evaluateFocusRule(rule, context, task, result);
}
}
for (EvaluatedPolicyRule rule : rules) {
if (hasSituationConstraint(rule) && isApplicableToFocus(rule)) {
if (hasSituationConstraint(rule) && isApplicableToObject(rule)) {
evaluateFocusRule(rule, context, task, result);
}
}
Expand All @@ -166,7 +166,7 @@ public <F extends FocusType> void evaluateFocusPolicyRules(LensContext<F> contex
private <F extends FocusType> void evaluateFocusRule(EvaluatedPolicyRule rule, LensContext<F> context, Task task, OperationResult result)
throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
context.getFocusContext().addPolicyRule(rule);
evaluateRule(new FocusPolicyRuleEvaluationContext<>(rule, context, task), result);
evaluateRule(new ObjectPolicyRuleEvaluationContext<>(rule, context, task), result);
}

private <F extends FocusType> void collectFocusRulesFromAssignments(List<EvaluatedPolicyRule> rules, LensContext<F> context) {
Expand All @@ -179,7 +179,7 @@ private <F extends FocusType> void collectFocusRulesFromAssignments(List<Evaluat
}
}

private <F extends FocusType> void collectGlobalFocusRules(List<EvaluatedPolicyRule> rules, LensContext<F> context,
private <F extends FocusType> void collectGlobalObjectRules(List<EvaluatedPolicyRule> rules, LensContext<F> context,
Task task, OperationResult result)
throws SchemaException, PolicyViolationException, ExpressionEvaluationException, ObjectNotFoundException {
PrismObject<SystemConfigurationType> systemConfiguration = context.getSystemConfiguration();
Expand Down Expand Up @@ -230,8 +230,8 @@ private boolean isApplicableToAssignment(EvaluatedPolicyRule rule) {
return PolicyRuleTypeUtil.isApplicableToAssignment(rule.getPolicyRule());
}

private boolean isApplicableToFocus(EvaluatedPolicyRule rule) {
return PolicyRuleTypeUtil.isApplicableToFocus(rule.getPolicyRule());
private boolean isApplicableToObject(EvaluatedPolicyRule rule) {
return PolicyRuleTypeUtil.isApplicableToObject(rule.getPolicyRule());
}

/**
Expand Down
Expand Up @@ -18,7 +18,7 @@

import com.evolveum.midpoint.model.api.context.EvaluatedPolicyRuleTrigger;
import com.evolveum.midpoint.model.impl.lens.LensFocusContext;
import com.evolveum.midpoint.model.impl.lens.projector.policy.FocusPolicyRuleEvaluationContext;
import com.evolveum.midpoint.model.impl.lens.projector.policy.ObjectPolicyRuleEvaluationContext;
import com.evolveum.midpoint.model.impl.lens.projector.policy.PolicyRuleEvaluationContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
Expand Down Expand Up @@ -50,10 +50,10 @@ public class ModificationConstraintEvaluator implements PolicyConstraintEvaluato
public <F extends FocusType> EvaluatedPolicyRuleTrigger<?> evaluate(JAXBElement<ModificationPolicyConstraintType> constraint,
PolicyRuleEvaluationContext<F> rctx, OperationResult result) throws SchemaException {

if (!(rctx instanceof FocusPolicyRuleEvaluationContext)) {
if (!(rctx instanceof ObjectPolicyRuleEvaluationContext)) {
return null;
}
FocusPolicyRuleEvaluationContext<F> ctx = (FocusPolicyRuleEvaluationContext<F>) rctx;
ObjectPolicyRuleEvaluationContext<F> ctx = (ObjectPolicyRuleEvaluationContext<F>) rctx;

if (modificationConstraintMatches(constraint.getValue(), ctx)) {
return new EvaluatedPolicyRuleTrigger<>(PolicyConstraintKindType.MODIFICATION,
Expand All @@ -64,7 +64,7 @@ public <F extends FocusType> EvaluatedPolicyRuleTrigger<?> evaluate(JAXBElement<
}

private <F extends FocusType> boolean modificationConstraintMatches(ModificationPolicyConstraintType constraint,
FocusPolicyRuleEvaluationContext<F> ctx) throws SchemaException {
ObjectPolicyRuleEvaluationContext<F> ctx) throws SchemaException {
if (!operationMatches(ctx.focusContext, constraint.getOperation())) {
LOGGER.trace("Rule {} operation not applicable", ctx.policyRule.getName());
return false;
Expand Down
Expand Up @@ -78,7 +78,7 @@ private <F extends FocusType> Collection<EvaluatedPolicyRule> selectTriggeredRul
// not trigger, whereas exclusions probably would.) Overall, our responsibility is simply to collect
// all triggered rules.
return evaluatedAssignment.getAllTargetsPolicyRules().stream()
.filter(r -> !r.getTriggers().isEmpty() && situations.contains(r.getPolicySituation()))
.filter(r -> r.isTriggered() && situations.contains(r.getPolicySituation()))
.collect(Collectors.toList());
}
}
Expand Up @@ -51,7 +51,7 @@

import static com.evolveum.midpoint.util.MiscUtil.getSingleValue;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.PolicyConstraintKindType.ASSIGNMENT_STATE;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.PolicyConstraintKindType.FOCUS_STATE;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.PolicyConstraintKindType.OBJECT_STATE;

/**
* @author mederly
Expand All @@ -73,20 +73,20 @@ public <F extends FocusType> EvaluatedPolicyRuleTrigger<?> evaluate(JAXBElement<
boolean assignmentState;
if (QNameUtil.match(constraint.getName(), PolicyConstraintsType.F_ASSIGNMENT_STATE)) {
assignmentState = true;
} else if (QNameUtil.match(constraint.getName(), PolicyConstraintsType.F_FOCUS_STATE)) {
} else if (QNameUtil.match(constraint.getName(), PolicyConstraintsType.F_OBJECT_STATE)) {
assignmentState = false;
} else {
throw new AssertionError("unexpected state constraint " + constraint.getName());
}

List<ObjectInState<F>> objects = new ArrayList<>();
List<FocusStateType> states = constraint.getValue().getCheckInState().isEmpty()
? Collections.singletonList(FocusStateType.CURRENT)
List<ObjectStateType> states = constraint.getValue().getCheckInState().isEmpty()
? Collections.singletonList(ObjectStateType.CURRENT)
: constraint.getValue().getCheckInState();
for (FocusStateType state : states) {
for (ObjectStateType state : states) {
PrismObject<F> object;
if (state == null) {
state = FocusStateType.CURRENT;
state = ObjectStateType.CURRENT;
}
switch (state) {
case OLD: object = rctx.focusContext.getObjectOld(); break;
Expand Down Expand Up @@ -120,9 +120,9 @@ public <F extends FocusType> EvaluatedPolicyRuleTrigger<?> evaluate(JAXBElement<

private static class ObjectInState<F extends FocusType> {
final PrismObject<F> object;
final FocusStateType state;
final ObjectStateType state;

private ObjectInState(PrismObject<F> object, FocusStateType state) {
private ObjectInState(PrismObject<F> object, ObjectStateType state) {
this.object = object;
this.state = state;
}
Expand Down Expand Up @@ -156,7 +156,8 @@ private <F extends FocusType> EvaluatedPolicyRuleTrigger<?> evaluateForFocus(Sta
}

if (match) {
return new EvaluatedPolicyRuleTrigger<>(FOCUS_STATE, constraint, "Focus state ("+ objectInState.state.value() + ") matches " +
return new EvaluatedPolicyRuleTrigger<>(
OBJECT_STATE, constraint, "Focus state ("+ objectInState.state.value() + ") matches " +
(constraint.getName() != null ? "constraint '" + constraint.getName() + "'" : "the constraint"));
}
}
Expand Down
Expand Up @@ -109,7 +109,7 @@ public void test005JackAttemptAssignRoleJudge() throws Exception {
//dumpPolicySituations(context);

assertEvaluatedTargetPolicyRules(context, 7);
assertTargetTriggers(context, PolicyConstraintKindType.FOCUS_STATE, 2);
assertTargetTriggers(context, PolicyConstraintKindType.OBJECT_STATE, 2);
assertTargetTriggers(context, PolicyConstraintKindType.ASSIGNMENT, 4);
assertTargetTriggers(context, null, 6);
}
Expand Down

0 comments on commit de74933

Please sign in to comment.