331f875c570edbea367f69ae8de0dd5f6e469da7
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / NeutronSubnetIPAllocationPoolTest.java
1 /*
2  * Copyright (c) 2015 IBM Corporation.  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
9 package org.opendaylight.neutron.spi;
10
11 import java.math.BigInteger;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
15
16 public class NeutronSubnetIPAllocationPoolTest {
17     @Test
18     public void convertTest() {
19         Assert.assertEquals("Convert Test 1: null Argument failed",
20               0, NeutronSubnetIPAllocationPool.convert(null));
21
22         Assert.assertEquals("Convert Test 2: convert of 32.20.10.0 failed",
23               538184192, NeutronSubnetIPAllocationPool.convert("32.20.10.0"));
24
25         Assert.assertEquals("Convert Test 3: convert of 192.168.2.140 failed",
26               3232236172L, NeutronSubnetIPAllocationPool.convert("192.168.2.140"));
27     }
28
29     @Test
30     public void convertV6Test() {
31         boolean result = BigInteger.ZERO.equals(NeutronSubnetIPAllocationPool.convertV6(null));
32         Assert.assertTrue("Convert V6 Test 1: null Argument failed", result);
33
34         result = new BigInteger("42550872755692912415807417417958686721").
35             equals(NeutronSubnetIPAllocationPool.convertV6("2003:0:0:0:0:0:0:1"));
36         Assert.assertTrue("Convert V6 Test 2: 2003:0:0:0:0:0:0:1 Argument failed", result);
37     }
38
39
40     @Test
41     public void longToIPTest() {
42         Assert.assertEquals("longToIP Test 1: convert of 538184192L failed",
43               "32.20.10.0", NeutronSubnetIPAllocationPool.longToIP(538184192L));
44
45         Assert.assertEquals("longToIP Test 2: convert of 3232236172L failed",
46               "192.168.2.140", NeutronSubnetIPAllocationPool.longToIP(3232236172L));
47     }
48
49     @Test
50     public void bigIntegerToIPTest() {
51         BigInteger start = new BigInteger("42550872755692912415807417417958686721");
52         Assert.assertEquals("longToIP Test 1: convert of 42550872755692912415807417417958686721 failed",
53              "2003:0:0:0:0:0:0:1", NeutronSubnetIPAllocationPool.bigIntegerToIP(start));
54     }
55     @Test
56     public void containsTest() {
57         NeutronSubnetIPAllocationPool apUT = new NeutronSubnetIPAllocationPool();
58         apUT.setPoolStart("10.18.0.2");
59         apUT.setPoolEnd("10.18.0.4");
60
61         Assert.assertFalse("Contains Test 1: address one below pool start failed",
62               apUT.contains("10.18.0.1"));
63
64         Assert.assertTrue("Contains Test 2: address == pool start failed",
65               apUT.contains("10.18.0.2"));
66
67         Assert.assertTrue("Contains Test 3: adress in pool failed",
68               apUT.contains("10.18.0.3"));
69
70         Assert.assertTrue("Contains Test 4: address == pool end failed",
71               apUT.contains("10.18.0.4"));
72
73         Assert.assertFalse("Contains Test 5: one above pool end failed",
74               apUT.contains("10.18.0.5"));
75     }
76
77     @Test
78     public void containsV6Test() {
79         NeutronSubnetIPAllocationPool apUT = new NeutronSubnetIPAllocationPool();
80         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
81         apUT.setPoolEnd("2015:0:0:0:0:0:0:4");
82
83         Assert.assertFalse("Contains V6 Test 1: address one below pool start failed",
84               apUT.containsV6("2015:0:0:0:0:0:0:1"));
85
86         Assert.assertTrue("Contains V6 Test 2: address == pool start failed",
87               apUT.containsV6("2015:0:0:0:0:0:0:2"));
88
89         Assert.assertTrue("Contains V6 Test 3: adress in pool failed",
90               apUT.containsV6("2015:0:0:0:0:0:0:3"));
91
92         Assert.assertTrue("Contains V6 Test 4: address == pool end failed",
93               apUT.containsV6("2015:0:0:0:0:0:0:4"));
94
95         Assert.assertFalse("Contains V6 Test 5: one above pool end failed",
96               apUT.containsV6("2015:0:0:0:0:0:0:5"));
97     }
98
99     @Test
100     public void splitPoolTest() {
101         NeutronSubnetIPAllocationPool apUT = new NeutronSubnetIPAllocationPool();
102         apUT.setPoolStart("10.18.0.2");
103         apUT.setPoolEnd("10.18.0.6");
104         List<NeutronSubnetIPAllocationPool> result = apUT.splitPool("10.18.0.2");
105         Assert.assertEquals("splitPool Test 1.1: address == pool start (result size) failed",
106               1, result.size());
107         Assert.assertEquals("splitPool Test 1.2: address == pool start (pool start) failed",
108               "10.18.0.3", result.get(0).getPoolStart());
109         Assert.assertEquals("splitPool Test 1.3: address == pool start (pool end) failed",
110               "10.18.0.6", result.get(0).getPoolEnd());
111
112         apUT = new NeutronSubnetIPAllocationPool();
113         apUT.setPoolStart("10.18.0.2");
114         apUT.setPoolEnd("10.18.0.6");
115         result = apUT.splitPool("10.18.0.3");
116         Assert.assertEquals("splitPool Test 2.1: address one above pool start (result size) failed",
117               2, result.size());
118         Assert.assertEquals("splitPool Test 2.2: address one above pool start (pool 1 start) failed",
119               "10.18.0.2", result.get(0).getPoolStart());
120         Assert.assertEquals("splitPool Test 2.3: address one above pool start (pool 1 end) failed",
121               "10.18.0.2", result.get(0).getPoolEnd());
122         Assert.assertEquals("splitPool Test 2.4: address one above pool start (pool 2 start) failed",
123               "10.18.0.4", result.get(1).getPoolStart());
124         Assert.assertEquals("splitPool Test 2.5: address one above pool start (pool 2 end) failed",
125               "10.18.0.6", result.get(1).getPoolEnd());
126
127         apUT = new NeutronSubnetIPAllocationPool();
128         apUT.setPoolStart("10.18.0.2");
129         apUT.setPoolEnd("10.18.0.6");
130         result = apUT.splitPool("10.18.0.4");
131         Assert.assertEquals("splitPool Test 3.1: address one above pool start (result size) failed",
132               2, result.size());
133         Assert.assertEquals("splitPool Test 3.2: address one above pool start (pool 1 start) failed",
134               "10.18.0.2", result.get(0).getPoolStart());
135         Assert.assertEquals("splitPool Test 3.3: address one above pool start (pool 1 end) failed",
136               "10.18.0.3", result.get(0).getPoolEnd());
137         Assert.assertEquals("splitPool Test 3.4: address one above pool start (pool 2 start) failed",
138               "10.18.0.5", result.get(1).getPoolStart());
139         Assert.assertEquals("splitPool Test 3.5: address one above pool start (pool 2 end) failed",
140               "10.18.0.6", result.get(1).getPoolEnd());
141
142         apUT = new NeutronSubnetIPAllocationPool();
143         apUT.setPoolStart("10.18.0.2");
144         apUT.setPoolEnd("10.18.0.6");
145         result = apUT.splitPool("10.18.0.5");
146         Assert.assertEquals("splitPool Test 4.1: address one above pool start (result size) failed",
147               2, result.size());
148         Assert.assertEquals("splitPool Test 4.2: address one above pool start (pool 1 start) failed",
149               "10.18.0.2", result.get(0).getPoolStart());
150         Assert.assertEquals("splitPool Test 4.3: address one above pool start (pool 1 end) failed",
151               "10.18.0.4", result.get(0).getPoolEnd());
152         Assert.assertEquals("splitPool Test 4.4: address one above pool start (pool 2 start) failed",
153               "10.18.0.6", result.get(1).getPoolStart());
154         Assert.assertEquals("splitPool Test 4.5: address one above pool start (pool 2 end) failed",
155               "10.18.0.6", result.get(1).getPoolEnd());
156
157         apUT = new NeutronSubnetIPAllocationPool();
158         apUT.setPoolStart("10.18.0.2");
159         apUT.setPoolEnd("10.18.0.6");
160         result = apUT.splitPool("10.18.0.6");
161         Assert.assertEquals("splitPool Test 5.1: address == pool start (result size) failed",
162               1, result.size());
163         Assert.assertEquals("splitPool Test 5.2: address == pool start (pool start) failed",
164               "10.18.0.2", result.get(0).getPoolStart());
165         Assert.assertEquals("splitPool Test 5.3: address == pool start (pool end) failed",
166               "10.18.0.5", result.get(0).getPoolEnd());
167     }
168
169     @Test
170     public void splitPoolV6Test() {
171         NeutronSubnetIPAllocationPool apUT = new NeutronSubnetIPAllocationPool();
172         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
173         apUT.setPoolEnd("2015:0:0:0:0:0:0:6");
174         List<NeutronSubnetIPAllocationPool> result = apUT.splitPoolV6("2015:0:0:0:0:0:0:2");
175         Assert.assertEquals("splitPoolV6 Test 1.1: address == pool start (result size) failed",
176               1, result.size());
177         Assert.assertEquals("splitPoolV6 Test 1.2: address == pool start (pool start) failed",
178               "2015:0:0:0:0:0:0:3", result.get(0).getPoolStart());
179         Assert.assertEquals("splitPoolV6 Test 1.3: address == pool start (pool end) failed",
180               "2015:0:0:0:0:0:0:6", result.get(0).getPoolEnd());
181
182         apUT = new NeutronSubnetIPAllocationPool();
183         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
184         apUT.setPoolEnd("2015:0:0:0:0:0:0:6");
185         result = apUT.splitPoolV6("2015:0:0:0:0:0:0:3");
186         Assert.assertEquals("splitPoolV6 Test 2.1: address one above pool start (result size) failed",
187               2, result.size());
188         Assert.assertEquals("splitPoolV6 Test 2.2: address one above pool start (pool 1 start) failed",
189               "2015:0:0:0:0:0:0:2", result.get(0).getPoolStart());
190         Assert.assertEquals("splitPoolV6 Test 2.3: address one above pool start (pool 1 end) failed",
191               "2015:0:0:0:0:0:0:2", result.get(0).getPoolEnd());
192         Assert.assertEquals("splitPoolV6 Test 2.4: address one above pool start (pool 2 start) failed",
193               "2015:0:0:0:0:0:0:4", result.get(1).getPoolStart());
194         Assert.assertEquals("splitPoolV6 Test 2.5: address one above pool start (pool 2 end) failed",
195               "2015:0:0:0:0:0:0:6", result.get(1).getPoolEnd());
196
197         apUT = new NeutronSubnetIPAllocationPool();
198         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
199         apUT.setPoolEnd("2015:0:0:0:0:0:0:6");
200         result = apUT.splitPoolV6("2015:0:0:0:0:0:0:4");
201         Assert.assertEquals("splitPoolV6 Test 3.1: address one above pool start (result size) failed",
202               2, result.size());
203         Assert.assertEquals("splitPoolV6 Test 3.2: address one above pool start (pool 1 start) failed",
204               "2015:0:0:0:0:0:0:2", result.get(0).getPoolStart());
205         Assert.assertEquals("splitPoolV6 Test 3.3: address one above pool start (pool 1 end) failed",
206               "2015:0:0:0:0:0:0:3", result.get(0).getPoolEnd());
207         Assert.assertEquals("splitPoolV6 Test 3.4: address one above pool start (pool 2 start) failed",
208               "2015:0:0:0:0:0:0:5", result.get(1).getPoolStart());
209         Assert.assertEquals("splitPoolV6 Test 3.5: address one above pool start (pool 2 end) failed",
210               "2015:0:0:0:0:0:0:6", result.get(1).getPoolEnd());
211
212         apUT = new NeutronSubnetIPAllocationPool();
213         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
214         apUT.setPoolEnd("2015:0:0:0:0:0:0:6");
215         result = apUT.splitPoolV6("2015:0:0:0:0:0:0:5");
216         Assert.assertEquals("splitPoolV6 Test 4.1: address one above pool start (result size) failed",
217               2, result.size());
218         Assert.assertEquals("splitPoolV6 Test 4.2: address one above pool start (pool 1 start) failed",
219               "2015:0:0:0:0:0:0:2", result.get(0).getPoolStart());
220         Assert.assertEquals("splitPoolV6 Test 4.3: address one above pool start (pool 1 end) failed",
221               "2015:0:0:0:0:0:0:4", result.get(0).getPoolEnd());
222         Assert.assertEquals("splitPoolV6 Test 4.4: address one above pool start (pool 2 start) failed",
223               "2015:0:0:0:0:0:0:6", result.get(1).getPoolStart());
224         Assert.assertEquals("splitPoolV6 Test 4.5: address one above pool start (pool 2 end) failed",
225               "2015:0:0:0:0:0:0:6", result.get(1).getPoolEnd());
226
227         apUT = new NeutronSubnetIPAllocationPool();
228         apUT.setPoolStart("2015:0:0:0:0:0:0:2");
229         apUT.setPoolEnd("2015:0:0:0:0:0:0:6");
230         result = apUT.splitPoolV6("2015:0:0:0:0:0:0:6");
231         Assert.assertEquals("splitPoolV6 Test 5.1: address == pool start (result size) failed",
232               1, result.size());
233         Assert.assertEquals("splitPoolV6 Test 5.2: address == pool start (pool start) failed",
234               "2015:0:0:0:0:0:0:2", result.get(0).getPoolStart());
235         Assert.assertEquals("splitPoolV6 Test 5.3: address == pool start (pool end) failed",
236               "2015:0:0:0:0:0:0:5", result.get(0).getPoolEnd());
237     }
238 }