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 com.google.common.collect.ImmutableSet;
13 import java.nio.ByteBuffer;
14 import java.util.Iterator;
16 import junitx.framework.ArrayAssert;
17 import junitx.framework.Assert;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
20 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
30 public class AfiListSerializerTest extends BaseTestCase {
33 public void deserialize__Simple() throws Exception {
34 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
36 + "00 01 AA BB CC DD " // IPv4
37 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
39 assertEquals(AfiListLcaf.VALUE, address.getAddressType());
40 AfiList afiList = (AfiList) address.getAddress();
42 Set<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
43 assertEquals(2, addressList.size());
45 Iterator<SimpleAddress> it = addressList.iterator();
46 assertEquals("170.187.204.221", it.next().stringValue());
47 assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", it.next().stringValue());
51 public void deserialize__NoAddresses() throws Exception {
52 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
55 assertEquals(AfiListLcaf.VALUE, address.getAddressType());
56 AfiList afiList = (AfiList) address.getAddress();
58 assertEquals(Set.of(), afiList.getAfiList().getAddressList());
61 @Test(expected = LispSerializationException.class)
62 public void deserialize__ShorterBuffer() throws Exception {
63 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
67 00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
70 @Test(expected = LispSerializationException.class)
71 public void deserialize__ShorterBuffer2() throws Exception {
72 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
77 public void serialize__Simple() throws Exception {
78 AfiListBuilder listBuilder = new AfiListBuilder();
79 listBuilder.setAddressList(ImmutableSet.of(
80 SimpleAddressBuilder.getDefaultInstance("170.187.204.221"),
81 SimpleAddressBuilder.getDefaultInstance("1122:3344:1122:3344:1122:3344:1122:3344")));
83 RlocBuilder rb = new RlocBuilder();
84 rb.setAddressType(AfiListLcaf.VALUE);
85 rb.setVirtualNetworkId(null);
86 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
87 .lisp.address.address.AfiListBuilder()
88 .setAfiList(listBuilder.build()).build());
90 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
91 LispAddressSerializer.getInstance().serialize(buf, rb.build());
92 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
94 + "00 01 AA BB CC DD " // IPv4
95 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
96 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
100 public void serialize__NoAddresses() throws Exception {
101 AfiListBuilder listBuilder = new AfiListBuilder();
102 listBuilder.setAddressList(Set.of());
104 RlocBuilder rb = new RlocBuilder();
105 rb.setAddressType(AfiListLcaf.VALUE);
106 rb.setVirtualNetworkId(null);
107 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
108 .lisp.address.address.AfiListBuilder()
109 .setAfiList(listBuilder.build()).build());
111 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
112 LispAddressSerializer.getInstance().serialize(buf, rb.build());
113 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
115 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
119 public void equals__Simple() throws Exception {
120 final SimpleAddress ip1 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:1");
121 final SimpleAddress ip2 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:2");
123 AfiListBuilder listBuilder = new AfiListBuilder().setAddressList(Set.of(ip1));
125 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
126 .lisp.address.address.afi.list.AfiList address1 = listBuilder.build();
127 listBuilder.setAddressList(Set.of(ip1));
128 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
129 .lisp.address.address.afi.list.AfiList address2 = listBuilder.build();
130 listBuilder.setAddressList(Set.of(ip2));
131 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
132 .lisp.address.address.afi.list.AfiList address3 = listBuilder.build();
134 assertEquals(address1, address2);
135 Assert.assertNotEquals(address2, address3);
136 Assert.assertNotEquals(address1, address3);