Example usage for io.netty.buffer ByteBuf slice

List of usage examples for io.netty.buffer ByteBuf slice

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf slice.

Prototype

public abstract ByteBuf slice(int index, int length);

Source Link

Document

Returns a slice of this buffer's sub-region.

Usage

From source file:org.opendaylight.protocol.pcep.impl.PCEPObjectParserTest.java

License:Open Source License

@Test
public void testProcTimeObject() throws PCEPDeserializerException {
    final byte[] proctimeBytes = {
            /* object header */
            0x1A, 0x10, 0x00, 0x1C,/* w  ww . ja  va 2  s . c o m*/
            /* E flag */
            0x00, 0x00, 0x00, 0x01,
            /* current proc. time */
            0x00, 0x00, 0x00, 0x01,
            /* min proc. time */
            0x00, 0x00, 0x00, 0x02,
            /* max proc time */
            0x00, 0x00, 0x00, 0x03,
            /* average proc time */
            0x00, 0x00, 0x00, 0x04,
            /* variance proc time */
            0x00, 0x00, 0x00, 0x05, };
    final PCEPProcTimeObjectParser parser = new PCEPProcTimeObjectParser();
    final ProcTime procTime = new ProcTimeBuilder().setEstimated(true).setAverageProcTime(4L)
            .setCurrentProcTime(1L).setMaxProcTime(3L).setMinProcTime(2L).setVarianceProcTime(5L).build();
    final ByteBuf result = Unpooled.wrappedBuffer(proctimeBytes);
    assertEquals(procTime, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

    final ByteBuf buf = Unpooled.buffer(proctimeBytes.length);
    parser.serializeObject(procTime, buf);
    assertArrayEquals(proctimeBytes, buf.array());
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPObjectParserTest.java

License:Open Source License

@Test
public void testOverloadObject() throws PCEPDeserializerException {
    final byte[] overloadBytes = {
            /* object header */
            0x1B, 0x10, 0x00, 0x08,//from   w  w w. j  av a2 s .c  o m
            /* overload duration */
            0x00, 0x00, 0x00, 0x78 };
    final PCEPOverloadObjectParser parser = new PCEPOverloadObjectParser();
    final Overload overload = new OverloadBuilder().setDuration(120).build();
    final ByteBuf result = Unpooled.wrappedBuffer(overloadBytes);
    assertEquals(overload, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

    final ByteBuf buf = Unpooled.buffer(overloadBytes.length);
    parser.serializeObject(overload, buf);
    assertArrayEquals(overloadBytes, buf.array());
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPObjectParserTest.java

License:Open Source License

@Test
public void testRpObjectWithPstTlvParser() throws PCEPDeserializerException {

    final byte[] rpObjectWithPstTlvBytes = { 0x2, 0x10, 0x0, 0x14, 0x0, 0x0, 0x4, 0x2d, (byte) 0xde,
            (byte) 0xad, (byte) 0xbe, (byte) 0xef,
            /* pst-tlv */
            0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0 };

    final PCEPRequestParameterObjectParser parser = new PCEPRequestParameterObjectParser(this.tlvRegistry,
            this.viTlvRegistry);
    final RpBuilder builder = new RpBuilder();
    builder.setProcessingRule(false);/*ww w  .  j  av a2s .c  o m*/
    builder.setIgnore(false);
    builder.setReoptimization(true);
    builder.setBiDirectional(false);
    builder.setLoose(true);
    builder.setMakeBeforeBreak(true);
    builder.setOrder(false);
    builder.setPathKey(false);
    builder.setSupplyOf(false);
    builder.setFragmentation(false);
    builder.setP2mp(false);
    builder.setEroCompression(false);
    builder.setPriority((short) 5);
    builder.setRequestId(new RequestId(0xdeadbeefL));
    builder.setTlvs(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder()
                    .setPathSetupType(new PathSetupTypeBuilder().setPst((short) 0).build()).build());

    final ByteBuf result = Unpooled.wrappedBuffer(rpObjectWithPstTlvBytes);
    assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));
    final ByteBuf buf = Unpooled.buffer();
    parser.serializeObject(builder.build(), buf);
    assertArrayEquals(rpObjectWithPstTlvBytes, ByteArray.getAllBytes(buf));
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testOpenMsg() throws IOException, PCEPDeserializerException {
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPOpenMessage1.bin"));
    final PCEPOpenMessageParser parser = new PCEPOpenMessageParser(this.ctx.getObjectHandlerRegistry());
    final OpenMessageBuilder builder = new OpenMessageBuilder();

    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder();
    b.setProcessingRule(false);/*  ww w .  j  a  va 2s .  co  m*/
    b.setIgnore(false);
    b.setVersion(new ProtocolVersion((short) 1));
    b.setKeepalive((short) 30);
    b.setDeadTimer((short) 120);
    b.setSessionId((short) 1);
    b.setTlvs(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder()
                    .build());
    builder.setOpen(b.build());

    assertEquals(new OpenBuilder().setOpenMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    final ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new OpenBuilder().setOpenMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    try {
        parser.serializeMessage(new OpenBuilder().setOpenMessage(new OpenMessageBuilder().build()).build(),
                null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Open Object must be present in Open Message.", e.getMessage());
    }
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testKeepAliveMsg() throws IOException, PCEPDeserializerException {
    final ByteBuf result = Unpooled.wrappedBuffer(new byte[] { 32, 2, 0, 4 });
    final PCEPKeepAliveMessageParser parser = new PCEPKeepAliveMessageParser(this.objectRegistry);
    final KeepaliveBuilder builder = new KeepaliveBuilder()
            .setKeepaliveMessage(new KeepaliveMessageBuilder().build());

    assertEquals(builder.build(),/*from  w  ww.j  a  va  2  s .c  o  m*/
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    final ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(builder.build(), buf);
    assertArrayEquals(result.array(), buf.array());
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testStartTLSMsg() throws Exception {
    final ByteBuf result = Unpooled.wrappedBuffer(new byte[] { 32, 20, 0, 4 });
    final PCEPStartTLSMessageParser parser = new PCEPStartTLSMessageParser(this.objectRegistry);
    final StarttlsBuilder builder = new StarttlsBuilder()
            .setStartTlsMessage(new StartTlsMessageBuilder().build());

    assertEquals(builder.build(),//from   ww w . j  a  v  a2 s .c om
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    final ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(builder.build(), buf);
    assertArrayEquals(result.array(), buf.array());
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testCloseMsg() throws IOException, PCEPDeserializerException {
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPCloseMessage1.bin"));

    final PCEPCloseMessageParser parser = new PCEPCloseMessageParser(this.objectRegistry);
    final CloseBuilder builder = new CloseBuilder().setCCloseMessage(
            new CCloseMessageBuilder().setCClose(new CCloseBuilder().setIgnore(false).setProcessingRule(false)
                    .setReason((short) 5).setTlvs(new TlvsBuilder().build()).build()).build());

    assertEquals(builder.build(),/* w  w w  . j a  v  a  2 s.  co  m*/
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    final ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(builder.build(), buf);
    assertArrayEquals(result.array(), buf.array());

    try {
        parser.serializeMessage(new CloseBuilder().setCCloseMessage(new CCloseMessageBuilder().build()).build(),
                null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Close Object must be present in Close Message.", e.getMessage());
    }
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testRequestMsg() throws IOException, PCEPDeserializerException {
    ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPRequestMessage1.bin"));

    final PCEPRequestMessageParser parser = new PCEPRequestMessageParser(this.objectRegistry);

    final PcreqMessageBuilder builder = new PcreqMessageBuilder();
    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.Requests> reqs1 = Lists
            .newArrayList();//w ww .j a v  a 2 s.  c om
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.RequestsBuilder rBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.RequestsBuilder();
    rBuilder.setRp(this.rpTrue);
    rBuilder.setSegmentComputation(new SegmentComputationBuilder()
            .setP2p(new P2pBuilder().setEndpointsObj(this.endpoints).build()).build());
    reqs1.add(rBuilder.build());
    builder.setRequests(reqs1);

    assertEquals(new PcreqBuilder().setPcreqMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcreqBuilder().setPcreqMessage(builder.build()).build(), buf);

    assertArrayEquals(result.array(), buf.array());

    result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCReq.3.bin"));

    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.Requests> reqs2 = Lists
            .newArrayList();
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.RequestsBuilder rBuilder1 = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.RequestsBuilder();
    rBuilder1.setRp(this.rpTrue);
    final P2pBuilder p2pBuilder = new P2pBuilder();
    p2pBuilder.setEndpointsObj(this.endpoints);
    p2pBuilder.setMetrics(Lists.newArrayList(this.metrics));
    p2pBuilder.setIro(this.iro);
    rBuilder1.setSegmentComputation(new SegmentComputationBuilder().setP2p(p2pBuilder.build()).build());
    reqs2.add(rBuilder1.build());
    builder.setRequests(reqs2);
    builder.setSvec(Lists.newArrayList(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder()
                    .setSvec(this.svec).build()));

    assertEquals(new PcreqBuilder().setPcreqMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcreqBuilder().setPcreqMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    try {
        parser.serializeMessage(new PcreqBuilder().setPcreqMessage(new PcreqMessageBuilder().build()).build(),
                null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Requests cannot be null or empty.", e.getMessage());
    }
    try {
        parser.serializeMessage(new PcreqBuilder()
                .setPcreqMessage(
                        new PcreqMessageBuilder().setRequests(Collections.<Requests>emptyList()).build())
                .build(), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Requests cannot be null or empty.", e.getMessage());
    }
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testReplyMsg() throws IOException, PCEPDeserializerException {
    // only RP//from   www  .j a v a2 s  .  c om
    ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCRep.1.bin"));

    final PCEPReplyMessageParser parser = new PCEPReplyMessageParser(this.objectRegistry);

    final PcrepMessageBuilder builder = new PcrepMessageBuilder();
    RepliesBuilder rBuilder = new RepliesBuilder();

    final List<Replies> replies1 = Lists.newArrayList();
    rBuilder.setRp(this.rpTrue);
    replies1.add(rBuilder.build());
    builder.setReplies(replies1);

    assertEquals(new PcrepBuilder().setPcrepMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcrepBuilder().setPcrepMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    // simple Failure
    result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCRep.2.bin"));
    final List<Replies> replies2 = Lists.newArrayList();
    rBuilder = new RepliesBuilder();
    rBuilder.setRp(this.rpTrue);
    replies2.add(rBuilder.build());
    final RepliesBuilder rBuilder2 = new RepliesBuilder();
    rBuilder2.setRp(this.rpTrue);
    rBuilder2.setResult(new FailureCaseBuilder().setNoPath(this.noPath).build());
    replies2.add(rBuilder2.build());
    builder.setReplies(replies2);

    assertEquals(new PcrepBuilder().setPcrepMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcrepBuilder().setPcrepMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    // Failure with attributes
    result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCRep.3.bin"));
    final List<Replies> replies3 = Lists.newArrayList();
    rBuilder = new RepliesBuilder();
    rBuilder.setRp(this.rpTrue);
    rBuilder.setResult(new FailureCaseBuilder().setNoPath(this.noPath).setLspa(this.lspa)
            .setMetrics(Lists.newArrayList(this.metrics)).setIro(this.iro).build());
    replies3.add(rBuilder.build());
    builder.setReplies(replies3);

    assertEquals(new PcrepBuilder().setPcrepMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcrepBuilder().setPcrepMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    // Success
    result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCRep.5.bin"));
    final List<Replies> replies4 = Lists.newArrayList();
    rBuilder = new RepliesBuilder();
    rBuilder.setRp(this.rpTrue);
    final List<Paths> paths = Lists.newArrayList();
    final PathsBuilder paBuilder = new PathsBuilder();
    paBuilder.setEro(this.ero);
    paBuilder.setLspa(this.lspa);
    paBuilder.setMetrics(Lists.newArrayList(this.metrics));
    paBuilder.setIro(this.iro);
    paBuilder.setOf(this.of);
    paths.add(paBuilder.build());
    rBuilder.setResult(
            new SuccessCaseBuilder().setSuccess(new SuccessBuilder().setPaths(paths).build()).build()).build();
    replies4.add(rBuilder.build());
    builder.setReplies(replies4);

    assertEquals(new PcrepBuilder().setPcrepMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcrepBuilder().setPcrepMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());

    try {
        parser.serializeMessage(new PcrepBuilder().setPcrepMessage(new PcrepMessageBuilder().build()).build(),
                null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Replies cannot be null or empty.", e.getMessage());
    }
    try {
        parser.serializeMessage(new PcrepBuilder()
                .setPcrepMessage(new PcrepMessageBuilder().setReplies(Collections.<Replies>emptyList()).build())
                .build(), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Replies cannot be null or empty.", e.getMessage());
    }
}

From source file:org.opendaylight.protocol.pcep.impl.PCEPValidatorTest.java

License:Open Source License

@Test
public void testNotificationMsg() throws IOException, PCEPDeserializerException {
    final CNotification cn1 = new CNotificationBuilder().setIgnore(false).setProcessingRule(false)
            .setType((short) 1).setValue((short) 1).build();

    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> innerNot = Lists
            .newArrayList();//from   w  w  w.java2  s.  com
    innerNot.add(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder()
                    .setCNotification(cn1).build());
    final List<Rps> rps = Lists.newArrayList();
    rps.add(new RpsBuilder().setRp(this.rpFalse).build());

    final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCNtf.5.bin"));

    final PCEPNotificationMessageParser parser = new PCEPNotificationMessageParser(this.objectRegistry);
    final PcntfMessageBuilder builder = new PcntfMessageBuilder();

    final List<Notifications> nots = Lists.newArrayList();
    final NotificationsBuilder b = new NotificationsBuilder();
    b.setNotifications(innerNot);
    b.setRps(rps);
    nots.add(b.build());

    final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> innerNot1 = Lists
            .newArrayList();
    innerNot1.add(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder()
                    .setCNotification(cn1).build());
    innerNot1.add(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder()
                    .setCNotification(cn1).build());
    final List<Rps> rps1 = Lists.newArrayList();
    rps1.add(new RpsBuilder().setRp(this.rpFalse).build());
    rps1.add(new RpsBuilder().setRp(this.rpFalse).build());

    b.setNotifications(innerNot1);
    b.setRps(rps1);
    nots.add(b.build());
    builder.setNotifications(nots);

    assertEquals(new PcntfBuilder().setPcntfMessage(builder.build()).build(),
            parser.parseMessage(result.slice(4, result.readableBytes() - 4), Collections.<Message>emptyList()));
    final ByteBuf buf = Unpooled.buffer(result.readableBytes());
    parser.serializeMessage(new PcntfBuilder().setPcntfMessage(builder.build()).build(), buf);
    assertArrayEquals(result.array(), buf.array());
}