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;
13 import java.util.ArrayList;
14 import java.util.List;
16 import junitx.framework.ArrayAssert;
17 import junitx.framework.Assert;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
22 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
24 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.Addresses;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.AddressesBuilder;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafList;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafListBuilder;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv6;
33 public class LispListLCAFAddressTest extends BaseTestCase {
36 public void deserialize__Simple() throws Exception {
37 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
39 "00 01 AA BB CC DD " + // IPv4
40 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
42 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
43 LcafListAddress lcafList = (LcafListAddress) address;
45 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
47 List<Addresses> addressList = lcafList.getAddresses();
48 assertEquals(2, addressList.size());
50 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")), addressList.get(0).getPrimitiveAddress());
51 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344")), addressList.get(1)
52 .getPrimitiveAddress());
56 public void deserialize__NoAddresses() throws Exception {
57 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
60 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
61 LcafListAddress lcafList = (LcafListAddress) address;
63 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
65 List<Addresses> addressList = lcafList.getAddresses();
66 assertEquals(0, addressList.size());
69 @Test(expected = LispSerializationException.class)
70 public void deserialize__ShorterBuffer() throws Exception {
71 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
73 "00 01 AA BB CC DD " + // IPv4
74 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
77 @Test(expected = LispSerializationException.class)
78 public void deserialize__ShorterBuffer2() throws Exception {
79 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
84 public void serialize__Simple() throws Exception {
85 LcafListBuilder listBuilder = new LcafListBuilder();
86 listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
87 listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
88 List<Addresses> addressList = new ArrayList<Addresses>();
89 addressList.add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")))
91 addressList.add(new AddressesBuilder().setPrimitiveAddress(
92 LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344"))).build());
93 listBuilder.setAddresses(addressList);
94 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
95 LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
96 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
98 "00 01 AA BB CC DD " + // IPv4
99 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
100 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
104 public void serialize__NoAddresses() throws Exception {
105 LcafListBuilder listBuilder = new LcafListBuilder();
106 listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
107 listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
108 List<Addresses> addressList = new ArrayList<Addresses>();
109 listBuilder.setAddresses(addressList);
110 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
111 LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
112 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
114 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
118 public void equals__Simple() throws Exception {
119 Ipv6 ip1 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"));
120 Ipv6 ip2 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:2"));
121 LcafListBuilder listBuilder = new LcafListBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
122 .setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode()).setAddresses(new ArrayList<Addresses>());
123 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
124 LcafList address1 = listBuilder.build();
125 listBuilder.setAddresses(new ArrayList<Addresses>());
126 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
127 LcafList address2 = listBuilder.build();
128 listBuilder.setAddresses(new ArrayList<Addresses>());
129 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip2).build());
130 LcafList address3 = listBuilder.build();
132 assertEquals(address1, address2);
133 Assert.assertNotEquals(address2, address3);
134 Assert.assertNotEquals(address1, address3);