Merge "Fix for Bug 3"
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / NeutronSubnet_IPAllocationPool.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron;
10
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
19
20 @XmlRootElement
21 @XmlAccessorType(XmlAccessType.NONE)
22 public class NeutronSubnet_IPAllocationPool implements Serializable {
23     private static final long serialVersionUID = 1L;
24
25     // See OpenStack Network API v2.0 Reference for description of
26     // annotated attributes
27
28     @XmlElement(name="start")
29     String poolStart;
30
31     @XmlElement(name="end")
32     String poolEnd;
33
34     public NeutronSubnet_IPAllocationPool() {
35     }
36
37     public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
38         poolStart = lowAddress;
39         poolEnd = highAddress;
40     }
41
42     public String getPoolStart() {
43         return poolStart;
44     }
45
46     public void setPoolStart(String poolStart) {
47         this.poolStart = poolStart;
48     }
49
50     public String getPoolEnd() {
51         return poolEnd;
52     }
53
54     public void setPoolEnd(String poolEnd) {
55         this.poolEnd = poolEnd;
56     }
57
58     /**
59      * This method determines if this allocation pool contains the
60      * input IPv4 address
61      *
62      * @param inputString
63      *            IPv4 address in dotted decimal format
64      * @returns a boolean on whether the pool contains the address or not
65      */
66
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);
72     }
73
74     /**
75      * This static method converts the supplied IPv4 address to a long
76      * integer for comparison
77      *
78      * @param inputString
79      *            IPv4 address in dotted decimal format
80      * @returns high-endian representation of the IPv4 address as a long
81      */
82
83     static long convert(String inputString) {
84         long ans = 0;
85         String[] parts = inputString.split("\\.");
86         for (String part: parts) {
87             ans <<= 8;
88             ans |= Integer.parseInt(part);
89         }
90         return ans;
91     }
92
93     /**
94      * This static method converts the supplied high-ending long back
95      * into a dotted decimal representation of an IPv4 address
96      *
97      * @param l
98      *            high-endian representation of the IPv4 address as a long
99      * @returns IPv4 address in dotted decimal format
100      */
101     static String longtoIP(long l) {
102         int i;
103         String[] parts = new String[4];
104         for (i=0; i<4; i++) {
105             parts[3-i] = String.valueOf(l & 255);
106             l >>= 8;
107         }
108         return join(parts,".");
109     }
110
111     /*
112      * helper routine used by longtoIP
113      */
114     public static String join(String r[],String d)
115     {
116         if (r.length == 0) {
117             return "";
118         }
119         StringBuilder sb = new StringBuilder();
120         int i;
121         for(i=0;i<r.length-1;i++) {
122             sb.append(r[i]+d);
123         }
124         return sb.toString()+r[i];
125     }
126
127     /*
128      * This method splits the current instance by removing the supplied
129      * parameter.
130      *
131      * If the parameter is either the low or high address,
132      * then that member is adjusted and a list containing just this instance
133      * is returned.
134      *
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
138      */
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);
144         long i;
145         NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
146         boolean poolStarted = false;
147         for (i=sIP; i<=eIP; i++) {
148             if (i == sIP) {
149                 if (i != gIP) {
150                     p.setPoolStart(poolStart);
151                     poolStarted = true;
152                 }
153             }
154             if (i == eIP) {
155                 if (i != gIP) {
156                     p.setPoolEnd(poolEnd);
157                 } else {
158                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
159                 }
160                 ans.add(p);
161             }
162             if (i != sIP && i != eIP) {
163                 if (i != gIP) {
164                     if (!poolStarted) {
165                         p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
166                         poolStarted = true;
167                     }
168                 } else {
169                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
170                     poolStarted = false;
171                     ans.add(p);
172                     p = new NeutronSubnet_IPAllocationPool();
173                 }
174             }
175         }
176         return ans;
177     }
178 }