Search in sources :

Example 21 with ObjectDeltaOperation

use of com.evolveum.midpoint.schema.ObjectDeltaOperation in project midpoint by Evolveum.

the class RefreshHelper method refreshShadowRetryOperations.

private RefreshShadowOperation refreshShadowRetryOperations(ProvisioningContext ctx, PrismObject<ShadowType> repoShadow, List<PendingOperationType> sortedOperations, ProvisioningOperationOptions options, Task task, OperationResult parentResult) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, ExpressionEvaluationException, EncryptionException {
    ShadowType shadowType = repoShadow.asObjectable();
    OperationResult retryResult = new OperationResult(OP_REFRESH_RETRY);
    if (ShadowUtil.isDead(shadowType)) {
        RefreshShadowOperation rso = new RefreshShadowOperation();
        retryResult.recordSuccess();
        rso.setRefreshedShadow(repoShadow);
        rso.setRefreshResult(retryResult);
        return rso;
    }
    Duration retryPeriod = ProvisioningUtil.getRetryPeriod(ctx);
    Collection<ObjectDeltaOperation<ShadowType>> executedDeltas = new ArrayList<>();
    for (PendingOperationType pendingOperation : sortedOperations) {
        if (!needsRetry(pendingOperation)) {
            continue;
        }
        // We really want to get "now" here. Retrying operation may take some time. We want good timestamps that do not lie.
        XMLGregorianCalendar now = clock.currentTimeXMLGregorianCalendar();
        if (!isAfterRetryPeriod(pendingOperation, retryPeriod, now)) {
            if (PendingOperationTypeType.RETRY != pendingOperation.getType()) {
                continue;
            }
            if (!ProvisioningOperationOptions.isForceRetry(options)) {
                continue;
            }
        }
        LOGGER.trace("Going to retry operation {} on {}", pendingOperation, repoShadow);
        // Record attempt number and timestamp before the operation
        // TODO: later use this as an optimistic lock to make sure that two threads won't retry the operation at the same time
        // TODO: move to a better place
        ObjectDelta<ShadowType> shadowDelta = repoShadow.createModifyDelta();
        ItemPath containerPath = pendingOperation.asPrismContainerValue().getPath();
        int attemptNumber = pendingOperation.getAttemptNumber() + 1;
        PropertyDelta<Integer> attemptNumberDelta = shadowDelta.createPropertyModification(containerPath.append(PendingOperationType.F_ATTEMPT_NUMBER));
        attemptNumberDelta.setRealValuesToReplace(attemptNumber);
        shadowDelta.addModification(attemptNumberDelta);
        PropertyDelta<XMLGregorianCalendar> lastAttemptTimestampDelta = shadowDelta.createPropertyModification(containerPath.append(PendingOperationType.F_LAST_ATTEMPT_TIMESTAMP));
        lastAttemptTimestampDelta.setRealValuesToReplace(now);
        shadowDelta.addModification(lastAttemptTimestampDelta);
        PropertyDelta<OperationResultStatusType> resultStatusDelta = shadowDelta.createPropertyModification(containerPath.append(PendingOperationType.F_RESULT_STATUS));
        resultStatusDelta.setRealValuesToReplace(OperationResultStatusType.IN_PROGRESS);
        shadowDelta.addModification(resultStatusDelta);
        shadowManager.modifyShadowAttributes(ctx, repoShadow, shadowDelta.getModifications(), parentResult);
        shadowDelta.applyTo(repoShadow);
        ObjectDeltaType pendingDeltaType = pendingOperation.getDelta();
        ObjectDelta<ShadowType> pendingDelta = DeltaConvertor.createObjectDelta(pendingDeltaType, prismContext);
        ProvisioningOperationState<? extends AsynchronousOperationResult> opState = ProvisioningOperationState.fromPendingOperation(repoShadow, pendingOperation);
        LOGGER.debug("Retrying operation {} on {}, attempt #{}", pendingDelta, repoShadow, attemptNumber);
        ObjectDeltaOperation<ShadowType> objectDeltaOperation = new ObjectDeltaOperation<>(pendingDelta);
        OperationResult result = parentResult.createSubresult(OP_OPERATION_RETRY);
        try {
            retryOperation(ctx, pendingDelta, opState, task, result);
            repoShadow = opState.getRepoShadow();
            result.computeStatus();
            if (result.isError()) {
                retryResult.setStatus(result.getStatus());
            }
            // TODO maybe add whole "result" as subresult to the retryResult?
            result.muteError();
        } catch (CommunicationException | GenericFrameworkException | ObjectAlreadyExistsException | SchemaException | ObjectNotFoundException | ConfigurationException | SecurityViolationException e) {
            // This is final failure: the error is not handled.
            // Therefore the operation is now completed - finished with an error.
            // But we do not want to stop the task. Just log the error.
            LOGGER.error("Operation {} on {} ended up with an error after {} retries: {}", pendingDelta, repoShadow, attemptNumber, e.getMessage(), e);
            // The retry itself was a success. Operation that was retried might have failed.
            // And that is recorded in the shadow. But we have successfully retried the operation.
            result.recordHandledError(e);
            retryResult.recordFatalError("Operation " + pendingDelta + " on " + repoShadow + " ended with an error after " + attemptNumber + " retries: " + e.getMessage());
        } catch (Throwable e) {
            // This is unexpected error during retry. This means that there was other
            // failure that we did not expected. This is likely to be bug - or maybe wrong
            // error handling. This means that the retry was a failure.
            result.recordFatalError(e);
            retryResult.recordFatalError(e);
        } finally {
            // Status should be set by now, we just want to close the result
            result.computeStatusIfUnknown();
        }
        objectDeltaOperation.setExecutionResult(result);
        executedDeltas.add(objectDeltaOperation);
    }
    RefreshShadowOperation rso = new RefreshShadowOperation();
    rso.setExecutedDeltas(executedDeltas);
    rso.setRefreshedShadow(repoShadow);
    parentResult.computeStatus();
    rso.setRefreshResult(retryResult);
    LOGGER.trace("refreshShadowOperation {}", rso.debugDumpLazily());
    return rso;
}
Also used : ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AsynchronousOperationResult(com.evolveum.midpoint.schema.result.AsynchronousOperationResult) GenericFrameworkException(com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException) RefreshShadowOperation(com.evolveum.midpoint.schema.RefreshShadowOperation) Duration(javax.xml.datatype.Duration) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 22 with ObjectDeltaOperation

