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.
81 * This method will return 0 if the input is null.
84 static long convert(String inputString) {
86 if (inputString != null) {
87 String[] parts = inputString.split("\\.");
88 for (String part: parts) {
90 ans |= Integer.parseInt(part);
97 * This static method converts the supplied high-ending long back
98 * into a dotted decimal representation of an IPv4 address
101 * high-endian representation of the IPv4 address as a long
102 * @returns IPv4 address in dotted decimal format
104 static String longtoIP(long l) {
106 String[] parts = new String[4];
107 for (i=0; i<4; i++) {
108 parts[3-i] = String.valueOf(l & 255);
111 return join(parts,".");
115 * helper routine used by longtoIP
117 public static String join(String r[],String d)
122 StringBuilder sb = new StringBuilder();
124 for(i=0;i<r.length-1;i++) {
127 return sb.toString()+r[i];
131 * This method splits the current instance by removing the supplied
134 * If the parameter is either the low or high address,
135 * then that member is adjusted and a list containing just this instance
138 * If the parameter is in the middle of the pool, then
139 * create two new instances, one ranging from low to parameter-1
140 * the other ranging from parameter+1 to high
142 public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
143 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
144 long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
145 long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
146 long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
148 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
149 boolean poolStarted = false;
150 for (i=sIP; i<=eIP; i++) {
153 p.setPoolStart(poolStart);
157 p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i+1));
162 p.setPoolEnd(poolEnd);
164 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
168 if (i != sIP && i != eIP) {
171 p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
175 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
178 p = new NeutronSubnet_IPAllocationPool();