Checkstyle: enforce 120 character limit
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / KeyValueAddressSerializerTest.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.exception.LispSerializationException;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.KeyValueAddressLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.key.value.address.KeyValueAddressBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
29
30 public class KeyValueAddressSerializerTest extends BaseTestCase {
31
32     @Test
33     public void deserialize__Simple() throws Exception {
34         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
35                 "0F 20 00 0C " + //
36                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
37                 "00 01 22 33 44 55"), null); // AFI=1, IP=0x22334455
38
39         assertEquals(KeyValueAddressLcaf.class, address.getAddressType());
40         KeyValueAddress srcDestAddress = (KeyValueAddress) address.getAddress();
41
42         assertEquals("17.34.51.68", String.valueOf(srcDestAddress.getKeyValueAddress().getKey().getValue()));
43         assertEquals("34.51.68.85", String.valueOf(srcDestAddress.getKeyValueAddress().getValue().getValue()));
44     }
45
46     @Test(expected = LispSerializationException.class)
47     public void deserialize__ShorterBuffer() throws Exception {
48         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
49                 "02 20 00 0A " + //
50                 "AA BB "), null);
51     }
52
53     @Test(expected = LispSerializationException.class)
54     public void deserialize__UnknownLCAFType() throws Exception {
55         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
56                 "AA 20 00 0A " + // Type AA is unknown
57                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
58                 "00 01 22 33 44 55"), null); // AFI=1, IP=0x22334455
59     }
60
61     @Test
62     public void deserialize__Ipv6() throws Exception {
63         Eid srcAddress = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
64                 "0F 20 00 24 " + //
65                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
66                 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD"), null); // AFI=2,
67         // IPv6
68
69         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd", String.valueOf(
70                 ((KeyValueAddress) srcAddress.getAddress()).getKeyValueAddress().getKey().getValue()));
71         assertEquals("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd", String.valueOf(
72                 ((KeyValueAddress) srcAddress.getAddress()).getKeyValueAddress().getValue().getValue()));
73     }
74
75     @Test
76     public void serialize__Simple() throws Exception {
77         KeyValueAddressBuilder addressBuilder = new KeyValueAddressBuilder();
78         addressBuilder.setKey(new SimpleAddress(new IpAddress(new Ipv4Address("17.34.51.68"))));
79         addressBuilder.setValue(new SimpleAddress(new IpAddress(new Ipv4Address("34.51.68.85"))));
80
81         EidBuilder eb = new EidBuilder();
82         eb.setAddressType(KeyValueAddressLcaf.class);
83         eb.setVirtualNetworkId(null);
84         eb.setAddress((Address)
85                 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
86                 .lisp.address.address.KeyValueAddressBuilder()
87                 .setKeyValueAddress(addressBuilder.build()).build());
88
89         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
90         LispAddressSerializer.getInstance().serialize(buf, eb.build());
91
92         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
93                 "0F 00 00 0C " + //
94                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
95                 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
96         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
97     }
98 }