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.serializer.address;
10 import static org.junit.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
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.lisp.proto.rev151105.LcafKeyValueAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lcafkeyvalueaddress.KeyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lcafkeyvalueaddress.ValueBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.lispaddresscontainer.address.lcafkeyvalue.LcafKeyValueAddressAddrBuilder;
29 public class LispKeyValueLCAFAddressTest extends BaseTestCase {
32 public void deserialize__Simple() throws Exception {
33 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
35 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
36 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
38 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
39 LcafKeyValueAddress srcDestAddress = (LcafKeyValueAddress) address;
41 assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), srcDestAddress.getKey().getPrimitiveAddress());
42 assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85"), srcDestAddress.getValue().getPrimitiveAddress());
43 assertEquals(LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode(), srcDestAddress.getLcafType().byteValue());
46 @Test(expected = LispSerializationException.class)
47 public void deserialize__ShorterBuffer() throws Exception {
48 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
53 @Test(expected = LispSerializationException.class)
54 public void deserialize__UnknownLCAFType() throws Exception {
55 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
56 "AA 20 00 0A " + // Type AA is unknown
57 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
58 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
62 public void deserialize__Ipv6() throws Exception {
63 LcafKeyValueAddress srcAddress = (LcafKeyValueAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
65 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
66 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD")); // AFI=2,
69 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), srcAddress.getKey()
70 .getPrimitiveAddress());
71 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd")), srcAddress
72 .getValue().getPrimitiveAddress());
76 public void serialize__Simple() throws Exception {
77 LcafKeyValueAddressAddrBuilder addressBuilder = new LcafKeyValueAddressAddrBuilder();
78 addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
79 addressBuilder.setKey(new KeyBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
80 addressBuilder.setValue(new ValueBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("34.51.68.85")).build());
81 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
82 LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
83 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
85 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
86 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
87 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());