Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / LSPIdentifierIPv6TlvParser.java
index 11206a5fbb2ecfbb8a4decf76f8a3231a64ec061..0133e85b55b97e15e4fce0b8cd31e258dce34a49 100644 (file)
@@ -7,47 +7,57 @@
  */
 package org.opendaylight.protocol.pcep.impl.tlv;
 
-import org.opendaylight.protocol.concepts.Ipv6Util;
+import org.opendaylight.protocol.concepts.IPv6Address;
 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.TlvParser;
+import org.opendaylight.protocol.pcep.concepts.IPv6ExtendedTunnelIdentifier;
+import org.opendaylight.protocol.pcep.concepts.LSPIdentifier;
+import org.opendaylight.protocol.pcep.concepts.TunnelIdentifier;
+import org.opendaylight.protocol.pcep.tlv.IPv6LSPIdentifiersTlv;
+import org.opendaylight.protocol.pcep.tlv.LSPIdentifiersTlv;
 import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspIdentifiersTlv;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.address.family.Ipv6Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.tlvs.LspIdentifiersBuilder;
-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;
 
 /**
- * Parser for {@link LspIdentifiersTlv}
+ * Parser for {@link org.opendaylight.protocol.pcep.tlv.LSPIdentifiersTlv LSPIdentifiersTlv}
+ * parameterized as IPv6Address
  */
-public class LSPIdentifierIPv6TlvParser implements TlvParser {
-
-       public static final int TYPE = 19;
+public class LSPIdentifierIPv6TlvParser {
 
        private static final int IP_F_LENGTH = 16;
        private static final int LSP_ID_F_LENGTH = 2;
        private static final int TUNNEL_ID_F_LENGTH = 2;
        private static final int EX_TUNNEL_ID_F_LENGTH = 16;
 
-       @Override
-       public LspIdentifiersTlv parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
-               int position = 0;
+       private static final int IP_F_OFFSET = 0;
+       private static final int LSP_ID_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
+       private static final int TUNNLE_ID_F_OFFSET = LSP_ID_F_OFFSET + LSP_ID_F_LENGTH;
+       private static final int EX_TUNNEL_ID_F_OFFSET = TUNNLE_ID_F_OFFSET + TUNNEL_ID_F_LENGTH;
+
+       private static final int SIZE = EX_TUNNEL_ID_F_OFFSET + EX_TUNNEL_ID_F_LENGTH;
+
+       public static LSPIdentifiersTlv<IPv6Address> parse(byte[] valueBytes) throws PCEPDeserializerException {
                if (valueBytes == null || valueBytes.length == 0)
                        throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
+               if (valueBytes.length != SIZE)
+                       throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + valueBytes.length + "; Expected: " + SIZE + ".");
 
-               final AddressFamily afi = new Ipv6Builder().setIpv6TunnelSenderAddress(
-                               Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, position, IP_F_LENGTH))).setIpv6ExtendedTunnelId(
-                               new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, position += IP_F_LENGTH,
-                                               EX_TUNNEL_ID_F_LENGTH)))).build();
-
-               return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(
-                               new LspId(ByteArray.bytesToLong(ByteArray.subByte(valueBytes, position += EX_TUNNEL_ID_F_LENGTH, LSP_ID_F_LENGTH)))).setTunnelId(
-                               new TunnelId(ByteArray.bytesToInt(ByteArray.subByte(valueBytes, position += LSP_ID_F_LENGTH, TUNNEL_ID_F_LENGTH)))).build();
+               return new IPv6LSPIdentifiersTlv(new IPv6Address(
+                               ByteArray.subByte(valueBytes, IP_F_OFFSET, IP_F_LENGTH)), new LSPIdentifier(ByteArray.subByte(valueBytes, LSP_ID_F_OFFSET, LSP_ID_F_LENGTH)),
+                               new TunnelIdentifier(ByteArray.subByte(valueBytes, TUNNLE_ID_F_OFFSET, TUNNEL_ID_F_LENGTH)),
+                               new IPv6ExtendedTunnelIdentifier(new IPv6Address(ByteArray.subByte(valueBytes, EX_TUNNEL_ID_F_OFFSET, EX_TUNNEL_ID_F_LENGTH))));
        }
 
-       public int getType() {
-               return TYPE;
+       public static byte[] put(IPv6LSPIdentifiersTlv objToSerialize) {
+               if (objToSerialize == null)
+                       throw new IllegalArgumentException("IPv6LSPIdentifiersTlv is mandatory.");
+
+               final byte[] retBytes = new byte[SIZE];
+
+               ByteArray.copyWhole(objToSerialize.getSenderAddress().getAddress(), retBytes, IP_F_OFFSET);
+               ByteArray.copyWhole(objToSerialize.getLspID().getLspId(), retBytes, LSP_ID_F_OFFSET);
+               ByteArray.copyWhole(objToSerialize.getTunnelID().getBytes(), retBytes, TUNNLE_ID_F_OFFSET);
+               ByteArray.copyWhole(objToSerialize.getExtendedTunnelID().getIdentifier().getAddress(), retBytes, EX_TUNNEL_ID_F_OFFSET);
+
+               return retBytes;
        }
+
 }