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;
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.lisp.serializer.address.LispAddressSerializer;
21 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
22 import org.opendaylight.lispflowmapping.lisp.type.AddressFamilyNumberEnum;
23 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
24 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
25 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LcafListAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lcaflistaddress.Addresses;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lcaflistaddress.AddressesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.lispaddresscontainer.address.lcaflist.LcafListAddr;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.lispaddresscontainer.address.lcaflist.LcafListAddrBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispsimpleaddress.primitiveaddress.Ipv6;
34 public class LispListLCAFAddressTest extends BaseTestCase {
37 public void deserialize__Simple() throws Exception {
38 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
40 "00 01 AA BB CC DD " + // IPv4
41 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
43 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
44 LcafListAddress lcafList = (LcafListAddress) address;
46 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
48 List<Addresses> addressList = lcafList.getAddresses();
49 assertEquals(2, addressList.size());
51 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")), addressList.get(0).getPrimitiveAddress());
52 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344")), addressList.get(1)
53 .getPrimitiveAddress());
57 public void deserialize__NoAddresses() throws Exception {
58 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
61 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
62 LcafListAddress lcafList = (LcafListAddress) address;
64 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
66 List<Addresses> addressList = lcafList.getAddresses();
67 assertEquals(0, addressList.size());
70 @Test(expected = LispSerializationException.class)
71 public void deserialize__ShorterBuffer() throws Exception {
72 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
74 "00 01 AA BB CC DD " + // IPv4
75 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
78 @Test(expected = LispSerializationException.class)
79 public void deserialize__ShorterBuffer2() throws Exception {
80 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
85 public void serialize__Simple() throws Exception {
86 LcafListAddrBuilder listBuilder = new LcafListAddrBuilder();
87 listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
88 listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
89 List<Addresses> addressList = new ArrayList<Addresses>();
90 addressList.add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")))
92 addressList.add(new AddressesBuilder().setPrimitiveAddress(
93 LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344"))).build());
94 listBuilder.setAddresses(addressList);
95 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
96 LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
97 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
99 "00 01 AA BB CC DD " + // IPv4
100 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
101 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
105 public void serialize__NoAddresses() throws Exception {
106 LcafListAddrBuilder listBuilder = new LcafListAddrBuilder();
107 listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
108 listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
109 List<Addresses> addressList = new ArrayList<Addresses>();
110 listBuilder.setAddresses(addressList);
111 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
112 LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
113 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
115 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
119 public void equals__Simple() throws Exception {
120 Ipv6 ip1 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"));
121 Ipv6 ip2 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:2"));
122 LcafListAddrBuilder listBuilder = new LcafListAddrBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
123 .setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode()).setAddresses(new ArrayList<Addresses>());
124 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
125 LcafListAddr address1 = listBuilder.build();
126 listBuilder.setAddresses(new ArrayList<Addresses>());
127 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
128 LcafListAddr address2 = listBuilder.build();
129 listBuilder.setAddresses(new ArrayList<Addresses>());
130 listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip2).build());
131 LcafListAddr address3 = listBuilder.build();
133 assertEquals(address1, address2);
134 Assert.assertNotEquals(address2, address3);
135 Assert.assertNotEquals(address1, address3);