List of usage examples for io.netty.buffer ByteBuf readUnsignedByte
public abstract short readUnsignedByte();
From source file:org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageParser.java
License:Open Source License
/** * Parses BGP Notification message to bytes. * * @param body ByteBuf to be parsed/* w w w.ja v a2 s . c o m*/ * @param messageLength the length of the message * @return {@link Notify} which represents BGP notification message * @throws BGPDocumentedException if parsing goes wrong */ @Override public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException { Preconditions.checkArgument(body != null, "Buffer cannot be null."); if (body.readableBytes() < ERROR_SIZE) { throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength); } final int errorCode = body.readUnsignedByte(); final int errorSubcode = body.readUnsignedByte(); final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode) .setErrorSubcode((short) errorSubcode); if (body.isReadable()) { builder.setData(ByteArray.readAllBytes(body)); } LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode), Arrays.toString(builder.getData())); return builder.build(); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser.java
License:Open Source License
/** * Parses given byte array to BGP Open message * * @param body byte array representing BGP Open message, without header * @param messageLength the length of the message * @return {@link Open} BGP Open Message * @throws BGPDocumentedException if the parsing was unsuccessful *//* w w w .j av a2 s. c o m*/ @Override public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException { Preconditions.checkArgument(body != null, "Buffer cannot be null."); if (body.readableBytes() < MIN_MSG_LENGTH) { throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength); } final int version = body.readUnsignedByte(); if (version != BGP_VERSION) { throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED); } final AsNumber as = new AsNumber((long) body.readUnsignedShort()); final int holdTime = body.readUnsignedShort(); if (holdTime == 1 || holdTime == 2) { throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC); } Ipv4Address bgpId = null; try { bgpId = Ipv4Util.addressForByteBuf(body); } catch (final IllegalArgumentException e) { throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e); } final int optLength = body.readUnsignedByte(); final List<BgpParameters> optParams = new ArrayList<>(); if (optLength > 0) { fillParams(body.slice(), optParams); } LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams); return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime) .setBgpIdentifier(bgpId).setBgpParameters(optParams).build(); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser.java
License:Open Source License
private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "BUffer cannot be null or empty."); if (LOG.isTraceEnabled()) { LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer)); }/*from w w w . jav a 2s .c o m*/ while (buffer.isReadable()) { if (buffer.readableBytes() <= 2) { throw new BGPDocumentedException( "Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED); } final int paramType = buffer.readUnsignedByte(); final int paramLength = buffer.readUnsignedByte(); final ByteBuf paramBody = buffer.readSlice(paramLength); final BgpParameters param; try { param = this.reg.parseParameter(paramType, paramBody); } catch (final BGPParsingException e) { throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e); } if (param != null) { params.add(param); } else { LOG.debug("Ignoring BGP Parameter type: {}", paramType); } } LOG.trace("Parsed BGP parameters: {}", params); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.open.AddPathCapabilityHandler.java
License:Open Source License
@Override public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { final List<AddressFamilies> families = new ArrayList<>(); while (buffer.isReadable()) { final int afiVal = buffer.readUnsignedShort(); final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal); if (afi == null) { throw new BGPParsingException("Address Family Identifier: '" + afiVal + "' not supported."); }// www . j av a2 s. com final int safiVal = buffer.readUnsignedByte(); final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal); if (safi == null) { throw new BGPParsingException( "Subsequent Address Family Identifier: '" + safiVal + "' not supported."); } final SendReceive sendReceive = SendReceive.forValue(buffer.readUnsignedByte()); if (sendReceive != null) { families.add( new AddressFamiliesBuilder().setAfi(afi).setSafi(safi).setSendReceive(sendReceive).build()); } } return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder() .setAddPathCapability(new AddPathCapabilityBuilder().setAddressFamilies(families).build()).build()) .build(); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.open.CapabilityParameterParser.java
License:Open Source License
private OptionalCapabilities parseOptionalCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { final int capCode = buffer.readUnsignedByte(); final int capLength = buffer.readUnsignedByte(); final ByteBuf paramBody = buffer.readSlice(capLength); final CParameters ret = this.reg.parseCapability(capCode, paramBody); if (ret == null) { LOG.info("Ignoring unsupported capability {}", capCode); return null; }/*from www . j a v a 2s . c o m*/ return new OptionalCapabilitiesBuilder().setCParameters(ret).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 v a2 s . c om*/ 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.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser.java
License:Open Source License
/** * Parses AS_PATH from bytes.// www .jav a 2 s. co m * * @param refCache ReferenceCache shared reference of object * @param buffer bytes to be parsed * @return new ASPath object * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory) * @throws BGPParsingException */ private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { if (!buffer.isReadable()) { return EMPTY; } final ArrayList<Segments> ases = new ArrayList<>(); boolean isSequence = false; while (buffer.isReadable()) { final int type = buffer.readUnsignedByte(); final SegmentType segmentType = AsPathSegmentParser.parseType(type); if (segmentType == null) { throw new BGPParsingException("AS Path segment type unknown : " + type); } final int count = buffer.readUnsignedByte(); final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH)); if (segmentType == SegmentType.AS_SEQUENCE) { ases.add(new SegmentsBuilder().setAsSequence(asList).build()); isSequence = true; } else { ases.add(new SegmentsBuilder().setAsSet(asList).build()); } } if (!isSequence) { throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED); } ases.trimToSize(); return new AsPathBuilder().setSegments(ases).build(); }
From source file:org.opendaylight.protocol.bgp.parser.impl.message.update.BgpPrefixSidAttributeParser.java
License:Open Source License
@Override public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException { final BgpPrefixSidBuilder sid = new BgpPrefixSidBuilder(); final List<BgpPrefixSidTlvs> tlvList = new ArrayList<BgpPrefixSidTlvs>(); while (buffer.isReadable()) { final BgpPrefixSidTlv tlv = this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer); tlvList.add(new BgpPrefixSidTlvsBuilder().setBgpPrefixSidTlv(tlv).build()); }//from w ww . j a va2 s .c o m builder.setBgpPrefixSid(sid.setBgpPrefixSidTlvs(tlvList).build()); }
From source file:org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil.java
License:Open Source License
public static Optional<BgpTableType> parseMPAfiSafi(final ByteBuf buffer, final AddressFamilyRegistry afiReg, final SubsequentAddressFamilyRegistry safiReg) { final int afiVal = buffer.readUnsignedShort(); final Class<? extends AddressFamily> afi = afiReg.classForFamily(afiVal); if (afi == null) { LOG.info("Unsupported AFI {} parsed.", afiVal); return Optional.empty(); }//w w w .j a v a2s . c o m // skip reserved buffer.skipBytes(RESERVED); final int safiVal = buffer.readUnsignedByte(); final Class<? extends SubsequentAddressFamily> safi = safiReg.classForFamily(safiVal); if (safi == null) { LOG.info("Unsupported SAFI {} parsed.", safiVal); return Optional.empty(); } return Optional.of(new BgpTableTypeImpl(afi, safi)); }
From source file:org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleAttributeRegistry.java
License:Open Source License
private void addAttribute(final ByteBuf buffer, final Map<Integer, RawAttribute> attributes) throws BGPDocumentedException { final BitArray flags = BitArray.valueOf(buffer.readByte()); final int type = buffer.readUnsignedByte(); final int len = (flags.get(EXTENDED_LENGTH_BIT)) ? buffer.readUnsignedShort() : buffer.readUnsignedByte(); if (!attributes.containsKey(type)) { final AttributeParser parser = this.handlers.getParser(type); if (parser == null) { processUnrecognized(flags, type, buffer, len); } else {/*from w w w. j a v a 2 s . c o m*/ attributes.put(type, new RawAttribute(parser, buffer.readSlice(len))); } } else { LOG.debug("Ignoring duplicate attribute type {}", type); } }