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