/** * From string ipAddress creates an InetAddress and puts it into ByteBuf. * @param ipAddress Ipv4 address * @return ByteBuf with filled in bytes from ipAddress */ public static ByteBuf byteBufForAddress(final Ipv4Address ipAddress) { return Unpooled.wrappedBuffer(bytesForAddress(ipAddress)); }
/** * Writes IPv4 address if not null, otherwise writes zeros to the * <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4. * * @param ipv4Address * IPv4 address to be written to the output. * @param output * ByteBuf, where ipv4Address or zeros are written. */ public static void writeIpv4Address(final Ipv4Address ipv4Address, final ByteBuf output) { if (ipv4Address != null) { output.writeBytes(Ipv4Util.bytesForAddress(ipv4Address)); } else { output.writeZero(Ipv4Util.IP4_LENGTH); } }
@Override public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) { Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object."); final OriginatorId originator = ((Attributes) attribute).getOriginatorId(); if (originator == null) { return; } final ByteBuf originatorIdBuf = Unpooled.buffer(); originatorIdBuf.writeBytes(Ipv4Util.bytesForAddress(originator.getOriginator())); AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, originatorIdBuf, byteAggregator); } }
@Override public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) { Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object."); final ClusterId cid = ((Attributes) attribute).getClusterId(); if (cid == null) { return; } final List<ClusterIdentifier> cluster = cid.getCluster(); if (cluster == null || cluster.isEmpty()) { return; } final ByteBuf clusterIdBuffer = Unpooled.buffer(); for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) { clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier)); } AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator); } }
static ByteBuf serializeOrigRouteIp(final IpAddress origRouteIp) { final ByteBuf body = Unpooled.buffer(); if (origRouteIp.getIpv4Address() != null) { body.writeByte(Ipv4Util.IP4_BITS_LENGTH); body.writeBytes(Ipv4Util.bytesForAddress(origRouteIp.getIpv4Address())); } else if (origRouteIp.getIpv6Address() != null) { body.writeByte(Ipv6Util.IPV6_BITS_LENGTH); body.writeBytes(Ipv6Util.bytesForAddress(origRouteIp.getIpv6Address())); } else { body.writeZero(ZERO_BYTE); } return body; } }
private static void serializeForwardingAddress(final IpAddress forwardingAddress, final ByteBuf byteAggregator) { if (forwardingAddress != null) { final ByteBuf ospfBuf = Unpooled.buffer(); if (forwardingAddress.getIpv4Address() != null) { ospfBuf.writeBytes(Ipv4Util.bytesForAddress(forwardingAddress.getIpv4Address())); } else if (forwardingAddress.getIpv6Address() != null) { ospfBuf.writeBytes(Ipv6Util.bytesForAddress(forwardingAddress.getIpv6Address())); } TlvUtil.writeTLV(FORWARDING_ADDRESS, ospfBuf, byteAggregator); } } }
/** * 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); }
@Override public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) { Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object."); final Attributes pathAttributes = (Attributes) attribute; final Aggregator aggregator = pathAttributes.getAggregator(); if (aggregator == null) { return; } Preconditions.checkArgument(aggregator.getAsNumber() != null, "Missing AS number that formed the aggregate route (encoded as 2 octets)."); final ShortAsNumber shortAsNumber = new ShortAsNumber(aggregator.getAsNumber()); final ByteBuf buffer = Unpooled.buffer(); buffer.writeInt(shortAsNumber.getValue().intValue()); buffer.writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress())); AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator); } }
private static ByteBuf serializeIp(final IpAddress ipAddress) { final ByteBuf body = Unpooled.buffer(); if (ipAddress != null) { if (ipAddress.getIpv4Address() != null) { body.writeByte(Ipv4Util.IP4_BITS_LENGTH); body.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address())); } else if (ipAddress.getIpv6Address() != null) { body.writeByte(Ipv6Util.IPV6_BITS_LENGTH); body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address())); } else { body.writeZero(ZERO_BYTE); } } else { body.writeZero(ZERO_BYTE); } return body; }
/** * Writes serialized cnextHop IP address as byte value into ByteBuf. * * @param cnextHop next hop to be serialized * @param byteAggregator where the next hop will be written */ public static void serializeNextHop(final CNextHop cnextHop, final ByteBuf byteAggregator) { if (cnextHop instanceof Ipv4NextHopCase) { byteAggregator.writeBytes(Ipv4Util.bytesForAddress(((Ipv4NextHopCase) cnextHop).getIpv4NextHop().getGlobal())); } else if (cnextHop instanceof Ipv6NextHopCase) { final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop(); Preconditions.checkArgument(nextHop.getGlobal() != null, "Ipv6 Next Hop is missing Global address."); byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal())); if (nextHop.getLinkLocal() != null) { byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal())); } } else if (!(cnextHop instanceof EmptyNextHopCase)) { throw new IllegalArgumentException("Cannot serialize NEXT_HOP. Class not supported: " + cnextHop); } }