BUG-612 : switched Labels to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / Type1LabelParser.java
index 4e538b7629c0ef0b983ce73cfe1732210f17a7fa..a0d8f6d3874471caac497ed54d82976fbfb9d095 100644 (file)
@@ -7,16 +7,23 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.pcep.spi.LabelParser;
 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
+import org.opendaylight.protocol.pcep.spi.LabelUtil;
+import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1Label;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
 
-import com.google.common.primitives.UnsignedInts;
+import com.google.common.base.Preconditions;
 
+/**
+ * Parser for {@link Type1LabelCase}
+ */
 public class Type1LabelParser implements LabelParser, LabelSerializer {
 
        public static final int CTYPE = 1;
@@ -24,27 +31,22 @@ public class Type1LabelParser implements LabelParser, LabelSerializer {
        public static final int LABEL_LENGTH = 4;
 
        @Override
-       public LabelType parseLabel(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 != LABEL_LENGTH) {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: " + LABEL_LENGTH
+       public LabelType parseLabel(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() != LABEL_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + LABEL_LENGTH
                                        + ".");
                }
-               return new Type1LabelBuilder().setType1Label(UnsignedInts.toLong(ByteArray.bytesToInt(buffer))).build();
+               return new Type1LabelCaseBuilder().setType1Label(
+                               new Type1LabelBuilder().setType1Label(buffer.readUnsignedInt()).build()).build();
        }
 
        @Override
-       public byte[] serializeLabel(final LabelType subobject) {
-               if (!(subobject instanceof Type1Label)) {
-                       throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass() + ". Needed Type1Label.");
+       public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
+               if (!(subobject instanceof Type1LabelCase)) {
+                       throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
+                                       + ". Needed Type1LabelCase.");
                }
-               return ByteArray.subByte(ByteArray.longToBytes(((Type1Label) subobject).getType1Label().longValue()), 4, LABEL_LENGTH);
-       }
-
-       @Override
-       public int getType() {
-               return CTYPE;
+               return LabelUtil.formatLabel(CTYPE, unidirectional, global, ByteArray.longToBytes(((Type1LabelCase) subobject).getType1Label().getType1Label().longValue(), LABEL_LENGTH));
        }
 }