BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07StatefulCapabilityTlvParser.java
index ecddf39e2423674fc1a978fcfbe52dffd0a0e545..ba863fbedc55f6cb3ec282d3daec33683a30201e 100644 (file)
@@ -7,17 +7,14 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
-
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.util.BitSet;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
 import org.opendaylight.protocol.pcep.spi.TlvUtil;
-import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.BitArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
@@ -29,7 +26,7 @@ public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSeri
 
     public static final int TYPE = 16;
 
-    protected static final int FLAGS_F_LENGTH = 4;
+    protected static final int FLAGS_F_LENGTH = 32;
 
     protected static final int U_FLAG_OFFSET = 31;
 
@@ -38,12 +35,11 @@ public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSeri
         if (buffer == null) {
             return null;
         }
-        if (buffer.readableBytes() < FLAGS_F_LENGTH) {
+        if (buffer.readableBytes() < FLAGS_F_LENGTH / Byte.SIZE) {
             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
-                    + FLAGS_F_LENGTH + ".");
+                    + FLAGS_F_LENGTH / Byte.SIZE + ".");
         }
-        final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
-
+        final BitArray flags = BitArray.valueOf(buffer, FLAGS_F_LENGTH);
         final StatefulBuilder sb = new StatefulBuilder();
         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
         return sb.build();
@@ -53,12 +49,8 @@ public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSeri
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
         Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
         final Stateful sct = (Stateful) tlv;
-        final ByteBuf body = Unpooled.buffer();
-        final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-        if (sct.isLspUpdateCapability() != null) {
-            flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
-        }
-        writeBitSet(flags, FLAGS_F_LENGTH, body);
-        TlvUtil.formatTlv(TYPE, body, buffer);
+        final BitArray flags = new BitArray(FLAGS_F_LENGTH);
+        flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
+        TlvUtil.formatTlv(TYPE, Unpooled.wrappedBuffer(flags.array()), buffer);
     }
 }