Getting rid of readerIndex handling.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROUnnumberedInterfaceSubobjectParser.java
index d67be93d870b891c7c2be69052f8b4e359b8e19a..07306296077dfdfa6cddfbbe81bb1e9a671c52a5 100644 (file)
@@ -7,69 +7,77 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
+
+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.ByteArray;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.Subobjects;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.SubobjectsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedBuilder;
-
-import com.google.common.primitives.UnsignedInts;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
 
 /**
- * Parser for {@link org.opendaylight.protocol.pcep.subobject.RROUnnumberedInterfaceSubobject
- * RROUnnumberedInterfaceSubobject}
+ * Parser for {@link UnnumberedCase}
  */
 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
 
-       public static final int TYPE = 4;
-
-       public static final int ROUTER_ID_NUMBER_LENGTH = 4;
-       public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
-
-       public static final int ROUTER_ID_NUMBER_OFFSET = 2; // added reserved field of size 2
-       public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
-
-       public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
-
-       @Override
-       public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
-               if (buffer == null || buffer.length == 0)
-                       throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
-               if (buffer.length != CONTENT_LENGTH)
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
-                                       + CONTENT_LENGTH + ".");
-
-               final SubobjectsBuilder builder = new SubobjectsBuilder();
-               final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
-               ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
-               ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
-                               INTERFACE_ID_NUMBER_LENGTH))));
-               builder.setSubobjectType(ubuilder.build());
-               return builder.build();
-       }
+    public static final int TYPE = 4;
 
-       @Override
-       public byte[] serializeSubobject(final Subobjects subobject) {
-               if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject))
-                       throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
-                                       + ". Needed UnnumberedSubobject.");
+    private static final int FLAGS_F_LENGTH = 1;
+    private static final int RESERVED = 1;
 
-               byte[] retBytes;
-               retBytes = new byte[CONTENT_LENGTH];
-               final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
+    private static final int CONTENT_LENGTH = 10;
 
-               ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId()), retBytes, ROUTER_ID_NUMBER_OFFSET);
-               System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
-                               INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
+    private static final int LPA_F_OFFSET = 7;
+    private static final int LPIU_F_OFFSET = 6;
 
-               return retBytes;
-       }
+    @Override
+    public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
+        Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        if (buffer.readableBytes() != CONTENT_LENGTH) {
+            throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
+                    + CONTENT_LENGTH + ".");
+        }
+        final SubobjectBuilder builder = new SubobjectBuilder();
+        final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
+        builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
+        builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
+        final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
+        buffer.skipBytes(RESERVED);
+        ubuilder.setRouterId(buffer.readUnsignedInt());
+        ubuilder.setInterfaceId(buffer.readUnsignedInt());
+        builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
+        return builder.build();
+    }
 
-       @Override
-       public int getType() {
-               return TYPE;
-       }
+    @Override
+    public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
+        Preconditions.checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
+        final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
+        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 ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
+        writeBitSet(flags, FLAGS_F_LENGTH, body);
+        body.writeZero(RESERVED);
+        Preconditions.checkArgument(specObj.getRouterId() != null, "RouterId is mandatory.");
+        writeUnsignedInt(specObj.getRouterId(), body);
+        Preconditions.checkArgument(specObj.getInterfaceId() != null, "InterfaceId is mandatory.");
+        writeUnsignedInt(specObj.getInterfaceId(), body);
+        RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
+    }
 }