Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / RedirectIpv4EcHandler.java
index ca4a50e491022f38fbaed935e0dc8c04cf6725b8..3c3cf7ec4b8d477c9fbab88518f0f703691f3369 100644 (file)
@@ -5,43 +5,43 @@
  * 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.bgp.flowspec.extended.communities;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
 import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ipv4.extended.community.RedirectIpv4;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ipv4.extended.community.RedirectIpv4Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectIpv4ExtendedCommunityCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public class RedirectIpv4EcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
-
     private static final int TYPE = 129;
-
     private static final int SUBTYPE = 8;
 
     @Override
     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
-        Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.RedirectIpv4ExtendedCommunity,
+        checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
+            .flowspec.rev180329.RedirectIpv4ExtendedCommunity,
                 "The extended community %s is not RedirectIpv4ExtendedCommunityCase type.", extendedCommunity);
-        final RedirectIpv4 redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.RedirectIpv4ExtendedCommunity) extendedCommunity).getRedirectIpv4();
-        ByteBufWriteUtil.writeIpv4Address(redirect.getGlobalAdministrator(), byteAggregator);
-        ByteBufWriteUtil.writeUnsignedShort(redirect.getLocalAdministrator(), byteAggregator);
+        final RedirectIpv4 redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec
+                .rev180329.RedirectIpv4ExtendedCommunity) extendedCommunity).getRedirectIpv4();
+        Ipv4Util.writeIpv4Address(redirect.getGlobalAdministrator(), byteAggregator);
+        ByteBufUtils.writeOrZero(byteAggregator, redirect.getLocalAdministrator());
     }
 
     @Override
-    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
-        final RedirectIpv4Builder builder = new RedirectIpv4Builder();
-        builder.setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer));
-        builder.setLocalAdministrator(buffer.readUnsignedShort());
-        return new RedirectIpv4ExtendedCommunityCaseBuilder().setRedirectIpv4(builder.build()).build();
+    public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
+        return new RedirectIpv4ExtendedCommunityCaseBuilder()
+                .setRedirectIpv4(new RedirectIpv4Builder()
+                    .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
+                    .setLocalAdministrator(ByteBufUtils.readUint16(buffer))
+                    .build())
+                .build();
     }
 
     @Override
@@ -53,5 +53,4 @@ public class RedirectIpv4EcHandler implements ExtendedCommunityParser, ExtendedC
     public int getSubType() {
         return SUBTYPE;
     }
-
 }