Search in sources :

Example 1 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class TestStandardRemoteGroupPort method testReceiveRaw.

@Test
public void testReceiveRaw() throws Exception {
    setupMock(SiteToSiteTransportProtocol.RAW, TransferDirection.RECEIVE);
    setupMockProcessSession();
    final String peerUrl = "nifi://node1.example.com:9090";
    final PeerDescription peerDescription = new PeerDescription("node1.example.com", 9090, true);
    try (final SocketChannel socketChannel = SocketChannel.open()) {
        final CommunicationsSession commsSession = new SocketChannelCommunicationsSession(socketChannel);
        commsSession.setUserDn("nifi.node1.example.com");
        final Peer peer = new Peer(peerDescription, commsSession, peerUrl, REMOTE_CLUSTER_URL);
        doReturn(peer).when(transaction).getCommunicant();
        final String sourceFlowFileUuid = "flowfile-uuid";
        final Map<String, String> attributes = new HashMap<>();
        attributes.put(CoreAttributes.UUID.key(), sourceFlowFileUuid);
        final byte[] dataPacketContents = "DataPacket Contents".getBytes();
        final ByteArrayInputStream dataPacketInputStream = new ByteArrayInputStream(dataPacketContents);
        final DataPacket dataPacket = new StandardDataPacket(attributes, dataPacketInputStream, dataPacketContents.length);
        // Return null when it gets called second time.
        doReturn(dataPacket).doReturn(null).when(this.transaction).receive();
        port.onTrigger(processContext, processSession);
        // Assert provenance.
        final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
        assertEquals(1, provenanceEvents.size());
        final ProvenanceEventRecord provenanceEvent = provenanceEvents.get(0);
        assertEquals(ProvenanceEventType.RECEIVE, provenanceEvent.getEventType());
        assertEquals(peerUrl + "/" + sourceFlowFileUuid, provenanceEvent.getTransitUri());
        assertEquals("Remote DN=nifi.node1.example.com", provenanceEvent.getDetails());
        // Assert received flow files.
        processSession.assertAllFlowFilesTransferred(Relationship.ANONYMOUS);
        final List<MockFlowFile> flowFiles = processSession.getFlowFilesForRelationship(Relationship.ANONYMOUS);
        assertEquals(1, flowFiles.size());
        final MockFlowFile flowFile = flowFiles.get(0);
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_HOST.key(), peer.getHost());
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_ADDRESS.key(), peer.getHost() + ":" + peer.getPort());
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HashMap(java.util.HashMap) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) SocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession) CommunicationsSession(org.apache.nifi.remote.protocol.CommunicationsSession) HttpCommunicationsSession(org.apache.nifi.remote.io.http.HttpCommunicationsSession) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) MockFlowFile(org.apache.nifi.util.MockFlowFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) SocketChannelCommunicationsSession(org.apache.nifi.remote.io.socket.SocketChannelCommunicationsSession) Test(org.junit.Test)

Example 2 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class TestHttpFlowFileServerProtocol method receiveFlowFiles.

