Add curly braces to for and if statements.
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / NeutronSubnet_IPAllocationPool.java
1 /*\r
2  * Copyright IBM Corporation, 2013.  All rights reserved.\r
3  *\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
7  */\r
8 \r
9 package org.opendaylight.controller.networkconfig.neutron;\r
10 \r
11 import java.util.ArrayList;\r
12 import java.util.List;\r
13 \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
18 \r
19 @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
24 \r
25     @XmlElement(name="start")\r
26     String poolStart;\r
27 \r
28     @XmlElement(name="end")\r
29     String poolEnd;\r
30 \r
31     public NeutronSubnet_IPAllocationPool() { }\r
32 \r
33     public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {\r
34         poolStart = lowAddress;\r
35         poolEnd = highAddress;\r
36     }\r
37 \r
38     public String getPoolStart() {\r
39         return poolStart;\r
40     }\r
41 \r
42     public void setPoolStart(String poolStart) {\r
43         this.poolStart = poolStart;\r
44     }\r
45 \r
46     public String getPoolEnd() {\r
47         return poolEnd;\r
48     }\r
49 \r
50     public void setPoolEnd(String poolEnd) {\r
51         this.poolEnd = poolEnd;\r
52     }\r
53 \r
54     /**\r
55      * This method determines if this allocation pool contains the\r
56      * input IPv4 address\r
57      *\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
61      */\r
62 \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
68     }\r
69 \r
70     /**\r
71      * This static method converts the supplied IPv4 address to a long\r
72      * integer for comparison\r
73      *\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
77      */\r
78 \r
79     static long convert(String inputString) {\r
80         long ans = 0;\r
81         String[] parts = inputString.split("\\.");\r
82         for (String part: parts) {\r
83             ans <<= 8;\r
84             ans |= Integer.parseInt(part);\r
85         }\r
86         return ans;\r
87     }\r
88 \r
89     /**\r
90      * This static method converts the supplied high-ending long back\r
91      * into a dotted decimal representation of an IPv4 address\r
92      *\r
93      * @param l\r
94      *            high-endian representation of the IPv4 address as a long\r
95      * @returns IPv4 address in dotted decimal format\r
96      */\r
97     static String longtoIP(long l) {\r
98         int i;\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
102             l >>= 8;\r
103         }\r
104         return join(parts,".");\r
105     }\r
106 \r
107     /*\r
108      * helper routine used by longtoIP\r
109      */\r
110     public static String join(String r[],String d)\r
111     {\r
112         if (r.length == 0) return "";\r
113         StringBuilder sb = new StringBuilder();\r
114         int i;\r
115         for(i=0;i<r.length-1;i++) {\r
116             sb.append(r[i]+d);\r
117         }\r
118         return sb.toString()+r[i];\r
119     }\r
120 \r
121     /*\r
122      * This method splits the current instance by removing the supplied\r
123      * parameter.\r
124      *\r
125      * If the parameter is either the low or high address,\r
126      * then that member is adjusted and a list containing just this instance\r
127      * is returned.\r
128      *\r
129      * If the parameter is in the middle of the pool, then\r
130      * create two new instances, one ranging from low to parameter-1\r
131      * the other ranging from parameter+1 to high\r
132      */\r
133     public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {\r
134         List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();\r
135         long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);\r
136         long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);\r
137         long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);\r
138         long i;\r
139         NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();\r
140         boolean poolStarted = false;\r
141         for (i=sIP; i<=eIP; i++) {\r
142             if (i == sIP) {\r
143                 if (i != gIP) {\r
144                     p.setPoolStart(poolStart);\r
145                     poolStarted = true;\r
146                 }\r
147             }\r
148             if (i == eIP) {\r
149                 if (i != gIP) {\r
150                     p.setPoolEnd(poolEnd);\r
151                 } else {\r
152                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
153                 }\r
154                 ans.add(p);\r
155             }\r
156             if (i != sIP && i != eIP) {\r
157                 if (i != gIP) {\r
158                     if (!poolStarted) {\r
159                         p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));\r
160                         poolStarted = true;\r
161                     }\r
162                 } else {\r
163                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
164                     poolStarted = false;\r
165                     ans.add(p);\r
166                     p = new NeutronSubnet_IPAllocationPool();\r
167                 }\r
168             }\r
169         }\r
170         return ans;\r
171     }\r
172 }\r