import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
public static final int DESTINATION_PREFIX_VALUE = 1;
@Override
- public void serializeType(FlowspecType value, ByteBuf output) {
+ public void serializeType(final FlowspecType value, final ByteBuf output) {
Preconditions.checkArgument(value instanceof DestinationPrefixCase, "DestinationPrefixCase class is mandatory!");
output.writeByte(DESTINATION_PREFIX_VALUE);
- output.writeBytes(Ipv4Util.bytesForPrefixBegin(((DestinationPrefixCase) value).getDestinationPrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(((DestinationPrefixCase) value).getDestinationPrefix(), output);
}
@Override
- public FlowspecType parseType(ByteBuf buffer) {
+ public FlowspecType parseType(final ByteBuf buffer) {
Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
return new DestinationPrefixCaseBuilder().setDestinationPrefix(Ipv4Util.prefixForByteBuf(buffer)).build();
}
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.SourcePrefixCase;
public static final int SOURCE_PREFIX_VALUE = 2;
@Override
- public void serializeType(FlowspecType value, ByteBuf output) {
+ public void serializeType(final FlowspecType value, final ByteBuf output) {
Preconditions.checkArgument(value instanceof SourcePrefixCase, "SourcePrefixCase class is mandatory!");
output.writeByte(SOURCE_PREFIX_VALUE);
- output.writeBytes(Ipv4Util.bytesForPrefixBegin(((SourcePrefixCase) value).getSourcePrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(((SourcePrefixCase) value).getSourcePrefix(), output);
}
@Override
- public FlowspecType parseType(ByteBuf buffer) {
+ public FlowspecType parseType(final ByteBuf buffer) {
Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
return new SourcePrefixCaseBuilder().setSourcePrefix(Ipv4Util.prefixForByteBuf(buffer)).build();
}
package org.opendaylight.protocol.bgp.flowspec.handlers;
import com.google.common.base.Preconditions;
-import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.Ipv6Util;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
public static final int IPV6_DESTINATION_PREFIX_VALUE = 1;
@Override
- public void serializeType(FlowspecType value, ByteBuf output) {
+ public void serializeType(final FlowspecType value, final ByteBuf output) {
Preconditions.checkArgument(value instanceof DestinationIpv6PrefixCase, "DestinationIpv6PrefixCase class is mandatory!");
output.writeByte(IPV6_DESTINATION_PREFIX_VALUE);
- output.writeBytes(insertOffsetByte(Ipv6Util.bytesForPrefixBegin(((DestinationIpv6PrefixCase) value).getDestinationPrefix())));
+
+ FSIpv6SourcePrefixHandler.writePrefix(((DestinationIpv6PrefixCase) value).getDestinationPrefix(), output);
}
@Override
- public FlowspecType parseType(ByteBuf buffer) {
+ public FlowspecType parseType(final ByteBuf buffer) {
Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
- return new DestinationIpv6PrefixCaseBuilder().setDestinationPrefix(parseIpv6Prefix(buffer)).build();
- }
-
- private static Ipv6Prefix parseIpv6Prefix(final ByteBuf nlri) {
- final int bitLength = nlri.readUnsignedByte();
- nlri.readUnsignedByte();
- return Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, bitLength / Byte.SIZE), bitLength);
- }
-
- private static byte[] insertOffsetByte(final byte[] ipPrefix) {
- // income <len, prefix>
- return Bytes.concat(new byte[] { ipPrefix[0] }, new byte[] { 0 }, ByteArray.subByte(ipPrefix, 1 , ipPrefix.length-1));
+ return new DestinationIpv6PrefixCaseBuilder().setDestinationPrefix(
+ FSIpv6SourcePrefixHandler.parseIpv6Prefix(buffer)).build();
}
}
package org.opendaylight.protocol.bgp.flowspec.handlers;
import com.google.common.base.Preconditions;
-import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
-import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
public static final int SOURCE_PREFIX_VALUE = 2;
@Override
- public void serializeType(FlowspecType value, ByteBuf output) {
+ public void serializeType(final FlowspecType value, final ByteBuf output) {
Preconditions.checkArgument(value instanceof SourceIpv6PrefixCase, "SourceIpv6PrefixCase class is mandatory!");
output.writeByte(SOURCE_PREFIX_VALUE);
- output.writeBytes(insertOffsetByte(Ipv6Util.bytesForPrefixBegin(((SourceIpv6PrefixCase) value).getSourcePrefix())));
+ writePrefix(((SourceIpv6PrefixCase) value).getSourcePrefix(), output);
+ }
+
+ static void writePrefix(final Ipv6Prefix prefix, final ByteBuf output) {
+ final byte[] bytes = Ipv6Util.bytesForPrefix(prefix);
+ final byte prefixBits = bytes[Ipv6Util.IPV6_LENGTH];
+ output.writeByte(prefixBits);
+ output.writeByte(0);
+ output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
}
@Override
- public FlowspecType parseType(ByteBuf buffer) {
+ public FlowspecType parseType(final ByteBuf buffer) {
Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
return new SourceIpv6PrefixCaseBuilder().setSourcePrefix(parseIpv6Prefix(buffer)).build();
}
- private static Ipv6Prefix parseIpv6Prefix(final ByteBuf nlri) {
+ static Ipv6Prefix parseIpv6Prefix(final ByteBuf nlri) {
final int bitLength = nlri.readUnsignedByte();
nlri.readUnsignedByte();
+ // FIXME: this does not look right if bitLenght % Byte.SIZE != 0
return Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, bitLength / Byte.SIZE), bitLength);
}
-
- private static byte[] insertOffsetByte(final byte[] ipPrefix) {
- // income <len, prefix>
- return Bytes.concat(new byte[] { ipPrefix[0] }, new byte[] { 0 }, ByteArray.subByte(ipPrefix, 1 , ipPrefix.length-1));
- }
}
import io.netty.buffer.Unpooled;
import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
}
if (descriptors.getIpReachabilityInformation() != null) {
final IpPrefix prefix = descriptors.getIpReachabilityInformation();
- byte[] prefixBytes = null;
+
+ final ByteBuf buf;
if (prefix.getIpv4Prefix() != null) {
- prefixBytes = Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
+ buf = Unpooled.buffer(Ipv4Util.IP4_LENGTH + 1);
+ ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv4Prefix(), buf);
} else if (prefix.getIpv6Prefix() != null) {
- prefixBytes = Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
+ buf = Unpooled.buffer(Ipv6Util.IPV6_LENGTH + 1);
+ ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), buf);
+ } else {
+ buf = null;
}
- TlvUtil.writeTLV(IP_REACHABILITY, Unpooled.wrappedBuffer(prefixBytes), buffer);
+ TlvUtil.writeTLV(IP_REACHABILITY, buf, buffer);
}
}
import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
if (withdrawnRoutes != null) {
final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
for (final Ipv4Prefix prefix : withdrawnRoutes.getWithdrawnRoutes()) {
- withdrawnRoutesBuf.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
+ ByteBufWriteUtil.writeMinimalPrefix(prefix, withdrawnRoutesBuf);
}
messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
messageBody.writeBytes(withdrawnRoutesBuf);
final Nlri nlri = update.getNlri();
if (nlri != null) {
for (final Ipv4Prefix prefix : nlri.getNlri()) {
- messageBody.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
+ ByteBufWriteUtil.writeMinimalPrefix(prefix, messageBody);
}
}
MessageUtil.formatMessage(TYPE, messageBody, bytes);
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4Case;
if (routes.getDestinationType() instanceof DestinationIpv4Case) {
final DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) routes.getDestinationType();
for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
- byteAggregator.writeBytes(Ipv4Util.bytesForPrefixBegin(ipv4Prefix.getPrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
}
} else if (routes.getDestinationType() instanceof DestinationIpv6Case) {
final DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) routes.getDestinationType();
for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
- byteAggregator.writeBytes(Ipv6Util.bytesForPrefixBegin(ipv6Prefix.getPrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
}
}
}
import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4;
Preconditions.checkArgument(attribute instanceof Nlri, "Attribute parameter is not a Nlri object.");
final Nlri nlri = (Nlri) attribute;
for (final Ipv4Prefix ipv4Prefix : nlri.getNlri()) {
- byteAggregator.writeBytes(Ipv4Util.bytesForPrefixBegin(ipv4Prefix));
+ ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix, byteAggregator);
}
}
- private DestinationIpv4 prefixes(final ByteBuf nlri) {
+ private static DestinationIpv4 prefixes(final ByteBuf nlri) {
final List<Ipv4Prefix> prefs = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(nlri));
final List<Ipv4Prefixes> prefixes = new ArrayList<>(prefs.size());
for (final Ipv4Prefix p : prefs) {
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case;
final DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) routes.getDestinationType();
if (destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes() != null) {
for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
- byteAggregator.writeBytes(Ipv4Util.bytesForPrefixBegin(ipv4Prefix.getPrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
}
}
} else if (routes.getDestinationType() instanceof DestinationIpv6Case) {
final DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) routes.getDestinationType();
if (destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes() != null) {
for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
- byteAggregator.writeBytes(Ipv6Util.bytesForPrefixBegin(ipv6Prefix.getPrefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
}
}
}
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
return p;
}
- private void validateAs(final AsNumber remoteAs, final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
+ private static void validateAs(final AsNumber remoteAs, final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
if (!remoteAs.equals(localPref.getExpectedRemoteAs())) {
LOG.warn("Unexpected remote AS number. Expecting {}, got {}", remoteAs, localPref.getExpectedRemoteAs());
throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
final InetAddress inetAddress = ((InetSocketAddress) socketAddress).getAddress();
Preconditions.checkArgument(inetAddress instanceof Inet4Address || inetAddress instanceof Inet6Address, "Expecting %s or %s but was %s", Inet4Address.class, Inet6Address.class, inetAddress.getClass());
- if(inetAddress instanceof Inet4Address) {
- return new IpAddress(new Ipv4Address(inetAddress.getHostAddress()));
- }
- return new IpAddress(new Ipv6Address(inetAddress.getHostAddress()));
+ return IetfInetUtil.INSTANCE.ipAddressFor(inetAddress);
}
@Override
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SidType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrSubobject;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.subobject.Nai;
return buffer;
}
- private void serializeNai(final Nai nai, final SidType sidType, final ByteBuf buffer) {
+ private static void serializeNai(final Nai nai, final SidType sidType, final ByteBuf buffer) {
switch (sidType) {
case Ipv4NodeId:
writeIpv4Address(((IpNodeId) nai).getIpAddress().getIpv4Address(), buffer);
}
}
- private Nai parseNai(final SidType sidType, final ByteBuf buffer) {
+ private static Nai parseNai(final SidType sidType, final ByteBuf buffer) {
switch (sidType) {
case Ipv4NodeId:
return new IpNodeIdBuilder().setIpAddress(
- new IpAddress(new Ipv4Address(Ipv4Util.addressForByteBuf(buffer)))).build();
+ new IpAddress(Ipv4Util.addressForByteBuf(buffer))).build();
case Ipv6NodeId:
return new IpNodeIdBuilder().setIpAddress(
new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build();
}
}
- protected final SrSubobject parseSrSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
+ protected static SrSubobject parseSrSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
final int sidTypeByte = buffer.readByte() >> SID_TYPE_BITS_OFFSET;
final SidType sidType = SidType.forValue(sidTypeByte);
final BitArray bitSet = BitArray.valueOf(buffer.readByte());
import io.netty.buffer.ByteBuf;
import java.math.BigInteger;
import java.util.BitSet;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
}
}
+ public static void writeMinimalPrefix(final Ipv4Prefix ipv4Prefix, final ByteBuf output) {
+ final byte[] bytes = IetfInetUtil.INSTANCE.ipv4PrefixToBytes(ipv4Prefix);
+ writeMinimalPrefix(output, bytes, bytes[Ipv4Util.IP4_LENGTH]);
+ }
+
+ public static void writeMinimalPrefix(final Ipv6Prefix ipv6Prefix, final ByteBuf output) {
+ final byte[] bytes = IetfInetUtil.INSTANCE.ipv6PrefixToBytes(ipv6Prefix);
+ writeMinimalPrefix(output, bytes, bytes[Ipv6Util.IPV6_LENGTH]);
+ }
+
+ private static void writeMinimalPrefix(final ByteBuf output, final byte[] bytes, final byte prefixBits) {
+ output.writeByte(prefixBits);
+ output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
+ }
+
/**
* Writes Float32 <code>value</code> if not null, otherwise writes zeros to
* the <code>output</code> ByteBuf. ByteBuf's writerIndex is increased by 4.
import com.google.common.base.Preconditions;
import com.google.common.net.InetAddresses;
-import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
-import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
/**
* Util class for creating generated Ipv4Address.
*/
public final class Ipv4Util {
+ public static final int IP4_LENGTH = 4;
+ private static final Ipv4Prefix EMPTY_PREFIX = new Ipv4Prefix("0.0.0.0/0");
private Ipv4Util() {
throw new UnsupportedOperationException();
}
- public static final int IP4_LENGTH = 4;
-
- /**
- * Converts byte array to Inet4Address.
- *
- * @param bytes to be converted
- * @return InetAddress instance
- * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
- */
- private static InetAddress getAddress(final byte[] bytes) {
- try {
- return Inet4Address.getByAddress(bytes);
- } catch (final UnknownHostException e) {
- throw new IllegalArgumentException("Failed to construct IPv4 address", e);
- }
- }
-
/**
* Reads from ByteBuf buffer and converts bytes to Ipv4Address.
*
* @return Ipv4Address
*/
public static Ipv4Address addressForByteBuf(final ByteBuf buffer) {
- return new Ipv4Address(InetAddresses.toAddrString(getAddress(ByteArray.readBytes(buffer, IP4_LENGTH))));
+ return IetfInetUtil.INSTANCE.ipv4AddressFor(ByteArray.readBytes(buffer, IP4_LENGTH));
}
/**
* @return byte array
*/
public static byte[] bytesForAddress(final Ipv4Address address) {
- final InetAddress a = InetAddresses.forString(address.getValue());
- Preconditions.checkArgument(a instanceof Inet4Address);
- return a.getAddress();
+ return IetfInetUtil.INSTANCE.ipv4AddressBytes(address);
+ }
+
+ public static int prefixBitsToBytes(final int bits) {
+ if (bits % Byte.SIZE != 0) {
+ return (bits / Byte.SIZE) + 1;
+ }
+ return bits / Byte.SIZE;
}
/**
* @return
*/
public static int getPrefixLengthBytes(final String prefix) {
- final int bits = Ipv4Util.getPrefixLength(prefix);
- if (bits % Byte.SIZE != 0) {
- return (bits / Byte.SIZE) + 1;
- }
- return bits / Byte.SIZE;
+ return prefixBitsToBytes(Ipv4Util.getPrefixLength(prefix));
}
/**
* @return byte array with prefix length at the end
*/
public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
- final String p = prefix.getValue();
- final int sep = p.indexOf('/');
- final InetAddress a = InetAddresses.forString(p.substring(0, sep));
- Preconditions.checkArgument(a instanceof Inet4Address);
- final byte[] bytes = a.getAddress();
- return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
+ return IetfInetUtil.INSTANCE.ipv4PrefixToBytes(prefix);
}
/**
*
* @param prefix Ipv4Prefix to be converted
* @return byte array with the prefix length at the beginning
+ *
+ * @deprecated This is inefficient, refactor code to use {@link #bytesForAddress(Ipv4Address)} or
+ * {@link ByteBufWriteUtil#writeMinimalPrefix(Ipv4Prefix, ByteBuf)}.
*/
+ @Deprecated
public static byte[] bytesForPrefixBegin(final Ipv4Prefix prefix) {
- final String p = prefix.getValue();
- final int length = getPrefixLength(p);
- if (length == 0) {
+ final byte[] addrWithPrefix = bytesForPrefix(prefix);
+ return prefixedBytes(addrWithPrefix[IP4_LENGTH], addrWithPrefix);
+ }
+
+ static byte[] prefixedBytes(final byte prefixBits, final byte[] address) {
+ if (prefixBits != 0) {
+ final int prefixBytes = prefixBitsToBytes(Byte.toUnsignedInt(prefixBits));
+ final byte[] ret = new byte[prefixBytes + 1];
+ ret[0] = prefixBits;
+ System.arraycopy(address, 0, ret, 1, prefixBytes);
+ return ret;
+ } else {
return new byte[] { 0 };
}
- final int sep = p.indexOf('/');
- final InetAddress a = InetAddresses.forString(p.substring(0, sep));
- Preconditions.checkArgument(a instanceof Inet4Address);
- final byte[] bytes = a.getAddress();
- return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , getPrefixLengthBytes(p)));
}
/**
public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
final byte[] tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
- final InetAddress a = getAddress(tmp);
- return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
+ return IetfInetUtil.INSTANCE.ipv4PrefixFor(tmp, length);
}
/**
final int prefixLength = bytes.readByte();
final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: %s", bytes.readableBytes());
- return Ipv4Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+ return prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
}
/**
byteOffset += 1;
// if length == 0, default route will be added
if (bitLength == 0) {
- list.add(new Ipv4Prefix("0.0.0.0/0"));
+ list.add(EMPTY_PREFIX);
continue;
}
final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;
* @return IpAddress
*/
public static IpAddress getIpAddress(final InetAddress inetAddress) {
- final String address = InetAddresses.toAddrString(inetAddress);
- if (inetAddress instanceof Inet4Address) {
- return new IpAddress(new Ipv4Address(address));
- }
- return new IpAddress(new Ipv6Address(address));
+ return IetfInetUtil.INSTANCE.ipAddressFor(inetAddress);
}
/**
import com.google.common.base.Preconditions;
import com.google.common.net.InetAddresses;
-import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IetfInetUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
* Util class for creating generated Ipv6Address.
*/
public final class Ipv6Util {
+ public static final int IPV6_LENGTH = 16;
+ private static final Ipv6Prefix EMPTY_PREFIX = new Ipv6Prefix("::/0");
private Ipv6Util() {
throw new UnsupportedOperationException();
}
- public static final int IPV6_LENGTH = 16;
-
- /**
- * Converts byte array to Inet6Address.
- *
- * @param bytes to be converted
- * @return InetAddress instance
- * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
- */
- private static InetAddress getAddress(final byte[] bytes) {
- try {
- return Inet6Address.getByAddress(bytes);
- } catch (final UnknownHostException e) {
- throw new IllegalArgumentException("Failed to construct IPv6 address", e);
- }
- }
-
/**
* Creates uncompressed IP Address
*
* @return Ipv6Address
*/
public static Ipv6Address addressForByteBuf(final ByteBuf buffer) {
- return new Ipv6Address(InetAddresses.toAddrString(getAddress((ByteArray.readBytes(buffer, IPV6_LENGTH)))));
+ return IetfInetUtil.INSTANCE.ipv6AddressFor(ByteArray.readBytes(buffer, IPV6_LENGTH));
}
/**
* @return byte array
*/
public static byte[] bytesForAddress(final Ipv6Address address) {
- final InetAddress a = InetAddresses.forString(address.getValue());
- Preconditions.checkArgument(a instanceof Inet6Address);
- return a.getAddress();
+ return IetfInetUtil.INSTANCE.ipv6AddressBytes(address);
}
/**
* @return byte array with prefix length at the end
*/
public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
- final String p = prefix.getValue();
- final int sep = p.indexOf('/');
- final InetAddress a = InetAddresses.forString(p.substring(0, sep));
- Preconditions.checkArgument(a instanceof Inet6Address);
- final byte[] bytes = a.getAddress();
- return Bytes.concat(bytes, new byte[] { UnsignedBytes.parseUnsignedByte(p.substring(sep + 1, p.length())) });
+ return IetfInetUtil.INSTANCE.ipv6PrefixToBytes(prefix);
}
/**
*
* @param prefix Ipv6Prefix to be converted
* @return byte array with the prefix length at the beginning
+ *
+ * @deprecated This is inefficient, refactor code to use {@link #bytesForAddress(Ipv6Address)} or
+ * {@link ByteBufWriteUtil#writeMinimalPrefix(Ipv6Prefix, ByteBuf)}.
*/
+ @Deprecated
public static byte[] bytesForPrefixBegin(final Ipv6Prefix prefix) {
- final String p = prefix.getValue();
- final int length = Ipv4Util.getPrefixLength(p);
- if (length == 0) {
- return new byte[] { 0 };
- }
- final int sep = p.indexOf('/');
- final InetAddress a = InetAddresses.forString(p.substring(0, sep));
- Preconditions.checkArgument(a instanceof Inet6Address);
- final byte[] bytes = a.getAddress();
- return Bytes.concat(new byte[] { UnsignedBytes.checkedCast(length) }, ByteArray.subByte(bytes, 0 , Ipv4Util.getPrefixLengthBytes(p)));
+ final byte[] addrWithPrefix = bytesForPrefix(prefix);
+ return Ipv4Util.prefixedBytes(addrWithPrefix[IPV6_LENGTH], addrWithPrefix);
}
/**
public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
- final InetAddress a = getAddress(tmp);
- return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
+ return IetfInetUtil.INSTANCE.ipv6PrefixFor(tmp, length);
}
/**
final int prefixLength = bytes.readByte();
final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
Preconditions.checkArgument(size <= bytes.readableBytes(), "Illegal length of IP prefix: %s", bytes.readableBytes());
- return Ipv6Util.prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
+ return prefixForBytes(ByteArray.readBytes(bytes, size), prefixLength);
}
byteOffset += 1;
// if length == 0, default route will be added
if (bitLength == 0) {
- list.add(new Ipv6Prefix("::/0"));
+ list.add(EMPTY_PREFIX);
continue;
}
final int byteCount = (bitLength % Byte.SIZE != 0) ? (bitLength / Byte.SIZE) + 1 : bitLength / Byte.SIZE;