Search in sources :

Example 6 with WorkflowOperationDefinitionImpl

use of org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl in project opencast by opencast.

the class WorkflowServiceImpl method handleOperationException.

/**
 * Callback for workflow operations that were throwing an exception. This implementation assumes that the operation
 * worker has already adjusted the current operation's state appropriately.
 *
 * @param workflow
 *          the workflow instance
 * @param operation
 *          the current workflow operation
 * @return the workflow instance
 * @throws WorkflowParsingException
 */
protected WorkflowInstance handleOperationException(WorkflowInstance workflow, WorkflowOperationInstance operation) throws WorkflowDatabaseException, WorkflowParsingException, UnauthorizedException {
    WorkflowOperationInstanceImpl currentOperation = (WorkflowOperationInstanceImpl) operation;
    int failedAttempt = currentOperation.getFailedAttempts() + 1;
    currentOperation.setFailedAttempts(failedAttempt);
    currentOperation.addToExecutionHistory(currentOperation.getId());
    // Operation was aborted by the user, after going into hold state
    if (ERROR_RESOLUTION_HANDLER_ID.equals(currentOperation.getTemplate()) && OperationState.FAILED.equals(currentOperation.getState())) {
        int position = currentOperation.getPosition();
        // Advance to operation that actually failed
        if (workflow.getOperations().size() > position + 1) {
            // This should always be true...
            currentOperation = (WorkflowOperationInstanceImpl) workflow.getOperations().get(position + 1);
            // It's currently in RETRY state, change to FAILED
            currentOperation.setState(OperationState.FAILED);
        }
        handleFailedOperation(workflow, currentOperation);
    } else if (currentOperation.getMaxAttempts() != -1 && failedAttempt == currentOperation.getMaxAttempts()) {
        handleFailedOperation(workflow, currentOperation);
    } else {
        switch(currentOperation.getRetryStrategy()) {
            case NONE:
                handleFailedOperation(workflow, currentOperation);
                break;
            case RETRY:
                currentOperation.setState(OperationState.RETRY);
                break;
            case HOLD:
                currentOperation.setState(OperationState.RETRY);
                List<WorkflowOperationInstance> operations = workflow.getOperations();
                WorkflowOperationDefinitionImpl errorResolutionDefinition = new WorkflowOperationDefinitionImpl(ERROR_RESOLUTION_HANDLER_ID, "Error Resolution Operation", "error", false);
                WorkflowOperationInstanceImpl errorResolutionInstance = new WorkflowOperationInstanceImpl(errorResolutionDefinition, currentOperation.getPosition());
                errorResolutionInstance.setExceptionHandlingWorkflow(currentOperation.getExceptionHandlingWorkflow());
                operations.add(currentOperation.getPosition(), errorResolutionInstance);
                workflow.setOperations(operations);
                break;
            default:
                break;
        }
    }
    return workflow;
}
Also used : WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AccessControlList(org.opencastproject.security.api.AccessControlList) List(java.util.List)

Example 7 with WorkflowOperationDefinitionImpl

use of org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl in project opencast by opencast.

the class WorkflowServiceImplAuthzTest method testWorkflowWithoutSecurityPolicy.