use of com.evolveum.midpoint.schema.ObjectDeltaOperation in project midpoint by Evolveum.

the class ObjectChangesExecutorImpl method executeChangesSync.

private Collection<ObjectDeltaOperation<? extends ObjectType>> executeChangesSync(Collection<ObjectDelta<? extends ObjectType>> deltas, ModelExecuteOptions options, Task task, OperationResult result) {
    try {
        MidPointApplication application = MidPointApplication.get();
        ModelService service = application.getModel();
        Collection<ObjectDeltaOperation<? extends ObjectType>> executedDeltas = service.executeChanges(deltas, options, task, result);
        result.computeStatusIfUnknown();
        return executedDeltas;
    } catch (CommonException | RuntimeException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Error executing changes", e);
        if (!result.isFatalError()) {
            // just to be sure the exception is recorded into the result
            result.recordFatalError(e.getMessage(), e);
        }
    }
    return null;
}
Also used : MidPointApplication(com.evolveum.midpoint.web.security.MidPointApplication) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) CommonException(com.evolveum.midpoint.util.exception.CommonException) ModelService(com.evolveum.midpoint.model.api.ModelService)

Example 23 with ObjectDeltaOperation

use of com.evolveum.midpoint.schema.ObjectDeltaOperation in project midpoint by Evolveum.

the class ProgressReporter method updateStatusItemState.

