fc6d2a00d9104b18d66316571fbf0f61fb2e0657
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispKeyValueLCAFAddressTest.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.LcafKeyValueAddress;
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.lcafkeyvalueaddress.KeyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafkeyvalueaddress.ValueBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafkeyvalue.LcafKeyValueAddressAddrBuilder;
27
28 public class LispKeyValueLCAFAddressTest extends BaseTestCase {
29
30     @Test
31     public void deserialize__Simple() throws Exception {
32         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
33                 "0F 20 00 0C " + //
34                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
35                 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
36
37         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
38         LcafKeyValueAddress srcDestAddress = (LcafKeyValueAddress) address;
39
40         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), srcDestAddress.getKey().getPrimitiveAddress());
41         assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85"), srcDestAddress.getValue().getPrimitiveAddress());
42         assertEquals(LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode(), srcDestAddress.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 01 11 22 33 44 " + // AFI=1, IP=0x11223344
57                 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
58     }
59
60     @Test
61     public void deserialize__Ipv6() throws Exception {
62         LcafKeyValueAddress srcAddress = (LcafKeyValueAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
63                 "0F 20 00 24 " + //
64                 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
65                 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD")); // AFI=2,
66         // IPv6
67
68         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), srcAddress.getKey()
69                 .getPrimitiveAddress());
70         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd")), srcAddress
71                 .getValue().getPrimitiveAddress());
72     }
73
74     @Test
75     public void serialize__Simple() throws Exception {
76         LcafKeyValueAddressAddrBuilder addressBuilder = new LcafKeyValueAddressAddrBuilder();
77         addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
78         addressBuilder.setKey(new KeyBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
79         addressBuilder.setValue(new ValueBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85")).build());
80         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
81         LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
82         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
83                 "0F 00 00 0C " + //
84                 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
85                 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
86         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
87     }
88 }