private void receiveFlowFiles(final HttpFlowFileServerProtocol serverProtocol, final String transactionId, final Peer peer, final DataPacket... dataPackets) throws IOException {
    final HttpRemoteSiteListener remoteSiteListener = HttpRemoteSiteListener.getInstance(NiFiProperties.createBasicNiFiProperties(null, null));
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    serverProtocol.handshake(peer);
    assertTrue(serverProtocol.isHandshakeSuccessful());
    setupMockProcessSession();
    // Emulate dataPackets sent from a Site-to-Site client.
    final FlowFileCodec negotiatedCoded = serverProtocol.negotiateCodec(peer);
    final ByteArrayOutputStream testDataOs = new ByteArrayOutputStream();
    for (final DataPacket dataPacket : dataPackets) {
        negotiatedCoded.encode(dataPacket, testDataOs);
    }
    final InputStream httpInputStream = new ByteArrayInputStream(testDataOs.toByteArray());
    ((HttpInput) commsSession.getInput()).setInputStream(httpInputStream);
    // Execute test using mock
    final int flowFileReceived = serverProtocol.receiveFlowFiles(peer, processContext, processSession, negotiatedCoded);
    assertEquals(dataPackets.length, flowFileReceived);
    assertTrue(remoteSiteListener.isTransactionActive(transactionId));
}
Also used : HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HttpInput(org.apache.nifi.remote.io.http.HttpInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpRemoteSiteListener(org.apache.nifi.remote.HttpRemoteSiteListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) FlowFileCodec(org.apache.nifi.remote.codec.FlowFileCodec) StandardFlowFileCodec(org.apache.nifi.remote.codec.StandardFlowFileCodec)

Example 3 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class DataPacketDto method getDataPacketStream.

public static Stream<DataPacket> getDataPacketStream(InputStream inputStream) throws IOException {
    JsonParser jsonParser = new JsonFactory().createParser(inputStream);
    if (jsonParser.nextToken() != JsonToken.START_ARRAY) {
        throw new IOException("Expecting start array token to begin object array.");
    }
    jsonParser.setCodec(new ObjectMapper());
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<DataPacket>() {

        DataPacket next = getNext();

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public DataPacket next() {
            DataPacket next = this.next;
            this.next = getNext();
            return next;
        }

        DataPacket getNext() throws RuntimeException {
            try {
                if (jsonParser.nextToken() == JsonToken.END_ARRAY) {
                    return null;
                }
                DataPacketDto dataPacketDto = jsonParser.readValueAs(DATA_PACKET_DTO_TYPE_REFERENCE);
                return dataPacketDto.toDataPacket();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }, Spliterator.ORDERED), false);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) Iterator(java.util.Iterator) IOException(java.io.IOException) DataPacket(org.apache.nifi.remote.protocol.DataPacket) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 4 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class SiteToSiteReceiver method receiveFiles.

public TransactionCompletion receiveFiles() throws IOException {
    Transaction transaction = siteToSiteClient.createTransaction(TransferDirection.RECEIVE);
    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(output);
    jsonGenerator.writeStartArray();
    DataPacket dataPacket;
    while ((dataPacket = transaction.receive()) != null) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("attributes");
        jsonGenerator.writeStartObject();
        Map<String, String> attributes = dataPacket.getAttributes();
        if (attributes != null) {
            for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
                jsonGenerator.writeStringField(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        jsonGenerator.writeEndObject();
        InputStream data = dataPacket.getData();
        if (data != null) {
            jsonGenerator.writeBinaryField("data", IOUtils.toByteArray(data));
        }
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    jsonGenerator.close();
    transaction.confirm();
    return transaction.complete();
}
Also used : Transaction(org.apache.nifi.remote.Transaction) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DataPacket(org.apache.nifi.remote.protocol.DataPacket) Map(java.util.Map)

Example 5 with DataPacket

use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.

the class TestHttpClient method testReceiveSlowClientSuccess.

@Test
public void testReceiveSlowClientSuccess() throws Exception {
    try (SiteToSiteClient client = getDefaultBuilder().portName("output-running").build()) {
        final Transaction transaction = client.createTransaction(TransferDirection.RECEIVE);
        assertNotNull(transaction);
        DataPacket packet;
        while ((packet = transaction.receive()) != null) {
            consumeDataPacket(packet);
            Thread.sleep(500);
        }
        transaction.confirm();
        transaction.complete();
    }
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) Transaction(org.apache.nifi.remote.Transaction) DataPacket(org.apache.nifi.remote.protocol.DataPacket) StandardDataPacket(org.apache.nifi.remote.util.StandardDataPacket) Test(org.junit.Test)

Aggregations

DataPacket (org.apache.nifi.remote.protocol.DataPacket)36 Test (org.junit.Test)21 StandardDataPacket (org.apache.nifi.remote.util.StandardDataPacket)20 Transaction (org.apache.nifi.remote.Transaction)13 InputStream (java.io.InputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 SiteToSiteClient (org.apache.nifi.remote.client.SiteToSiteClient)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)8 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)8 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)7 Peer (org.apache.nifi.remote.Peer)5 Response (org.apache.nifi.remote.protocol.Response)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4