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 testExcludeRouteObject() throws Exception {
    final PCEPExcludeRouteObjectParser parser = new PCEPExcludeRouteObjectParser(
            this.ctx.getXROSubobjectHandlerRegistry());
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPExcludeRouteObject.1.bin"));

    final XroBuilder builder = new XroBuilder();
    builder.setProcessingRule(false);//from w ww  . jav  a 2  s .  co m
    builder.setIgnore(false);
    builder.setFlags(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags(
                    true));
    final List<Subobject> subs = Lists.newArrayList();
    subs.add(
            new SubobjectBuilder().setMandatory(true)
                    .setSubobjectType(new IpPrefixCaseBuilder()
                            .setIpPrefix(new IpPrefixBuilder()
                                    .setIpPrefix(new IpPrefix(new Ipv4Prefix("192.168.0.0/16"))).build())
                            .build())
                    .setAttribute(Attribute.Node).build());
    subs.add(new SubobjectBuilder().setMandatory(false)
            .setSubobjectType(new AsNumberCaseBuilder()
                    .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x1234L)).build()).build())
            .build());
    builder.setSubobject(subs);

    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(result.array(), ByteArray.getAllBytes(buf));

    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
}

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

License:Open Source License

@Test
public void testPathKeyObject() throws Exception {
    final PCEPPathKeyObjectParser parser = new PCEPPathKeyObjectParser(
            this.ctx.getEROSubobjectHandlerRegistry());
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPPathKeyObject.bin"));

    final PathKeyBuilder builder = new PathKeyBuilder();
    builder.setProcessingRule(true);//from   w  w  w .  j  av  a2  s  .  c o  m
    builder.setIgnore(false);
    final List<PathKeys> list = Lists.newArrayList();
    list.add(new PathKeysBuilder().setLoose(true).setPathKey(new PathKey(0x1234))
            .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 })).build());
    builder.setPathKeys(list);

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

    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
}

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

License:Open Source License

@Test
public void testObjectiveFunctionObject() throws IOException, PCEPDeserializerException {
    final PCEPObjectiveFunctionObjectParser parser = new PCEPObjectiveFunctionObjectParser(this.tlvRegistry,
            this.viTlvRegistry);
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPObjectiveFunctionObject.1.bin"));

    final OfBuilder builder = new OfBuilder();
    builder.setProcessingRule(true);//from   w  w w  .j a  v  a2 s.c  o  m
    builder.setIgnore(false);
    builder.setCode(new OfId(4));
    builder.setTlvs(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.of.TlvsBuilder()
                    .build());

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

    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
}

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

License:Open Source License

@Test
public void testGlobalConstraintsObject() throws IOException, PCEPDeserializerException {
    final PCEPGlobalConstraintsObjectParser parser = new PCEPGlobalConstraintsObjectParser(this.tlvRegistry,
            this.viTlvRegistry);
    final ByteBuf result = Unpooled
            .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPGlobalConstraintsObject.1.bin"));

    final GcBuilder builder = new GcBuilder();
    builder.setProcessingRule(true);/*  w  w w . j a v a 2s  . c  om*/
    builder.setIgnore(false);
    builder.setMaxHop((short) 1);
    builder.setMaxUtilization((short) 0);
    builder.setMinUtilization((short) 100);
    builder.setOverBookingFactor((short) 99);
    builder.setTlvs(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.gc.TlvsBuilder()
                    .build());

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

    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), null);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
    try {
        parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
        fail();
    } catch (final IllegalArgumentException e) {
        assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
    }
}

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

License:Open Source License

@Test
public void testCloseObjectWithVendorInformationTlv() throws IOException, PCEPDeserializerException {
    final byte[] closeBytes = { 0x0f, 0x10, 0x00, 0x14, 0x00, 0x00, 0x00, 0x05,
            /* vendor-information TLV */
            0x00, 0x07, 0x00, 0x08,//from   w  w  w .  j  a  v  a2 s  .co m
            /* enterprise number */
            0x00, 0x00, 0x00, 0x00,
            /* enterprise specific information */
            0x00, 0x00, 0x00, 0x05 };
    final PCEPCloseObjectParser parser = new PCEPCloseObjectParser(this.tlvRegistry, this.viTlvRegistry);
    final ByteBuf result = Unpooled.wrappedBuffer(closeBytes);

    final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
    final VendorInformationTlv viTlv = new VendorInformationTlvBuilder()
            .setEnterpriseNumber(new EnterpriseNumber(0L)).setEnterpriseSpecificInformation(esInfo).build();
    final CCloseBuilder builder = new CCloseBuilder();
    builder.setProcessingRule(false);
    builder.setIgnore(false);
    builder.setReason((short) 5);
    builder.setTlvs(
            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.TlvsBuilder()
                    .setVendorInformationTlv(Lists.newArrayList(viTlv)).build());

    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(result.array(), ByteArray.getAllBytes(buf));
}

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

