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)); } 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); } }
/** * Serializes given BGP Open message to byte array, without the header. * * @param msg BGP Open message to be serialized. * @param bytes ByteBuf where the message will be serialized */ @Override public void serializeMessage(final Notification msg, final ByteBuf bytes) { Preconditions.checkArgument(msg instanceof Open, "Message needs to be of type Open"); final Open open = (Open) msg; final ByteBuf msgBody = Unpooled.buffer(); msgBody.writeByte(BGP_VERSION); // When our AS number does not fit into two bytes, we report it as AS_TRANS int openAS = open.getMyAsNumber(); if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) { openAS = AS_TRANS; } msgBody.writeShort(openAS); msgBody.writeShort(open.getHoldTimer()); msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier())); final ByteBuf paramsBuffer = Unpooled.buffer(); if (open.getBgpParameters() != null) { for (final BgpParameters param : open.getBgpParameters()) { this.reg.serializeParameter(param, paramsBuffer); } } msgBody.writeByte(paramsBuffer.writerIndex()); msgBody.writeBytes(paramsBuffer); MessageUtil.formatMessage(TYPE, msgBody, bytes); }