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.controller.networkconfig.neutron;
11 import java.util.ArrayList;
12 import java.util.List;
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
20 @XmlAccessorType(XmlAccessType.NONE)
21 public class NeutronSubnet_IPAllocationPool {
22 // See OpenStack Network API v2.0 Reference for description of
23 // annotated attributes
25 @XmlElement(name="start")
28 @XmlElement(name="end")
31 public NeutronSubnet_IPAllocationPool() { }
33 public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
34 poolStart = lowAddress;
35 poolEnd = highAddress;
38 public String getPoolStart() {
42 public void setPoolStart(String poolStart) {
43 this.poolStart = poolStart;
46 public String getPoolEnd() {
50 public void setPoolEnd(String poolEnd) {
51 this.poolEnd = poolEnd;
55 * This method determines if this allocation pool contains the
59 * IPv4 address in dotted decimal format
60 * @returns a boolean on whether the pool contains the address or not
63 public boolean contains(String inputString) {
64 long inputIP = convert(inputString);
65 long startIP = convert(poolStart);
66 long endIP = convert(poolEnd);
67 return (inputIP >= startIP && inputIP <= endIP);
71 * This static method converts the supplied IPv4 address to a long
72 * integer for comparison
75 * IPv4 address in dotted decimal format
76 * @returns high-endian representation of the IPv4 address as a long
79 static long convert(String inputString) {
81 String[] parts = inputString.split("\\.");
82 for (String part: parts) {
84 ans |= Integer.parseInt(part);
90 * This static method converts the supplied high-ending long back
91 * into a dotted decimal representation of an IPv4 address
94 * high-endian representation of the IPv4 address as a long
95 * @returns IPv4 address in dotted decimal format
97 static String longtoIP(long l) {
99 String[] parts = new String[4];
100 for (i=0; i<4; i++) {
101 parts[3-i] = String.valueOf(l & 255);
104 return join(parts,".");
108 * helper routine used by longtoIP
110 public static String join(String r[],String d)
115 StringBuilder sb = new StringBuilder();
117 for(i=0;i<r.length-1;i++) {
120 return sb.toString()+r[i];
124 * This method splits the current instance by removing the supplied
127 * If the parameter is either the low or high address,
128 * then that member is adjusted and a list containing just this instance
131 * If the parameter is in the middle of the pool, then
132 * create two new instances, one ranging from low to parameter-1
133 * the other ranging from parameter+1 to high
135 public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
136 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
137 long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
138 long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
139 long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
141 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
142 boolean poolStarted = false;
143 for (i=sIP; i<=eIP; i++) {
146 p.setPoolStart(poolStart);
152 p.setPoolEnd(poolEnd);
154 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
158 if (i != sIP && i != eIP) {
161 p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
165 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
168 p = new NeutronSubnet_IPAllocationPool();