Search in sources :

Example 1 with AssignmentHolderType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType in project midpoint by Evolveum.

the class StateConstraintEvaluator method evaluateForObject.

private <AH extends AssignmentHolderType> EvaluatedStateTrigger evaluateForObject(JAXBElement<StatePolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    StatePolicyConstraintType constraint = constraintElement.getValue();
    int count = (constraint.getFilter() != null ? 1 : 0) + (constraint.getExpression() != null ? 1 : 0) + (constraint.getMessageExpression() != null ? 1 : 0) + (constraint.getExecuteScript() != null ? 1 : 0);
    if (count != 1) {
        throw new SchemaException("Exactly one of filter, expression, messageExpression, executeScript element must be present.");
    }
    PrismObject<AH> object = ctx.getObject();
    if (object == null) {
        return null;
    }
    if (constraint.getFilter() != null) {
        ObjectFilter filter = prismContext.getQueryConverter().parseFilter(constraint.getFilter(), object.asObjectable().getClass());
        if (!filter.match(object.getValue(), matchingRuleRegistry)) {
            return null;
        }
    }
    if (constraint.getExecuteScript() != null) {
        VariablesMap variables = new VariablesMap();
        variables.put(VAR_OBJECT, object, object.getDefinition());
        variables.put(VAR_RULE_EVALUATION_CONTEXT, ctx, PolicyRuleEvaluationContext.class);
        ExecutionContext resultingContext;
        try {
            resultingContext = scriptingExpressionEvaluator.evaluateExpressionPrivileged(constraint.getExecuteScript(), variables, ctx.task, result);
        } catch (ScriptExecutionException e) {
            // TODO
            throw new SystemException(e);
        }
        PipelineData output = resultingContext.getFinalOutput();
        LOGGER.trace("Scripting expression returned {} item(s); console output is:\n{}", output != null ? output.getData().size() : null, resultingContext.getConsoleOutput());
        List<PipelineItem> items = output != null ? output.getData() : emptyList();
        if (items.isEmpty()) {
            return null;
        }
    // TODO retrieve localization messages from output
    }
    if (constraint.getMessageExpression() != null) {
        LocalizableMessageType messageBean = evaluatorHelper.evaluateLocalizableMessageType(constraint.getMessageExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "message expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result);
        if (messageBean == null) {
            return null;
        } else {
            LocalizableMessage message = LocalizationUtil.toLocalizableMessage(messageBean);
            return new EvaluatedStateTrigger(OBJECT_STATE, constraint, message, message);
        }
    }
    if (constraint.getExpression() != null) {
        if (!evaluatorHelper.evaluateBoolean(constraint.getExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "expression in object state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result)) {
            return null;
        }
    }
    return new EvaluatedStateTrigger(OBJECT_STATE, constraint, createMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result), createShortMessage(OBJECT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, false, result));
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ScriptExecutionException(com.evolveum.midpoint.util.exception.ScriptExecutionException) LocalizableMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.LocalizableMessageType) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) StatePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.StatePolicyConstraintType) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) EvaluatedStateTrigger(com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SystemException(com.evolveum.midpoint.util.exception.SystemException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 2 with AssignmentHolderType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType in project midpoint by Evolveum.

the class StateConstraintEvaluator method evaluateForAssignment.

private <AH extends AssignmentHolderType> EvaluatedStateTrigger evaluateForAssignment(JAXBElement<StatePolicyConstraintType> constraintElement, AssignmentPolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    StatePolicyConstraintType constraint = constraintElement.getValue();
    if (constraint.getFilter() != null) {
        throw new UnsupportedOperationException("Filter is not supported for assignment state constraints yet.");
    }
    if (constraint.getExpression() == null) {
        // shouldn't occur
        return null;
    }
    if (!ctx.isApplicableToState()) {
        return null;
    }
    boolean match = evaluatorHelper.evaluateBoolean(constraint.getExpression(), evaluatorHelper.createVariablesMap(ctx, constraintElement), "expression in assignment state constraint " + constraint.getName() + " (" + ctx.state + ")", ctx.task, result);
    if (match) {
        return new EvaluatedStateTrigger(ASSIGNMENT_STATE, constraint, createMessage(ASSIGNMENT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, true, result), createShortMessage(ASSIGNMENT_CONSTRAINT_KEY_PREFIX, constraintElement, ctx, true, result));
    }
    return null;
}
Also used : EvaluatedStateTrigger(com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger) StatePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.StatePolicyConstraintType)

Example 3 with AssignmentHolderType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType in project midpoint by Evolveum.

the class AlwaysTrueConstraintEvaluator method createBuiltInMessage.

@NotNull
private <AH extends AssignmentHolderType> LocalizableMessage createBuiltInMessage(String keyPrefix, JAXBElement<AlwaysTruePolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    AlwaysTruePolicyConstraintType constraint = constraintElement.getValue();
    List<Object> args = new ArrayList<>();
    args.add(evaluatorHelper.createBeforeAfterMessage(ctx));
    String keySuffix;
    if (constraint.getName() != null) {
        args.add(constraint.getName());
        keySuffix = KEY_NAMED;
    } else {
        keySuffix = KEY_UNNAMED;
    }
    LocalizableMessage builtInMessage = new LocalizableMessageBuilder().key(keyPrefix + keySuffix).args(args).build();
    return evaluatorHelper.createLocalizableMessage(constraintElement, ctx, builtInMessage, result);
}
Also used : ArrayList(java.util.ArrayList) AlwaysTruePolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.AlwaysTruePolicyConstraintType) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with AssignmentHolderType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType in project midpoint by Evolveum.

