added source/dest lookup lcaf TELSDN-176: #close 23/1023/3
authorDavid Goldberg <david.goldberg@contextream.com>
Sat, 3 Aug 2013 08:24:33 +0000 (11:24 +0300)
committerDavid Goldberg <david.goldberg@contextream.com>
Mon, 5 Aug 2013 15:38:47 +0000 (18:38 +0300)
Change-Id: Ieabc171f81598b66b4c26155fe30acec3cc1426c
Signed-off-by: David Goldberg <david.goldberg@contextream.com>
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/type/LispCanonicalAddressFormatEnum.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/type/lisp/address/LispSourceDestLCAFAddress.java [new file with mode: 0644]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispAddressSerializerFactory.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispLCAFAddressSerializer.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSegmentLCAFAddressSerializer.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressSerializer.java [new file with mode: 0644]
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSegmentLCAFAddressTest.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressTest.java [new file with mode: 0644]

index bb626c98701f00cfd5a382cdac95f058b48efcab..aec35da248ef794c86b347a643e25dd716c96c8a 100644 (file)
@@ -12,11 +12,13 @@ import org.opendaylight.lispflowmapping.type.lisp.address.LispApplicationDataLCA
 import org.opendaylight.lispflowmapping.type.lisp.address.LispLCAFAddress;
 import org.opendaylight.lispflowmapping.type.lisp.address.LispListLCAFAddress;
 import org.opendaylight.lispflowmapping.type.lisp.address.LispSegmentLCAFAddress;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispSourceDestLCAFAddress;
 
 public enum LispCanonicalAddressFormatEnum {
     LIST(1, LispListLCAFAddress.class), //
     SEGMENT(2, LispSegmentLCAFAddress.class), //
     APPLICATION_DATA(4, LispApplicationDataLCAFAddress.class), //
+    SOURCE_DEST(12, LispSourceDestLCAFAddress.class), //
     UNKNOWN(-1, null);
 
     private byte lispCode;
diff --git a/mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/type/lisp/address/LispSourceDestLCAFAddress.java b/mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/type/lisp/address/LispSourceDestLCAFAddress.java
new file mode 100644 (file)
index 0000000..83f3701
--- /dev/null
@@ -0,0 +1,70 @@
+package org.opendaylight.lispflowmapping.type.lisp.address;
+
+import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
+
+public class LispSourceDestLCAFAddress extends LispLCAFAddress {
+
+    private LispAddress srcAddress;
+    private LispAddress dstAddress;
+    private byte srcMaskLength;
+    private byte dstMaskLength;
+    private short reserved;
+
+    public LispSourceDestLCAFAddress(byte res2) {
+        super(LispCanonicalAddressFormatEnum.SOURCE_DEST, res2);
+    }
+
+    public LispSourceDestLCAFAddress(byte res2, short reserved, byte srcMaskLength, byte dstMaskLength, LispAddress srcAddress, LispAddress dstAddress) {
+        super(LispCanonicalAddressFormatEnum.SOURCE_DEST, res2);
+        this.srcAddress = srcAddress;
+        this.dstAddress = dstAddress;
+        this.srcMaskLength = srcMaskLength;
+        this.dstMaskLength = dstMaskLength;
+        this.reserved = reserved;
+    }
+
+    public LispAddress getSrcAddress() {
+        return srcAddress;
+    }
+
+    public void setSrcAddress(LispAddress srcAddress) {
+        this.srcAddress = srcAddress;
+    }
+
+    public LispAddress getDstAddress() {
+        return dstAddress;
+    }
+
+    public void setDstAddress(LispAddress dstAddress) {
+        this.dstAddress = dstAddress;
+    }
+
+    public byte getSrcMaskLength() {
+        return srcMaskLength;
+    }
+
+    public void setSrcMaskLength(byte srcMaskLength) {
+        this.srcMaskLength = srcMaskLength;
+    }
+
+    public byte getDstMaskLength() {
+        return dstMaskLength;
+    }
+
+    public void setDstMaskLength(byte dstMaskLength) {
+        this.dstMaskLength = dstMaskLength;
+    }
+
+    public short getReserved() {
+        return reserved;
+    }
+
+    public void setReserved(short reserved) {
+        this.reserved = reserved;
+    }
+
+    @Override
+    public String toString() {
+        return "LispSourceDestLCAFAddress#[srcAddress=" + srcAddress + "/" + (int)srcMaskLength + " dstAddress=" + dstAddress + "/" + (int)dstMaskLength + "]" + super.toString();
+    }
+}
index 621dea2f72fadd320753a8cb49c3ce49806850dd..deb385bf6c3d21d7ecaa39c044595c4f9cdd2382 100644 (file)
@@ -24,6 +24,7 @@ public class LispAddressSerializerFactory {
                lcafToSearializerMap.put(LispCanonicalAddressFormatEnum.LIST, LispListLCAFAddressSerializer.getInstance());
                lcafToSearializerMap.put(LispCanonicalAddressFormatEnum.SEGMENT, LispSegmentLCAFAddressSerializer.getInstance());
                lcafToSearializerMap.put(LispCanonicalAddressFormatEnum.APPLICATION_DATA, LispApplicationDataLCAFAddressSerializer.getInstance());
+               lcafToSearializerMap.put(LispCanonicalAddressFormatEnum.SOURCE_DEST, LispSourceDestLCAFAddressSerializer.getInstance());
        }
        
        public static LispAddressSerializer getSerializer(AddressFamilyNumberEnum afi) {
index bd57106cc3fe1aa3ba48690b02949e0bcef7ab5b..5740586a6010d3107ef58aaa1781d92b7cb99a93 100644 (file)
@@ -40,7 +40,7 @@ public class LispLCAFAddressSerializer extends LispAddressSerializer{
 
     @Override
     public int getAddressSize(LispAddress lispAddress) {
-        return super.getAddressSize(lispAddress) + Length.LCAF_HEADER + getLcafLength(lispAddress);
+        return super.getAddressSize(lispAddress) + Length.LCAF_HEADER + LispAddressSerializerFactory.getLCAFSerializer(((LispLCAFAddress)lispAddress).getType()).getLcafLength(lispAddress);
     }
     
     public short getLcafLength(LispAddress lispAddress) {
index 0dba4cdf8dd05c0533c2f4637f779b99dab4fad9..f64967166c6bbb2bb53049eed0b62b1627fc540b 100644 (file)
@@ -22,7 +22,7 @@ public class LispSegmentLCAFAddressSerializer extends LispLCAFAddressSerializer{
        @Override
     public short getLcafLength(LispAddress lispAddress) {
                LispAddressSerializer serializer = LispAddressSerializerFactory.getSerializer(((LispSegmentLCAFAddress)lispAddress).getAddress().getAfi());
-        return (short) (Length.INSTANCE + serializer.getAddressSize(lispAddress));
+        return (short) (Length.INSTANCE + serializer.getAddressSize(((LispSegmentLCAFAddress)lispAddress).getAddress()));
     }
 
        @Override
diff --git a/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressSerializer.java b/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressSerializer.java
new file mode 100644 (file)
index 0000000..02903dc
--- /dev/null
@@ -0,0 +1,66 @@
+package org.opendaylight.lispflowmapping.southbound.serializer.address;
+
+import java.nio.ByteBuffer;
+
+import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispSourceDestLCAFAddress;
+
+public class LispSourceDestLCAFAddressSerializer extends LispLCAFAddressSerializer {
+
+    private static final LispSourceDestLCAFAddressSerializer INSTANCE = new LispSourceDestLCAFAddressSerializer();
+
+    // Private constructor prevents instantiation from other classes
+    private LispSourceDestLCAFAddressSerializer() {
+    }
+
+    public static LispSourceDestLCAFAddressSerializer getInstance() {
+        return INSTANCE;
+    }
+
+    @Override
+    public short getLcafLength(LispAddress lispAddress) {
+        LispAddressSerializer srcSerializer = LispAddressSerializerFactory.getSerializer(((LispSourceDestLCAFAddress) lispAddress).getSrcAddress()
+                .getAfi());
+        LispAddressSerializer dstSerializer = LispAddressSerializerFactory.getSerializer(((LispSourceDestLCAFAddress) lispAddress).getDstAddress()
+                .getAfi());
+        return (short) (Length.ALL_FIELDS + srcSerializer.getAddressSize(((LispSourceDestLCAFAddress) lispAddress).getSrcAddress()) + dstSerializer
+                .getAddressSize(((LispSourceDestLCAFAddress) lispAddress).getDstAddress()));
+    }
+
+    @Override
+    public void serialize(ByteBuffer buffer, LispAddress lispAddress) {
+        super.internalSerialize(buffer, lispAddress);
+        LispSourceDestLCAFAddress lispSourceDestLCAFAddress = ((LispSourceDestLCAFAddress) lispAddress);
+        buffer.putShort(lispSourceDestLCAFAddress.getReserved());
+        buffer.put(lispSourceDestLCAFAddress.getSrcMaskLength());
+        buffer.put(lispSourceDestLCAFAddress.getDstMaskLength());
+        LispAddressSerializer srcSerializer = LispAddressSerializerFactory.getSerializer(lispSourceDestLCAFAddress.getSrcAddress().getAfi());
+        if (srcSerializer == null) {
+            throw new LispMalformedPacketException("Unknown AFI type=" + lispSourceDestLCAFAddress.getSrcAddress().getAfi());
+        }
+        srcSerializer.serialize(buffer, lispSourceDestLCAFAddress.getSrcAddress());
+        LispAddressSerializer dstSerializer = LispAddressSerializerFactory.getSerializer(lispSourceDestLCAFAddress.getDstAddress().getAfi());
+        if (dstSerializer == null) {
+            throw new LispMalformedPacketException("Unknown AFI type=" + lispSourceDestLCAFAddress.getDstAddress().getAfi());
+        }
+        dstSerializer.serialize(buffer, lispSourceDestLCAFAddress.getDstAddress());
+    }
+
+    @Override
+    public LispSourceDestLCAFAddress innerDeserialize(ByteBuffer buffer, byte res2, short length) {
+        short res = buffer.getShort();
+        byte srcMaskLength = buffer.get();
+        byte dstMaskLength = buffer.get();
+        LispAddress srcAddress = LispAddressSerializer.getInstance().deserialize(buffer);
+        LispAddress dstAddress = LispAddressSerializer.getInstance().deserialize(buffer);
+
+        return new LispSourceDestLCAFAddress(res2, res, srcMaskLength, dstMaskLength, srcAddress, dstAddress);
+    }
+
+    private interface Length {
+        int SOURCE_MASK_LENGTH = 2;
+        int DEST_MASK_LENGTH = 2;
+        int ALL_FIELDS = SOURCE_MASK_LENGTH + DEST_MASK_LENGTH;
+    }
+}
index 57961907e6e4333c00c9950e3aa63104c87e3ffc..394b281b656c945bc665df5573f185511b1b2008 100644 (file)
@@ -82,4 +82,19 @@ public class LispSegmentLCAFAddressTest extends BaseTestCase {
                 "00 01 11 22 33 44");
         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
     }
+    
+    @Test
+    public void serialize__Recursive_Segment() throws Exception {
+        LispSegmentLCAFAddress address = new LispSegmentLCAFAddress((byte) 0x06, 0x01020304, new LispSegmentLCAFAddress((byte) 0x06, 0x01020305, new LispIpv4Address(0x11223344)));
+        ByteBuffer buf = ByteBuffer.allocate(LispSegmentLCAFAddressSerializer.getInstance().getAddressSize(address));
+        LispSegmentLCAFAddressSerializer.getInstance().serialize(buf,address);
+        ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
+                "02 06 00 16 " + //
+                "01 02 03 04 " + // instance ID
+                "40 03 00 00 " + //
+                "02 06 00 0A " + //
+                "01 02 03 05 " + // instance ID
+                "00 01 11 22 33 44");
+        ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
+    }
 }
diff --git a/mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressTest.java b/mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/serializer/address/LispSourceDestLCAFAddressTest.java
new file mode 100644 (file)
index 0000000..3ece488
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.lispflowmapping.southbound.serializer.address;
+
+import static junit.framework.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+
+import junitx.framework.ArrayAssert;
+
+import org.junit.Test;
+import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
+import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
+import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
+import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
+import org.opendaylight.lispflowmapping.type.lisp.address.LispSourceDestLCAFAddress;
+
+public class LispSourceDestLCAFAddressTest extends BaseTestCase {
+
+    @Test
+    public void deserialize__Simple() throws Exception {
+        LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
+                "0C 20 00 10 " + //
+                "00 00 10 18 " + // reserved + masks
+                "00 01 11 22 33 44 " +  // AFI=1, IP=0x11223344
+                       "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
+
+        assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
+        LispSourceDestLCAFAddress srcDestAddress = (LispSourceDestLCAFAddress) address;
+        
+        assertEquals(0, srcDestAddress.getReserved());
+        assertEquals((byte)0x10, srcDestAddress.getSrcMaskLength());
+        assertEquals((byte)0x18, srcDestAddress.getDstMaskLength());
+
+        assertEquals(new LispIpv4Address(0x11223344), srcDestAddress.getSrcAddress());
+        assertEquals(new LispIpv4Address(0x22334455), srcDestAddress.getDstAddress());
+        assertEquals(LispCanonicalAddressFormatEnum.SOURCE_DEST, srcDestAddress.getType());
+        System.out.println(address);
+    }
+
+    @Test(expected = LispMalformedPacketException.class)
+    public void deserialize__ShorterBuffer() throws Exception {
+        LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
+                "02 20 00 0A " + //
+                "AA BB "));
+    }
+
+    @Test(expected = LispMalformedPacketException.class)
+    public void deserialize__UnknownLCAFType() throws Exception {
+        LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
+                "AA 20 00 0A " + // Type AA is unknown
+                "00 00 CC DD " + // reserved + masks
+                "00 01 11 22 33 44 " +  // AFI=1, IP=0x11223344
+                       "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
+    }
+
+    @Test
+    public void deserialize__Ipv6() throws Exception {
+       LispSourceDestLCAFAddress srcAddress = (LispSourceDestLCAFAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
+                       "0C 20 00 28 " + //
+                "00 00 CC DD " + // reserved + masks
+                "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
+                       "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD")); // AFI=2,
+        // IPv6
+
+        assertEquals(new LispIpv6Address(new byte[] { 0x11, 0x22, 0x33, 0x44, //
+                0x55, 0x66, 0x77, (byte) 0x88, //
+                (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
+                (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), srcAddress.getSrcAddress());
+        assertEquals(new LispIpv6Address(new byte[] { 0x44, 0x33, 0x22, 0x11, //
+                       (byte) 0x88, 0x77, 0x66, 0x55, //
+                       (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
+                       (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), srcAddress.getDstAddress());
+
+    }
+
+    @Test
+    public void serialize__Simple() throws Exception {
+       LispSourceDestLCAFAddress address = new LispSourceDestLCAFAddress((byte) 0x20, (short)0x0000, (byte) 0xCC, (byte) 0xDD, new LispIpv4Address(0x11223344), new LispIpv4Address(0x22334455));
+        ByteBuffer buf = ByteBuffer.allocate(LispSourceDestLCAFAddressSerializer.getInstance().getAddressSize(address));
+        LispSourceDestLCAFAddressSerializer.getInstance().serialize(buf,address);
+        ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
+                        "0C 20 00 10 " + //
+                 "00 00 CC DD " + // reserved + masks
+                 "00 01 11 22 33 44 " +  // AFI=1, IP=0x11223344
+                       "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
+        ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
+    }
+}