Search in sources :

Example 1 with DataFlow

use of org.apache.nifi.cluster.protocol.DataFlow in project nifi by apache.

the class TestFlowController method testSynchronizeFlowWhenProposedAuthorizationsAreNull.

@Test(expected = UninheritableFlowException.class)
public void testSynchronizeFlowWhenProposedAuthorizationsAreNull() {
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(StringEncryptor.createEncryptor(nifiProperties), nifiProperties);
    final DataFlow proposedDataFlow = Mockito.mock(DataFlow.class);
    when(proposedDataFlow.getAuthorizerFingerprint()).thenReturn(null);
    controller.synchronize(standardFlowSynchronizer, proposedDataFlow);
}
Also used : FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) Test(org.junit.Test)

Example 2 with DataFlow

use of org.apache.nifi.cluster.protocol.DataFlow in project nifi by apache.

the class TestFlowController method testSynchronizeFlowWithProcessorReferencingControllerService.

@Test
public void testSynchronizeFlowWithProcessorReferencingControllerService() throws IOException {
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(StringEncryptor.createEncryptor(nifiProperties), nifiProperties);
    // create a mock proposed data flow with the same auth fingerprint as the current authorizer
    final String authFingerprint = authorizer.getFingerprint();
    final DataFlow proposedDataFlow = Mockito.mock(DataFlow.class);
    when(proposedDataFlow.getAuthorizerFingerprint()).thenReturn(authFingerprint.getBytes(StandardCharsets.UTF_8));
    final File flowFile = new File("src/test/resources/conf/processor-with-cs-flow-0.7.0.xml");
    final String flow = IOUtils.toString(new FileInputStream(flowFile));
    when(proposedDataFlow.getFlow()).thenReturn(flow.getBytes(StandardCharsets.UTF_8));
    controller.synchronize(standardFlowSynchronizer, proposedDataFlow);
    // should be two controller services
    final Set<ControllerServiceNode> controllerServiceNodes = controller.getAllControllerServices();
    assertNotNull(controllerServiceNodes);
    assertEquals(1, controllerServiceNodes.size());
    // find the controller service that was moved to the root group
    final ControllerServiceNode rootGroupCs = controllerServiceNodes.stream().filter(c -> c.getProcessGroup() != null).findFirst().get();
    assertNotNull(rootGroupCs);
    // should be one processor
    final Set<ProcessorNode> processorNodes = controller.getGroup(controller.getRootGroupId()).getProcessors();
    assertNotNull(processorNodes);
    assertEquals(1, processorNodes.size());
    // verify the processor is still pointing at the controller service that got moved to the root group
    final ProcessorNode processorNode = processorNodes.stream().findFirst().get();
    final PropertyDescriptor procControllerServiceProp = processorNode.getProperties().entrySet().stream().filter(e -> e.getValue().equals(rootGroupCs.getIdentifier())).map(e -> e.getKey()).findFirst().get();
    assertNotNull(procControllerServiceProp);
}
Also used : MockPolicyBasedAuthorizer(org.apache.nifi.authorization.MockPolicyBasedAuthorizer) Bundle(org.apache.nifi.bundle.Bundle) Arrays(java.util.Arrays) ProcessorInstantiationException(org.apache.nifi.controller.exception.ProcessorInstantiationException) FlowFileEventRepository(org.apache.nifi.controller.repository.FlowFileEventRepository) ProcessGroup(org.apache.nifi.groups.ProcessGroup) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) URL(java.net.URL) AbstractPolicyBasedAuthorizer(org.apache.nifi.authorization.AbstractPolicyBasedAuthorizer) BundleDTO(org.apache.nifi.web.api.dto.BundleDTO) ReportingTaskInstantiationException(org.apache.nifi.controller.reporting.ReportingTaskInstantiationException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) ServiceB(org.apache.nifi.controller.service.mock.ServiceB) SchedulingStrategy(org.apache.nifi.scheduling.SchedulingStrategy) After(org.junit.After) Map(java.util.Map) MockProvenanceRepository(org.apache.nifi.provenance.MockProvenanceRepository) FlowRegistryClient(org.apache.nifi.registry.flow.FlowRegistryClient) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) Set(java.util.Set) UUID(java.util.UUID) StandardCharsets(java.nio.charset.StandardCharsets) User(org.apache.nifi.authorization.User) VariableRegistry(org.apache.nifi.registry.VariableRegistry) IOUtils(org.apache.commons.io.IOUtils) Stateful(org.apache.nifi.annotation.behavior.Stateful) Assert.assertFalse(org.junit.Assert.assertFalse) DummyProcessor(org.apache.nifi.controller.service.mock.DummyProcessor) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ExtensionManager(org.apache.nifi.nar.ExtensionManager) DummyReportingTask(org.apache.nifi.controller.service.mock.DummyReportingTask) AuditService(org.apache.nifi.admin.service.AuditService) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) Mockito.mock(org.mockito.Mockito.mock) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) HashMap(java.util.HashMap) Group(org.apache.nifi.authorization.Group) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) Relationship(org.apache.nifi.processor.Relationship) SystemBundle(org.apache.nifi.nar.SystemBundle) LogLevel(org.apache.nifi.logging.LogLevel) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) InstanceClassLoader(org.apache.nifi.nar.InstanceClassLoader) FileBasedVariableRegistry(org.apache.nifi.registry.variable.FileBasedVariableRegistry) MalformedURLException(java.net.MalformedURLException) RequestAction(org.apache.nifi.authorization.RequestAction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) LogRepository(org.apache.nifi.logging.LogRepository) Test(org.junit.Test) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Mockito.when(org.mockito.Mockito.when) ServiceA(org.apache.nifi.controller.service.mock.ServiceA) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) NiFiProperties(org.apache.nifi.util.NiFiProperties) AccessPolicy(org.apache.nifi.authorization.AccessPolicy) BulletinRepository(org.apache.nifi.reporting.BulletinRepository) Assert(org.junit.Assert) Collections(java.util.Collections) LogRepositoryFactory(org.apache.nifi.logging.LogRepositoryFactory) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) File(java.io.File) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with DataFlow

