Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROIpv4PrefixSubobjectParser.java
index ff21296293aabda103339fd70f9ebe856e66b0cc..a6c92bd18e08ff3c4d69eb6a7135f711de73ca8b 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.protocol.rsvp.parser.impl.subobject.ero;
 
-import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
+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.rsvp.parser.spi.EROSubobjectParser;
@@ -19,6 +18,7 @@ import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.Ipv4Util;
 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.rsvp.rev150820.IpPrefixSubobject;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
@@ -30,19 +30,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
  * Parser for {@link IpPrefixCase}.
  */
 public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
-
     public static final int TYPE = 1;
 
     private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
-
     private static final int RESERVED = 1;
-
     private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + RESERVED + 1;
 
     @Override
     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
-        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.");
         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
         builder.setLoose(loose);
         if (buffer.readableBytes() != CONTENT4_LENGTH) {
@@ -57,21 +53,21 @@ public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubo
 
     @Override
     public void serializeSubobject(final SubobjectContainer 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();
-        Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null,
-            "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
-        if (prefix.getIpv6Prefix() != null) {
-            new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
-        } else {
-            final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
-            Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
-            writeIpv4Prefix(prefix.getIpv4Prefix(), body);
-            body.writeZero(RESERVED);
-            EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
+        final Ipv6Prefix ipv6prefix = prefix.getIpv6Prefix();
+        if (ipv6prefix != null) {
+            EROIpv6PrefixSubobjectParser.serializeSubobject(buffer, subobject, ipv6prefix);
+            return;
         }
+
+        final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
+        checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
+        Ipv4Util.writeIpv4Prefix(prefix.getIpv4Prefix(), body);
+        body.writeZero(RESERVED);
+        EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
     }
 }