Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispSegmentLCAFAddressTest.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.implementation.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.implementation.lisp.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
21 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafSegmentAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsegmentaddress.AddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafsegment.LcafSegmentAddrBuilder;
26
27 public class LispSegmentLCAFAddressTest extends BaseTestCase {
28
29     @Test
30     public void deserialize__Simple() throws Exception {
31         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
32                 "02 20 00 0A " + //
33                 "00 BB CC DD " + // instance ID
34                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
35
36         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
37         LcafSegmentAddress segAddress = (LcafSegmentAddress) address;
38
39         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), segAddress.getAddress().getPrimitiveAddress());
40         assertEquals(0x00BBCCDD, segAddress.getInstanceId().intValue());
41         assertEquals(LispCanonicalAddressFormatEnum.SEGMENT.getLispCode(), segAddress.getLcafType().byteValue());
42     }
43
44     @Test(expected = LispSerializationException.class)
45     public void deserialize__ShorterBuffer() throws Exception {
46         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
47                 "02 20 00 0A " + //
48                 "AA BB "));
49     }
50
51     @Test(expected = LispSerializationException.class)
52     public void deserialize__UnknownLCAFType() throws Exception {
53         LispAddressSerializer.getInstance().deserialize(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     }
58
59     @Test(expected = LispSerializationException.class)
60     public void deserialize__LongInstanceID() throws Exception {
61         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
62                 "02 20 00 0A " + // Type AA is unknown
63                 "AA BB CC DD " + // instance ID
64                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
65     }
66
67     @Test
68     public void deserialize__Ipv6() throws Exception {
69         LcafSegmentAddress segAddress = (LcafSegmentAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
70                 "02 20 00 0A " + //
71                 "00 BB CC DD " + // instance ID
72                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
73         // IPv6
74
75         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), segAddress
76                 .getAddress().getPrimitiveAddress());
77
78     }
79
80     @Test
81     public void serialize__Simple() throws Exception {
82         LcafSegmentAddrBuilder addressBuilder = new LcafSegmentAddrBuilder();
83         addressBuilder.setInstanceId((long) 0x00020304);
84         addressBuilder.setIidMaskLength((short) 0);
85         addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
86         addressBuilder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
87         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
88         LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
89         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
90                 "02 00 00 0A " + //
91                 "00 02 03 04 " + // instance ID
92                 "00 01 11 22 33 44");
93         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
94     }
95
96 }