use of org.apache.nifi.cluster.protocol.DataFlow in project nifi by apache.

the class TestFlowController method testSynchronizeFlowWhenExistingMissingComponentsAreDifferent.

@Test
public void testSynchronizeFlowWhenExistingMissingComponentsAreDifferent() throws IOException {
    final StringEncryptor stringEncryptor = StringEncryptor.createEncryptor(nifiProperties);
    final FlowSynchronizer standardFlowSynchronizer = new StandardFlowSynchronizer(stringEncryptor, nifiProperties);
    final ProcessorNode mockProcessorNode = mock(ProcessorNode.class);
    when(mockProcessorNode.getIdentifier()).thenReturn("1");
    when(mockProcessorNode.isExtensionMissing()).thenReturn(true);
    final ControllerServiceNode mockControllerServiceNode = mock(ControllerServiceNode.class);
    when(mockControllerServiceNode.getIdentifier()).thenReturn("2");
    when(mockControllerServiceNode.isExtensionMissing()).thenReturn(true);
    final ReportingTaskNode mockReportingTaskNode = mock(ReportingTaskNode.class);
    when(mockReportingTaskNode.getIdentifier()).thenReturn("3");
    when(mockReportingTaskNode.isExtensionMissing()).thenReturn(true);
    final ProcessGroup mockRootGroup = mock(ProcessGroup.class);
    when(mockRootGroup.findAllProcessors()).thenReturn(Collections.singletonList(mockProcessorNode));
    final SnippetManager mockSnippetManager = mock(SnippetManager.class);
    when(mockSnippetManager.export()).thenReturn(new byte[0]);
    final FlowController mockFlowController = mock(FlowController.class);
    when(mockFlowController.getRootGroup()).thenReturn(mockRootGroup);
    when(mockFlowController.getAllControllerServices()).thenReturn(new HashSet<>(Arrays.asList(mockControllerServiceNode)));
    when(mockFlowController.getAllReportingTasks()).thenReturn(new HashSet<>(Arrays.asList(mockReportingTaskNode)));
    when(mockFlowController.getAuthorizer()).thenReturn(authorizer);
    when(mockFlowController.getSnippetManager()).thenReturn(mockSnippetManager);
    final DataFlow proposedDataFlow = Mockito.mock(DataFlow.class);
    when(proposedDataFlow.getMissingComponents()).thenReturn(new HashSet<>());
    try {
        standardFlowSynchronizer.sync(mockFlowController, proposedDataFlow, stringEncryptor);
        Assert.fail("Should have thrown exception");
    } catch (UninheritableFlowException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("Current flow has missing components that are not considered missing in the proposed flow (1,2,3)"));
    }
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) FlowSynchronizer(org.apache.nifi.controller.serialization.FlowSynchronizer) ProcessGroup(org.apache.nifi.groups.ProcessGroup) StringEncryptor(org.apache.nifi.encrypt.StringEncryptor) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) Test(org.junit.Test)

Example 4 with DataFlow

