BUG-2825: use IetfInetUtil to instantiate addresses 17/34917/15
authorRobert Varga <rovarga@cisco.com>
Thu, 18 Feb 2016 16:31:59 +0000 (17:31 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 26 Feb 2016 13:42:15 +0000 (14:42 +0100)
Rather than brewing our own strings from bytes, use the utility methods
provided with the model. Also use a single instance for empty prefix.

Change-Id: Idfbfc7f2374b8d0205f1d0e3407e3e347242dbc9
Signed-off-by: Robert Varga <rovarga@cisco.com>
14 files changed:
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/FSIpv4DestinationPrefixHandler.java
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/FSIpv4SourcePrefixHandler.java
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/FSIpv6DestinationPrefixHandler.java
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/handlers/FSIpv6SourcePrefixHandler.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPUpdateMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AdvertizedRoutesSerializer.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/Ipv4NlriParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/WithdrawnRoutesSerializer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing/AbstractSrSubobjectParser.java
util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java
util/src/main/java/org/opendaylight/protocol/util/Ipv4Util.java
util/src/main/java/org/opendaylight/protocol/util/Ipv6Util.java

index a2cff5e6631cbb53843273a4f598f8544484e4ce..cce51a7b82f84f86fdad7db9c2b4644db72942d6 100644 (file)
@@ -9,8 +9,7 @@ package org.opendaylight.protocol.bgp.flowspec.handlers;
 
 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;
@@ -20,14 +19,14 @@ public final class FSIpv4DestinationPrefixHandler implements FlowspecTypeParser,
     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();
     }
index d5f12b84101c6a5fbc513d860e571ba3345dc394..697d445fc52038a1a8f2338c8230fc59cb8a60a5 100644 (file)
@@ -9,8 +9,7 @@ package org.opendaylight.protocol.bgp.flowspec.handlers;
 
 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;
@@ -20,14 +19,14 @@ public final class FSIpv4SourcePrefixHandler implements FlowspecTypeParser, Flow
     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();
     }
index 90c7805f5fb544831f364e9f6995e4114d741d7d..a3a565e5073fe948e6ed6015b2ca23eddeed011a 100644 (file)
@@ -8,13 +8,7 @@
 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;
@@ -23,26 +17,17 @@ public final class FSIpv6DestinationPrefixHandler implements FlowspecTypeParser,
     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();
     }
 }
index 1c84ad2e873cc2b459943d6111884cc96ed26e06..acf6ed68d28509e20ed74ede59d796d4ff1aa34c 100644 (file)
@@ -8,11 +8,9 @@
 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;
@@ -23,26 +21,30 @@ public final class FSIpv6SourcePrefixHandler implements FlowspecTypeParser, Flow
     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));
-    }
 }
index 3728d07c3d18e76278c340c160454be548b442df..0dea9ccb78d2fe88070c68ee52fe55568a914e27 100644 (file)
@@ -15,6 +15,7 @@ import io.netty.buffer.ByteBufUtil;
 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;
@@ -98,13 +99,18 @@ public final class PrefixNlriParser {
         }
         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);
         }
     }
 
index 1c39deaf0e1ec64a871a4130fb2d3aae2021d2b4..bd28048a23f9e590c4660101fd0c9668cc0a7ddf 100644 (file)
@@ -20,6 +20,7 @@ import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
 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;
@@ -99,7 +100,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
         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);
@@ -117,7 +118,7 @@ public final class BGPUpdateMessageParser implements MessageParser, MessageSeria
         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);
index 26e897a45388e16fc8e472b9921b6a50b170eef1..8af2a06765898edaf2a72ead05de111f511a97e7 100644 (file)
@@ -10,8 +10,7 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 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;
@@ -39,12 +38,12 @@ public class AdvertizedRoutesSerializer implements NlriSerializer {
         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);
             }
         }
     }
