Migrate to use stringValue()
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / SourceDestKeySerializerTest.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.address.LispAddressSerializerContext;
17 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
28
29 public class SourceDestKeySerializerTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
34                 "0C 20 00 10 " + //
35                 "00 00 10 18 " + // reserved + masks
36                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
37                 "00 01 22 33 44 55"),  // AFI=1, IP=0x22334455
38                 new LispAddressSerializerContext(null));
39
40         assertEquals(SourceDestKeyLcaf.class, address.getAddressType());
41         SourceDestKey srcDestAddress = (SourceDestKey) address.getAddress();
42
43         assertEquals((byte) 0x10, MaskUtil.getMaskForAddress(srcDestAddress.getSourceDestKey().getSource()));
44         assertEquals((byte) 0x18, MaskUtil.getMaskForAddress(srcDestAddress.getSourceDestKey().getDest()));
45
46         assertEquals("17.34.51.68/16", srcDestAddress.getSourceDestKey().getSource().stringValue());
47         assertEquals("34.51.68.85/24", srcDestAddress.getSourceDestKey().getDest().stringValue());
48     }
49
50     @Test(expected = LispSerializationException.class)
51     public void deserialize__ShorterBuffer() throws Exception {
52         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
53                 "02 20 00 0A " + //
54                 "AA BB "), new LispAddressSerializerContext(null));
55     }
56
57     @Test(expected = LispSerializationException.class)
58     public void deserialize__UnknownLCAFType() throws Exception {
59         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
60                 "AA 20 00 0A " + // Type AA is unknown
61                 "00 00 CC DD " + // reserved + masks
62                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
63                 "00 01 22 33 44 55"),  // AFI=1, IP=0x22334455
64                 new LispAddressSerializerContext(null));
65     }
66
67     @Test
68     public void deserialize__Ipv6() throws Exception {
69         Eid srcAddress = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
70                 "0C 20 00 28 " + //
71                 "00 00 78 78 " + // reserved + masks
72                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
73                 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD"),  // AFI=2,
74                 new LispAddressSerializerContext(null));
75         // IPv6
76
77         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd/120",
78                 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getSource().stringValue());
79         assertEquals("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd/120",
80                 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getDest().stringValue());
81     }
82
83     @Test
84     public void serialize__Simple() throws Exception {
85         SourceDestKeyBuilder addressBuilder = new SourceDestKeyBuilder();
86         addressBuilder.setSource(new SimpleAddress(new IpPrefix(new Ipv4Prefix("17.34.51.68/8"))));
87         addressBuilder.setDest(new SimpleAddress(new IpPrefix(new Ipv4Prefix("34.51.68.85/16"))));
88
89         EidBuilder eb = new EidBuilder();
90         eb.setAddressType(SourceDestKeyLcaf.class);
91         eb.setVirtualNetworkId(null);
92         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
93             .lisp.address.address.SourceDestKeyBuilder()
94             .setSourceDestKey(addressBuilder.build()).build());
95
96         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
97         LispAddressSerializer.getInstance().serialize(buf, eb.build());
98
99         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
100                 "0C 00 00 10 " + //
101                 "00 00 08 10 " + // reserved + masks
102                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
103                 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
104         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
105     }
106 }