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;
--- /dev/null
+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();
+ }
+}
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) {
@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) {
@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
--- /dev/null
+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;
+ }
+}
"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());
+ }
}
--- /dev/null
+/*
+ * 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());
+ }
+}