Search in sources :

Example 1 with NetflowPacket

use of loghub.netflow.NetflowPacket in project LogHub by fbacchella.

the class PacketsTest method testParse.

@Test
public void testParse() {
    final List<NetflowPacket> packets = new ArrayList<>();
    Arrays.stream(captures).map(i -> {
        logger.debug(i + ": ");
        return i;
    }).map(i -> "/netflow/packets/" + i).map(i -> getClass().getResourceAsStream(i)).filter(i -> i != null).map(i -> {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[8 * 1024];
            for (int length; (length = i.read(buffer)) != -1; ) {
                out.write(buffer, 0, length);
            }
            return out;
        } catch (Exception e) {
            Assert.fail(e.getMessage());
            return null;
        }
    }).filter(i -> i != null).map(i -> Unpooled.wrappedBuffer(i.toByteArray())).forEach(i -> {
        try {
            while (i.isReadable()) {
                packets.add(PacketFactory.parsePacket(InetAddress.getLocalHost(), i));
            }
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    });
    packets.forEach(i -> {
        logger.debug("    {} {} {} {} {}\n", i.getVersion(), i.getLength(), i.getSequenceNumber(), i.getExportTime(), i.getId());
        i.getRecords().forEach(j -> logger.debug("        {}\n", j));
    });
    ;
}
Also used : Arrays(java.util.Arrays) Decoder(loghub.Decoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BeforeClass(org.junit.BeforeClass) Level(org.apache.logging.log4j.Level) IOException(java.io.IOException) Test(org.junit.Test) LogUtils(loghub.LogUtils) InetSocketAddress(java.net.InetSocketAddress) Tools(loghub.Tools) NetflowPacket(loghub.netflow.NetflowPacket) ArrayList(java.util.ArrayList) Unpooled(io.netty.buffer.Unpooled) InetAddress(java.net.InetAddress) List(java.util.List) Logger(org.apache.logging.log4j.Logger) IpConnectionContext(loghub.IpConnectionContext) Map(java.util.Map) PacketFactory(loghub.netflow.PacketFactory) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) NetflowDecoder(loghub.netflow.NetflowDecoder) NetflowPacket(loghub.netflow.NetflowPacket) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Unpooled (io.netty.buffer.Unpooled)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Decoder (loghub.Decoder)1 IpConnectionContext (loghub.IpConnectionContext)1 LogUtils (loghub.LogUtils)1 Tools (loghub.Tools)1 NetflowDecoder (loghub.netflow.NetflowDecoder)1 NetflowPacket (loghub.netflow.NetflowPacket)1 PacketFactory (loghub.netflow.PacketFactory)1 Level (org.apache.logging.log4j.Level)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Assert (org.junit.Assert)1