Fix checkstyle
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / ApplicationDataSerializerTest.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 import junitx.framework.ArrayAssert;
14 import org.junit.Test;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
16 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
17 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
18 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationDataBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint16;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30
31 public class ApplicationDataSerializerTest extends BaseTestCase {
32
33     @Test
34     public void deserialize__Simple() throws Exception {
35         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
36                 + "04 20 00 0E "
37                 + "AA BB CC DD " // IPTOS & protocol
38                 + "A6 A1 A6 A2 " // local port range
39                 + "FF DD FF DE " // remote port range
40                 + "00 01 11 22 33 44"), null); // AFI=1, IP=0x11223344
41
42         assertEquals(ApplicationDataLcaf.class, address.getAddressType());
43         ApplicationData appAddress = (ApplicationData) address.getAddress();
44
45         assertEquals("17.34.51.68", appAddress.getApplicationData().getAddress().stringValue());
46         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }),
47                 appAddress.getApplicationData().getIpTos().intValue());
48         assertEquals((byte) 0xDD, appAddress.getApplicationData().getProtocol().byteValue());
49         assertEquals((short) 0xA6A1, appAddress.getApplicationData().getLocalPortLow().getValue().shortValue());
50         assertEquals((short) 0xA6A2, appAddress.getApplicationData().getLocalPortHigh().getValue().shortValue());
51         assertEquals((short) 0xFFDD, appAddress.getApplicationData().getRemotePortLow().getValue().shortValue());
52         assertEquals((short) 0xFFDE, appAddress.getApplicationData().getRemotePortHigh().getValue().shortValue());
53     }
54
55     @Test(expected = LispSerializationException.class)
56     public void deserialize__ShorterBuffer() throws Exception {
57         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
58                 + "04 20 00 0A "
59                 + "AA BB "), null);
60     }
61
62     @Test(expected = LispSerializationException.class)
63     public void deserialize__UnknownLCAFType() throws Exception {
64         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
65                 + "AA 20 00 12 " // Type AA is unknown
66                 + "AA BB CC DD " // IPTOS & protocol
67                 + "A6 A1 A6 A2 " // local port range
68                 + "FF DD FF DE " // remote port range
69                 + "00 01 11 22 33 44"), null); // AFI=1, IP=0x11223344
70     }
71
72     @Test
73     public void deserialize__Ipv6() throws Exception {
74         Eid appAddress = LispAddressSerializer.getInstance().deserializeEid(
75                 hexToByteBuffer("40 03 00 00 "
76                         + "04 20 00 1E "
77                         + "AA BB CC DD " // IPTOS & protocol
78                         + "A6 A1 A6 A2 " // local port range
79                         + "FF DD FF DE " // remote port range
80                         + "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD"), null); // AFI=2,
81         // IPv6
82
83         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd",
84                 ((ApplicationData) appAddress.getAddress()).getApplicationData().getAddress().stringValue());
85     }
86
87     @Test
88     public void serialize__Simple() throws Exception {
89         ApplicationDataBuilder addressBuilder = new ApplicationDataBuilder()
90             .setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }))
91             .setProtocol(Uint8.valueOf(0xDD))
92             .setLocalPortLow(new PortNumber(Uint16.valueOf(0xA6A1)))
93             .setLocalPortHigh(new PortNumber(Uint16.valueOf(0xA6A2)))
94             .setRemotePortLow(new PortNumber(Uint16.valueOf(0xFFDD)))
95             .setRemotePortHigh(new PortNumber(Uint16.valueOf(0xFFDE)))
96             .setAddress(new SimpleAddress(new IpAddress(new Ipv4Address("17.34.51.68"))));
97
98         EidBuilder eb = new EidBuilder();
99         eb.setAddressType(ApplicationDataLcaf.class);
100         eb.setVirtualNetworkId(null);
101         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
102             .lisp.address.address.ApplicationDataBuilder()
103             .setApplicationData(addressBuilder.build()).build());
104
105         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
106         LispAddressSerializer.getInstance().serialize(buf, eb.build());
107         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
108                 + "04 00 00 12 " //
109                 + "AA BB CC DD " // IPTOS & protocol
110                 + "A6 A1 A6 A2 " // local port range
111                 + "FF DD FF DE " // remote port range
112                 + "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
113         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
114     }
115 }