BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / AbstractEROWithSubobjectsParser.java
index 5fd58c8f826b41a431bb2e235af2e34a83fc2023..38a471151a3f8cf75ced36cf095c002f0e61b818 100644 (file)
@@ -7,30 +7,29 @@
  */
 package org.opendaylight.protocol.pcep.impl.object;
 
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+
+import java.util.ArrayList;
 import java.util.List;
 
 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
 import org.opendaylight.protocol.pcep.spi.ObjectParser;
 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
 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.ero.Subobject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
+import com.google.common.primitives.UnsignedBytes;
 
 public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
 
        private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
 
-       private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
-       private static final int SUB_LENGTH_F_LENGTH = 1;
-
-       private static final int TYPE_FLAG_F_OFFSET = 0;
-       private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
-       private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
+       private static final int HEADER_LENGTH = 2;
 
        private final EROSubobjectRegistry subobjReg;
 
@@ -38,43 +37,26 @@ public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, O
                this.subobjReg = Preconditions.checkNotNull(subobjReg);
        }
 
-       protected List<Subobject> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
-               if (bytes == null) {
-                       throw new IllegalArgumentException("Byte array is mandatory.");
-               }
-
-               boolean loose = false;
-               int type;
-               byte[] soContentsBytes;
-               int length;
-               int offset = 0;
-
-               final List<Subobject> subs = Lists.newArrayList();
-
-               while (offset < bytes.length) {
-
-                       loose = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
-                       length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
-
-                       type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
-
-                       if (length > bytes.length - offset) {
+       protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
+               Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+               final List<Subobject> subs = new ArrayList<>();
+               while (buffer.isReadable()) {
+                       boolean loose = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
+                       int type = (buffer.readByte() & 0xff) & ~(1 << 7);
+                       int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
+                       if (length > buffer.readableBytes()) {
                                throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
-                                               + (bytes.length - offset));
+                                               + buffer.readableBytes());
                        }
-
-                       soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
-                       System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
-
-                       LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
-                       final Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, loose);
+                       LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
+                       final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), loose);
                        if (sub == null) {
                                LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
                        } else {
                                LOG.debug("Subobject was parsed. {}", sub);
                                subs.add(sub);
                        }
-                       offset += length;
+                       buffer.readerIndex(buffer.readerIndex() + length);
                }
                return subs;
        }