Fix checkstyle
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / InstanceIdSerializerTest.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.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
15 import org.junit.Test;
16 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
17 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
18 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
19 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
20 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25
26 public class InstanceIdSerializerTest extends BaseTestCase {
27
28     @Test
29     public void deserialize__Simple() throws Exception {
30         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
31                 "02 20 00 0A " + //
32                 "00 BB CC DD " + // instance ID
33                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
34                 new LispAddressSerializerContext(null));
35
36         assertEquals(Ipv4BinaryAfi.class, address.getAddressType());
37         Ipv4Binary ipv4 = (Ipv4Binary) address.getAddress();
38
39         assertArrayEquals(new byte[] {0x11, 0x22, 0x33, 0x44}, ipv4.getIpv4Binary().getValue());
40         assertEquals(0x00BBCCDD, address.getVirtualNetworkId().getValue().longValue());
41     }
42
43     @Test(expected = LispSerializationException.class)
44     public void deserialize__ShorterBuffer() throws Exception {
45         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
46                 "02 20 00 0A " + //
47                 "AA BB "),
48                 new LispAddressSerializerContext(null));
49     }
50
51     @Test(expected = LispSerializationException.class)
52     public void deserialize__UnknownLCAFType() throws Exception {
53         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
54                 "AA 20 00 0A " + // Type AA is unknown
55                 "00 BB CC DD " + // instance ID
56                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
57                 new LispAddressSerializerContext(null));
58     }
59
60     @Test(expected = LispSerializationException.class)
61     public void deserialize__LongInstanceID() throws Exception {
62         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
63                 "02 20 00 0A " + // Type AA is unknown
64                 "AA BB CC DD " + // instance ID
65                 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
66                 new LispAddressSerializerContext(null));
67     }
68
69     @Test
70     public void deserialize__Ipv6() throws Exception {
71         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
72                 "02 20 00 0A " + //
73                 "00 BB CC DD " + // instance ID
74                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD"), // AFI=2,
75                 new LispAddressSerializerContext(null));
76         // IPv6
77
78         assertArrayEquals(new byte[] {(byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
79                                       (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x88,
80                                       (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC,
81                                       (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD},
82                 ((Ipv6Binary) address.getAddress()).getIpv6Binary().getValue());
83
84     }
85
86     @Test
87     public void serialize__Simple() throws Exception {
88         Eid eid = LispAddressUtil.asIpv4Eid("17.34.51.68", (long) 0x00020304);
89
90         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eid));
91         LispAddressSerializer.getInstance().serialize(buf, eid);
92         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
93                 "02 20 00 0A " + //
94                 "00 02 03 04 " + // instance ID
95                 "00 01 11 22 33 44");
96         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
97     }
98 }