Java Code Examples for org.apache.nifi.groups.ProcessGroup#addProcessor()

The following examples show how to use org.apache.nifi.groups.ProcessGroup#addProcessor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardFlowSynchronizer.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void addProcessors(final Element processGroupElement, final ProcessGroup processGroup, final FlowController flowController, final FlowEncodingVersion encodingVersion) {
    final List<Element> processorNodeList = getChildrenByTagName(processGroupElement, "processor");
    for (final Element processorElement : processorNodeList) {
        final ProcessorDTO processorDTO = FlowFromDOMFactory.getProcessor(processorElement, encryptor, encodingVersion);

        BundleCoordinate coordinate;
        try {
            coordinate = BundleUtils.getCompatibleBundle(extensionManager, processorDTO.getType(), processorDTO.getBundle());
        } catch (final IllegalStateException e) {
            final BundleDTO bundleDTO = processorDTO.getBundle();
            if (bundleDTO == null) {
                coordinate = BundleCoordinate.UNKNOWN_COORDINATE;
            } else {
                coordinate = new BundleCoordinate(bundleDTO.getGroup(), bundleDTO.getArtifact(), bundleDTO.getVersion());
            }
        }

        final ProcessorNode procNode = flowController.getFlowManager().createProcessor(processorDTO.getType(), processorDTO.getId(), coordinate, false);
        procNode.setVersionedComponentId(processorDTO.getVersionedComponentId());
        processGroup.addProcessor(procNode);
        updateProcessor(procNode, processorDTO, processGroup, flowController);
    }
}
 
Example 2
Source File: TestStandardControllerServiceProvider.java    From nifi with Apache License 2.0 6 votes vote down vote up
private ProcessorNode createProcessor(final StandardProcessScheduler scheduler, final ControllerServiceProvider serviceProvider) {
    final ReloadComponent reloadComponent = Mockito.mock(ReloadComponent.class);

    final Processor processor = new DummyProcessor();
    final MockProcessContext context = new MockProcessContext(processor, Mockito.mock(StateManager.class), variableRegistry);
    final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context);
    processor.initialize(mockInitContext);

    final LoggableComponent<Processor> dummyProcessor = new LoggableComponent<>(processor, systemBundle.getBundleDetails().getCoordinate(), null);
    final ProcessorNode procNode = new StandardProcessorNode(dummyProcessor, mockInitContext.getIdentifier(),
            new StandardValidationContextFactory(serviceProvider, null), scheduler, serviceProvider,
            new StandardComponentVariableRegistry(VariableRegistry.EMPTY_REGISTRY), reloadComponent, extensionManager, new SynchronousValidationTrigger());

    final FlowManager flowManager = Mockito.mock(FlowManager.class);
    final FlowController flowController = Mockito.mock(FlowController.class );
    Mockito.when(flowController.getFlowManager()).thenReturn(flowManager);

    final ProcessGroup group = new StandardProcessGroup(UUID.randomUUID().toString(), serviceProvider, scheduler, null, null, flowController,
        new MutableVariableRegistry(variableRegistry));
    group.addProcessor(procNode);
    procNode.setProcessGroup(group);

    return procNode;
}
 
Example 3
Source File: StandardProcessorDAO.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ProcessorNode createProcessor(final String groupId, ProcessorDTO processorDTO) {
    if (processorDTO.getParentGroupId() != null && !flowController.areGroupsSame(groupId, processorDTO.getParentGroupId())) {
        throw new IllegalArgumentException("Cannot specify a different Parent Group ID than the Group to which the Processor is being added.");
    }

    // ensure the type is specified
    if (processorDTO.getType() == null) {
        throw new IllegalArgumentException("The processor type must be specified.");
    }

    // get the group to add the processor to
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    try {
        // attempt to create the processor
        ProcessorNode processor = flowController.createProcessor(processorDTO.getType(), processorDTO.getId());

        // ensure we can perform the update before we add the processor to the flow
        verifyUpdate(processor, processorDTO);

        // add the processor to the group
        group.addProcessor(processor);

        // configure the processor
        configureProcessor(processor, processorDTO);

        return processor;
    } catch (ProcessorInstantiationException pse) {
        throw new NiFiCoreException(String.format("Unable to create processor of type %s due to: %s", processorDTO.getType(), pse.getMessage()), pse);
    } catch (IllegalStateException | ComponentLifeCycleException ise) {
        throw new NiFiCoreException(ise.getMessage(), ise);
    }
}
 
