Search in sources :

Example 11 with FlowController

use of org.apache.nifi.controller.FlowController in project nifi by apache.

the class TestStandardControllerServiceProvider method validateEnableServicesWithDisabledMissingService.

@Test
public void validateEnableServicesWithDisabledMissingService() {
    StandardProcessScheduler scheduler = createScheduler();
    FlowController controller = Mockito.mock(FlowController.class);
    StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateManagerProvider, variableRegistry, niFiProperties);
    ProcessGroup procGroup = new MockProcessGroup(controller);
    Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(procGroup);
    ControllerServiceNode serviceNode1 = provider.createControllerService(ServiceA.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode2 = provider.createControllerService(ServiceA.class.getName(), "2", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode3 = provider.createControllerService(ServiceA.class.getName(), "3", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode4 = provider.createControllerService(ServiceB.class.getName(), "4", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode5 = provider.createControllerService(ServiceA.class.getName(), "5", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode6 = provider.createControllerService(ServiceB.class.getName(), "6", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode serviceNode7 = provider.createControllerService(ServiceC.class.getName(), "7", systemBundle.getBundleDetails().getCoordinate(), null, false);
    procGroup.addControllerService(serviceNode1);
    procGroup.addControllerService(serviceNode2);
    procGroup.addControllerService(serviceNode3);
    procGroup.addControllerService(serviceNode4);
    procGroup.addControllerService(serviceNode5);
    procGroup.addControllerService(serviceNode6);
    procGroup.addControllerService(serviceNode7);
    setProperty(serviceNode1, ServiceA.OTHER_SERVICE.getName(), "2");
    setProperty(serviceNode2, ServiceA.OTHER_SERVICE.getName(), "4");
    setProperty(serviceNode3, ServiceA.OTHER_SERVICE.getName(), "2");
    setProperty(serviceNode3, ServiceA.OTHER_SERVICE_2.getName(), "4");
    setProperty(serviceNode5, ServiceA.OTHER_SERVICE.getName(), "6");
    setProperty(serviceNode7, ServiceC.REQ_SERVICE_1.getName(), "2");
    setProperty(serviceNode7, ServiceC.REQ_SERVICE_2.getName(), "3");
    provider.enableControllerServices(Arrays.asList(serviceNode1, serviceNode2, serviceNode3, serviceNode4, serviceNode5, serviceNode7));
    assertFalse(serviceNode1.isActive());
    assertFalse(serviceNode2.isActive());
    assertFalse(serviceNode3.isActive());
    assertFalse(serviceNode4.isActive());
    assertFalse(serviceNode5.isActive());
    assertFalse(serviceNode6.isActive());
    provider.enableControllerService(serviceNode6);
    provider.enableControllerServices(Arrays.asList(serviceNode1, serviceNode2, serviceNode3, serviceNode4, serviceNode5));
    assertTrue(serviceNode1.isActive());
    assertTrue(serviceNode2.isActive());
    assertTrue(serviceNode3.isActive());
    assertTrue(serviceNode4.isActive());
    assertTrue(serviceNode5.isActive());
    assertTrue(serviceNode6.isActive());
}
Also used : ServiceC(org.apache.nifi.controller.service.mock.ServiceC) ServiceB(org.apache.nifi.controller.service.mock.ServiceB) ServiceA(org.apache.nifi.controller.service.mock.ServiceA) ProcessGroup(org.apache.nifi.groups.ProcessGroup) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessGroup(org.apache.nifi.groups.StandardProcessGroup) FlowController(org.apache.nifi.controller.FlowController) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessScheduler(org.apache.nifi.controller.scheduling.StandardProcessScheduler) Test(org.junit.Test)

Example 12 with FlowController

use of org.apache.nifi.controller.FlowController in project nifi by apache.

the class TestStandardControllerServiceProvider method validateEnableServices2.

/**
 * This test is similar to the above, but different combination of service
 * dependencies
 */
@Test
public void validateEnableServices2() {
    StandardProcessScheduler scheduler = createScheduler();
    FlowController controller = Mockito.mock(FlowController.class);
    StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateManagerProvider, variableRegistry, niFiProperties);
    ProcessGroup procGroup = new MockProcessGroup(controller);
    Mockito.when(controller.getGroup(Mockito.anyString())).thenReturn(procGroup);
    ControllerServiceNode A = provider.createControllerService(ServiceC.class.getName(), "A", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode B = provider.createControllerService(ServiceA.class.getName(), "B", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode C = provider.createControllerService(ServiceB.class.getName(), "C", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode D = provider.createControllerService(ServiceA.class.getName(), "D", systemBundle.getBundleDetails().getCoordinate(), null, false);
    ControllerServiceNode F = provider.createControllerService(ServiceA.class.getName(), "F", systemBundle.getBundleDetails().getCoordinate(), null, false);
    procGroup.addControllerService(A);
    procGroup.addControllerService(B);
    procGroup.addControllerService(C);
    procGroup.addControllerService(D);
    procGroup.addControllerService(F);
    setProperty(A, ServiceC.REQ_SERVICE_1.getName(), "B");
    setProperty(A, ServiceC.REQ_SERVICE_2.getName(), "D");
    setProperty(B, ServiceA.OTHER_SERVICE.getName(), "C");
    setProperty(F, ServiceA.OTHER_SERVICE.getName(), "D");
    setProperty(D, ServiceA.OTHER_SERVICE.getName(), "C");
    provider.enableControllerServices(Arrays.asList(C, F, A, B, D));
    assertTrue(A.isActive());
    assertTrue(B.isActive());
    assertTrue(C.isActive());
    assertTrue(D.isActive());
    assertTrue(F.isActive());
}
Also used : ServiceC(org.apache.nifi.controller.service.mock.ServiceC) ServiceB(org.apache.nifi.controller.service.mock.ServiceB) ServiceA(org.apache.nifi.controller.service.mock.ServiceA) ProcessGroup(org.apache.nifi.groups.ProcessGroup) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessGroup(org.apache.nifi.groups.StandardProcessGroup) FlowController(org.apache.nifi.controller.FlowController) MockProcessGroup(org.apache.nifi.controller.service.mock.MockProcessGroup) StandardProcessScheduler(org.apache.nifi.controller.scheduling.StandardProcessScheduler) Test(org.junit.Test)

Example 13 with FlowController

use of org.apache.nifi.controller.FlowController in project nifi by apache.

the class TestConnectableTask method testIsWorkToDo.

@Test
public void testIsWorkToDo() {
    final ProcessorNode procNode = Mockito.mock(ProcessorNode.class);
    Mockito.when(procNode.hasIncomingConnection()).thenReturn(false);
    final Processor processor = Mockito.mock(Processor.class);
    Mockito.when(procNode.getIdentifier()).thenReturn("123");
    Mockito.when(procNode.getRunnableComponent()).thenReturn(processor);
    final FlowController flowController = Mockito.mock(FlowController.class);
    Mockito.when(flowController.getStateManagerProvider()).thenReturn(Mockito.mock(StateManagerProvider.class));
    final RepositoryContext repoContext = Mockito.mock(RepositoryContext.class);
    Mockito.when(repoContext.getFlowFileEventRepository()).thenReturn(Mockito.mock(FlowFileEventRepository.class));
    final RepositoryContextFactory contextFactory = Mockito.mock(RepositoryContextFactory.class);
    Mockito.when(contextFactory.newProcessContext(Mockito.any(Connectable.class), Mockito.any(AtomicLong.class))).thenReturn(repoContext);
    final LifecycleState scheduleState = new LifecycleState();
    final StringEncryptor encryptor = Mockito.mock(StringEncryptor.class);
    ConnectableTask task = new ConnectableTask(Mockito.mock(SchedulingAgent.class), procNode, flowController, contextFactory, scheduleState, encryptor);
    // There is work to do because there are no incoming connections.
    assertFalse(task.invoke().isYield());
    // Test with only a single connection that is self-looping and empty
    final Connection selfLoopingConnection = Mockito.mock(Connection.class);
    when(selfLoopingConnection.getSource()).thenReturn(procNode);
    when(selfLoopingConnection.getDestination()).thenReturn(procNode);
    when(procNode.hasIncomingConnection()).thenReturn(true);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(selfLoopingConnection));
    assertFalse(task.invoke().isYield());
    // Test with only a single connection that is self-looping and empty
    final FlowFileQueue flowFileQueue = Mockito.mock(FlowFileQueue.class);
    when(flowFileQueue.isActiveQueueEmpty()).thenReturn(true);
    final FlowFileQueue nonEmptyQueue = Mockito.mock(FlowFileQueue.class);
    when(nonEmptyQueue.isActiveQueueEmpty()).thenReturn(false);
    when(selfLoopingConnection.getFlowFileQueue()).thenReturn(nonEmptyQueue);
    assertFalse(task.invoke().isYield());
    // Test with only a non-looping Connection that has no FlowFiles
    final Connection emptyConnection = Mockito.mock(Connection.class);
    when(emptyConnection.getSource()).thenReturn(Mockito.mock(ProcessorNode.class));
    when(emptyConnection.getDestination()).thenReturn(procNode);
    when(emptyConnection.getFlowFileQueue()).thenReturn(flowFileQueue);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(emptyConnection));
    // Create a new ConnectableTask because we want to have a different value for the 'hasNonLoopConnection' value, which is calculated in the task's constructor.
    task = new ConnectableTask(Mockito.mock(SchedulingAgent.class), procNode, flowController, contextFactory, scheduleState, encryptor);
    assertTrue(task.invoke().isYield());
    // test when the queue has data
    final Connection nonEmptyConnection = Mockito.mock(Connection.class);
    when(nonEmptyConnection.getSource()).thenReturn(Mockito.mock(ProcessorNode.class));
    when(nonEmptyConnection.getDestination()).thenReturn(procNode);
    when(nonEmptyConnection.getFlowFileQueue()).thenReturn(nonEmptyQueue);
    when(procNode.getIncomingConnections()).thenReturn(Collections.singletonList(nonEmptyConnection));
    assertFalse(task.invoke().isYield());
}
Also used : RepositoryContext(org.apache.nifi.controller.repository.RepositoryContext) Processor(org.apache.nifi.processor.Processor) Connection(org.apache.nifi.connectable.Connection) LifecycleState(org.apache.nifi.controller.scheduling.LifecycleState) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) ProcessorNode(org.apache.nifi.controller.ProcessorNode) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) RepositoryContextFactory(org.apache.nifi.controller.scheduling.RepositoryContextFactory) Connectable(org.apache.nifi.connectable.Connectable) FlowController(org.apache.nifi.controller.FlowController) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) SchedulingAgent(org.apache.nifi.controller.scheduling.SchedulingAgent) StateManagerProvider(org.apache.nifi.components.state.StateManagerProvider) Test(org.junit.Test)

Example 14 with FlowController

use of org.apache.nifi.controller.FlowController in project nifi by apache.

the class StandardNiFiServiceFacadeTest method setUp.

@Before
public void setUp() throws Exception {
    // audit service
    final AuditService auditService = mock(AuditService.class);
    when(auditService.getAction(anyInt())).then(invocation -> {
        final Integer actionId = invocation.getArgumentAt(0, Integer.class);
        FlowChangeAction action = null;
        if (ACTION_ID_1.equals(actionId)) {
            action = getAction(actionId, PROCESSOR_ID_1);
        } else if (ACTION_ID_2.equals(actionId)) {
            action = getAction(actionId, PROCESSOR_ID_2);
        }
        return action;
    });
    when(auditService.getActions(any(HistoryQuery.class))).then(invocation -> {
        final History history = new History();
        history.setActions(Arrays.asList(getAction(ACTION_ID_1, PROCESSOR_ID_1), getAction(ACTION_ID_2, PROCESSOR_ID_2)));
        return history;
    });
    // authorizable lookup
    final AuthorizableLookup authorizableLookup = mock(AuthorizableLookup.class);
    when(authorizableLookup.getProcessor(Mockito.anyString())).then(getProcessorInvocation -> {
        final String processorId = getProcessorInvocation.getArgumentAt(0, String.class);
        // processor-2 is no longer part of the flow
        if (processorId.equals(PROCESSOR_ID_2)) {
            throw new ResourceNotFoundException("");
        }
        // component authorizable
        final ComponentAuthorizable componentAuthorizable = mock(ComponentAuthorizable.class);
        when(componentAuthorizable.getAuthorizable()).then(getAuthorizableInvocation -> {
            // authorizable
            final Authorizable authorizable = new Authorizable() {

                @Override
                public Authorizable getParentAuthorizable() {
                    return null;
                }

                @Override
                public Resource getResource() {
                    return ResourceFactory.getComponentResource(ResourceType.Processor, processorId, processorId);
                }
            };
            return authorizable;
        });
        return componentAuthorizable;
    });
    // authorizer
    authorizer = mock(Authorizer.class);
    when(authorizer.authorize(any(AuthorizationRequest.class))).then(invocation -> {
        final AuthorizationRequest request = invocation.getArgumentAt(0, AuthorizationRequest.class);
        AuthorizationResult result = AuthorizationResult.denied();
        if (request.getResource().getIdentifier().endsWith(PROCESSOR_ID_1)) {
            if (USER_1.equals(request.getIdentity())) {
                result = AuthorizationResult.approved();
            }
        } else if (request.getResource().equals(ResourceFactory.getControllerResource())) {
            if (USER_2.equals(request.getIdentity())) {
                result = AuthorizationResult.approved();
            }
        }
        return result;
    });
    // flow controller
    final FlowController controller = mock(FlowController.class);
    when(controller.getResource()).thenCallRealMethod();
    when(controller.getParentAuthorizable()).thenCallRealMethod();
    // controller facade
    final ControllerFacade controllerFacade = new ControllerFacade();
    controllerFacade.setFlowController(controller);
    serviceFacade = new StandardNiFiServiceFacade();
    serviceFacade.setAuditService(auditService);
    serviceFacade.setAuthorizableLookup(authorizableLookup);
    serviceFacade.setAuthorizer(authorizer);
    serviceFacade.setEntityFactory(new EntityFactory());
    serviceFacade.setDtoFactory(new DtoFactory());
    serviceFacade.setControllerFacade(controllerFacade);
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) AuthorizationRequest(org.apache.nifi.authorization.AuthorizationRequest) HistoryQuery(org.apache.nifi.history.HistoryQuery) ControllerFacade(org.apache.nifi.web.controller.ControllerFacade) History(org.apache.nifi.history.History) AuthorizationResult(org.apache.nifi.authorization.AuthorizationResult) AuthorizableLookup(org.apache.nifi.authorization.AuthorizableLookup) Authorizer(org.apache.nifi.authorization.Authorizer) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) FlowController(org.apache.nifi.controller.FlowController) AuditService(org.apache.nifi.admin.service.AuditService) EntityFactory(org.apache.nifi.web.api.dto.EntityFactory) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Before(org.junit.Before)

Example 15 with FlowController

use of org.apache.nifi.controller.FlowController in project nifi by apache.

the class ApplicationStartupContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    final NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class);
    try {
        flowService = ctx.getBean("flowService", FlowService.class);
        flowController = ctx.getBean("flowController", FlowController.class);
        requestReplicator = ctx.getBean("requestReplicator", RequestReplicator.class);
        // the flow loading here when not clustered resolves this.
        if (!properties.isNode()) {
            logger.info("Starting Flow Controller...");
            // start and load the flow
            flowService.start();
            flowService.load(null);
            /*
                 * Start up all processors if specified.
                 *
                 * When operating as the node, the cluster manager controls
                 * which processors should start. As part of the flow
                 * reloading actions, the node will start the necessary
                 * processors.
                 */
            flowController.onFlowInitialized(properties.getAutoResumeState());
            logger.info("Flow Controller started successfully.");
        }
    } catch (BeansException | RepositoryPurgeException | IOException e) {
        shutdown();
        throw new NiFiCoreException("Unable to start Flow Controller.", e);
    }
    try {
        // attempt to get a few beans that we want to to ensure properly created since they are lazily initialized
        ctx.getBean("loginIdentityProvider", LoginIdentityProvider.class);
        ctx.getBean("authorizer", Authorizer.class);
    } catch (final BeansException e) {
        shutdown();
        throw new NiFiCoreException("Unable to start Flow Controller.", e);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) ApplicationContext(org.springframework.context.ApplicationContext) NiFiCoreException(org.apache.nifi.web.NiFiCoreException) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) FlowController(org.apache.nifi.controller.FlowController) IOException(java.io.IOException) RepositoryPurgeException(org.apache.nifi.controller.repository.RepositoryPurgeException) FlowService(org.apache.nifi.services.FlowService) BeansException(org.springframework.beans.BeansException)

Aggregations

FlowController (org.apache.nifi.controller.FlowController)16 ProcessGroup (org.apache.nifi.groups.ProcessGroup)10 Test (org.junit.Test)9 MockProcessGroup (org.apache.nifi.controller.service.mock.MockProcessGroup)7 StandardProcessScheduler (org.apache.nifi.controller.scheduling.StandardProcessScheduler)6 ServiceA (org.apache.nifi.controller.service.mock.ServiceA)6 ServiceB (org.apache.nifi.controller.service.mock.ServiceB)6 StandardProcessGroup (org.apache.nifi.groups.StandardProcessGroup)6 AuditService (org.apache.nifi.admin.service.AuditService)3 Authorizer (org.apache.nifi.authorization.Authorizer)3 FlowFileEventRepository (org.apache.nifi.controller.repository.FlowFileEventRepository)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 AuthorizationRequest (org.apache.nifi.authorization.AuthorizationRequest)2 ProcessorNode (org.apache.nifi.controller.ProcessorNode)2 ServiceC (org.apache.nifi.controller.service.mock.ServiceC)2 StringEncryptor (org.apache.nifi.encrypt.StringEncryptor)2 VolatileBulletinRepository (org.apache.nifi.events.VolatileBulletinRepository)2