Migrate to use stringValue()
[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
29 public class ApplicationDataSerializerTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         Eid address = LispAddressSerializer.getInstance().deserializeEid(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"), null); // AFI=1, IP=0x11223344
39
40         assertEquals(ApplicationDataLcaf.class, address.getAddressType());
41         ApplicationData appAddress = (ApplicationData) address.getAddress();
42
43         assertEquals("17.34.51.68", appAddress.getApplicationData().getAddress().stringValue());
44         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }),
45                 appAddress.getApplicationData().getIpTos().intValue());
46         assertEquals((byte) 0xDD, appAddress.getApplicationData().getProtocol().byteValue());
47         assertEquals((short) 0xA6A1, appAddress.getApplicationData().getLocalPortLow().getValue().shortValue());
48         assertEquals((short) 0xA6A2, appAddress.getApplicationData().getLocalPortHigh().getValue().shortValue());
49         assertEquals((short) 0xFFDD, appAddress.getApplicationData().getRemotePortLow().getValue().shortValue());
50         assertEquals((short) 0xFFDE, appAddress.getApplicationData().getRemotePortHigh().getValue().shortValue());
51     }
52
53     @Test(expected = LispSerializationException.class)
54     public void deserialize__ShorterBuffer() throws Exception {
55         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
56                 "04 20 00 0A " + //
57                 "AA BB "), null);
58     }
59
60     @Test(expected = LispSerializationException.class)
61     public void deserialize__UnknownLCAFType() throws Exception {
62         LispAddressSerializer.getInstance().deserializeEid(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"), null); // AFI=1, IP=0x11223344
68     }
69
70     @Test
71     public void deserialize__Ipv6() throws Exception {
72         Eid appAddress = LispAddressSerializer.getInstance().deserializeEid(
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"), null); // AFI=2,
79         // IPv6
80
81         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd",
82                 ((ApplicationData) appAddress.getAddress()).getApplicationData().getAddress().stringValue());
83     }
84
85     @Test
86     public void serialize__Simple() throws Exception {
87         ApplicationDataBuilder addressBuilder = new ApplicationDataBuilder();
88         addressBuilder.setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
89         addressBuilder.setProtocol((short) 0xDD);
90         addressBuilder.setLocalPortLow(new PortNumber(0xA6A1));
91         addressBuilder.setLocalPortHigh(new PortNumber(0xA6A2));
92         addressBuilder.setRemotePortLow(new PortNumber(0xFFDD));
93         addressBuilder.setRemotePortHigh(new PortNumber(0xFFDE));
94         addressBuilder.setAddress(new SimpleAddress(new IpAddress(new Ipv4Address("17.34.51.68"))));
95
96         EidBuilder eb = new EidBuilder();
97         eb.setAddressType(ApplicationDataLcaf.class);
98         eb.setVirtualNetworkId(null);
99         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
100             .lisp.address.address.ApplicationDataBuilder()
101             .setApplicationData(addressBuilder.build()).build());
102
103         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
104         LispAddressSerializer.getInstance().serialize(buf, eb.build());
105         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
106                 "04 00 00 12 " + //
107                 "AA BB CC DD " + // IPTOS & protocol
108                 "A6 A1 A6 A2 " + // local port range
109                 "FF DD FF DE " + // remote port range
110                 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
111         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
112     }
113 }