Move integration tests to mdsal-it-parent
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispListLCAFAddressTest.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.serializer.address;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import junitx.framework.ArrayAssert;
17 import junitx.framework.Assert;
18
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.urn.opendaylight.lfm.control.plane.rev150314.LcafListAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcaflistaddress.Addresses;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcaflistaddress.AddressesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcaflist.LcafListAddr;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcaflist.LcafListAddrBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.primitiveaddress.Ipv6;
32
33 public class LispListLCAFAddressTest extends BaseTestCase {
34
35     @Test
36     public void deserialize__Simple() throws Exception {
37         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
38                 "01 00 00 18 " + //
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
41
42         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
43         LcafListAddress lcafList = (LcafListAddress) address;
44
45         assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
46
47         List<Addresses> addressList = lcafList.getAddresses();
48         assertEquals(2, addressList.size());
49
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());
53     }
54
55     @Test
56     public void deserialize__NoAddresses() throws Exception {
57         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
58                 "01 00 00 00 "));
59
60         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
61         LcafListAddress lcafList = (LcafListAddress) address;
62
63         assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
64
65         List<Addresses> addressList = lcafList.getAddresses();
66         assertEquals(0, addressList.size());
67     }
68
69     @Test(expected = LispSerializationException.class)
70     public void deserialize__ShorterBuffer() throws Exception {
71         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
72                 "01 00 00 18 " + //
73                 "00 01 AA BB CC DD " + // IPv4
74                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
75     }
76
77     @Test(expected = LispSerializationException.class)
78     public void deserialize__ShorterBuffer2() throws Exception {
79         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
80                 "01 00 00 18 "));
81     }
82
83     @Test
84     public void serialize__Simple() throws Exception {
85         LcafListAddrBuilder listBuilder = new LcafListAddrBuilder();
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")))
90                 .build());
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 " + //
97                 "01 00 00 18 " + //
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());
101     }
102
103     @Test
104     public void serialize__NoAddresses() throws Exception {
105         LcafListAddrBuilder listBuilder = new LcafListAddrBuilder();
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 " + //
113                 "01 00 00 00");
114         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
115     }
116
117     @Test
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         LcafListAddrBuilder listBuilder = new LcafListAddrBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
122                 .setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode()).setAddresses(new ArrayList<Addresses>());
123         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
124         LcafListAddr address1 = listBuilder.build();
125         listBuilder.setAddresses(new ArrayList<Addresses>());
126         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
127         LcafListAddr address2 = listBuilder.build();
128         listBuilder.setAddresses(new ArrayList<Addresses>());
129         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip2).build());
130         LcafListAddr address3 = listBuilder.build();
131
132         assertEquals(address1, address2);
133         Assert.assertNotEquals(address2, address3);
134         Assert.assertNotEquals(address1, address3);
135     }
136 }