Search in sources :

Example 21 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class RelationshipAuditor method removeConnectionAdvice.

/**
 * Audits the removal of relationships via deleteConnection().
 *
 * @param proceedingJoinPoint join point
 * @param id id
 * @param connectionDAO dao
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ConnectionDAO+) && " + "execution(void deleteConnection(java.lang.String)) && " + "args(id) && " + "target(connectionDAO)")
public void removeConnectionAdvice(ProceedingJoinPoint proceedingJoinPoint, String id, ConnectionDAO connectionDAO) throws Throwable {
    // get the connection before performing the update
    Connection connection = connectionDAO.getConnection(id);
    // perform the underlying operation
    proceedingJoinPoint.proceed();
    // audit the connection creation
    final ConnectDetails connectDetails = createConnectDetails(connection, connection.getRelationships());
    final Action action = generateAuditRecordForConnection(connection, Operation.Disconnect, connectDetails);
    // save the actions
    if (action != null) {
        saveAction(action, logger);
    }
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) ConnectDetails(org.apache.nifi.action.details.ConnectDetails) FlowChangeConnectDetails(org.apache.nifi.action.details.FlowChangeConnectDetails) Connection(org.apache.nifi.connectable.Connection) Around(org.aspectj.lang.annotation.Around)

Example 22 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class SnippetAuditor method updateSnippetAdvice.

/**
 * Audits a bulk move.
 *
 * @param proceedingJoinPoint join point
 * @param snippetDTO dto
 * @param snippetDAO dao
 * @return snippet
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.SnippetDAO+) && " + "execution(org.apache.nifi.controller.Snippet updateSnippetComponents(org.apache.nifi.web.api.dto.SnippetDTO)) && " + "args(snippetDTO) && " + "target(snippetDAO)")
public Snippet updateSnippetAdvice(ProceedingJoinPoint proceedingJoinPoint, SnippetDTO snippetDTO, SnippetDAO snippetDAO) throws Throwable {
    // get the snippet before removing it
    Snippet snippet = snippetDAO.getSnippet(snippetDTO.getId());
    final String previousGroupId = snippet.getParentGroupId();
    // perform the underlying operation
    snippet = (Snippet) proceedingJoinPoint.proceed();
    // if this snippet is linked and its parent group id has changed
    final String groupId = snippetDTO.getParentGroupId();
    if (!previousGroupId.equals(groupId)) {
        // create move audit records for all items in this snippet
        final Collection<Action> actions = new ArrayList<>();
        for (String id : snippet.getProcessors().keySet()) {
            final ProcessorNode processor = processorDAO.getProcessor(id);
            final Action action = processorAuditor.generateAuditRecord(processor, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getFunnels().keySet()) {
            final Funnel funnel = funnelDAO.getFunnel(id);
            final Action action = funnelAuditor.generateAuditRecord(funnel, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getInputPorts().keySet()) {
            final Port port = inputPortDAO.getPort(id);
            final Action action = portAuditor.generateAuditRecord(port, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getOutputPorts().keySet()) {
            final Port port = outputPortDAO.getPort(id);
            final Action action = portAuditor.generateAuditRecord(port, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getRemoteProcessGroups().keySet()) {
            final RemoteProcessGroup remoteProcessGroup = remoteProcessGroupDAO.getRemoteProcessGroup(id);
            final Action action = remoteProcessGroupAuditor.generateAuditRecord(remoteProcessGroup, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getProcessGroups().keySet()) {
            final ProcessGroupDAO processGroupDAO = getProcessGroupDAO();
            final ProcessGroup processGroup = processGroupDAO.getProcessGroup(id);
            final Action action = processGroupAuditor.generateAuditRecord(processGroup, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        for (String id : snippet.getConnections().keySet()) {
            final Connection connection = connectionDAO.getConnection(id);
            final Action action = relationshipAuditor.generateAuditRecordForConnection(connection, Operation.Move, createMoveDetails(previousGroupId, groupId, logger));
            if (action != null) {
                actions.add(action);
            }
        }
        // save the actions
        if (CollectionUtils.isNotEmpty(actions)) {
            saveActions(actions, logger);
        }
    }
    return snippet;
}
Also used : Funnel(org.apache.nifi.connectable.Funnel) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) ProcessorNode(org.apache.nifi.controller.ProcessorNode) ProcessGroupDAO(org.apache.nifi.web.dao.ProcessGroupDAO) RemoteProcessGroupDAO(org.apache.nifi.web.dao.RemoteProcessGroupDAO) Port(org.apache.nifi.connectable.Port) ArrayList(java.util.ArrayList) ProcessGroup(org.apache.nifi.groups.ProcessGroup) RemoteProcessGroup(org.apache.nifi.groups.RemoteProcessGroup) Connection(org.apache.nifi.connectable.Connection) Snippet(org.apache.nifi.controller.Snippet) Around(org.aspectj.lang.annotation.Around)

Example 23 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class TestStandardProcessSession method createConnection.

@SuppressWarnings("unchecked")
private Connection createConnection(AtomicReference<FlowFileQueue> flowFileQueueReference) {
    final Connection connection = Mockito.mock(Connection.class);
    FlowFileQueue flowFileQueueFromReference = flowFileQueueReference.get();
    final FlowFileQueue localFlowFileQueue;
    if (flowFileQueueFromReference == null) {
        localFlowFileQueue = createFlowFileQueueSpy(connection);
        flowFileQueueReference.set(localFlowFileQueue);
    } else {
        localFlowFileQueue = flowFileQueueFromReference;
    }
    when(connection.getFlowFileQueue()).thenReturn(localFlowFileQueue);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            localFlowFileQueue.put((FlowFileRecord) invocation.getArguments()[0]);
            return null;
        }
    }).when(connection).enqueue(Mockito.any(FlowFileRecord.class));
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            localFlowFileQueue.putAll((Collection<FlowFileRecord>) invocation.getArguments()[0]);
            return null;
        }
    }).when(connection).enqueue(Mockito.any(Collection.class));
    final Connectable dest = Mockito.mock(Connectable.class);
    when(connection.getDestination()).thenReturn(dest);
    when(connection.getSource()).thenReturn(dest);
    Mockito.doAnswer(new Answer<FlowFile>() {

        @Override
        public FlowFile answer(InvocationOnMock invocation) throws Throwable {
            return localFlowFileQueue.poll(invocation.getArgumentAt(0, Set.class));
        }
    }).when(connection).poll(any(Set.class));
    Mockito.doAnswer(new Answer<List<FlowFileRecord>>() {

        @Override
        public List<FlowFileRecord> answer(InvocationOnMock invocation) throws Throwable {
            return localFlowFileQueue.poll(invocation.getArgumentAt(0, FlowFileFilter.class), invocation.getArgumentAt(1, Set.class));
        }
    }).when(connection).poll(any(FlowFileFilter.class), any(Set.class));
    Mockito.when(connection.getIdentifier()).thenReturn("conn-uuid");
    return connection;
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.apache.nifi.connectable.Connection) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Connectable(org.apache.nifi.connectable.Connectable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 24 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class TestStandardProcessSession method testRoundRobinAcrossConnectionsOnSessionGetWithCount.

@Test
@SuppressWarnings("unchecked")
public void testRoundRobinAcrossConnectionsOnSessionGetWithCount() {
    final AtomicReference<FlowFileQueue> queue1Reference = new AtomicReference<>();
    final AtomicReference<FlowFileQueue> queue2Reference = new AtomicReference<>();
    final List<Connection> connList = new ArrayList<>();
    final Connection conn1 = createConnection(queue1Reference);
    final Connection conn2 = createConnection(queue2Reference);
    connList.add(conn1);
    connList.add(conn2);
    final FlowFileQueue queue2 = queue2Reference.get();
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().id(1000L).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    queue2.put(flowFileRecord);
    when(connectable.getIncomingConnections()).thenReturn(connList);
    List<FlowFile> result = session.get(2);
    assertEquals(1, result.size());
    verify(conn1, times(1)).poll(any(FlowFileFilter.class), any(Set.class));
    verify(conn2, times(1)).poll(any(FlowFileFilter.class), any(Set.class));
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) FlowFileFilter(org.apache.nifi.processor.FlowFileFilter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardFlowFileQueue(org.apache.nifi.controller.StandardFlowFileQueue) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) Test(org.junit.Test)

Example 25 with Connection

use of org.apache.nifi.connectable.Connection in project nifi by apache.

the class TestStandardProcessSession method testRoundRobinOnSessionGetNoArgs.

@Test
@SuppressWarnings("unchecked")
public void testRoundRobinOnSessionGetNoArgs() {
    final List<Connection> connList = new ArrayList<>();
    final Connection conn1 = createConnection();
    final Connection conn2 = createConnection();
    connList.add(conn1);
    connList.add(conn2);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().id(1000L).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).build();
    flowFileQueue.put(flowFileRecord);
    flowFileQueue.put(flowFileRecord);
    when(connectable.getIncomingConnections()).thenReturn(connList);
    session.get();
    session.get();
    verify(conn1, times(1)).poll(any(Set.class));
    verify(conn2, times(1)).poll(any(Set.class));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Connection(org.apache.nifi.connectable.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Connection (org.apache.nifi.connectable.Connection)95 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)35 VersionedConnection (org.apache.nifi.registry.flow.VersionedConnection)30 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)28 Connectable (org.apache.nifi.connectable.Connectable)27 ProcessGroup (org.apache.nifi.groups.ProcessGroup)26 Relationship (org.apache.nifi.processor.Relationship)23 Port (org.apache.nifi.connectable.Port)21 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)21 ProcessorNode (org.apache.nifi.controller.ProcessorNode)19 RootGroupPort (org.apache.nifi.remote.RootGroupPort)19 LinkedHashSet (java.util.LinkedHashSet)18 Set (java.util.Set)17 RemoteGroupPort (org.apache.nifi.remote.RemoteGroupPort)17 Funnel (org.apache.nifi.connectable.Funnel)16 HashMap (java.util.HashMap)15 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)15 IOException (java.io.IOException)14 Map (java.util.Map)14