Use YANG java files instead of the old model TELSDN-474 #close
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispListLCAFAddressTest.java
1 /*
2  * Copyright (c) 2013 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 junit.framework.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import junitx.framework.ArrayAssert;
18 import junitx.framework.Assert;
19
20 import org.junit.Test;
21 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
22 import org.opendaylight.lispflowmapping.implementation.serializer.LispAFIConvertor;
23 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
24 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
25 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.Addresses;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.AddressesBuilder;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafList;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafListBuilder;
32 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.PrimitiveAddress;
33 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv6;
34 import org.opendaylight.yangtools.yang.binding.Augmentation;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36
37 public class LispListLCAFAddressTest extends BaseTestCase {
38
39     @Test
40     public void deserialize__Simple() throws Exception {
41         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
42                 "01 00 00 18 " + //
43                 "00 01 AA BB CC DD " + // IPv4
44                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
45
46         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
47         LcafListAddress lcafList = (LcafListAddress) address;
48
49         assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
50
51         List<Addresses> addressList = lcafList.getAddresses();
52         assertEquals(2, addressList.size());
53
54         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")), addressList.get(0).getPrimitiveAddress());
55         assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344")), addressList.get(1)
56                 .getPrimitiveAddress());
57     }
58
59     @Test
60     public void deserialize__NoAddresses() throws Exception {
61         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
62                 "01 00 00 00 "));
63
64         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
65         LcafListAddress lcafList = (LcafListAddress) address;
66
67         assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lcafList.getLcafType().byteValue());
68
69         List<Addresses> addressList = lcafList.getAddresses();
70         assertEquals(0, addressList.size());
71     }
72
73     @Test(expected = LispSerializationException.class)
74     public void deserialize__ShorterBuffer() throws Exception {
75         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
76                 "01 00 00 18 " + //
77                 "00 01 AA BB CC DD " + // IPv4
78                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
79     }
80
81     @Test(expected = LispSerializationException.class)
82     public void deserialize__ShorterBuffer2() throws Exception {
83         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
84                 "01 00 00 18 "));
85     }
86
87     @Test
88     public void serialize__Simple() throws Exception {
89         LcafListBuilder listBuilder = new LcafListBuilder();
90         listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
91         listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
92         List<Addresses> addressList = new ArrayList<Addresses>();
93         addressList.add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPAfiAddress("170.187.204.221")))
94                 .build());
95         addressList.add(new AddressesBuilder().setPrimitiveAddress(
96                 LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:1122:3344:1122:3344:1122:3344"))).build());
97         listBuilder.setAddresses(addressList);
98         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
99         LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
100         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
101                 "01 00 00 18 " + //
102                 "00 01 AA BB CC DD " + // IPv4
103                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
104         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
105     }
106
107     @Test
108     public void serialize__NoAddresses() throws Exception {
109         LcafListBuilder listBuilder = new LcafListBuilder();
110         listBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
111         listBuilder.setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
112         List<Addresses> addressList = new ArrayList<Addresses>();
113         listBuilder.setAddresses(addressList);
114         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(listBuilder.build()));
115         LispAddressSerializer.getInstance().serialize(buf, listBuilder.build());
116         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
117                 "01 00 00 00");
118         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
119     }
120
121     @Test
122     public void equals__Simple() throws Exception {
123         Ipv6 ip1 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"));
124         Ipv6 ip2 = (Ipv6) LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:2"));
125         LcafListBuilder listBuilder = new LcafListBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
126                 .setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode()).setAddresses(new ArrayList<Addresses>());
127         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
128         LcafList address1 = listBuilder.build();
129         listBuilder.setAddresses(new ArrayList<Addresses>());
130         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip1).build());
131         LcafList address2 = listBuilder.build();
132         listBuilder.setAddresses(new ArrayList<Addresses>());
133         listBuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(ip2).build());
134         LcafList address3 = listBuilder.build();
135
136         assertEquals(address1, address2);
137         Assert.assertNotEquals(address2, address3);
138         Assert.assertNotEquals(address1, address3);
139     }
140 }