private void updateStatusItemState(ProgressReportActivityDto si, ProgressInformation progressInformation, ModelContext modelContext) {
    si.setActivityType(progressInformation.getActivityType());
    si.setResourceShadowDiscriminator(progressInformation.getResourceShadowDiscriminator());
    if (progressInformation.getResourceShadowDiscriminator() != null) {
        String resourceOid = progressInformation.getResourceShadowDiscriminator().getResourceOid();
        String resourceName = resourceOid != null ? getResourceName(resourceOid) : "";
        si.setResourceName(resourceName);
    }
    if (progressInformation.getStateType() == null) {
        si.setStatus(null);
    } else if (progressInformation.getStateType() == ENTERING) {
        si.setStatus(OperationResultStatusType.IN_PROGRESS);
    } else {
        OperationResult result = progressInformation.getOperationResult();
        if (result != null) {
            OperationResultStatus status = result.getStatus();
            if (status == OperationResultStatus.UNKNOWN) {
                status = result.getComputeStatus();
            }
            if (isWriteOpResultForProgressActivity()) {
                si.setOperationResult(result);
            }
            si.setStatus(status.createStatusType());
        } else {
            si.setStatus(OperationResultStatusType.UNKNOWN);
        }
    }
    // information about modifications on a resource
    if (progressInformation.getActivityType() == RESOURCE_OBJECT_OPERATION && progressInformation.getStateType() == EXITING && progressInformation.getResourceShadowDiscriminator() != null && progressInformation.getResourceShadowDiscriminator().getResourceOid() != null) {
        ModelProjectionContext mpc = modelContext.findProjectionContext(progressInformation.getResourceShadowDiscriminator());
        if (mpc != null) {
            // it shouldn't be null!
            // operations performed (TODO aggregate them somehow?)
            List<ProgressReportActivityDto.ResourceOperationResult> resourceOperationResultList = new ArrayList<>();
            List<? extends ObjectDeltaOperation> executedDeltas = mpc.getExecutedDeltas();
            for (ObjectDeltaOperation executedDelta : executedDeltas) {
                ObjectDelta delta = executedDelta.getObjectDelta();
                if (delta != null) {
                    OperationResult r = executedDelta.getExecutionResult();
                    OperationResultStatus status = r.getStatus();
                    if (status == OperationResultStatus.UNKNOWN) {
                        status = r.getComputeStatus();
                    }
                    resourceOperationResultList.add(new ProgressReportActivityDto.ResourceOperationResult(delta.getChangeType(), status));
                }
            }
            si.setResourceOperationResultList(resourceOperationResultList);
            // object name
            PrismObject<ShadowType> object = mpc.getObjectNew();
            if (object == null) {
                object = mpc.getObjectOld();
            }
            String name = null;
            if (object != null) {
                if (object.asObjectable().getName() != null) {
                    name = PolyString.getOrig(object.asObjectable().getName());
                } else {
                    // determine from attributes
                    ResourceAttribute nameAttribute = ShadowUtil.getNamingAttribute(object);
                    if (nameAttribute != null) {
                        name = String.valueOf(nameAttribute.getAnyRealValue());
                    }
                }
            }
            if (name != null) {
                si.setResourceObjectName(name);
            }
        }
    }
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) OperationResultStatus(com.evolveum.midpoint.schema.result.OperationResultStatus) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ResourceAttribute(com.evolveum.midpoint.schema.processor.ResourceAttribute)

Example 24 with ObjectDeltaOperation

use of com.evolveum.midpoint.schema.ObjectDeltaOperation in project midpoint by Evolveum.

the class RObjectDeltaOperation method fromRepo.

public static ObjectDeltaOperation fromRepo(RObjectDeltaOperation operation, PrismContext prismContext) throws DtoTranslationException {
    ObjectDeltaOperation odo = new ObjectDeltaOperation();
    try {
        if (operation.getDelta() != null) {
            ObjectDeltaType delta = prismContext.parserFor(operation.getDelta()).parseRealValue(ObjectDeltaType.class);
            odo.setObjectDelta(DeltaConvertor.createObjectDelta(delta, prismContext));
        }
        if (operation.getFullResult() != null) {
            OperationResultType resultType = prismContext.parserFor(operation.getFullResult()).parseRealValue(OperationResultType.class);
            odo.setExecutionResult(OperationResult.createOperationResult(resultType));
        }
        odo.setObjectName(RPolyString.fromRepo(operation.getObjectName()));
        odo.setResourceOid(operation.getResourceOid());
        odo.setResourceName(RPolyString.fromRepo(operation.getResourceName()));
    } catch (Exception ex) {
        throw new DtoTranslationException(ex.getMessage(), ex);
    }
    return odo;
}
Also used : ObjectDeltaType(com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) OperationResultType(com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException)

Example 25 with ObjectDeltaOperation

use of com.evolveum.midpoint.schema.ObjectDeltaOperation in project midpoint by Evolveum.

the class DummyAuditService method assertAnyRequestDeltas.

public void assertAnyRequestDeltas() {
    AuditEventRecord requestRecord = getRequestRecord();
    Collection<ObjectDeltaOperation<? extends ObjectType>> requestDeltas = requestRecord.getDeltas();
    assert requestDeltas != null && !requestDeltas.isEmpty() : "Expected some deltas in audit request record but found none";
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord)

Aggregations

ObjectDeltaOperation (com.evolveum.midpoint.schema.ObjectDeltaOperation)51 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)24 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)15 Task (com.evolveum.midpoint.task.api.Task)12 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)11 Test (org.testng.annotations.Test)11 ArrayList (java.util.ArrayList)9 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)7 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)6 ModelProjectionContext (com.evolveum.midpoint.model.api.context.ModelProjectionContext)4 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)4 ModelService (com.evolveum.midpoint.model.api.ModelService)3 TestValidityRecomputeTask (com.evolveum.midpoint.model.intest.sync.TestValidityRecomputeTask)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 CommonException (com.evolveum.midpoint.util.exception.CommonException)3 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)3 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)3 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 AuditEventType (com.evolveum.midpoint.audit.api.AuditEventType)2