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