BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROAsNumberSubobjectParser.java
index 7ef9fe05c8f5ad1c2c9b9a5e8f3c2ae29d52c4ba..a3b3a2bceefd1785a63d0875f774f4ba11886fd8 100644 (file)
@@ -7,54 +7,62 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.SubobjectParser;
-import org.opendaylight.protocol.pcep.spi.SubobjectSerializer;
+import io.netty.buffer.ByteBuf;
+
+import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
+import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AsNumberSubobject;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CSubobject;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.route.subobjects.subobject.type.AsNumberBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
+
+import com.google.common.base.Preconditions;
 
 /**
- * Parser for {@link AsNumberSubobject}
+ * Parser for {@link AsNumberCase}
  */
+public class EROAsNumberSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
 
-public class EROAsNumberSubobjectParser implements SubobjectParser, SubobjectSerializer {
-       
        public static final int TYPE = 32;
-       
-       public static final int AS_NUMBER_LENGTH = 4;
+
+       public static final int AS_NUMBER_LENGTH = 2;
 
        public static final int AS_NUMBER_OFFSET = 0;
 
        public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
 
-       public AsNumberSubobject parseSubobject(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: "
+       @Override
+       public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) 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 + ".");
-
-               return new AsNumberBuilder().setAsNumber(new AsNumber(ByteArray.bytesToLong(buffer))).build();
+               }
+               return new SubobjectBuilder().setLoose(loose).setSubobjectType(
+                               new AsNumberCaseBuilder().setAsNumber(
+                                               new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build()).build();
        }
 
-       public byte[] serializeSubobject(CSubobject subobject) {
-               if (!(subobject instanceof AsNumberSubobject))
-                       throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getClass()
-                                       + ". Needed AsNumberSubobject.");
+       @Override
+       public byte[] serializeSubobject(final Subobject subobject) {
+               if (!(subobject.getSubobjectType() instanceof AsNumberCase)) {
+                       throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
+                                       + ". Needed AsNumberCase.");
+               }
 
                final byte[] retBytes = new byte[CONTENT_LENGTH];
 
-               System.arraycopy(ByteArray.longToBytes(((AsNumberSubobject) subobject).getAsNumber().getValue()), Long.SIZE / Byte.SIZE
-                               - AS_NUMBER_LENGTH, retBytes, AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
+               final AsNumberSubobject s = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
 
-               return retBytes;
-       }
+               System.arraycopy(ByteArray.longToBytes(s.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
+                               AS_NUMBER_LENGTH);
 
-       @Override
-       public int getType() {
-               return TYPE;
+               return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
        }
 }