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