Example 4
Source File: TestProcessorLifecycle.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Validates the processors start/stop sequence where the order of
 * operations can only be @OnScheduled, @OnUnscheduled, @OnStopped.
 */
@Test
@Ignore
public void validateSuccessfullAndOrderlyShutdown() throws Exception {
    fc = this.buildFlowControllerForTest();
    ProcessGroup testGroup = fc.createProcessGroup(UUID.randomUUID().toString());
    this.setControllerRootGroup(fc, testGroup);
    ProcessorNode testProcNode = fc.createProcessor(TestProcessor.class.getName(), UUID.randomUUID().toString());
    testProcNode.setProperties(properties);
    TestProcessor testProcessor = (TestProcessor) testProcNode.getProcessor();

    // sets the scenario for the processor to run
    int randomDelayLimit = 3000;
    this.randomOnTriggerDelay(testProcessor, randomDelayLimit);

    testProcNode.setMaxConcurrentTasks(4);
    testProcNode.setScheduldingPeriod("500 millis");
    testProcNode.setAutoTerminatedRelationships(Collections.singleton(new Relationship.Builder().name("success").build()));

    testGroup.addProcessor(testProcNode);

    fc.startProcessGroup(testGroup.getIdentifier());
    Thread.sleep(2000); // let it run for a while
    assertTrue(testProcNode.getScheduledState() == ScheduledState.RUNNING);

    fc.stopAllProcessors();

    Thread.sleep(randomDelayLimit); // up to randomDelayLimit, otherwise next assertion may fail as the processor still executing

    // validates that regardless of how many running tasks, lifecycle
    // operation are invoked atomically (once each).
    assertTrue(testProcNode.getScheduledState() == ScheduledState.STOPPED);
    // . . . hence only 3 operations must be in the list
    assertEquals(3, testProcessor.operationNames.size());
    // . . . and ordered as @OnScheduled, @OnUnscheduled, @OnStopped
    assertEquals("@OnScheduled", testProcessor.operationNames.get(0));
    assertEquals("@OnUnscheduled", testProcessor.operationNames.get(1));
    assertEquals("@OnStopped", testProcessor.operationNames.get(2));
}
 
Example 5
Source File: TestProcessorLifecycle.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * The successful processor start with ControllerService dependency.
 */
@Test
public void validateStartSucceedsOnProcessorWithEnabledService() throws Exception {
    fc = this.buildFlowControllerForTest();
    ProcessGroup testGroup = fc.createProcessGroup(UUID.randomUUID().toString());
    this.setControllerRootGroup(fc, testGroup);

    ControllerServiceNode testServiceNode = fc.createControllerService(TestService.class.getName(), "foo", true);
    testGroup.addControllerService(testServiceNode);

    ProcessorNode testProcNode = fc.createProcessor(TestProcessor.class.getName(), UUID.randomUUID().toString());
    testGroup.addProcessor(testProcNode);

    properties.put("S", testServiceNode.getIdentifier());
    testProcNode.setProperties(properties);

    TestProcessor testProcessor = (TestProcessor) testProcNode.getProcessor();
    testProcessor.withService = true;
    this.noop(testProcessor);

    ProcessScheduler ps = fc.getProcessScheduler();
    ps.enableControllerService(testServiceNode);
    ps.startProcessor(testProcNode);

    Thread.sleep(500);
    assertTrue(testProcNode.getScheduledState() == ScheduledState.RUNNING);
}
 
Example 6
Source File: TestStandardControllerServiceProvider.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private ProcessorNode createProcessor(final StandardProcessScheduler scheduler, final ControllerServiceProvider serviceProvider) {
    final ProcessorNode procNode = new StandardProcessorNode(new DummyProcessor(), UUID.randomUUID().toString(),
            new StandardValidationContextFactory(serviceProvider, null), scheduler, serviceProvider,
            NiFiProperties.createBasicNiFiProperties(null, null),
            VariableRegistry.EMPTY_REGISTRY, Mockito.mock(ComponentLog.class));

    final ProcessGroup group = new StandardProcessGroup(UUID.randomUUID().toString(), serviceProvider, scheduler, null, null, null, variableRegistry);
    group.addProcessor(procNode);
    procNode.setProcessGroup(group);

    return procNode;
}
 
