Bug-612: fix segment-routing serializers 22/8722/2
authorMilos Fabian <milfabia@cisco.com>
Mon, 7 Jul 2014 09:06:14 +0000 (11:06 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 7 Jul 2014 09:06:14 +0000 (11:06 +0200)
Change-Id: I1013c09e62c48a4276d6dadd9cd7029e10c9d7a2
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/lsp/setup/type01/PathSetupTypeTlvParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrEroSubobjectParser.java
pcep/segment-routing/src/main/java/org/opendaylight/protocol/pcep/segment/routing02/SrPceCapabilityTlvParser.java

index 219ad1adafc3eef72332520efb3a893b42ed47c4..413dce36996a3ae5340032fbf87712976ab4c050 100644 (file)
@@ -7,11 +7,11 @@
  */
 package org.opendaylight.protocol.pcep.lsp.setup.type01;
 
-import com.google.common.base.Preconditions;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBoolean;
 
+import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
@@ -24,16 +24,17 @@ public class PathSetupTypeTlvParser implements TlvParser, TlvSerializer {
 
     // http://tools.ietf.org/html/draft-sivabalan-pce-segment-routing-01#section-9.3
     public static final int TYPE = 27;
+    private static final int CONTENT_LENGTH = 4;
     private static final int PST_LENGTH = 1;
-    private static final int OFFSET = 4 - PST_LENGTH;
+    private static final int OFFSET = CONTENT_LENGTH - PST_LENGTH;
 
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
-        Preconditions.checkNotNull(tlv, "PathSetupType is mandatory.");
+        Preconditions.checkArgument(tlv != null && tlv instanceof PathSetupType, "PathSetupType is mandatory.");
         final PathSetupType pstTlv = (PathSetupType) tlv;
-        ByteBuf body = Unpooled.buffer();
+        ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
         body.writeZero(OFFSET);
-        body.writeBoolean((pstTlv.isPst() != null) ? pstTlv.isPst() : false);
+        writeBoolean(pstTlv.isPst(), body);
         TlvUtil.formatTlv(TYPE, body, buffer);
     }
 
index e9f6d9400f98a8423e1e9339e220cb05573a205f..c16a5a1b6bb2e0f7a734e24b4eaf0271c9a98bb2 100644 (file)
@@ -8,8 +8,13 @@
 
 package org.opendaylight.protocol.pcep.segment.routing02;
 
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
+
 import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.BitSet;
@@ -18,6 +23,7 @@ import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
+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;
@@ -52,47 +58,49 @@ public class SrEroSubobjectParser implements EROSubobjectParser, EROSubobjectSer
 
     @Override
     public void serializeSubobject(Subobject subobject, final ByteBuf buffer) {
-        Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrEroSubobject, "Unknown subobject instance. Passed %s. Needed SrEroSubobject.", subobject.getSubobjectType().getClass());
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrEroSubobject,
+                "Unknown subobject instance. Passed %s. Needed SrEroSubobject.", subobject.getSubobjectType()
+                        .getClass());
 
         final SrEroSubobject srEroSubobject = (SrEroSubobject) subobject.getSubobjectType();
         final ByteBuf body = Unpooled.buffer(MINIMAL_LENGTH);
-        body.writeByte(UnsignedBytes.checkedCast(srEroSubobject.getSidType().getIntValue()) << 4);
+        writeUnsignedByte((short)(srEroSubobject.getSidType().getIntValue() << 4), body);
 
         final Flags flags = srEroSubobject.getFlags();
         final BitSet bits = new BitSet();
