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