Migrate lisp-proto implementation to IETF YANG model
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / InstanceIdSerializerTest.java
1 /*
2  * Copyright (c) 2014 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.serializer.address;
9
10 import static org.junit.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.lisp.serializer.address.LispAddressSerializer;
18 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
19 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
20 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
26
27 public class InstanceIdSerializerTest extends BaseTestCase {
28
29     @Test
30     public void deserialize__Simple() throws Exception {
31         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
32                 "02 20 00 0A " + //
33                 "00 BB CC DD " + // instance ID
34                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
35                 new LispAddressSerializerContext(null));
36
37         assertEquals(Ipv4Afi.class, address.getAddressType());
38         Ipv4 ipv4 = (Ipv4) address.getAddress();
39
40         assertEquals("17.34.51.68", ipv4.getIpv4().getValue());
41         assertEquals(0x00BBCCDD, address.getVirtualNetworkId().getValue().longValue());
42     }
43
44     @Test(expected = LispSerializationException.class)
45     public void deserialize__ShorterBuffer() throws Exception {
46         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
47                 "02 20 00 0A " + //
48                 "AA BB "),
49                 new LispAddressSerializerContext(null));
50     }
51
52     @Test(expected = LispSerializationException.class)
53     public void deserialize__UnknownLCAFType() throws Exception {
54         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
55                 "AA 20 00 0A " + // Type AA is unknown
56                 "00 BB CC DD " + // instance ID
57                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
58                 new LispAddressSerializerContext(null));
59     }
60
61     @Test(expected = LispSerializationException.class)
62     public void deserialize__LongInstanceID() throws Exception {
63         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
64                 "02 20 00 0A " + // Type AA is unknown
65                 "AA BB CC DD " + // instance ID
66                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
67                 new LispAddressSerializerContext(null));
68     }
69
70     @Test
71     public void deserialize__Ipv6() throws Exception {
72         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
73                 "02 20 00 0A " + //
74                 "00 BB CC DD " + // instance ID
75                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD"), // AFI=2,
76                 new LispAddressSerializerContext(null));
77         // IPv6
78
79         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd", ((Ipv6) address.getAddress()).getIpv6().getValue());
80
81     }
82
83     @Test
84     public void serialize__Simple() throws Exception {
85         Eid eid = LispAddressUtil.asIpv4Eid("17.34.51.68", (long) 0x00020304);
86
87         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eid));
88         LispAddressSerializer.getInstance().serialize(buf, eid);
89         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
90                 "02 00 00 0A " + //
91                 "00 02 03 04 " + // instance ID
92                 "00 01 11 22 33 44");
93         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
94     }
95 }