f8d67b189b1a283e7d8d7c48ce7609ab1934e865
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispSourceDestLCAFAddressTest.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.implementation.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.implementation.lisp.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
21 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafSourceDestAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsourcedestaddress.DstAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsourcedestaddress.SrcAddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafsourcedest.LcafSourceDestAddrBuilder;
27
28 public class LispSourceDestLCAFAddressTest extends BaseTestCase {
29
30     @Test
31     public void deserialize__Simple() throws Exception {
32         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
33                 "0C 20 00 10 " + //
34                 "00 00 10 18 " + // reserved + masks
35                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
36                 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
37
38         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
39         LcafSourceDestAddress srcDestAddress = (LcafSourceDestAddress) address;
40
41         assertEquals((byte) 0x10, srcDestAddress.getSrcMaskLength().byteValue());
42         assertEquals((byte) 0x18, srcDestAddress.getDstMaskLength().byteValue());
43
44         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), srcDestAddress.getSrcAddress().getPrimitiveAddress());
45         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85"), srcDestAddress.getDstAddress().getPrimitiveAddress());
46         assertEquals(LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode(), srcDestAddress.getLcafType().byteValue());
47     }
48
49     @Test(expected = LispSerializationException.class)
50     public void deserialize__ShorterBuffer() throws Exception {
51         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
52                 "02 20 00 0A " + //
53                 "AA BB "));
54     }
55
56     @Test(expected = LispSerializationException.class)
57     public void deserialize__UnknownLCAFType() throws Exception {
58         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
59                 "AA 20 00 0A " + // Type AA is unknown
60                 "00 00 CC DD " + // reserved + masks
61                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
62                 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
63     }
64
65     @Test
66     public void deserialize__Ipv6() throws Exception {
67         LcafSourceDestAddress srcAddress = (LcafSourceDestAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
68                 "0C 20 00 28 " + //
69                 "00 00 CC DD " + // reserved + masks
70                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
71                 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD")); // AFI=2,
72         // IPv6
73
74         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), srcAddress
75                 .getSrcAddress().getPrimitiveAddress());
76         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd")), srcAddress
77                 .getDstAddress().getPrimitiveAddress());
78     }
79
80     @Test
81     public void serialize__Simple() throws Exception {
82         LcafSourceDestAddrBuilder addressBuilder = new LcafSourceDestAddrBuilder();
83         addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
84                 (short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
85         addressBuilder.setSrcMaskLength((short) 0xCC);
86         addressBuilder.setDstMaskLength((short) 0xDD);
87         addressBuilder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
88         addressBuilder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85")).build());
89         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
90         LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
91         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
92                 "0C 00 00 10 " + //
93                 "00 00 CC DD " + // reserved + masks
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 }