Search in sources :

Example 1 with Decoder

use of loghub.Decoder in project LogHub by fbacchella.

the class TestMsgpack method testmapsimple.

@Test
public void testmapsimple() throws IOException, DecodeException {
    Decoder d = new Msgpack();
    Map<Value, Value> destination = new HashMap<>();
    destination.put(ValueFactory.newString("a"), ValueFactory.newString("0"));
    destination.put(ValueFactory.newString("b"), ValueFactory.newInteger(1));
    destination.put(ValueFactory.newString("c"), ValueFactory.newBoolean(false));
    Value[] subdestination = new Value[4];
    subdestination[0] = ValueFactory.newString("0");
    subdestination[1] = ValueFactory.newInteger(1);
    subdestination[2] = ValueFactory.newFloat(2.0);
    subdestination[3] = ValueFactory.newNil();
    destination.put(ValueFactory.newString("d"), ValueFactory.newArray(subdestination));
    Value v = ValueFactory.newMap(destination);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MessagePacker packer = MessagePack.newDefaultPacker(out);
    packer.packValue(v);
    packer.close();
    byte[] packed = out.toByteArray();
    Map<String, Object> e = d.decode(ConnectionContext.EMPTY, packed);
    testContent(e);
}
Also used : MessagePacker(org.msgpack.core.MessagePacker) HashMap(java.util.HashMap) Value(org.msgpack.value.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(loghub.Decoder) Test(org.junit.Test)

Example 2 with Decoder

use of loghub.Decoder in project LogHub by fbacchella.

the class PacketsTest method testDecode.

@SuppressWarnings("unchecked")
@Test
public void testDecode() {
    Decoder nfd = new NetflowDecoder();
    IpConnectionContext dummyctx = new IpConnectionContext(new InetSocketAddress(0), new InetSocketAddress(0), null);
    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()) {
                Map<String, Object> content = nfd.decode(dummyctx, i);
                Assert.assertTrue(content.containsKey("version"));
                Assert.assertTrue(content.containsKey("sequenceNumber"));
                Assert.assertTrue(content.containsKey("records"));
                ((List<Map<String, Object>>) content.get("records")).forEach(j -> Assert.assertTrue(j.containsKey("_type")));
                if (((Integer) content.get("version")) < 10) {
                    Assert.assertTrue(content.containsKey("SysUptime"));
                }
                logger.debug(content);
            }
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    });
}
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) NetflowDecoder(loghub.netflow.NetflowDecoder) InetSocketAddress(java.net.InetSocketAddress) IpConnectionContext(loghub.IpConnectionContext) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(loghub.Decoder) NetflowDecoder(loghub.netflow.NetflowDecoder) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with Decoder

use of loghub.Decoder in project LogHub by fbacchella.

the class ProcessorTest method test.

@Test
public void test() throws IOException, DecodeException, ProcessorException, InterruptedException {
    Processor p = new Processor();
    InputStream is = getClass().getResourceAsStream("/netflow/packets/ipfix.dat");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[8 * 1024];
    for (int length; (length = is.read(buffer)) != -1; ) {
        out.write(buffer, 0, length);
    }
    ByteBuf bbuffer = Unpooled.wrappedBuffer(out.toByteArray());
    Decoder nfd = new NetflowDecoder();
    IpConnectionContext dummyctx = new IpConnectionContext(new InetSocketAddress(0), new InetSocketAddress(0), null);
    Map<String, Object> content = nfd.decode(dummyctx, bbuffer);
    Event e = Tools.getEvent();
    e.setTimestamp((Date) content.remove(Event.TIMESTAMPKEY));
    e.putAll(content);
    ProcessingStatus ps = Tools.runProcessing(e, "main", Collections.singletonList(p));
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    logger.debug(ps.mainQueue.remove());
    Assert.assertTrue(ps.mainQueue.isEmpty());
}
Also used : InputStream(java.io.InputStream) InetSocketAddress(java.net.InetSocketAddress) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuf(io.netty.buffer.ByteBuf) Decoder(loghub.Decoder) IpConnectionContext(loghub.IpConnectionContext) Event(loghub.Event) ProcessingStatus(loghub.Tools.ProcessingStatus) Test(org.junit.Test)

Example 4 with Decoder

use of loghub.Decoder in project LogHub by fbacchella.

the class TestMsgpack method testmap.

@Test
public void testmap() throws IOException, DecodeException {
    Decoder d = new Msgpack();
    Map<String, Object> e = d.decode(ConnectionContext.EMPTY, objectMapper.writeValueAsBytes(obj));
    testContent(e);
}
Also used : Decoder(loghub.Decoder) Test(org.junit.Test)

Aggregations

Decoder (loghub.Decoder)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InetSocketAddress (java.net.InetSocketAddress)2 IpConnectionContext (loghub.IpConnectionContext)2 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Event (loghub.Event)1 LogUtils (loghub.LogUtils)1 Tools (loghub.Tools)1 ProcessingStatus (loghub.Tools.ProcessingStatus)1 NetflowDecoder (loghub.netflow.NetflowDecoder)1