Merge "Use odlparent-lite as artifacts parent"
[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
16 import junitx.framework.ArrayAssert;
17 import junitx.framework.Assert;
18
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
21 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
22 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
31
32 public class AfiListSerializerTest extends BaseTestCase {
33
34     @Test
35     public void deserialize__Simple() throws Exception {
36         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
37                 "01 00 00 18 " + //
38                 "00 01 AA BB CC DD " + // IPv4
39                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
40
41         assertEquals(AfiListLcaf.class, address.getAddressType());
42         AfiList afiList = (AfiList) address.getAddress();
43
44         List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
45         assertEquals(2, addressList.size());
46
47         assertEquals("170.187.204.221", String.valueOf(addressList.get(0).getValue()));
48         assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", String.valueOf(addressList.get(1).getValue()));
49     }
50
51     @Test
52     public void deserialize__NoAddresses() throws Exception {
53         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
54                 "01 00 00 00 "));
55
56         assertEquals(AfiListLcaf.class, address.getAddressType());
57         AfiList afiList = (AfiList) address.getAddress();
58
59         List<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
60         assertEquals(0, addressList.size());
61     }
62
63     @Test(expected = LispSerializationException.class)
64     public void deserialize__ShorterBuffer() throws Exception {
65         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
66                 "01 00 00 18 " + //
67                 "00 01 AA BB CC DD " + // IPv4
68                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
69     }
70
71     @Test(expected = LispSerializationException.class)
72     public void deserialize__ShorterBuffer2() throws Exception {
73         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 " + //
74                 "01 00 00 18 "));
75     }
76
77     @Test
78     public void serialize__Simple() throws Exception {
79         AfiListBuilder listBuilder = new AfiListBuilder();
80         List<SimpleAddress> addressList = new ArrayList<SimpleAddress>();
81         addressList.add(SimpleAddressBuilder.getDefaultInstance("170.187.204.221"));
82         addressList.add(SimpleAddressBuilder.getDefaultInstance("1122:3344:1122:3344:1122:3344:1122:3344"));
83         listBuilder.setAddressList(addressList);
84
85         RlocBuilder rb = new RlocBuilder();
86         rb.setAddressType(AfiListLcaf.class);
87         rb.setVirtualNetworkId(null);
88         rb.setAddress((Address)
89                 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiListBuilder()
90                 .setAfiList(listBuilder.build()).build());
91
92         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
93         LispAddressSerializer.getInstance().serialize(buf, rb.build());
94         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
95                 "01 00 00 18 " + //
96                 "00 01 AA BB CC DD " + // IPv4
97                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
98         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
99     }
100
101     @Test
102     public void serialize__NoAddresses() throws Exception {
103         AfiListBuilder listBuilder = new AfiListBuilder();
104         List<SimpleAddress> addressList = new ArrayList<SimpleAddress>();
105         listBuilder.setAddressList(addressList);
106
107         RlocBuilder rb = new RlocBuilder();
108         rb.setAddressType(AfiListLcaf.class);
109         rb.setVirtualNetworkId(null);
110         rb.setAddress((Address)
111                 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiListBuilder()
112                 .setAfiList(listBuilder.build()).build());
113
114         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
115         LispAddressSerializer.getInstance().serialize(buf, rb.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         SimpleAddress ip1 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:1");
124         SimpleAddress ip2 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:2");
125
126         AfiListBuilder listBuilder = new AfiListBuilder().setAddressList(new ArrayList<SimpleAddress>());
127
128         listBuilder.getAddressList().add(ip1);
129         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiList address1 = listBuilder.build();
130         listBuilder.setAddressList(new ArrayList<SimpleAddress>());
131         listBuilder.getAddressList().add(ip1);
132         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiList address2 = listBuilder.build();
133         listBuilder.setAddressList(new ArrayList<SimpleAddress>());
134         listBuilder.getAddressList().add(ip2);
135         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.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 }