refactoring of listmappingservice into Northbound (future REST) and Southbound (LISP...
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / serializer / address / LispSegmentLCAFAddressTest.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.southbound.serializer.address;
9
10 import static junit.framework.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13
14 import junitx.framework.ArrayAssert;
15
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
18 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
19 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
20 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
23 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
24 import org.opendaylight.lispflowmapping.type.lisp.address.LispSegmentLCAFAddress;
25
26 public class LispSegmentLCAFAddressTest extends BaseTestCase {
27
28     @Test
29     public void deserialize__Simple() throws Exception {
30         LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
31                 "02 20 00 0A " + //
32                 "AA BB CC DD " + // instance ID
33                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
34
35         assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
36         LispSegmentLCAFAddress segAddress = (LispSegmentLCAFAddress) address;
37
38         assertEquals(new LispIpv4Address(0x11223344), segAddress.getAddress());
39         assertEquals(0xAABBCCDD, segAddress.getInstanceId());
40         assertEquals(0x20, segAddress.getIdMaskLen());
41         assertEquals(LispCanonicalAddressFormatEnum.SEGMENT, segAddress.getType());
42     }
43
44     @Test(expected = LispMalformedPacketException.class)
45     public void deserialize__ShorterBuffer() throws Exception {
46         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
47                 "02 20 00 0A " + //
48                 "AA BB "));
49     }
50
51     @Test(expected = LispMalformedPacketException.class)
52     public void deserialize__UnknownLCAFType() throws Exception {
53         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
54                 "AA 20 00 0A " + // Type AA is unknown
55                 "AA BB CC DD " + // instance ID
56                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
57     }
58
59     @Test
60     public void deserialize__Ipv6() throws Exception {
61         LispSegmentLCAFAddress segAddress = (LispSegmentLCAFAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
62                 "02 20 00 0A " + //
63                 "AA BB CC DD " + // instance ID
64                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
65         // IPv6
66
67         assertEquals(new LispIpv6Address(new byte[] { 0x11, 0x22, 0x33, 0x44, //
68                 0x55, 0x66, 0x77, (byte) 0x88, //
69                 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
70                 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), segAddress.getAddress());
71
72     }
73
74     @Test
75     public void serialize__Simple() throws Exception {
76         LispSegmentLCAFAddress address = new LispSegmentLCAFAddress((byte) 0x06, 0x01020304, new LispIpv4Address(0x11223344));
77         ByteBuffer buf = ByteBuffer.allocate(LispSegmentLCAFAddressSerializer.getInstance().getAddressSize(address));
78         LispSegmentLCAFAddressSerializer.getInstance().serialize(buf,address);
79         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
80                 "02 06 00 0A " + //
81                 "01 02 03 04 " + // instance ID
82                 "00 01 11 22 33 44");
83         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
84     }
85 }