Initial push of Neutron interface
[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         return sb.toString()+r[i];\r
118     }\r
119 \r
120     /*\r
121      * This method splits the current instance by removing the supplied\r
122      * parameter.\r
123      *\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
126      * is returned.\r
127      *\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
131      */\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
137         long i;\r
138         NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();\r
139         boolean poolStarted = false;\r
140         for (i=sIP; i<=eIP; i++) {\r
141             if (i == sIP) {\r
142                 if (i != gIP) {\r
143                     p.setPoolStart(poolStart);\r
144                     poolStarted = true;\r
145                 }\r
146             }\r
147             if (i == eIP) {\r
148                 if (i != gIP) {\r
149                     p.setPoolEnd(poolEnd);\r
150                 } else {\r
151                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
152                 }\r
153                 ans.add(p);\r
154             }\r
155             if (i != sIP && i != eIP) {\r
156                 if (i != gIP) {\r
157                     if (!poolStarted) {\r
158                         p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));\r
159                         poolStarted = true;\r
160                     }\r
161                 } else {\r
162                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
163                     poolStarted = false;\r
164                     ans.add(p);\r
165                     p = new NeutronSubnet_IPAllocationPool();\r
166                 }\r
167             }\r
168         }\r
169         return ans;\r
170     }\r
171 }\r