BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIpv4PrefixSubobjectParser.java
index 0e0788cf08b4234f584ef96a2cf7dc8b4c1fd189..85ecd63e0831569a6e30f7d38224e460612153a4 100644 (file)
@@ -7,17 +7,16 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
 
 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.RROSubobjectParser;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
+import org.opendaylight.protocol.util.BitArray;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
@@ -36,11 +35,11 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
     public static final int TYPE = 1;
 
     private static final int PREFIX_F_LENGTH = 1;
-    private static final int FLAGS_F_LENGTH = 1;
+    private static final int FLAGS_SIZE = 8;
 
     private static final int PREFIX4_F_OFFSET = 0 + Ipv4Util.IP4_LENGTH;
 
-    private static final int CONTENT4_LENGTH = Ipv4Util.IP4_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
+    private static final int CONTENT4_LENGTH = Ipv4Util.IP4_LENGTH + PREFIX_F_LENGTH + FLAGS_SIZE / Byte.SIZE;
 
     private static final int LPA_F_OFFSET = 7;
     private static final int LPIU_F_OFFSET = 6;
@@ -56,7 +55,7 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefix prefix = new IpPrefixBuilder().setIpPrefix(
                 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))).build();
         buffer.skipBytes(PREFIX_F_LENGTH);
-        final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
+        final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix).build());
@@ -72,17 +71,13 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
         if (prefix.getIpv6Prefix() != null) {
             new RROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
         } else {
-            final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
-            if (subobject.isProtectionAvailable() != null) {
-                flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
-            }
-            if (subobject.isProtectionInUse() != null) {
-                flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
-            }
+            final BitArray flags = new BitArray(FLAGS_SIZE);
+            flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
+            flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
             final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
             Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
             writeIpv4Prefix(prefix.getIpv4Prefix(), body);
-            writeBitSet(flags, FLAGS_F_LENGTH, body);
+            flags.toByteBuf(body);
             RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
         }
     }