Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / EROIpv6PrefixSubobjectParser.java
index 8eeccaebbb5dc69ee0c532c52159b94f5920b166..274e1a3769a993e3732f557deedd5b49826f8c55 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.protocol.pcep.parser.subobject;
 
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Prefix;
+import static com.google.common.base.Preconditions.checkArgument;
 
-import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
@@ -19,6 +18,7 @@ import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
@@ -30,19 +30,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
  * Parser for {@link IpPrefixCase}.
  */
 public class EROIpv6PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
-
     public static final int TYPE = 2;
 
     private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
-
     private static final int RESERVED = 1;
-
     private static final int CONTENT_LENGTH = PREFIX_F_OFFSET + RESERVED + 1;
 
     @Override
     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.");
+        checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
         if (buffer.readableBytes() != CONTENT_LENGTH) {
             throw new PCEPDeserializerException(
                     "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
@@ -58,13 +54,17 @@ public class EROIpv6PrefixSubobjectParser implements EROSubobjectParser, EROSubo
 
     @Override
     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
-        Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
+        checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
                 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
-        final IpPrefix prefix = specObj.getIpPrefix();
+        final Ipv6Prefix ipv6prefix = specObj.getIpPrefix().getIpv6Prefix();
+        checkArgument(ipv6prefix != null, "Ipv6Prefix is mandatory.");
+        serializeSubobject(buffer, subobject, ipv6prefix);
+    }
+
+    static void serializeSubobject(final ByteBuf buffer, final Subobject subobject, final Ipv6Prefix ipv6prefix) {
         final ByteBuf body = Unpooled.buffer();
-        Preconditions.checkArgument(prefix.getIpv6Prefix() != null, "Ipv6Prefix is mandatory.");
-        writeIpv6Prefix(prefix.getIpv6Prefix(), body);
+        Ipv6Util.writeIpv6Prefix(ipv6prefix, body);
         body.writeZero(RESERVED);
         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
     }