2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation.serializer.address;
10 import static junit.framework.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
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.lispflowmapping.rev131031.LcafKeyValueAddress;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.KeyBuilder;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.ValueBuilder;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafKeyValueBuilder;
28 public class LispKeyValueLCAFAddressTest extends BaseTestCase {
31 public void deserialize__Simple() throws Exception {
32 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
34 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
35 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
37 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
38 LcafKeyValueAddress srcDestAddress = (LcafKeyValueAddress) address;
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());
45 @Test(expected = LispSerializationException.class)
46 public void deserialize__ShorterBuffer() throws Exception {
47 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
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
61 public void deserialize__Ipv6() throws Exception {
62 LcafKeyValueAddress srcAddress = (LcafKeyValueAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
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,
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());
75 public void serialize__Simple() throws Exception {
76 LcafKeyValueBuilder addressBuilder = new LcafKeyValueBuilder();
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 " + //
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());