use of org.apache.nifi.cluster.protocol.DataFlow in project nifi by apache.

the class NodeClusterCoordinator method handleConnectionRequest.

private ConnectionResponseMessage handleConnectionRequest(final ConnectionRequestMessage requestMessage) {
    final NodeIdentifier proposedIdentifier = requestMessage.getConnectionRequest().getProposedNodeIdentifier();
    final NodeIdentifier withRequestorDn = addRequestorDn(proposedIdentifier, requestMessage.getRequestorDN());
    final DataFlow dataFlow = requestMessage.getConnectionRequest().getDataFlow();
    final ConnectionRequest requestWithDn = new ConnectionRequest(withRequestorDn, dataFlow);
    // Resolve Node identifier.
    final NodeIdentifier resolvedNodeId = resolveNodeId(proposedIdentifier);
    if (requireElection) {
        final DataFlow electedDataFlow = flowElection.castVote(dataFlow, withRequestorDn);
        if (electedDataFlow == null) {
            logger.info("Received Connection Request from {}; responding with Flow Election In Progress message", withRequestorDn);
            return createFlowElectionInProgressResponse();
        } else {
            logger.info("Received Connection Request from {}; responding with DataFlow that was elected", withRequestorDn);
            return createConnectionResponse(requestWithDn, resolvedNodeId, electedDataFlow);
        }
    }
    logger.info("Received Connection Request from {}; responding with my DataFlow", withRequestorDn);
    return createConnectionResponse(requestWithDn, resolvedNodeId);
}
Also used : ConnectionRequest(org.apache.nifi.cluster.protocol.ConnectionRequest) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) DataFlow(org.apache.nifi.cluster.protocol.DataFlow)

Example 5 with DataFlow

use of org.apache.nifi.cluster.protocol.DataFlow in project nifi by apache.

the class TestPopularVoteFlowElection method testDifferentEmptyFlows.

@Test
public void testDifferentEmptyFlows() throws IOException {
    final FingerprintFactory fingerprintFactory = Mockito.mock(FingerprintFactory.class);
    Mockito.when(fingerprintFactory.createFingerprint(Mockito.any(byte[].class))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(final InvocationOnMock invocation) throws Throwable {
            final byte[] flow = invocation.getArgumentAt(0, byte[].class);
            final String xml = new String(flow);
            // Return the ID of the root group as the fingerprint.
            final String fingerprint = xml.replaceAll("(?s:(.*<id>)(.*?)(</id>.*))", "$2");
            return fingerprint;
        }
    });
    final PopularVoteFlowElection election = new PopularVoteFlowElection(1, TimeUnit.MINUTES, 3, fingerprintFactory);
    final byte[] flow1 = Files.readAllBytes(Paths.get("src/test/resources/conf/empty-flow.xml"));
    final byte[] flow2 = Files.readAllBytes(Paths.get("src/test/resources/conf/different-empty-flow.xml"));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow1), createNodeId(1)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    assertNull(election.castVote(createDataFlow(flow1), createNodeId(2)));
    assertFalse(election.isElectionComplete());
    assertNull(election.getElectedDataFlow());
    final DataFlow electedDataFlow = election.castVote(createDataFlow(flow2), createNodeId(3));
    assertNotNull(electedDataFlow);
    final String electedFlowXml = new String(electedDataFlow.getFlow());
    assertTrue(new String(flow1).equals(electedFlowXml) || new String(flow2).equals(electedFlowXml));
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) FingerprintFactory(org.apache.nifi.fingerprint.FingerprintFactory) Matchers.anyString(org.mockito.Matchers.anyString) DataFlow(org.apache.nifi.cluster.protocol.DataFlow) StandardDataFlow(org.apache.nifi.cluster.protocol.StandardDataFlow) Test(org.junit.Test)

Aggregations

DataFlow (org.apache.nifi.cluster.protocol.DataFlow)20 Test (org.junit.Test)14 StandardDataFlow (org.apache.nifi.cluster.protocol.StandardDataFlow)11 FlowSynchronizer (org.apache.nifi.controller.serialization.FlowSynchronizer)9 FingerprintFactory (org.apache.nifi.fingerprint.FingerprintFactory)6 IOException (java.io.IOException)5 ProcessGroup (org.apache.nifi.groups.ProcessGroup)5 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 FlowSynchronizationException (org.apache.nifi.controller.serialization.FlowSynchronizationException)4 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)4 StringEncryptor (org.apache.nifi.encrypt.StringEncryptor)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 URL (java.net.URL)3 StandardCharsets (java.nio.charset.StandardCharsets)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3