@Test
public void testWorkflowWithoutSecurityPolicy() throws Exception {
    // Mock up an authorization service that always returns "false" for hasPermission()
    AuthorizationService authzService = EasyMock.createNiceMock(AuthorizationService.class);
    EasyMock.expect(authzService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(new AccessControlList(), AclScope.Series)).anyTimes();
    EasyMock.expect(authzService.hasPermission((MediaPackage) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(false).anyTimes();
    EasyMock.replay(authzService);
    service.setAuthorizationService(authzService);
    dao.setAuthorizationService(authzService);
    // Create the workflow and its dependent object graph
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.add(new WorkflowOperationDefinitionImpl("op1", "op1", null, true));
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    // As an instructor, create a workflow
    userResponder.setResponse(instructor1);
    WorkflowInstance workflow = service.start(def, mp);
    service.suspend(workflow.getId());
    // Ensure that this instructor can access the workflow
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the organization admin can access that workflow
    userResponder.setResponse(DEFAULT_ORG_ADMIN);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the global admin can access that workflow
    userResponder.setResponse(globalAdmin);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the other instructor can not see the workflow, since there is no security policy granting access
    userResponder.setResponse(instructor2);
    try {
        service.getWorkflowById(workflow.getId());
        fail();
    } catch (UnauthorizedException e) {
    // expected
    }
    assertEquals(0, service.countWorkflowInstances());
    // Ensure the instructor from a different org can not see the workflow, even though they share a role
    organizationResponder.setResponse(otherOrganization);
    userResponder.setResponse(instructorFromDifferentOrg);
    try {
        service.getWorkflowById(workflow.getId());
        fail();
    } catch (Exception e) {
    // expected
    }
    assertEquals(0, service.countWorkflowInstances());
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Test(org.junit.Test)

Example 8 with WorkflowOperationDefinitionImpl

use of org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl in project opencast by opencast.

the class WorkflowServiceImplTest method testRetryStrategyFailover.

@Test
@Ignore
public void testRetryStrategyFailover() throws Exception {
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.setId("workflow-definition-1");
    def.setTitle("workflow-definition-1");
    def.setDescription("workflow-definition-1");
    def.setPublished(true);
    service.registerWorkflowDefinition(def);
    WorkflowOperationDefinitionImpl opDef = new WorkflowOperationDefinitionImpl("failOnHost", "fails on host", null, true);
    opDef.setRetryStrategy(RetryStrategy.RETRY);
    def.add(opDef);
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    WorkflowInstance workflow = startAndWait(def, mp, WorkflowState.SUCCEEDED);
    Assert.assertTrue(service.getWorkflowById(workflow.getId()).getOperations().get(0).getState() == OperationState.SUCCEEDED);
    Assert.assertTrue(service.getWorkflowById(workflow.getId()).getOperations().get(0).getMaxAttempts() == 2);
    Assert.assertTrue(service.getWorkflowById(workflow.getId()).getOperations().get(0).getFailedAttempts() == 1);
}
Also used : WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with WorkflowOperationDefinitionImpl

use of org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl in project opencast by opencast.

the class WorkflowServiceImplTest method testRetryStrategyHoldFailureByUser.

@Test
public void testRetryStrategyHoldFailureByUser() throws Exception {
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.setId("workflow-definition-1");
    def.setTitle("workflow-definition-1");
    def.setDescription("workflow-definition-1");
    def.setPublished(true);
    service.registerWorkflowDefinition(def);
    WorkflowOperationDefinitionImpl opDef = new WorkflowOperationDefinitionImpl("failTwice", "fails twice", null, true);
    opDef.setRetryStrategy(RetryStrategy.HOLD);
    opDef.setMaxAttempts(3);
    opDef.setFailWorkflowOnException(true);
    def.add(opDef);
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    WorkflowInstance workflow = startAndWait(def, mp, WorkflowState.PAUSED);
    WorkflowOperationInstance errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(0);
    WorkflowOperationInstance failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
    Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
    Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
    Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.RETRY);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 1);
    // Try operation a second time, simulate user selecting RETRY
    retryAndWait(service.getWorkflowById(workflow.getId()), "RETRY", WorkflowState.PAUSED);
    errorResolutionOperation = service.getWorkflowById(workflow.getId()).getOperations().get(1);
    failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(2);
    Assert.assertTrue(errorResolutionOperation.getTemplate().equals(WorkflowServiceImpl.ERROR_RESOLUTION_HANDLER_ID));
    Assert.assertTrue(errorResolutionOperation.getState() == OperationState.PAUSED);
    Assert.assertTrue(errorResolutionOperation.getFailedAttempts() == 0);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.RETRY);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 2);
    // Simulate user selecting 'fail'
    retryAndWait(service.getWorkflowById(workflow.getId()), "NONE", WorkflowState.FAILED);
    failTwiceOperation = service.getWorkflowById(workflow.getId()).getOperations().get(2);
    Assert.assertTrue("failTwice".equals(failTwiceOperation.getTemplate()));
    Assert.assertTrue(failTwiceOperation.getState() == OperationState.FAILED);
    Assert.assertTrue(failTwiceOperation.getMaxAttempts() == 3);
    Assert.assertTrue(failTwiceOperation.getFailedAttempts() == 2);
}
Also used : WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 10 with WorkflowOperationDefinitionImpl

use of org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl in project opencast by opencast.

the class WorkflowServiceImplAuthzTest method testWorkflowWithSecurityPolicy.

@Test
public void testWorkflowWithSecurityPolicy() throws Exception {
    // Create an ACL for the authorization service to return
    AccessControlList acl = new AccessControlList();
    acl.getEntries().add(new AccessControlEntry("ROLE_INSTRUCTOR", Permissions.Action.READ.toString(), true));
    acl.getEntries().add(new AccessControlEntry("ROLE_INSTRUCTOR", Permissions.Action.WRITE.toString(), true));
    // Mock up an authorization service that always returns "true" for hasPermission()
    AuthorizationService authzService = EasyMock.createNiceMock(AuthorizationService.class);
    EasyMock.expect(authzService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(acl, AclScope.Series)).anyTimes();
    EasyMock.expect(authzService.hasPermission((MediaPackage) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(true).anyTimes();
    EasyMock.replay(authzService);
    service.setAuthorizationService(authzService);
    dao.setAuthorizationService(authzService);
    // Create the workflow and its dependent object graph
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.add(new WorkflowOperationDefinitionImpl("op1", "op1", null, true));
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    // As an instructor, create a workflow. We don't care if it passes or fails. We just care about access to it.
    userResponder.setResponse(instructor1);
    WorkflowInstance workflow = service.start(def, mp);
    service.suspend(workflow.getId());
    // Ensure that this instructor can access the workflow
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the organization admin can access that workflow
    userResponder.setResponse(DEFAULT_ORG_ADMIN);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the global admin can access that workflow
    userResponder.setResponse(globalAdmin);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the other instructor from this organization can also see the workflow, since this is specified in the
    // security policy
    userResponder.setResponse(instructor2);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // TODO change to answer show in episode or series how to do it. Cool stuff
    // Ensure the instructor from a different org can not see the workflow, even though they share the same role
    organizationResponder.setResponse(otherOrganization);
    userResponder.setResponse(instructorFromDifferentOrg);
    try {
        service.getWorkflowById(workflow.getId());
        fail();
    } catch (Exception e) {
    // expected
    }
    assertEquals(0, service.countWorkflowInstances());
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) AccessControlEntry(org.opencastproject.security.api.AccessControlEntry) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Test(org.junit.Test)

Aggregations

WorkflowOperationDefinitionImpl (org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl)10 MediaPackage (org.opencastproject.mediapackage.MediaPackage)9 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)9 Test (org.junit.Test)8 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)8 AccessControlList (org.opencastproject.security.api.AccessControlList)3 AuthorizationService (org.opencastproject.security.api.AuthorizationService)3 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)3 ArrayList (java.util.ArrayList)2 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 DefaultMediaPackageSerializerImpl (org.opencastproject.mediapackage.DefaultMediaPackageSerializerImpl)1