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