the class OrphanedConstraintEvaluator method createBuiltInMessage.

@NotNull
private <AH extends AssignmentHolderType> LocalizableMessage createBuiltInMessage(String keyPrefix, JAXBElement<OrphanedPolicyConstraintType> constraintElement, PolicyRuleEvaluationContext<AH> ctx, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
    OrphanedPolicyConstraintType constraint = constraintElement.getValue();
    List<Object> args = new ArrayList<>();
    args.add(evaluatorHelper.createBeforeAfterMessage(ctx));
    String keySuffix;
    if (constraint.getName() != null) {
        args.add(constraint.getName());
        keySuffix = KEY_NAMED;
    } else {
        keySuffix = KEY_UNNAMED;
    }
    LocalizableMessage builtInMessage = new LocalizableMessageBuilder().key(keyPrefix + keySuffix).args(args).build();
    return evaluatorHelper.createLocalizableMessage(constraintElement, ctx, builtInMessage, result);
}
Also used : ArrayList(java.util.ArrayList) LocalizableMessage(com.evolveum.midpoint.util.LocalizableMessage) PrismObject(com.evolveum.midpoint.prism.PrismObject) LocalizableMessageBuilder(com.evolveum.midpoint.util.LocalizableMessageBuilder) OrphanedPolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.OrphanedPolicyConstraintType) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with AssignmentHolderType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType in project midpoint by Evolveum.

the class PolicySituationConstraintEvaluator method evaluate.

@Override
public <AH extends AssignmentHolderType> EvaluatedSituationTrigger evaluate(@NotNull JAXBElement<PolicySituationPolicyConstraintType> constraint, @NotNull PolicyRuleEvaluationContext<AH> rctx, OperationResult parentResult) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    OperationResult result = parentResult.subresult(OP_EVALUATE).setMinor().build();
    try {
        // "situation" constraint is present directly on it.
        if (rctx instanceof AssignmentPolicyRuleEvaluationContext && !((AssignmentPolicyRuleEvaluationContext) rctx).isDirect) {
            return null;
        }
        // Single pass only (for the time being)
        PolicySituationPolicyConstraintType situationConstraint = constraint.getValue();
        Collection<EvaluatedPolicyRule> sourceRules = selectTriggeredRules(rctx, situationConstraint.getSituation());
        if (sourceRules.isEmpty()) {
            return null;
        }
        return new EvaluatedSituationTrigger(situationConstraint, createMessage(sourceRules, constraint, rctx, result), createShortMessage(sourceRules, constraint, rctx, result), sourceRules);
    } catch (Throwable t) {
        result.recordFatalError(t.getMessage(), t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : EvaluatedPolicyRule(com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule) AssignmentPolicyRuleEvaluationContext(com.evolveum.midpoint.model.impl.lens.projector.policy.AssignmentPolicyRuleEvaluationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) EvaluatedSituationTrigger(com.evolveum.midpoint.model.api.context.EvaluatedSituationTrigger) PolicySituationPolicyConstraintType(com.evolveum.midpoint.xml.ns._public.common.common_3.PolicySituationPolicyConstraintType)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 NotNull (org.jetbrains.annotations.NotNull)6 LocalizableMessage (com.evolveum.midpoint.util.LocalizableMessage)5 LensContext (com.evolveum.midpoint.model.impl.lens.LensContext)4 LensFocusContext (com.evolveum.midpoint.model.impl.lens.LensFocusContext)4 Trace (com.evolveum.midpoint.util.logging.Trace)4 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)4 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Qualifier (org.springframework.beans.factory.annotation.Qualifier)4 Component (org.springframework.stereotype.Component)4 EvaluatedAssignmentImpl (com.evolveum.midpoint.model.impl.lens.assignments.EvaluatedAssignmentImpl)3 RelationRegistry (com.evolveum.midpoint.schema.RelationRegistry)3 ArrayList (java.util.ArrayList)3 EvaluatedExclusionTrigger (com.evolveum.midpoint.model.api.context.EvaluatedExclusionTrigger)2 EvaluatedStateTrigger (com.evolveum.midpoint.model.api.context.EvaluatedStateTrigger)2 LinkManager (com.evolveum.midpoint.model.common.LinkManager)2 ModelExpressionThreadLocalHolder (com.evolveum.midpoint.model.common.expression.ModelExpressionThreadLocalHolder)2 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2