BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpv4PrefixSubobjectParser.java
index 54cebdb1350f985db0f88b81739f59ebb7eafc42..3ca7d960416a4e10f03c9a2abd45d6c5e293cb12 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.impl.subobject;
 
+import io.netty.buffer.ByteBuf;
+
 import org.opendaylight.protocol.concepts.Ipv4Util;
 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
@@ -21,6 +23,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.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
 
+import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
 
 /**
@@ -38,19 +41,16 @@ public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubo
        private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1;
 
        @Override
-       public Subobject 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.");
-               }
+       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.");
                final SubobjectBuilder builder = new SubobjectBuilder();
                builder.setLoose(loose);
-
-               if (buffer.length != CONTENT4_LENGTH) {
-                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
+               if (buffer.readableBytes() != CONTENT4_LENGTH) {
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
                }
-               final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
-               builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
-                               new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build()).build());
+               final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
+               IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH), length)));
+               builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
                return builder.build();
        }