BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROAsNumberSubobjectParser.java
index d7b4dfa4adffb8bab1b355290ff0c7a1ce6155bb..a3b3a2bceefd1785a63d0875f774f4ba11886fd8 100644 (file)
@@ -7,19 +7,25 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+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.Subobjects;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
+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.basic.explicit.route.subobjects.SubobjectType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.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 {
 
@@ -32,38 +38,31 @@ public class EROAsNumberSubobjectParser implements EROSubobjectParser, EROSubobj
        public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
 
        @Override
-       public Subobjects parseSubobject(final byte[] buffer, final boolean loose) 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: "
+       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 SubobjectsBuilder().setLoose(loose).setSubobjectType(
-                               new AsNumberBuilder().setAsNumber(new AsNumber(ByteArray.bytesToLong(buffer))).build()).build();
+               return new SubobjectBuilder().setLoose(loose).setSubobjectType(
+                               new AsNumberCaseBuilder().setAsNumber(
+                                               new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build()).build();
        }
 
        @Override
-       public byte[] serializeSubobject(final Subobjects subobject) {
-               if (!(subobject.getSubobjectType() instanceof AsNumberSubobject)) {
+       public byte[] serializeSubobject(final Subobject subobject) {
+               if (!(subobject.getSubobjectType() instanceof AsNumberCase)) {
                        throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
-                                       + ". Needed AsNumberSubobject.");
+                                       + ". Needed AsNumberCase.");
                }
 
                final byte[] retBytes = new byte[CONTENT_LENGTH];
 
-               final SubobjectType s = subobject.getSubobjectType();
+               final AsNumberSubobject s = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
 
-               System.arraycopy(ByteArray.longToBytes(((AsNumberSubobject) s).getAsNumber().getValue()), Long.SIZE / Byte.SIZE - AS_NUMBER_LENGTH,
-                               retBytes, AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
+               System.arraycopy(ByteArray.longToBytes(s.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
+                               AS_NUMBER_LENGTH);
 
-               return retBytes;
-       }
-
-       @Override
-       public int getType() {
-               return TYPE;
+               return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
        }
 }