-        bits.set(M_FLAG_POSITION, flags.isM());
-        bits.set(C_FLAG_POSITION, flags.isC());
-        bits.set(S_FLAG_POSITION, flags.isS());
-        bits.set(F_FLAG_POSITION, flags.isF());
-        body.writeByte(ByteArray.bitSetToBytes(bits, FLAGS_OFFSET)[0]);
-
-        if(srEroSubobject.getSid() != null) {
-            body.writeInt(srEroSubobject.getSid().intValue());
+        if (flags != null) {
+            bits.set(M_FLAG_POSITION, flags.isM());
+            bits.set(C_FLAG_POSITION, flags.isC());
+            bits.set(S_FLAG_POSITION, flags.isS());
+            bits.set(F_FLAG_POSITION, flags.isF());
         }
+        writeBitSet(bits, FLAGS_OFFSET, body);
+
+        writeUnsignedInt(srEroSubobject.getSid(), body);
 
         final Nai nai = srEroSubobject.getNai();
-        if(nai != null) {
+        if (nai != null) {
             switch (srEroSubobject.getSidType()) {
             case Ipv4NodeId:
-                body.writeBytes(Ipv4Util.bytesForAddress(((IpNodeId)nai).getIpAddress().getIpv4Address()));
+                writeIpv4Address(((IpNodeId) nai).getIpAddress().getIpv4Address(), body);
                 break;
             case Ipv6NodeId:
-                body.writeBytes(Ipv6Util.bytesForAddress(((IpNodeId)nai).getIpAddress().getIpv6Address()));
+                writeIpv6Address(((IpNodeId) nai).getIpAddress().getIpv6Address(), body);
                 break;
             case Ipv4Adjacency:
-                body.writeBytes(Ipv4Util.bytesForAddress(((IpAdjacency)nai).getLocalIpAddress().getIpv4Address()));
-                body.writeBytes(Ipv4Util.bytesForAddress(((IpAdjacency)nai).getRemoteIpAddress().getIpv4Address()));
+                writeIpv4Address(((IpAdjacency) nai).getLocalIpAddress().getIpv4Address(), body);
+                writeIpv4Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv4Address(), body);
                 break;
             case Ipv6Adjacency:
-                body.writeBytes(Ipv6Util.bytesForAddress(((IpAdjacency)nai).getLocalIpAddress().getIpv6Address()));
-                body.writeBytes(Ipv6Util.bytesForAddress(((IpAdjacency)nai).getRemoteIpAddress().getIpv6Address()));
+                writeIpv6Address(((IpAdjacency) nai).getLocalIpAddress().getIpv6Address(), body);
+                writeIpv6Address(((IpAdjacency) nai).getRemoteIpAddress().getIpv6Address(), body);
                 break;
             case Unnumbered:
-                final UnnumberedAdjacency unnumbered = (UnnumberedAdjacency)nai;
-                body.writeInt(unnumbered.getLocalNodeId().intValue());
-                body.writeInt(unnumbered.getLocalInterfaceId().intValue());
-                body.writeInt(unnumbered.getRemoteNodeId().intValue());
-                body.writeInt(unnumbered.getRemoteInterfaceId().intValue());
+                final UnnumberedAdjacency unnumbered = (UnnumberedAdjacency) nai;
+                ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalNodeId(), body);
+                ByteBufWriteUtil.writeUnsignedInt(unnumbered.getLocalInterfaceId(), body);
+                ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteNodeId(), body);
+                ByteBufWriteUtil.writeUnsignedInt(unnumbered.getRemoteInterfaceId(), body);
                 break;
             }
         }
@@ -122,21 +130,24 @@ public class SrEroSubobjectParser implements EROSubobjectParser, EROSubobjectSer
 
         final long sid = buffer.readUnsignedInt();
         srEroSubobjectBuilder.setSid(sid);
-        if(sidType != null) {
+        if (sidType != null) {
             switch (sidType) {
             case Ipv4NodeId:
-                srEroSubobjectBuilder.setNai(new IpNodeIdBuilder()
-                        .setIpAddress(new IpAddress(new Ipv4Address(Ipv4Util.addressForByteBuf(buffer)))).build());
+                srEroSubobjectBuilder.setNai(new IpNodeIdBuilder().setIpAddress(
+                        new IpAddress(new Ipv4Address(Ipv4Util.addressForByteBuf(buffer)))).build());
                 break;
             case Ipv6NodeId:
-                srEroSubobjectBuilder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build());
+                srEroSubobjectBuilder.setNai(new IpNodeIdBuilder().setIpAddress(
+                        new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build());
                 break;
             case Ipv4Adjacency:
-                srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)))
+                srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder()
+                        .setLocalIpAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)))
                         .setRemoteIpAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer))).build());
                 break;
             case Ipv6Adjacency:
-                srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder().setLocalIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)))
+                srEroSubobjectBuilder.setNai(new IpAdjacencyBuilder()
+                        .setLocalIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)))
                         .setRemoteIpAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer))).build());
                 break;
             case Unnumbered:
index fffeab667605094868235172bdacbef76dcde7e5..d3afbae1aa48d49bd739c0d02298f8016faee6e7 100644 (file)
@@ -7,11 +7,11 @@
  */
 package org.opendaylight.protocol.pcep.segment.routing02;
 
-import com.google.common.base.Preconditions;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
 
+import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
@@ -25,12 +25,16 @@ public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
     public static final int TYPE = 26;
 
     private static final int MSD_LENGTH = 1;
-    private static final int OFFSET = 4 - MSD_LENGTH;
+    private static final int CONTENT_LENGTH = 4;
+    private static final int OFFSET = CONTENT_LENGTH - MSD_LENGTH;
 
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
-        Preconditions.checkNotNull(tlv, "SrPceCapability is mandatory.");
-        TlvUtil.formatTlv(TYPE, Unpooled.copyInt(((SrPceCapability) tlv).getMsd()), buffer);
+        Preconditions.checkArgument(tlv != null && tlv instanceof SrPceCapability, "SrPceCapability is mandatory.");
+        final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        body.writerIndex(OFFSET);
+        writeUnsignedByte(((SrPceCapability) tlv).getMsd(), body);
+        TlvUtil.formatTlv(TYPE, body, buffer);
     }
 
     @Override