Example 7
Source File: StandardProcessorDAO.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ProcessorNode createProcessor(final String groupId, ProcessorDTO processorDTO) {
    if (processorDTO.getParentGroupId() != null && !flowController.getFlowManager().areGroupsSame(groupId, processorDTO.getParentGroupId())) {
        throw new IllegalArgumentException("Cannot specify a different Parent Group ID than the Group to which the Processor is being added.");
    }

    // ensure the type is specified
    if (processorDTO.getType() == null) {
        throw new IllegalArgumentException("The processor type must be specified.");
    }

    // get the group to add the processor to
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    try {
        // attempt to create the processor
        final BundleCoordinate bundleCoordinate = BundleUtils.getBundle(flowController.getExtensionManager(), processorDTO.getType(), processorDTO.getBundle());
        ProcessorNode processor = flowController.getFlowManager().createProcessor(processorDTO.getType(), processorDTO.getId(), bundleCoordinate);

        // ensure we can perform the update before we add the processor to the flow
        verifyUpdate(processor, processorDTO);

        // add the processor to the group
        group.addProcessor(processor);

        // configure the processor
        configureProcessor(processor, processorDTO);

        return processor;
    } catch (IllegalStateException | ComponentLifeCycleException ise) {
        throw new NiFiCoreException(ise.getMessage(), ise);
    }
}
 
Example 8
Source File: ProcessorLifecycleIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Validates the processors start/stop sequence where the order of
 * operations can only be @OnScheduled, @OnUnscheduled, @OnStopped.
 */
@Test
@Ignore
public void validateSuccessfulAndOrderlyShutdown() throws Exception {
    final FlowManagerAndSystemBundle fcsb = this.buildFlowControllerForTest();
    flowManager = fcsb.getFlowManager();
    ProcessGroup testGroup = flowManager.createProcessGroup(UUID.randomUUID().toString());
    ProcessorNode testProcNode = flowManager.createProcessor(TestProcessor.class.getName(), UUID.randomUUID().toString(),
            fcsb.getSystemBundle().getBundleDetails().getCoordinate());
    testProcNode.setProperties(properties);
    TestProcessor testProcessor = (TestProcessor) testProcNode.getProcessor();

    // sets the scenario for the processor to run
    int randomDelayLimit = 3000;
    this.randomOnTriggerDelay(testProcessor, randomDelayLimit);

    testProcNode.setMaxConcurrentTasks(4);
    testProcNode.setScheduldingPeriod("500 millis");
    testProcNode.setAutoTerminatedRelationships(Collections.singleton(new Relationship.Builder().name("success").build()));

    testGroup.addProcessor(testProcNode);

    flowManager.getGroup(testGroup.getIdentifier()).startProcessing();
    assertCondition(() -> ScheduledState.RUNNING == testProcNode.getScheduledState(), SHORT_DELAY_TOLERANCE);

    flowManager.getRootGroup().stopProcessing();

    Thread.sleep(randomDelayLimit); // up to randomDelayLimit, otherwise next assertion may fail as the processor still executing

    // validates that regardless of how many running tasks, lifecycle
    // operation are invoked atomically (once each).
    assertCondition(() -> ScheduledState.STOPPED == testProcNode.getScheduledState(), SHORT_DELAY_TOLERANCE);
    // . . . hence only 3 operations must be in the list
    assertCondition(() -> testProcessor.operationNames.size() == 3, SHORT_DELAY_TOLERANCE);
    // . . . and ordered as @OnScheduled, @OnUnscheduled, @OnStopped
    assertEquals("@OnScheduled", testProcessor.operationNames.get(0));
    assertEquals("@OnUnscheduled", testProcessor.operationNames.get(1));
    assertEquals("@OnStopped", testProcessor.operationNames.get(2));
}