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.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.List;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
21 @XmlAccessorType(XmlAccessType.NONE)
22 public class NeutronSubnet_IPAllocationPool implements Serializable {
23 private static final long serialVersionUID = 1L;
25 // See OpenStack Network API v2.0 Reference for description of
26 // annotated attributes
28 @XmlElement(name="start")
31 @XmlElement(name="end")
34 public NeutronSubnet_IPAllocationPool() {
37 public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
38 poolStart = lowAddress;
39 poolEnd = highAddress;
42 public String getPoolStart() {
46 public void setPoolStart(String poolStart) {
47 this.poolStart = poolStart;
50 public String getPoolEnd() {
54 public void setPoolEnd(String poolEnd) {
55 this.poolEnd = poolEnd;
59 * This method determines if this allocation pool contains the
63 * IPv4 address in dotted decimal format
64 * @returns a boolean on whether the pool contains the address or not
67 public boolean contains(String inputString) {
68 long inputIP = convert(inputString);
69 long startIP = convert(poolStart);
70 long endIP = convert(poolEnd);
71 return (inputIP >= startIP && inputIP <= endIP);
75 * This static method converts the supplied IPv4 address to a long
76 * integer for comparison
79 * IPv4 address in dotted decimal format
80 * @returns high-endian representation of the IPv4 address as a long
83 static long convert(String inputString) {
85 String[] parts = inputString.split("\\.");
86 for (String part: parts) {
88 ans |= Integer.parseInt(part);
94 * This static method converts the supplied high-ending long back
95 * into a dotted decimal representation of an IPv4 address
98 * high-endian representation of the IPv4 address as a long
99 * @returns IPv4 address in dotted decimal format
101 static String longtoIP(long l) {
103 String[] parts = new String[4];
104 for (i=0; i<4; i++) {
105 parts[3-i] = String.valueOf(l & 255);
108 return join(parts,".");
112 * helper routine used by longtoIP
114 public static String join(String r[],String d)
119 StringBuilder sb = new StringBuilder();
121 for(i=0;i<r.length-1;i++) {
124 return sb.toString()+r[i];
128 * This method splits the current instance by removing the supplied
131 * If the parameter is either the low or high address,
132 * then that member is adjusted and a list containing just this instance
135 * If the parameter is in the middle of the pool, then
136 * create two new instances, one ranging from low to parameter-1
137 * the other ranging from parameter+1 to high
139 public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
140 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
141 long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
142 long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
143 long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
145 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
146 boolean poolStarted = false;
147 for (i=sIP; i<=eIP; i++) {
150 p.setPoolStart(poolStart);
156 p.setPoolEnd(poolEnd);
158 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
162 if (i != sIP && i != eIP) {
165 p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
169 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
172 p = new NeutronSubnet_IPAllocationPool();