BUG-612 : switched RRO subobjects to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIpv6PrefixSubobjectParser.java
index 87c959f10908fc388b3ae7ed5af241c7e68ed86a..1fa07834c4ec5bcfc8fd3d93b950e95445ded899 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
-import java.util.Arrays;
+import io.netty.buffer.ByteBuf;
+
 import java.util.BitSet;
 
 import org.opendaylight.protocol.concepts.Ipv4Util;
@@ -25,6 +26,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
 
+import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 /**
@@ -50,21 +52,19 @@ public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubo
        private static final int LPIU_F_OFFSET = 6;
 
        @Override
-       public Subobject parseSubobject(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.");
-               }
+       public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
+               Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
                final SubobjectBuilder builder = new SubobjectBuilder();
-               if (buffer.length != CONTENT_LENGTH) {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
+               if (buffer.readableBytes() != CONTENT_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
                }
-               final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
-               final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
+               final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX_F_OFFSET));
+               IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, IP_F_LENGTH), length)));
+               buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
+               final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
                builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
                builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
-               builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
-                               new IpPrefixBuilder().setIpPrefix(
-                                               new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP_F_LENGTH), length))).build()).build());
+               builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
                return builder.build();
        }