Bug-2230: Revision of module RSVP
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07LSPIdentifierIpv6TlvParser.java
index e155d87f8357e35a1a99ede0edc783329a8a3df5..3d53c3eb4f56f007c04d3d593045ac557f8ff990 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
+import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -14,7 +17,6 @@ import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
 import org.opendaylight.protocol.pcep.spi.TlvParser;
 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
 import org.opendaylight.protocol.pcep.spi.TlvUtil;
-import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
@@ -24,9 +26,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.iet
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.Ipv6ExtendedTunnelId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.TunnelId;
 
 /**
  * Parser for {@link LspIdentifiers}
@@ -35,8 +37,6 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl
 
     public static final int TYPE = 19;
 
-    private static final int EX_TUNNEL_ID6_F_LENGTH = 16;
-
     private static final int V6_LENGTH = 52;
 
     @Override
@@ -44,31 +44,33 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl
         if (buffer == null) {
             return null;
         }
-        if (buffer.readableBytes() != V6_LENGTH) {
-            throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv6 tlv length.");
-        }
+        Preconditions.checkArgument(buffer.readableBytes() == V6_LENGTH, "Length %s does not match LSP Identifiers Ipv6 tlv length.", buffer.readableBytes());
         final Ipv6Builder builder = new Ipv6Builder();
-        builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
+        builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForByteBuf(buffer));
         final LspId lspId = new LspId((long) buffer.readUnsignedShort());
         final TunnelId tunnelId = new TunnelId(buffer.readUnsignedShort());
-        builder.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer,
-                EX_TUNNEL_ID6_F_LENGTH))));
-        builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH)));
+        builder.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForByteBuf(buffer)));
+        builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForByteBuf(buffer));
         final AddressFamily afi = new Ipv6CaseBuilder().setIpv6(builder.build()).build();
         return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
     }
 
     @Override
     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
-        Preconditions.checkArgument(tlv != null, "LspIdentifiersTlv is mandatory.");
+        Preconditions.checkArgument(tlv instanceof LspIdentifiers, "LspIdentifiersTlv is mandatory.");
         final LspIdentifiers lsp = (LspIdentifiers) tlv;
         final ByteBuf body = Unpooled.buffer();
         final Ipv6 ipv6 = ((Ipv6Case) lsp.getAddressFamily()).getIpv6();
-        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()));
-        body.writeShort(lsp.getLspId().getValue().shortValue());
-        body.writeShort(lsp.getTunnelId().getValue().shortValue());
-        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()));
-        body.writeBytes(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()));
+        Preconditions.checkArgument(ipv6.getIpv6TunnelSenderAddress() != null, "Ipv6TunnelSenderAddress is mandatory.");
+        writeIpv6Address(ipv6.getIpv6TunnelSenderAddress(), body);
+        Preconditions.checkArgument(lsp.getLspId() != null, "LspId is mandatory.");
+        writeShort(lsp.getLspId().getValue().shortValue(), body);
+        Preconditions.checkArgument(lsp.getTunnelId() != null, "TunnelId is mandatory.");
+        writeUnsignedShort(lsp.getTunnelId().getValue(), body);
+        Preconditions.checkArgument(ipv6.getIpv6ExtendedTunnelId() != null, "Ipv6ExtendedTunnelId is mandatory.");
+        writeIpv6Address(ipv6.getIpv6ExtendedTunnelId(), body);
+        Preconditions.checkArgument(ipv6.getIpv6TunnelEndpointAddress() != null, "Ipv6TunnelEndpointAddress is mandatory.");
+        writeIpv6Address(ipv6.getIpv6TunnelEndpointAddress(), body);
         TlvUtil.formatTlv(TYPE, body, buffer);
     }
 }