83d90d6c5b70f29cf918ad6633d7ff0c1bc796ba
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / AfiListSerializerTest.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.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 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;
28
29 public class AfiListSerializerTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
34                 + "01 00 00 18 "
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
37
38         assertEquals(AfiListLcaf.class, address.getAddressType());
39         AfiList afiList = (AfiList) address.getAddress();
40
41         List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
42         assertEquals(2, addressList.size());
43
44         assertEquals("170.187.204.221", addressList.get(0).stringValue());
45         assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", addressList.get(1).stringValue());
46     }
47
48     @Test
49     public void deserialize__NoAddresses() throws Exception {
50         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
51                 + "01 00 00 00 "));
52
53         assertEquals(AfiListLcaf.class, address.getAddressType());
54         AfiList afiList = (AfiList) address.getAddress();
55
56         List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
57         assertEquals(0, addressList.size());
58     }
59
60     @Test(expected = LispSerializationException.class)
61     public void deserialize__ShorterBuffer() throws Exception {
62         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
63                 + "01 00 00 18 "
64                 + "00 01 AA BB CC DD " // IPv4
65                 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
66     }
67
68     @Test(expected = LispSerializationException.class)
69     public void deserialize__ShorterBuffer2() throws Exception {
70         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
71                 + "01 00 00 18 "));
72     }
73
74     @Test
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);
81
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());
88
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 "
92                 + "01 00 00 18 "
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());
96     }
97
98     @Test
99     public void serialize__NoAddresses() throws Exception {
100         AfiListBuilder listBuilder = new AfiListBuilder();
101         List<SimpleAddress> addressList = new ArrayList<>();
102         listBuilder.setAddressList(addressList);
103
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());
110
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 "
114                 + "01 00 00 00");
115         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
116     }
117
118     @Test
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");
122
123         AfiListBuilder listBuilder = new AfiListBuilder().setAddressList(new ArrayList<SimpleAddress>());
124
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();
136
137         assertEquals(address1, address2);
138         Assert.assertNotEquals(address2, address3);
139         Assert.assertNotEquals(address1, address3);
140     }
141 }