List of usage examples for io.netty.buffer ByteBuf readShort
public abstract short readShort();
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10StatsReplyMessageFactoryTest.java
License:Open Source License
@Test public void testQueueBodySerialize() throws Exception { MultipartReplyMessageBuilder builder; builder = new MultipartReplyMessageBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID); builder.setFlags(new MultipartRequestFlags(true)); builder.setType(MultipartType.forValue(5)); MultipartReplyQueueCaseBuilder queueCase = new MultipartReplyQueueCaseBuilder(); MultipartReplyQueueBuilder queue = new MultipartReplyQueueBuilder(); queue.setQueueStats(createQueueStats()); queueCase.setMultipartReplyQueue(queue.build()); builder.setMultipartReplyBody(queueCase.build()); MultipartReplyMessage message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 44); Assert.assertEquals("Wrong type", MultipartType.OFPMPQUEUE.getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong flags", message.getFlags(), createMultipartRequestFlags(serializedBuffer.readShort())); MultipartReplyQueueCase body = (MultipartReplyQueueCase) message.getMultipartReplyBody(); MultipartReplyQueue messageOutput = body.getMultipartReplyQueue(); QueueStats queueStats = messageOutput.getQueueStats().get(0); Assert.assertEquals("Wrong length", 32, serializedBuffer.readUnsignedShort()); serializedBuffer.skipBytes(2);//from ww w.j av a2s. c o m Assert.assertEquals("Wrong queue id", queueStats.getQueueId().intValue(), serializedBuffer.readUnsignedInt()); Assert.assertEquals("Wrong tx bytes", queueStats.getTxBytes().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong tx packets", queueStats.getTxPackets().longValue(), serializedBuffer.readLong()); Assert.assertEquals("Wrong tx errors", queueStats.getTxErrors().longValue(), serializedBuffer.readLong()); }
From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.QueueGetConfigReplyMessageFactoryTest.java
License:Open Source License
@Test public void testSerialize() throws Exception { GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder(); BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); builder.setPort(new PortNumber(0x00010203L)); builder.setQueues(createQueuesList()); GetQueueConfigOutput message = builder.build(); ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(); factory.serialize(message, serializedBuffer); BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 80); Assert.assertEquals("Wrong port", message.getPort().getValue().longValue(), serializedBuffer.readInt()); serializedBuffer.skipBytes(PADDING); Assert.assertEquals("Wrong queue Id", message.getQueues().get(0).getQueueId().getValue().longValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong port", message.getQueues().get(0).getPort().getValue().longValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong length", 32, serializedBuffer.readShort()); serializedBuffer.skipBytes(QUEUE_PADDING); List<QueueProperty> properties = message.getQueues().get(0).getQueueProperty(); Assert.assertEquals("Wrong property", properties.get(0).getProperty().getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong property length", 16, serializedBuffer.readShort()); serializedBuffer.skipBytes(PROPERTY_HEADER_PADDING); RateQueueProperty rateQueueProperty = properties.get(0).getAugmentation(RateQueueProperty.class); Assert.assertEquals("Wrong rate", rateQueueProperty.getRate().intValue(), serializedBuffer.readShort()); serializedBuffer.skipBytes(PROPERTY_RATE_PADDING); Assert.assertEquals("Wrong queue Id", message.getQueues().get(1).getQueueId().getValue().longValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong queue Id", message.getQueues().get(1).getPort().getValue().longValue(), serializedBuffer.readInt()); Assert.assertEquals("Wrong length", 32, serializedBuffer.readShort()); serializedBuffer.skipBytes(QUEUE_PADDING); List<QueueProperty> propertiesTwo = message.getQueues().get(1).getQueueProperty(); Assert.assertEquals("Wrong property", propertiesTwo.get(0).getProperty().getIntValue(), serializedBuffer.readShort()); Assert.assertEquals("Wrong property length", 16, serializedBuffer.readShort()); serializedBuffer.skipBytes(PROPERTY_HEADER_PADDING); RateQueueProperty rateQueuePropertyTwo = propertiesTwo.get(0).getAugmentation(RateQueueProperty.class); Assert.assertEquals("Wrong rate", rateQueuePropertyTwo.getRate().intValue(), serializedBuffer.readShort()); serializedBuffer.skipBytes(PROPERTY_RATE_PADDING); }
From source file:org.opendaylight.protocol.bgp.linkstate.attribute.LinkAttributesParser.java
License:Open Source License
/** * Parse Link Attributes./* www . j a va 2 s . com*/ * * @param attributes key is the tlv type and value is the value of the tlv * @param protocolId to differentiate parsing methods * @return {@link LinkStateAttribute} */ static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) { final LinkAttributesBuilder builder = new LinkAttributesBuilder(); final List<SrAdjIds> srAdjIds = new ArrayList<SrAdjIds>(); final List<SrLanAdjIds> srLanAdjIds = new ArrayList<SrLanAdjIds>(); final List<PeerSetSids> peerSetSids = new ArrayList<PeerSetSids>(); for (final Entry<Integer, ByteBuf> entry : attributes.entries()) { LOG.trace("Link attribute TLV {}", entry.getKey()); final int key = entry.getKey(); final ByteBuf value = entry.getValue(); switch (key) { case TlvUtil.LOCAL_IPV4_ROUTER_ID: builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value))); LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId()); break; case TlvUtil.LOCAL_IPV6_ROUTER_ID: builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value))); LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId()); break; case REMOTE_IPV4_ROUTER_ID: builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value))); LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId()); break; case REMOTE_IPV6_ROUTER_ID: builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value))); LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId()); break; case ADMIN_GROUP: builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt())); LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup()); break; case MAX_BANDWIDTH: builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth()); break; case MAX_RESERVABLE_BANDWIDTH: builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth()); break; case UNRESERVED_BANDWIDTH: parseUnreservedBandwidth(value, builder); break; case TE_METRIC: builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getTeMetric()); break; case LINK_PROTECTION_TYPE: builder.setLinkProtection(LinkProtectionType.forValue(value.readShort())); LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection()); break; case MPLS_PROTOCOL: final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE); builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT))); LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol()); break; case METRIC: // length can 3, 2 or 1 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getMetric()); break; case SHARED_RISK_LINK_GROUP: parseSrlg(value, builder); break; case LINK_OPAQUE: LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value)); break; case LINK_NAME: builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII)); LOG.debug("Parsed Link Name : {}", builder.getLinkName()); break; case SR_ADJ_ID: srAdjIds.add(new SrAdjIdsBuilder( SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build()); LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size() - 1)); break; case SR_LAN_ADJ_ID: srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId)); LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size() - 1)); break; case PEER_NODE_SID_CODE: builder.setPeerNodeSid( new PeerNodeSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)) .build()); LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid()); break; case PEER_ADJ_SID_CODE: builder.setPeerAdjSid( new PeerAdjSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)) .build()); LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid()); break; case PEER_SET_SID_CODE: peerSetSids.add( new PeerSetSidsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)) .build()); LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size() - 1)); break; default: LOG.warn("TLV {} is not a valid link attribute, ignoring it", key); } } if (!srAdjIds.isEmpty()) { builder.setSrAdjIds(srAdjIds); } if (!srLanAdjIds.isEmpty()) { builder.setSrLanAdjIds(srLanAdjIds); } if (!peerSetSids.isEmpty()) { builder.setPeerSetSids(peerSetSids); } LOG.trace("Finished parsing Link Attributes."); return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build(); }
From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.LinkAttributesParser.java
License:Open Source License
/** * Parse Link Attributes.//from ww w. java 2s.c o m * * @param attributes key is the tlv type and value is the value of the tlv * @param protocolId to differentiate parsing methods * @return {@link LinkStateAttribute} */ static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) { final LinkAttributesBuilder builder = new LinkAttributesBuilder(); final List<SrAdjIds> srAdjIds = new ArrayList<>(); final List<SrLanAdjIds> srLanAdjIds = new ArrayList<>(); final List<PeerSetSids> peerSetSids = new ArrayList<>(); for (final Entry<Integer, ByteBuf> entry : attributes.entries()) { LOG.trace("Link attribute TLV {}", entry.getKey()); final int key = entry.getKey(); final ByteBuf value = entry.getValue(); switch (key) { case TlvUtil.LOCAL_IPV4_ROUTER_ID: builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value))); LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId()); break; case TlvUtil.LOCAL_IPV6_ROUTER_ID: builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value))); LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId()); break; case REMOTE_IPV4_ROUTER_ID: builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value))); LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId()); break; case REMOTE_IPV6_ROUTER_ID: builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value))); LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId()); break; case ADMIN_GROUP: builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt())); LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup()); break; case MAX_BANDWIDTH: builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth()); break; case MAX_RESERVABLE_BANDWIDTH: builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value))); LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth()); break; case UNRESERVED_BANDWIDTH: parseUnreservedBandwidth(value, builder); break; case TE_METRIC: builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getTeMetric()); break; case LINK_PROTECTION_TYPE: builder.setLinkProtection(LinkProtectionType.forValue(value.readShort())); LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection()); break; case MPLS_PROTOCOL: final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE); builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT))); LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol()); break; case METRIC: // length can 3, 2 or 1 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))); LOG.debug("Parsed Metric {}", builder.getMetric()); break; case SHARED_RISK_LINK_GROUP: parseSrlg(value, builder); break; case LINK_OPAQUE: LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value)); break; case LINK_NAME: builder.setLinkName(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII)); LOG.debug("Parsed Link Name : {}", builder.getLinkName()); break; case SR_ADJ_ID: srAdjIds.add(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)); LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size() - 1)); break; case SR_LAN_ADJ_ID: srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId)); LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size() - 1)); break; case PEER_NODE_SID_CODE: builder.setPeerNodeSid( new PeerNodeSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)) .build()); LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid()); break; case PEER_ADJ_SID_CODE: builder.setPeerAdjSid( new PeerAdjSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)) .build()); LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid()); break; case PEER_SET_SID_CODE: peerSetSids.add( new PeerSetSidsBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)) .build()); LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size() - 1)); break; default: LOG.warn("TLV {} is not a valid link attribute, ignoring it", key); } } if (!srAdjIds.isEmpty()) { builder.setSrAdjIds(srAdjIds); } if (!srLanAdjIds.isEmpty()) { builder.setSrLanAdjIds(srLanAdjIds); } if (!peerSetSids.isEmpty()) { builder.setPeerSetSids(peerSetSids); } LOG.trace("Finished parsing Link Attributes."); return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build(); }
From source file:org.opendaylight.protocol.bgp.linkstate.impl.tlvs.MultiTopoIdTlvParser.java
License:Open Source License
@Override public TopologyIdentifier parseTlvBody(final ByteBuf value) { return new TopologyIdentifier(value.readShort() & TlvUtil.TOPOLOGY_ID_OFFSET); }
From source file:org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriParser.java
License:Open Source License
static PrefixDescriptors parsePrefixDescriptors(final ByteBuf buffer, final boolean ipv4) throws BGPParsingException { final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder(); while (buffer.isReadable()) { final int type = buffer.readUnsignedShort(); final int length = buffer.readUnsignedShort(); final ByteBuf value = buffer.readSlice(length); if (LOG.isTraceEnabled()) { LOG.trace("Parsing Prefix Descriptor: {}", ByteBufUtil.hexDump(value)); }//ww w . j av a 2 s . c o m switch (type) { case TlvUtil.MULTI_TOPOLOGY_ID: final TopologyIdentifier topologyId = new TopologyIdentifier( value.readShort() & TlvUtil.TOPOLOGY_ID_OFFSET); builder.setMultiTopologyId(topologyId); LOG.trace("Parsed Topology Identifier: {}", topologyId); break; case OSPF_ROUTE_TYPE: final int rt = value.readByte(); final OspfRouteType routeType = OspfRouteType.forValue(rt); if (routeType == null) { throw new BGPParsingException("Unknown OSPF Route Type: " + rt); } builder.setOspfRouteType(routeType); LOG.trace("Parser RouteType: {}", routeType); break; case IP_REACHABILITY: final IpPrefix prefix = (ipv4) ? new IpPrefix(Ipv4Util.prefixForByteBuf(value)) : new IpPrefix(Ipv6Util.prefixForByteBuf(value)); builder.setIpReachabilityInformation(prefix); LOG.trace("Parsed IP reachability info: {}", prefix); break; default: throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type); } } LOG.debug("Finished parsing Prefix descriptors."); return builder.build(); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.open.GracefulCapabilityHandler.java
License:Open Source License
@Override public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { final GracefulRestartCapabilityBuilder cb = new GracefulRestartCapabilityBuilder(); final int flagBits = (buffer.getByte(0) >> RESTART_FLAGS_SIZE); cb.setRestartFlags(new RestartFlags((flagBits & Byte.SIZE) != 0)); final int timer = ((buffer.readUnsignedByte() & TIMER_TOPBITS_MASK) << Byte.SIZE) + buffer.readUnsignedByte(); cb.setRestartTime(timer);/*from w w w . j a va2 s . co m*/ final List<Tables> tables = new ArrayList<>(); while (buffer.readableBytes() != 0) { final int afiVal = buffer.readShort(); final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal); if (afi == null) { LOG.debug("Ignoring GR capability for unknown address family {}", afiVal); buffer.skipBytes(PER_AFI_SAFI_SIZE - 2); continue; } final int safiVal = buffer.readUnsignedByte(); final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal); if (safi == null) { LOG.debug("Ignoring GR capability for unknown subsequent address family {}", safiVal); buffer.skipBytes(1); continue; } final int flags = buffer.readUnsignedByte(); tables.add(new TablesBuilder().setAfi(afi).setSafi(safi) .setAfiFlags(new AfiFlags((flags & AFI_FLAG_FORWARDING_STATE) != 0)).build()); } cb.setTables(tables); return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(cb.build()).build()).build(); }
From source file:org.projectfloodlight.openflow.protocol.ver12.OFOvsTcpFlagSerializerVer12.java
public static Set<OFOvsTcpFlag> readFrom(ByteBuf bb) throws OFParseError { try {// w w w . j av a 2 s . c om return ofWireValue(bb.readShort()); } catch (IllegalArgumentException e) { throw new OFParseError(e); } }
From source file:org.projectfloodlight.openflow.protocol.ver13.OFBsnFabricPortRoleSerializerVer13.java
public static OFBsnFabricPortRole readFrom(ByteBuf bb) throws OFParseError { try {/*from w w w. ja va 2 s . c o m*/ return ofWireValue(bb.readShort()); } catch (IllegalArgumentException e) { throw new OFParseError(e); } }
From source file:org.projectfloodlight.openflow.protocol.ver13.OFBsnGentableErrorCodeSerializerVer13.java
public static OFBsnGentableErrorCode readFrom(ByteBuf bb) throws OFParseError { try {//from ww w . j a va 2s. co m return ofWireValue(bb.readShort()); } catch (IllegalArgumentException e) { throw new OFParseError(e); } }