}
/**
- * Returns byte array containing IpAddress length and IpAddress.
+ * Writes the length of an {@link IpAddressNoZone} and its contents to specified buffer.
*
* @param address containing ipv4 or ipv6 address
- * @return byte array
+ * @param output output buffer
*/
- public static @NonNull ByteBuf bytesFor(final IpAddressNoZone address) {
- final ByteBuf body = Unpooled.buffer();
- if (address.getIpv4AddressNoZone() != null) {
- body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
- body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4AddressNoZone()));
- } else if (address.getIpv6AddressNoZone() != null) {
- body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
- body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6AddressNoZone()));
+ public static void writeBytesFor(final IpAddressNoZone address, final ByteBuf output) {
+ final var ipv4 = address.getIpv4AddressNoZone();
+ if (ipv4 != null) {
+ output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
+ output.writeBytes(Ipv4Util.bytesForAddress(ipv4));
} else {
- body.writeByte(0);
+ final var ipv6 = address.getIpv6AddressNoZone();
+ if (ipv6 != null) {
+ output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
+ output.writeBytes(Ipv6Util.bytesForAddress(ipv6));
+ } else {
+ throw new IllegalStateException("Invalid address " + address);
+ }
}
- return body;
}
/**
final EsRoute evpn = ((EsRouteCase) evpnInput).getEsRoute();
final ByteBuf body = Unpooled.buffer();
SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
- final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
- checkArgument(orig.readableBytes() > 0);
- body.writeBytes(orig);
+ IpAddressUtil.writeBytesFor(evpn.getOrigRouteIp(), body);
return body;
}
final IncMultiEthernetTagRes evpn = ((IncMultiEthernetTagResCase) evpnChoice).getIncMultiEthernetTagRes();
final ByteBuf body = Unpooled.buffer();
ByteBufUtils.writeOrZero(body, evpn.getEthernetTagId().getVlanId());
- final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
- Preconditions.checkArgument(orig.readableBytes() > 0);
- body.writeBytes(orig);
+ IpAddressUtil.writeBytesFor(evpn.getOrigRouteIp(), body);
return body;
}
static final void serializeRDMulticastSource(final MulticastSourceRdGrouping route, final ByteBuf output) {
RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), output);
- output.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
+ IpAddressUtil.writeBytesFor(route.getMulticastSource(), output);
}
}
final ByteBuf nlriByteBuf = Unpooled.buffer();
RouteDistinguisherUtil.serializeRouteDistinquisher(route.getRouteDistinguisher(), nlriByteBuf);
nlriByteBuf.writeInt(route.getSourceAs().getValue().intValue());
- nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(route.getMulticastSource()));
+ IpAddressUtil.writeBytesFor(route.getMulticastSource(), nlriByteBuf);
MulticastGroupOpaqueUtil.bytesForMulticastGroup(route.getMulticastGroup(), nlriByteBuf);
return nlriByteBuf;
}
}
static MulticastGroup multicastGroupForByteBuf(final ByteBuf buffer) {
- final short multicastGroupLength = buffer.readUnsignedByte();
- switch (multicastGroupLength) {
- case Ipv4Util.IP4_BITS_LENGTH:
- return new CGAddressCaseBuilder()
- .setCGAddress(new IpAddressNoZone(Ipv4Util.addressForByteBuf(buffer))).build();
- case Ipv6Util.IPV6_BITS_LENGTH:
- return new CGAddressCaseBuilder()
- .setCGAddress(new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer))).build();
- default:
- return new LdpMpOpaqueValueCaseBuilder()
- .setLdpMpOpaqueValue(new LdpMpOpaqueValueBuilder(OpaqueUtil.parseOpaque(buffer)).build()).build();
- }
+ return switch (buffer.readUnsignedByte()) {
+ case Ipv4Util.IP4_BITS_LENGTH -> new CGAddressCaseBuilder()
+ .setCGAddress(new IpAddressNoZone(Ipv4Util.addressForByteBuf(buffer)))
+ .build();
+ case Ipv6Util.IPV6_BITS_LENGTH -> new CGAddressCaseBuilder()
+ .setCGAddress(new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer)))
+ .build();
+ default -> new LdpMpOpaqueValueCaseBuilder()
+ .setLdpMpOpaqueValue(new LdpMpOpaqueValueBuilder(OpaqueUtil.parseOpaque(buffer)).build())
+ .build();
+ };
}
static void bytesForMulticastGroup(final @NonNull MulticastGroup group, final ByteBuf nlriByteBuf) {
- if (group instanceof CGAddressCase) {
- nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(((CGAddressCase) group).getCGAddress()));
+ if (group instanceof CGAddressCase cgAddress) {
+ IpAddressUtil.writeBytesFor(cgAddress.getCGAddress(), nlriByteBuf);
} else {
OpaqueUtil.serializeOpaque(((LdpMpOpaqueValueCase) group).getLdpMpOpaqueValue(), nlriByteBuf);
}
final SourceActiveAD route = mvpn.getSourceActiveAD();
final ByteBuf nlriByteBuf = Unpooled.buffer();
serializeRDMulticastSource(route, nlriByteBuf);
- nlriByteBuf.writeBytes(IpAddressUtil.bytesFor(route.getMulticastGroup()));
+ IpAddressUtil.writeBytesFor(route.getMulticastGroup(), nlriByteBuf);
return nlriByteBuf;
}
}