Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / RROIpv4PrefixSubobjectParser.java
index d0332feeb0f9c2acc31cd197a605173519922ce0..ef81d3535c4baa37e61905bcd2027a8caa4c008c 100644 (file)
@@ -5,12 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.rsvp.parser.impl.subobject.rro;
 
-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.RROSubobjectParser;
@@ -20,6 +18,7 @@ import org.opendaylight.protocol.util.BitArray;
 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.record.route.subobjects.list.SubobjectContainer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
@@ -46,8 +45,7 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
 
     @Override
     public SubobjectContainer parseSubobject(final ByteBuf buffer) 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.");
         if (buffer.readableBytes() != CONTENT4_LENGTH) {
             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
         }
@@ -66,24 +64,23 @@ public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubo
 
     @Override
     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
-        Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
-            "Unknown subobject instance. Passed %s. Needed IpPrefixCase.",
-            subobject.getSubobjectType().getClass());
+        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 RROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
-        } else {
-            final BitArray flags = new BitArray(FLAGS_SIZE);
-            flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
-            flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
-            final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
-            Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
-            writeIpv4Prefix(prefix.getIpv4Prefix(), body);
-            flags.toByteBuf(body);
-            RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
+        final Ipv6Prefix ipv6Prefix = prefix.getIpv6Prefix();
+        if (ipv6Prefix != null) {
+            RROIpv6PrefixSubobjectParser.serializeSubobject(buffer, subobject, ipv6Prefix);
+            return;
         }
+
+        final BitArray flags = new BitArray(FLAGS_SIZE);
+        flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
+        flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
+        final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
+        checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
+        Ipv4Util.writeIpv4Prefix(prefix.getIpv4Prefix(), body);
+        flags.toByteBuf(body);
+        RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
     }
 }