2 * Copyright IBM Corporation, 2013. All rights reserved.
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
9 package org.opendaylight.neutron.spi;
11 import java.io.Serializable;
12 import java.math.BigInteger;
13 import java.net.InetAddress;
14 import java.net.Inet6Address;
15 import java.util.ArrayList;
16 import java.util.List;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
24 @XmlAccessorType(XmlAccessType.NONE)
25 public class NeutronSubnet_IPAllocationPool implements Serializable {
26 private static final long serialVersionUID = 1L;
28 // See OpenStack Network API v2.0 Reference for description of
29 // annotated attributes
31 @XmlElement(name="start")
34 @XmlElement(name="end")
37 public NeutronSubnet_IPAllocationPool() {
40 public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
41 poolStart = lowAddress;
42 poolEnd = highAddress;
45 public String getPoolStart() {
49 public void setPoolStart(String poolStart) {
50 this.poolStart = poolStart;
53 public String getPoolEnd() {
57 public void setPoolEnd(String poolEnd) {
58 this.poolEnd = poolEnd;
62 * This method determines if this allocation pool contains the
66 * IPv4 address in dotted decimal format
67 * @returns a boolean on whether the pool contains the address or not
70 public boolean contains(String inputString) {
71 long inputIP = convert(inputString);
72 long startIP = convert(poolStart);
73 long endIP = convert(poolEnd);
74 return (inputIP >= startIP && inputIP <= endIP);
78 * This static method converts the supplied IPv4 address to a long
79 * integer for comparison
82 * IPv4 address in dotted decimal format
83 * @returns high-endian representation of the IPv4 address as a long.
84 * This method will return 0 if the input is null.
87 static long convert(String inputString) {
89 if (inputString != null) {
90 String[] parts = inputString.split("\\.");
91 for (String part: parts) {
93 ans |= Integer.parseInt(part);
100 * This method determines if this allocation pool contains the
104 * IPv4 address in dotted decimal format
105 * @returns a boolean on whether the pool contains the address or not
108 public boolean contains_V6(String inputString) {
109 BigInteger inputIP = convert_V6(inputString);
110 BigInteger startIP = convert_V6(poolStart);
111 BigInteger endIP = convert_V6(poolEnd);
112 return (inputIP.compareTo(startIP) >= 0 && inputIP.compareTo(endIP) <= 0);
116 * This static method converts the supplied IPv4 address to a long
117 * integer for comparison
120 * IPv6 address in dotted decimal format
121 * @returns high-endian representation of the IPv4 address as a BigInteger.
122 * This method will return 0 if the input is null.
125 static BigInteger convert_V6(String inputString) {
126 if (inputString == null) {
127 return BigInteger.ZERO;
130 return new BigInteger(((Inet6Address) InetAddress.getByName(inputString)).getAddress());
131 } catch (Exception e) {
132 return BigInteger.ZERO;
137 * This static method converts the supplied high-ending long back
138 * into a dotted decimal representation of an IPv4 address
141 * high-endian representation of the IPv4 address as a long
142 * @returns IPv4 address in dotted decimal format
144 static String longToIP(long l) {
146 String[] parts = new String[4];
147 for (i=0; i<4; i++) {
148 parts[3-i] = String.valueOf(l & 255);
151 return join(parts,".");
155 * This static method converts the supplied high-ending long back
156 * into a dotted decimal representation of an IPv4 address
159 * high-endian representation of the IPv4 address as a long
160 * @returns IPv4 address in dotted decimal format
162 static String bigIntegerToIP(BigInteger b) {
164 return Inet6Address.getByAddress(b.toByteArray()).getHostAddress();
165 } catch (Exception e) {
171 * helper routine used by longToIP
173 public static String join(String r[],String d)
178 StringBuilder sb = new StringBuilder();
180 for(i=0;i<r.length-1;i++) {
183 return sb.toString()+r[i];
187 * This method splits the current instance by removing the supplied
190 * If the parameter is either the low or high address,
191 * then that member is adjusted and a list containing just this instance
194 * If the parameter is in the middle of the pool, then
195 * create two new instances, one ranging from low to parameter-1
196 * the other ranging from parameter+1 to high
198 public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
199 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
200 long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
201 long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
202 long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
204 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
205 boolean poolStarted = false;
206 for (i=sIP; i<=eIP; i++) {
209 p.setPoolStart(poolStart);
213 p.setPoolStart(NeutronSubnet_IPAllocationPool.longToIP(i+1));
218 p.setPoolEnd(poolEnd);
220 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longToIP(i-1));
224 if (i != sIP && i != eIP) {
227 p.setPoolStart(NeutronSubnet_IPAllocationPool.longToIP(i));
231 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longToIP(i-1));
234 p = new NeutronSubnet_IPAllocationPool();
236 p.setPoolStart(NeutronSubnet_IPAllocationPool.longToIP(i+1));
245 public String toString() {
246 return "NeutronSubnetIPAllocationPool [" +
247 "start=" + poolStart +
248 ", end=" + poolEnd + "]";
252 * This method splits the current instance by removing the supplied
255 * If the parameter is either the low or high address,
256 * then that member is adjusted and a list containing just this instance
259 * If the parameter is in the middle of the pool, then
260 * create two new instances, one ranging from low to parameter-1
261 * the other ranging from parameter+1 to high
262 * If the pool is a single address, return null
264 public List<NeutronSubnet_IPAllocationPool> splitPool_V6(String ipAddress) {
265 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
266 BigInteger gIP = NeutronSubnet_IPAllocationPool.convert_V6(ipAddress);
267 BigInteger sIP = NeutronSubnet_IPAllocationPool.convert_V6(poolStart);
268 BigInteger eIP = NeutronSubnet_IPAllocationPool.convert_V6(poolEnd);
269 if (gIP.compareTo(sIP) == 0 && gIP.compareTo(eIP) < 0) {
270 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
271 p.setPoolStart(NeutronSubnet_IPAllocationPool.bigIntegerToIP(sIP.add(BigInteger.ONE)));
272 p.setPoolEnd(poolEnd);
276 if (gIP.compareTo(eIP) == 0 && gIP.compareTo(sIP) > 0) {
277 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
278 p.setPoolStart(poolStart);
279 p.setPoolEnd(NeutronSubnet_IPAllocationPool.bigIntegerToIP(eIP.subtract(BigInteger.ONE)));
283 if (gIP.compareTo(eIP) < 0 && gIP.compareTo(sIP) > 0) {
284 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
285 p.setPoolStart(poolStart);
286 p.setPoolEnd(NeutronSubnet_IPAllocationPool.bigIntegerToIP(gIP.subtract(BigInteger.ONE)));
288 NeutronSubnet_IPAllocationPool p2 = new NeutronSubnet_IPAllocationPool();
289 p2.setPoolStart(NeutronSubnet_IPAllocationPool.bigIntegerToIP(gIP.add(BigInteger.ONE)));
290 p2.setPoolEnd(poolEnd);