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 org.junit.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.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;
27 public class LispSegmentLCAFAddressTest extends BaseTestCase {
30 public void deserialize__Simple() throws Exception {
31 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
33 "00 BB CC DD " + // instance ID
34 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
36 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
37 LcafSegmentAddress segAddress = (LcafSegmentAddress) address;
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());
44 @Test(expected = LispSerializationException.class)
45 public void deserialize__ShorterBuffer() throws Exception {
46 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
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
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
68 public void deserialize__Ipv6() throws Exception {
69 LcafSegmentAddress segAddress = (LcafSegmentAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
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,
75 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), segAddress
76 .getAddress().getPrimitiveAddress());
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 " + //
91 "00 02 03 04 " + // instance ID
93 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());