2 * Copyright IBM Corporation, 2013. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.controller.networkconfig.neutron;
\r
11 import java.util.ArrayList;
\r
12 import java.util.List;
\r
14 import javax.xml.bind.annotation.XmlAccessType;
\r
15 import javax.xml.bind.annotation.XmlAccessorType;
\r
16 import javax.xml.bind.annotation.XmlElement;
\r
17 import javax.xml.bind.annotation.XmlRootElement;
\r
20 @XmlAccessorType(XmlAccessType.NONE)
\r
21 public class NeutronSubnet_IPAllocationPool {
\r
22 // See OpenStack Network API v2.0 Reference for description of
\r
23 // annotated attributes
\r
25 @XmlElement(name="start")
\r
28 @XmlElement(name="end")
\r
31 public NeutronSubnet_IPAllocationPool() { }
\r
33 public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
\r
34 poolStart = lowAddress;
\r
35 poolEnd = highAddress;
\r
38 public String getPoolStart() {
\r
42 public void setPoolStart(String poolStart) {
\r
43 this.poolStart = poolStart;
\r
46 public String getPoolEnd() {
\r
50 public void setPoolEnd(String poolEnd) {
\r
51 this.poolEnd = poolEnd;
\r
55 * This method determines if this allocation pool contains the
\r
56 * input IPv4 address
\r
58 * @param inputString
\r
59 * IPv4 address in dotted decimal format
\r
60 * @returns a boolean on whether the pool contains the address or not
\r
63 public boolean contains(String inputString) {
\r
64 long inputIP = convert(inputString);
\r
65 long startIP = convert(poolStart);
\r
66 long endIP = convert(poolEnd);
\r
67 return (inputIP >= startIP && inputIP <= endIP);
\r
71 * This static method converts the supplied IPv4 address to a long
\r
72 * integer for comparison
\r
74 * @param inputString
\r
75 * IPv4 address in dotted decimal format
\r
76 * @returns high-endian representation of the IPv4 address as a long
\r
79 static long convert(String inputString) {
\r
81 String[] parts = inputString.split("\\.");
\r
82 for (String part: parts) {
\r
84 ans |= Integer.parseInt(part);
\r
90 * This static method converts the supplied high-ending long back
\r
91 * into a dotted decimal representation of an IPv4 address
\r
94 * high-endian representation of the IPv4 address as a long
\r
95 * @returns IPv4 address in dotted decimal format
\r
97 static String longtoIP(long l) {
\r
99 String[] parts = new String[4];
\r
100 for (i=0; i<4; i++) {
\r
101 parts[3-i] = String.valueOf(l & 255);
\r
104 return join(parts,".");
\r
108 * helper routine used by longtoIP
\r
110 public static String join(String r[],String d)
\r
112 if (r.length == 0) return "";
\r
113 StringBuilder sb = new StringBuilder();
\r
115 for(i=0;i<r.length-1;i++)
\r
117 return sb.toString()+r[i];
\r
121 * This method splits the current instance by removing the supplied
\r
124 * If the parameter is either the low or high address,
\r
125 * then that member is adjusted and a list containing just this instance
\r
128 * If the parameter is in the middle of the pool, then
\r
129 * create two new instances, one ranging from low to parameter-1
\r
130 * the other ranging from parameter+1 to high
\r
132 public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
\r
133 List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
\r
134 long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
\r
135 long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
\r
136 long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
\r
138 NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
\r
139 boolean poolStarted = false;
\r
140 for (i=sIP; i<=eIP; i++) {
\r
143 p.setPoolStart(poolStart);
\r
144 poolStarted = true;
\r
149 p.setPoolEnd(poolEnd);
\r
151 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
\r
155 if (i != sIP && i != eIP) {
\r
157 if (!poolStarted) {
\r
158 p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
\r
159 poolStarted = true;
\r
162 p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
\r
163 poolStarted = false;
\r
165 p = new NeutronSubnet_IPAllocationPool();
\r