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.tools.junit.BaseTestCase;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
32 public class AfiListSerializerTest extends BaseTestCase {
35 public void deserialize__Simple() throws Exception {
36 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
38 "00 01 AA BB CC DD " + // IPv4
39 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
41 assertEquals(AfiListLcaf.class, address.getAddressType());
42 AfiList afiList = (AfiList) address.getAddress();
44 List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
45 assertEquals(2, addressList.size());
47 assertEquals("170.187.204.221", String.valueOf(addressList.get(0).getValue()));
48 assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", String.valueOf(addressList.get(1).getValue()));
52 public void deserialize__NoAddresses() throws Exception {
53 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
56 assertEquals(AfiListLcaf.class, address.getAddressType());
57 AfiList afiList = (AfiList) address.getAddress();
59 List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
60 assertEquals(0, addressList.size());
63 @Test(expected = LispSerializationException.class)
64 public void deserialize__ShorterBuffer() throws Exception {
65 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
67 "00 01 AA BB CC DD " + // IPv4
68 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
71 @Test(expected = LispSerializationException.class)
72 public void deserialize__ShorterBuffer2() throws Exception {
73 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
78 public void serialize__Simple() throws Exception {
79 AfiListBuilder listBuilder = new AfiListBuilder();
80 List<SimpleAddress> addressList = new ArrayList<SimpleAddress>();
81 addressList.add(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"));
82 addressList.add(SimpleAddressBuilder.getDefaultInstance("1122:3344:1122:3344:1122:3344:1122:3344"));
83 listBuilder.setAddressList(addressList);
85 RlocBuilder rb = new RlocBuilder();
86 rb.setAddressType(AfiListLcaf.class);
87 rb.setVirtualNetworkId(null);
88 rb.setAddress((Address)
89 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiListBuilder()
90 .setAfiList(listBuilder.build()).build());
92 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
93 LispAddressSerializer.getInstance().serialize(buf, rb.build());
94 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
96 "00 01 AA BB CC DD " + // IPv4
97 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
98 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
102 public void serialize__NoAddresses() throws Exception {
103 AfiListBuilder listBuilder = new AfiListBuilder();
104 List<SimpleAddress> addressList = new ArrayList<SimpleAddress>();
105 listBuilder.setAddressList(addressList);
107 RlocBuilder rb = new RlocBuilder();
108 rb.setAddressType(AfiListLcaf.class);
109 rb.setVirtualNetworkId(null);
110 rb.setAddress((Address)
111 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiListBuilder()
112 .setAfiList(listBuilder.build()).build());
114 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
115 LispAddressSerializer.getInstance().serialize(buf, rb.build());
116 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
118 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
122 public void equals__Simple() throws Exception {
123 SimpleAddress ip1 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:1");
124 SimpleAddress ip2 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:2");
126 AfiListBuilder listBuilder = new AfiListBuilder().setAddressList(new ArrayList<SimpleAddress>());
128 listBuilder.getAddressList().add(ip1);
129 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiList address1 = listBuilder.build();
130 listBuilder.setAddressList(new ArrayList<SimpleAddress>());
131 listBuilder.getAddressList().add(ip1);
132 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiList address2 = listBuilder.build();
133 listBuilder.setAddressList(new ArrayList<SimpleAddress>());
134 listBuilder.getAddressList().add(ip2);
135 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiList address3 = listBuilder.build();
137 assertEquals(address1, address2);
138 Assert.assertNotEquals(address2, address3);
139 Assert.assertNotEquals(address1, address3);