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;
15 import junitx.framework.ArrayAssert;
16 import junitx.framework.Assert;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
19 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
20 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
29 public class AfiListSerializerTest extends BaseTestCase {
32 public void deserialize__Simple() throws Exception {
33 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
35 + "00 01 AA BB CC DD " // IPv4
36 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
38 assertEquals(AfiListLcaf.class, address.getAddressType());
39 AfiList afiList = (AfiList) address.getAddress();
41 List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
42 assertEquals(2, addressList.size());
44 assertEquals("170.187.204.221", addressList.get(0).stringValue());
45 assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", addressList.get(1).stringValue());
49 public void deserialize__NoAddresses() throws Exception {
50 Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
53 assertEquals(AfiListLcaf.class, address.getAddressType());
54 AfiList afiList = (AfiList) address.getAddress();
56 List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
57 assertEquals(0, addressList.size());
60 @Test(expected = LispSerializationException.class)
61 public void deserialize__ShorterBuffer() throws Exception {
62 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
64 + "00 01 AA BB CC DD " // IPv4
65 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
68 @Test(expected = LispSerializationException.class)
69 public void deserialize__ShorterBuffer2() throws Exception {
70 LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
75 public void serialize__Simple() throws Exception {
76 AfiListBuilder listBuilder = new AfiListBuilder();
77 List<SimpleAddress> addressList = new ArrayList<>();
78 addressList.add(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"));
79 addressList.add(SimpleAddressBuilder.getDefaultInstance("1122:3344:1122:3344:1122:3344:1122:3344"));
80 listBuilder.setAddressList(addressList);
82 RlocBuilder rb = new RlocBuilder();
83 rb.setAddressType(AfiListLcaf.class);
84 rb.setVirtualNetworkId(null);
85 rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
86 .lisp.address.address.AfiListBuilder()
87 .setAfiList(listBuilder.build()).build());
89 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
90 LispAddressSerializer.getInstance().serialize(buf, rb.build());
91 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
93 + "00 01 AA BB CC DD " // IPv4
94 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
95 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
99 public void serialize__NoAddresses() throws Exception {
100 AfiListBuilder listBuilder = new AfiListBuilder();
101 List<SimpleAddress> addressList = new ArrayList<>();
102 listBuilder.setAddressList(addressList);
104 RlocBuilder rb = new RlocBuilder();
105 rb.setAddressType(AfiListLcaf.class);
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(new ArrayList<SimpleAddress>());
125 listBuilder.getAddressList().add(ip1);
126 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
127 .lisp.address.address.afi.list.AfiList address1 = listBuilder.build();
128 listBuilder.setAddressList(new ArrayList<SimpleAddress>());
129 listBuilder.getAddressList().add(ip1);
130 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
131 .lisp.address.address.afi.list.AfiList address2 = listBuilder.build();
132 listBuilder.setAddressList(new ArrayList<SimpleAddress>());
133 listBuilder.getAddressList().add(ip2);
134 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
135 .lisp.address.address.afi.list.AfiList address3 = listBuilder.build();
137 assertEquals(address1, address2);
138 Assert.assertNotEquals(address2, address3);
139 Assert.assertNotEquals(address1, address3);