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