Prepare the YANG models for RESTCONF
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispApplicationDataLCAFAddressTest.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 junit.framework.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.ByteUtil;
19 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
20 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
21 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
22 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafApplicationDataAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafapplicationdataaddress.AddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafapplicationdata.LcafApplicationDataAddrBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
28
29 public class LispApplicationDataLCAFAddressTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
34                 "04 20 00 0A " + //
35                 "AA BB CC DD " + // IPTOS & protocol
36                 "A6 A1 FF DD " + // local port & remote port
37                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
38
39         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
40         LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) address;
41
42         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), appAddress.getAddress().getPrimitiveAddress());
43         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }), appAddress.getIpTos().intValue());
44         assertEquals((byte) 0xDD, appAddress.getProtocol().byteValue());
45         assertEquals((short) 0xA6A1, appAddress.getLocalPort().getValue().shortValue());
46         assertEquals((short) 0xFFDD, appAddress.getRemotePort().getValue().shortValue());
47         assertEquals(LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode(), appAddress.getLcafType().byteValue());
48     }
49
50     @Test(expected = LispSerializationException.class)
51     public void deserialize__ShorterBuffer() throws Exception {
52         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
53                 "04 20 00 0A " + //
54                 "AA BB "));
55     }
56
57     @Test(expected = LispSerializationException.class)
58     public void deserialize__UnknownLCAFType() throws Exception {
59         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
60                 "AA 20 00 0A " + // Type AA is unknown
61                 "AA BB CC DD " + // IPTOS & protocol
62                 "A6 A1 FF DD " + // local port & remote port
63                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
64     }
65
66     @Test
67     public void deserialize__Ipv6() throws Exception {
68         LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) LispAddressSerializer.getInstance().deserialize(
69                 hexToByteBuffer("40 03 00 00 " + //
70                         "04 20 00 0A " + //
71                         "AA BB CC DD " + // IPTOS & protocol
72                         "A6 A1 FF DD " + // local port & remote port
73                         "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
74         // IPv6
75
76         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), appAddress
77                 .getAddress().getPrimitiveAddress());
78
79     }
80
81     @Test
82     public void serialize__Simple() throws Exception {
83         LcafApplicationDataAddrBuilder addressBuilder = new LcafApplicationDataAddrBuilder();
84         addressBuilder.setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
85         addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
86                 (short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
87         addressBuilder.setProtocol((short) 0xDD);
88         addressBuilder.setLocalPort(new PortNumber(0xA6A1));
89         addressBuilder.setRemotePort(new PortNumber(0xFFDD));
90         addressBuilder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
91         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
92         LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
93         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
94                 "04 00 00 0E " + //
95                 "AA BB CC DD " + // IPTOS & protocol
96                 "A6 A1 FF DD " + // local port & remote port
97                 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
98         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
99     }
100 }