index a4d515ac3b644a1cb889c2cd9a4117d3a5c5c29e..97cf90468cffdeda5ce7ad249ed8245453fd747f 100644 (file)
@@ -15,6 +15,7 @@ import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 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;
@@ -36,11 +37,11 @@ public final class Ipv4NlriParser implements NlriParser, NlriSerializer {
         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) {
index abd8700481f69a0e6d2426a53eb98dcc3697eeaa..215c910e7590db49b1903cd4eb94fef704c7b8d8 100644 (file)
@@ -10,8 +10,7 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 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;
@@ -46,14 +45,14 @@ public class WithdrawnRoutesSerializer implements NlriSerializer {
             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);
                 }
             }
         }
index 0a57de9204df27ec66440a0aacf3ca898ea5f4cf..25433f74117d7f886c52bbe393cd79099ee3149e 100644 (file)
@@ -33,9 +33,9 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 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;
@@ -169,7 +169,7 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         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);
@@ -232,10 +232,7 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         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
index ed37f76aab330ef54a5e3cd0122651e05543e80f..d16b341fdc9a077081134af878bc6e079c9b0997 100644 (file)
@@ -22,7 +22,6 @@ 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.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;
@@ -127,7 +126,7 @@ public abstract class AbstractSrSubobjectParser   {
         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);
@@ -155,11 +154,11 @@ public abstract class AbstractSrSubobjectParser   {
         }
     }
 
-    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();
@@ -180,7 +179,7 @@ public abstract class AbstractSrSubobjectParser   {
         }
     }
 
-    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());
index 162f8c27a1ef05fbc6385f8f82fe26baeabe2d7a..7d7135ee0ac3dbcbf58d2a273b5bf32259a7ec30 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.base.Preconditions;
 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;
@@ -278,6 +279,21 @@ public final class ByteBufWriteUtil {
         }
     }
 
+    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.
index be7a7e668bdbe9a2ed80214d94b8ec5de5f4bf9c..1899d5021f0d0c9a60e505a1ce6cca478e6cbef3 100644 (file)
@@ -9,50 +9,32 @@ package org.opendaylight.protocol.util;
 
 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.
      *
@@ -60,7 +42,7 @@ public final class Ipv4Util {
      * @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));
     }
 
     /**
@@ -79,9 +61,14 @@ public final class Ipv4Util {
      * @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;
     }
 
     /**
@@ -91,11 +78,7 @@ public final class Ipv4Util {
      * @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));
     }
 
     /**
@@ -105,12 +88,7 @@ public final class Ipv4Util {
      * @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);
     }
 
     /**
@@ -119,18 +97,26 @@ public final class Ipv4Util {
      *
      * @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)));
     }
 
     /**
@@ -143,8 +129,7 @@ public final class Ipv4Util {
     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);
     }
 
     /**
@@ -158,7 +143,7 @@ public final class Ipv4Util {
         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);
     }
 
     /**
@@ -178,7 +163,7 @@ public final class Ipv4Util {
             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;
@@ -207,11 +192,7 @@ public final class Ipv4Util {
      * @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);
     }
 
     /**
index c762c50ea0acaab37cbd0922f6ddfc215c7fb27f..876f30ae55a721937502b60d97aa6827de32e069 100644 (file)
@@ -9,17 +9,14 @@ package org.opendaylight.protocol.util;
 
 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;
 
@@ -27,28 +24,13 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
  * 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
      *
@@ -66,7 +48,7 @@ public final class Ipv6Util {
      * @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));
     }
 
     /**
@@ -85,9 +67,7 @@ public final class Ipv6Util {
      * @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);
     }
 
     /**
@@ -97,12 +77,7 @@ public final class Ipv6Util {
      * @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);
     }
 
     /**
@@ -111,18 +86,14 @@ public final class Ipv6Util {
      *
      * @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);
     }
 
     /**
@@ -135,8 +106,7 @@ public final class Ipv6Util {
     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);
     }
 
     /**
@@ -150,7 +120,7 @@ public final class Ipv6Util {
         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);
     }
 
 
@@ -171,7 +141,7 @@ public final class Ipv6Util {
             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;