License:Open Source License

@Test
public void testMonitoringObject() throws PCEPDeserializerException {
    final byte[] monitoringBytes = {
            /* object header */
            0x13, 0x10, 0x00, 0x0C,/*w w  w.j  av a  2s .c o  m*/
            /* flags */
            0x00, 0x00, 0x00, 0x01,
            /* monitoring-id=16 */
            0x00, 0x00, 0x00, 0x10 };
    final PCEPMonitoringObjectParser parser = new PCEPMonitoringObjectParser(this.tlvRegistry,
            this.viTlvRegistry);
    final Monitoring monitoring = new MonitoringBuilder().setMonitoringId(16L)
            .setFlags(new Flags(false, false, true, false, false))
            .setTlvs(
                    new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.monitoring.TlvsBuilder()
                            .build())
            .build();
    final ByteBuf result = Unpooled.wrappedBuffer(monitoringBytes);
    assertEquals(monitoring, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

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

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

License:Open Source License

@Test
public void testPccIdReqIPv4Object() throws PCEPDeserializerException {
    final byte[] pccIdReqBytes = {
            /* object header */
            0x14, 0x10, 0x00, 0x08,/*from  ww  w. ja va 2 s .  c  o m*/
            /* ipv4 address */
            0x7f, 0x00, 0x00, 0x01 };
    final PCEPPccIdReqIPv4ObjectParser parser = new PCEPPccIdReqIPv4ObjectParser();
    final PccIdReq pccIdReq = new PccIdReqBuilder().setIpAddress(new IpAddress(new Ipv4Address("127.0.0.1")))
            .build();
    final ByteBuf result = Unpooled.wrappedBuffer(pccIdReqBytes);
    assertEquals(pccIdReq, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

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

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

License:Open Source License

@Test
public void testPccIdReqIPv6Object() throws PCEPDeserializerException {
    final byte[] pccIdReqBytes = {
            /* object header */
            0x14, 0x20, 0x00, 0x14,/* ww  w  . j a v a 2s  . c  o m*/
            /* ipv6 address */
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
    final PCEPPccIdReqIPv6ObjectParser parser = new PCEPPccIdReqIPv6ObjectParser();
    final PccIdReq pccIdReq = new PccIdReqBuilder().setIpAddress(new IpAddress(new Ipv6Address("::1"))).build();
    final ByteBuf result = Unpooled.wrappedBuffer(pccIdReqBytes);
    assertEquals(pccIdReq, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

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

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

License:Open Source License

@Test
public void testPceIdIPv4Object() throws PCEPDeserializerException {
    final byte[] pccIdReqBytes = {
            /* object header */
            0x19, 0x10, 0x00, 0x08,//from w w  w.j a v  a2 s .  c o  m
            /* ipv4 address */
            0x7f, 0x00, 0x00, 0x01 };
    final PCEPPceIdIPv4ObjectParser parser = new PCEPPceIdIPv4ObjectParser();
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pce.id.object.PceId pceId = new PceIdBuilder()
            .setIpAddress(new IpAddress(new Ipv4Address("127.0.0.1"))).build();
    final ByteBuf result = Unpooled.wrappedBuffer(pccIdReqBytes);
    assertEquals(pceId, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

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

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

License:Open Source License

@Test
public void testPceIdIPv6Object() throws PCEPDeserializerException {
    final byte[] pccIdReqBytes = {
            /* object header */
            0x19, 0x20, 0x00, 0x14,/*ww  w  .  ja  va 2  s  .  co m*/
            /* ipv6 header */
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
    final PCEPPceIdIPv6ObjectParser parser = new PCEPPceIdIPv6ObjectParser();
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pce.id.object.PceId pccIdReq = new PceIdBuilder()
            .setIpAddress(new IpAddress(new Ipv6Address("::1"))).build();
    final ByteBuf result = Unpooled.wrappedBuffer(pccIdReqBytes);
    assertEquals(pccIdReq, parser.parseObject(new ObjectHeaderImpl(false, false),
            result.slice(4, result.readableBytes() - 4)));

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