BUG-612 : switched Labels to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROLabelSubobjectParser.java
index b266e06404bd9f0914ba2baeffc28de27a29484f..274492fda833d7ec57a25b9eb11de92be7ad55cf 100644 (file)
@@ -7,20 +7,22 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
+
 import java.util.BitSet;
 
-import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+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.LabelHandlerRegistry;
-import org.opendaylight.protocol.pcep.spi.LabelParser;
-import org.opendaylight.protocol.pcep.spi.LabelSerializer;
+import org.opendaylight.protocol.pcep.spi.LabelRegistry;
+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.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.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.Label;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelBuilder;
+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.basic.explicit.route.subobjects.subobject.type.LabelCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
 
 import com.google.common.base.Preconditions;
@@ -42,63 +44,41 @@ public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobject
 
        private static final int U_FLAG_OFFSET = 0;
 
-       private final LabelHandlerRegistry registry;
+       private final LabelRegistry registry;
 
-       public EROLabelSubobjectParser(final LabelHandlerRegistry labelReg) {
+       public EROLabelSubobjectParser(final LabelRegistry labelReg) {
                this.registry = Preconditions.checkNotNull(labelReg);
        }
 
        @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 < HEADER_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() < HEADER_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
                                        + HEADER_LENGTH + ".");
                }
-               final BitSet reserved = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, RES_F_OFFSET, RES_F_LENGTH));
-
-               final short cType = (short) UnsignedBytes.toInt(buffer[C_TYPE_F_OFFSET]);
+               final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
+               final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
 
-               final LabelParser parser = this.registry.getLabelParser(cType);
-
-               if (parser == null) {
+               final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
+               if (labelType == null) {
                        throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
                }
                final LabelBuilder builder = new LabelBuilder();
                builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
-               builder.setLabelType(parser.parseLabel(ByteArray.cutBytes(buffer, HEADER_LENGTH)));
-               return new SubobjectsBuilder().setLoose(loose).setSubobjectType(builder.build()).build();
+               builder.setLabelType(labelType);
+               return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
        }
 
        @Override
-       public byte[] serializeSubobject(final Subobjects subobject) {
+       public byte[] serializeSubobject(final Subobject subobject) {
                Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
-
-               final Label label = (Label) subobject.getSubobjectType();
-               final LabelType l = label.getLabelType();
-
-               final LabelSerializer serializer = this.registry.getLabelSerializer(l);
-
-               if (serializer == null) {
+               final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
+               final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
+               if (labelbytes == null) {
                        throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
                                        + label.getLabelType().getImplementedInterface());
                }
-               final byte[] labelbytes = serializer.serializeLabel(l);
-               final byte[] retBytes = new byte[labelbytes.length + HEADER_LENGTH];
-
-               System.arraycopy(labelbytes, 0, retBytes, HEADER_LENGTH, labelbytes.length);
-
-               final BitSet reserved = new BitSet();
-               reserved.set(U_FLAG_OFFSET, label.isUniDirectional());
-               System.arraycopy(ByteArray.bitSetToBytes(reserved, RES_F_LENGTH), 0, retBytes, RES_F_OFFSET, RES_F_LENGTH);
-               retBytes[C_TYPE_F_OFFSET] = UnsignedBytes.checkedCast(serializer.getType());
-               return retBytes;
-       }
-
-       @Override
-       public int getType() {
-               return TYPE;
+               return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);
        }
 }