Search in sources :

Example 6 with Response

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

the class TestSocketFlowFileServerProtocol method testSendPeerListCluster.

@Test
public void testSendPeerListCluster() throws Exception {
    final SocketFlowFileServerProtocol protocol = getDefaultSocketFlowFileServerProtocol();
    final List<NodeInformation> nodeInfoList = new ArrayList<>();
    final ClusterNodeInformation clusterNodeInformation = new ClusterNodeInformation();
    clusterNodeInformation.setNodeInformation(nodeInfoList);
    final Optional<ClusterNodeInformation> clusterNodeInfo = Optional.of(clusterNodeInformation);
    for (int i = 0; i < 3; i++) {
        final String siteToSiteHostname = String.format("node%d.example.com", i);
        final Integer siteToSitePort = 8081;
        final Integer siteToSiteHttpPort = null;
        final int apiPort = 8080;
        final boolean isSiteToSiteSecure = true;
        final int numOfQueuedFlowFiles = 100 + i;
        final NodeInformation nodeInformation = new NodeInformation(siteToSiteHostname, siteToSitePort, siteToSiteHttpPort, apiPort, isSiteToSiteSecure, numOfQueuedFlowFiles);
        nodeInfoList.add(nodeInformation);
    }
    final NodeInformation self = nodeInfoList.get(0);
    final HandshakeProperties handshakeProperties = new HandshakeProperties();
    handshakeProperties.setCommsIdentifier("communication-identifier");
    handshakeProperties.setTransitUriPrefix("uri-prefix");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final Peer peer = getDefaultPeer(handshakeProperties, outputStream);
    protocol.handshake(peer);
    protocol.sendPeerList(peer, clusterNodeInfo, self);
    try (final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        final Response handshakeResponse = Response.read(dis);
        assertEquals(ResponseCode.PROPERTIES_OK, handshakeResponse.getCode());
        final int numPeers = dis.readInt();
        assertEquals(nodeInfoList.size(), numPeers);
        for (int i = 0; i < nodeInfoList.size(); i++) {
            final NodeInformation node = nodeInfoList.get(i);
            assertEquals(node.getSiteToSiteHostname(), dis.readUTF());
            assertEquals(node.getSiteToSitePort().intValue(), dis.readInt());
            assertEquals(node.isSiteToSiteSecure(), dis.readBoolean());
            assertEquals(node.getTotalFlowFiles(), dis.readInt());
        }
    }
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) Peer(org.apache.nifi.remote.Peer) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Response(org.apache.nifi.remote.protocol.Response) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties) Test(org.junit.Test)

Example 7 with Response

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

the class TestSocketFlowFileServerProtocol method testSendPeerListStandalone.

@Test
public void testSendPeerListStandalone() throws Exception {
    final SocketFlowFileServerProtocol protocol = getDefaultSocketFlowFileServerProtocol();
    final Optional<ClusterNodeInformation> clusterNodeInfo = Optional.empty();
    final String siteToSiteHostname = "node1.example.com";
    final Integer siteToSitePort = 8081;
    final Integer siteToSiteHttpPort = null;
    final int apiPort = 8080;
    final boolean isSiteToSiteSecure = true;
    final int numOfQueuedFlowFiles = 100;
    final NodeInformation self = new NodeInformation(siteToSiteHostname, siteToSitePort, siteToSiteHttpPort, apiPort, isSiteToSiteSecure, numOfQueuedFlowFiles);
    final HandshakeProperties handshakeProperties = new HandshakeProperties();
    handshakeProperties.setCommsIdentifier("communication-identifier");
    handshakeProperties.setTransitUriPrefix("uri-prefix");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final Peer peer = getDefaultPeer(handshakeProperties, outputStream);
    protocol.handshake(peer);
    protocol.sendPeerList(peer, clusterNodeInfo, self);
    try (final DataInputStream dis = new DataInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
        final Response handshakeResponse = Response.read(dis);
        assertEquals(ResponseCode.PROPERTIES_OK, handshakeResponse.getCode());
        final int numPeers = dis.readInt();
        assertEquals(1, numPeers);
        assertEquals(siteToSiteHostname, dis.readUTF());
        assertEquals(siteToSitePort.intValue(), dis.readInt());
        assertEquals(isSiteToSiteSecure, dis.readBoolean());
        assertEquals(numOfQueuedFlowFiles, dis.readInt());
    }
}
Also used : NodeInformation(org.apache.nifi.remote.cluster.NodeInformation) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) Peer(org.apache.nifi.remote.Peer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Response(org.apache.nifi.remote.protocol.Response) ClusterNodeInformation(org.apache.nifi.remote.cluster.ClusterNodeInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) HandshakeProperties(org.apache.nifi.remote.protocol.HandshakeProperties) Test(org.junit.Test)

Example 8 with Response

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

the class TestSocketClientTransaction method testReceiveOneFlowFile.

@Test
public void testReceiveOneFlowFile() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.MORE_DATA.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 1"), serverResponse);
    ResponseCode.FINISH_TRANSACTION.writeResponse(serverResponse);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Checksum has been verified at server.");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    execReceiveOneFlowFile(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals("Checksum should be calculated at client", "3680976076", confirmResponse.getMessage());
    Response completeResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.TRANSACTION_FINISHED, completeResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 9 with Response

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

the class TestSocketClientTransaction method testSendWithInvalidChecksum.

@Test
public void testSendWithInvalidChecksum() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Different checksum");
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
    execSendWithInvalidChecksum(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
    DataPacket packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 1", readContents(packetByClient));
    Response continueDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONTINUE_TRANSACTION, continueDataResponse.getCode());
    packetByClient = codec.decode(sentByClient);
    assertEquals("contents on client 2", readContents(packetByClient));
    Response endOfDataResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.FINISH_TRANSACTION, endOfDataResponse.getCode());
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.BAD_CHECKSUM, confirmResponse.getCode());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) DataPacket(org.apache.nifi.remote.protocol.DataPacket) SiteToSiteTestUtils.createDataPacket(org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket) Test(org.junit.Test)

Example 10 with Response

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

the class TestSocketClientTransaction method testReceiveWithInvalidChecksum.

@Test
public void testReceiveWithInvalidChecksum() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.MORE_DATA.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 1"), serverResponse);
    ResponseCode.CONTINUE_TRANSACTION.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 2"), serverResponse);
    ResponseCode.FINISH_TRANSACTION.writeResponse(serverResponse);
    ResponseCode.BAD_CHECKSUM.writeResponse(serverResponse);
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    execReceiveWithInvalidChecksum(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals("Checksum should be calculated at client", "2969091230", confirmResponse.getMessage());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

Response (org.apache.nifi.remote.protocol.Response)13 DataInputStream (java.io.DataInputStream)9 Test (org.junit.Test)9 DataOutputStream (java.io.DataOutputStream)7 ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)7 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)7 DataPacket (org.apache.nifi.remote.protocol.DataPacket)5 IOException (java.io.IOException)4 ProtocolException (org.apache.nifi.remote.exception.ProtocolException)4 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Peer (org.apache.nifi.remote.Peer)2 ClusterNodeInformation (org.apache.nifi.remote.cluster.ClusterNodeInformation)2 NodeInformation (org.apache.nifi.remote.cluster.NodeInformation)2 HandshakeProperties (org.apache.nifi.remote.protocol.HandshakeProperties)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 CheckedInputStream (java.util.zip.CheckedInputStream)1 CompressionInputStream (org.apache.nifi.remote.io.CompressionInputStream)1