a39a912f0359ca6cd0f6a0460db6d231ed80eb93
[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 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.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 0E " + //
35                 "AA BB CC DD " + // IPTOS & protocol
36                 "A6 A1 A6 A2 " + // local port range
37                 "FF DD FF DE " + // remote port range
38                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
39
40         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
41         LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) address;
42
43         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), appAddress.getAddress().getPrimitiveAddress());
44         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }), appAddress.getIpTos().intValue());
45         assertEquals((byte) 0xDD, appAddress.getProtocol().byteValue());
46         assertEquals((short) 0xA6A1, appAddress.getLocalPortLow().getValue().shortValue());
47         assertEquals((short) 0xA6A2, appAddress.getLocalPortHigh().getValue().shortValue());
48         assertEquals((short) 0xFFDD, appAddress.getRemotePortLow().getValue().shortValue());
49         assertEquals((short) 0xFFDE, appAddress.getRemotePortHigh().getValue().shortValue());
50         assertEquals(LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode(), appAddress.getLcafType().byteValue());
51     }
52
53     @Test(expected = LispSerializationException.class)
54     public void deserialize__ShorterBuffer() throws Exception {
55         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
56                 "04 20 00 0A " + //
57                 "AA BB "));
58     }
59
60     @Test(expected = LispSerializationException.class)
61     public void deserialize__UnknownLCAFType() throws Exception {
62         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
63                 "AA 20 00 12 " + // Type AA is unknown
64                 "AA BB CC DD " + // IPTOS & protocol
65                 "A6 A1 A6 A2 " + // local port range
66                 "FF DD FF DE " + // remote port range
67                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
68     }
69
70     @Test
71     public void deserialize__Ipv6() throws Exception {
72         LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) LispAddressSerializer.getInstance().deserialize(
73                 hexToByteBuffer("40 03 00 00 " + //
74                         "04 20 00 1E " + //
75                         "AA BB CC DD " + // IPTOS & protocol
76                         "A6 A1 A6 A2 " + // local port range
77                         "FF DD FF DE " + // remote port range
78                         "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
79         // IPv6
80
81         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), appAddress
82                 .getAddress().getPrimitiveAddress());
83
84     }
85
86     @Test
87     public void serialize__Simple() throws Exception {
88         LcafApplicationDataAddrBuilder addressBuilder = new LcafApplicationDataAddrBuilder();
89         addressBuilder.setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
90         addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
91                 (short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
92         addressBuilder.setProtocol((short) 0xDD);
93         addressBuilder.setLocalPortLow(new PortNumber(0xA6A1));
94         addressBuilder.setLocalPortHigh(new PortNumber(0xA6A2));
95         addressBuilder.setRemotePortLow(new PortNumber(0xFFDD));
96         addressBuilder.setRemotePortHigh(new PortNumber(0xFFDE));
97         addressBuilder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
98         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
99         LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
100         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
101                 "04 00 00 12 " + //
102                 "AA BB CC DD " + // IPTOS & protocol
103                 "A6 A1 A6 A2 " + // local port range
104                 "FF DD FF DE " + // remote port range
105                 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
106         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
107     }
108 }