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.Iterator;
\r
13 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
19 import org.apache.commons.net.util.SubnetUtils;
\r
20 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
\r
23 @XmlAccessorType(XmlAccessType.NONE)
\r
25 public class NeutronSubnet {
\r
26 // See OpenStack Network API v2.0 Reference for description of
\r
27 // annotated attributes
\r
29 @XmlElement (name="id")
\r
32 @XmlElement (name="network_id")
\r
35 @XmlElement (name="name")
\r
38 @XmlElement (defaultValue="4", name="ip_version")
\r
41 @XmlElement (name="cidr")
\r
44 @XmlElement (name="gateway_ip")
\r
47 @XmlElement (name="dns_nameservers")
\r
48 List<String> dnsNameservers;
\r
50 @XmlElement (name="allocation_pools")
\r
51 List<NeutronSubnet_IPAllocationPool> allocationPools;
\r
53 @XmlElement (name="host_routes")
\r
54 List<NeutronSubnet_HostRoute> hostRoutes;
\r
56 @XmlElement (defaultValue="true", name="enable_dhcp")
\r
59 @XmlElement (name="tenant_id")
\r
62 /* stores the OpenStackPorts associated with an instance
\r
63 * used to determine if that instance can be deleted.
\r
65 List<NeutronPort> myPorts;
\r
67 boolean gatewayIPAssigned;
\r
69 public NeutronSubnet() {
\r
70 myPorts = new ArrayList<NeutronPort>();
\r
73 public String getID() { return subnetUUID; }
\r
75 public String getSubnetUUID() {
\r
79 public void setSubnetUUID(String subnetUUID) {
\r
80 this.subnetUUID = subnetUUID;
\r
83 public String getNetworkUUID() {
\r
87 public void setNetworkUUID(String networkUUID) {
\r
88 this.networkUUID = networkUUID;
\r
91 public String getName() {
\r
95 public void setName(String name) {
\r
99 public Integer getIpVersion() {
\r
103 public void setIpVersion(Integer ipVersion) {
\r
104 this.ipVersion = ipVersion;
\r
107 public String getCidr() {
\r
111 public void setCidr(String cidr) {
\r
115 public String getGatewayIP() {
\r
119 public void setGatewayIP(String gatewayIP) {
\r
120 this.gatewayIP = gatewayIP;
\r
123 public List<String> getDnsNameservers() {
\r
124 return dnsNameservers;
\r
127 public void setDnsNameservers(List<String> dnsNameservers) {
\r
128 this.dnsNameservers = dnsNameservers;
\r
131 public List<NeutronSubnet_IPAllocationPool> getAllocationPools() {
\r
132 return allocationPools;
\r
135 public void setAllocationPools(List<NeutronSubnet_IPAllocationPool> allocationPools) {
\r
136 this.allocationPools = allocationPools;
\r
139 public List<NeutronSubnet_HostRoute> getHostRoutes() {
\r
143 public void setHostRoutes(List<NeutronSubnet_HostRoute> hostRoutes) {
\r
144 this.hostRoutes = hostRoutes;
\r
147 public boolean isEnableDHCP() {
\r
148 if (enableDHCP == null)
\r
153 public Boolean getEnableDHCP() { return enableDHCP; }
\r
155 public void setEnableDHCP(Boolean newValue) {
\r
156 this.enableDHCP = newValue;
\r
159 public String getTenantID() {
\r
163 public void setTenantID(String tenantID) {
\r
164 this.tenantID = tenantID;
\r
168 * This method copies selected fields from the object and returns them
\r
169 * as a new object, suitable for marshaling.
\r
172 * List of attributes to be extracted
\r
173 * @return an OpenStackSubnets object with only the selected fields
\r
177 public NeutronSubnet extractFields(List<String> fields) {
\r
178 NeutronSubnet ans = new NeutronSubnet();
\r
179 Iterator<String> i = fields.iterator();
\r
180 while (i.hasNext()) {
\r
181 String s = i.next();
\r
182 if (s.equals("id"))
\r
183 ans.setSubnetUUID(this.getSubnetUUID());
\r
184 if (s.equals("network_id"))
\r
185 ans.setNetworkUUID(this.getNetworkUUID());
\r
186 if (s.equals("name"))
\r
187 ans.setName(this.getName());
\r
188 if (s.equals("ip_version"))
\r
189 ans.setIpVersion(this.getIpVersion());
\r
190 if (s.equals("cidr"))
\r
191 ans.setCidr(this.getCidr());
\r
192 if (s.equals("gateway_ip"))
\r
193 ans.setGatewayIP(this.getGatewayIP());
\r
194 if (s.equals("dns_nameservers")) {
\r
195 List<String> nsList = new ArrayList<String>();
\r
196 nsList.addAll(this.getDnsNameservers());
\r
197 ans.setDnsNameservers(nsList);
\r
199 if (s.equals("allocation_pools")) {
\r
200 List<NeutronSubnet_IPAllocationPool> aPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
\r
201 aPools.addAll(this.getAllocationPools());
\r
202 ans.setAllocationPools(aPools);
\r
204 if (s.equals("host_routes")) {
\r
205 List<NeutronSubnet_HostRoute> hRoutes = new ArrayList<NeutronSubnet_HostRoute>();
\r
206 hRoutes.addAll(this.getHostRoutes());
\r
207 ans.setHostRoutes(hRoutes);
\r
209 if (s.equals("enable_dhcp"))
\r
210 ans.setEnableDHCP(this.getEnableDHCP());
\r
211 if (s.equals("tenant_id"))
\r
212 ans.setTenantID(this.getTenantID());
\r
217 /* test to see if the cidr address used to define this subnet
\r
218 * is a valid network address (an necessary condition when creating
\r
221 public boolean isValidCIDR() {
\r
223 SubnetUtils util = new SubnetUtils(cidr);
\r
224 SubnetInfo info = util.getInfo();
\r
225 if (!info.getNetworkAddress().equals(info.getAddress()))
\r
227 } catch (Exception e) {
\r
233 /* test to see if the gateway IP specified overlaps with specified
\r
234 * allocation pools (an error condition when creating a new subnet
\r
235 * or assigning a gateway IP)
\r
237 public boolean gatewayIP_Pool_overlap() {
\r
238 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
239 while (i.hasNext()) {
\r
240 NeutronSubnet_IPAllocationPool pool = i.next();
\r
241 if (pool.contains(gatewayIP))
\r
247 public void initDefaults() {
\r
248 if (enableDHCP == null)
\r
250 if (ipVersion == null)
\r
252 gatewayIPAssigned = false;
\r
253 dnsNameservers = new ArrayList<String>();
\r
254 allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
\r
255 hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
\r
257 SubnetUtils util = new SubnetUtils(cidr);
\r
258 SubnetInfo info = util.getInfo();
\r
259 if (gatewayIP == null)
\r
260 gatewayIP = info.getLowAddress();
\r
261 if (allocationPools.size() < 1) {
\r
262 NeutronSubnet_IPAllocationPool source =
\r
263 new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
\r
264 info.getHighAddress());
\r
265 allocationPools = source.splitPool(gatewayIP);
\r
267 } catch (Exception e) {
\r
272 public List<NeutronPort> getPortsInSubnet() {
\r
276 public void addPort(NeutronPort port) {
\r
280 public void removePort(NeutronPort port) {
\r
281 myPorts.remove(port);
\r
284 /* this method tests to see if the supplied IPv4 address
\r
285 * is valid for this subnet or not
\r
287 public boolean isValidIP(String ipAddress) {
\r
289 SubnetUtils util = new SubnetUtils(cidr);
\r
290 SubnetInfo info = util.getInfo();
\r
291 return info.isInRange(ipAddress);
\r
292 } catch (Exception e) {
\r
297 /* test to see if the supplied IPv4 address is part of one of the
\r
298 * available allocation pools or not
\r
300 public boolean isIPInUse(String ipAddress) {
\r
301 if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned )
\r
303 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
304 while (i.hasNext()) {
\r
305 NeutronSubnet_IPAllocationPool pool = i.next();
\r
306 if (pool.contains(ipAddress))
\r
312 /* method to get the lowest available address of the subnet.
\r
313 * go through all the allocation pools and keep the lowest of their
\r
316 public String getLowAddr() {
\r
318 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
319 while (i.hasNext()) {
\r
320 NeutronSubnet_IPAllocationPool pool = i.next();
\r
322 ans = pool.getPoolStart();
\r
324 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <
\r
325 NeutronSubnet_IPAllocationPool.convert(ans))
\r
326 ans = pool.getPoolStart();
\r
332 * allocate the parameter address. Because this uses an iterator to
\r
333 * check the instance's list of allocation pools and we want to modify
\r
334 * pools while the iterator is being used, it is necessary to
\r
335 * build a new list of allocation pools and replace the list when
\r
336 * finished (otherwise a split will cause undefined iterator behavior.
\r
338 public void allocateIP(String ipAddress) {
\r
339 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
340 List<NeutronSubnet_IPAllocationPool> newList = new ArrayList<NeutronSubnet_IPAllocationPool>(); // we have to modify a separate list
\r
341 while (i.hasNext()) {
\r
342 NeutronSubnet_IPAllocationPool pool = i.next();
\r
343 if (pool.getPoolEnd().equalsIgnoreCase(ipAddress) &&
\r
344 pool.getPoolStart().equalsIgnoreCase(ipAddress))
\r
345 ; // do nothing, i.e. don't add the current pool to the new list
\r
347 if (pool.contains(ipAddress)) {
\r
348 List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);
\r
349 newList.addAll(pools);
\r
353 allocationPools = newList;
\r
357 * release an IP address back to the subnet. Although an iterator
\r
358 * is used, the list is not modified until the iterator is complete, so
\r
359 * an extra list is not necessary.
\r
361 public void releaseIP(String ipAddress) {
\r
362 NeutronSubnet_IPAllocationPool lPool = null;
\r
363 NeutronSubnet_IPAllocationPool hPool = null;
\r
364 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
365 long sIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
\r
366 //look for lPool where ipAddr - 1 is high address
\r
367 //look for hPool where ipAddr + 1 is low address
\r
368 while (i.hasNext()) {
\r
369 NeutronSubnet_IPAllocationPool pool = i.next();
\r
370 long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());
\r
371 long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());
\r
377 //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high
\r
378 if (lPool == null && hPool == null)
\r
379 allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));
\r
380 //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr
\r
381 if (lPool == null && hPool != null)
\r
382 hPool.setPoolStart(ipAddress);
\r
383 //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr
\r
384 if (lPool != null && hPool == null)
\r
385 lPool.setPoolEnd(ipAddress);
\r
386 //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool
\r
387 // where low address = lPool.low address and high address = hPool.high Address
\r
388 if (lPool != null && hPool != null) {
\r
389 allocationPools.remove(lPool);
\r
390 allocationPools.remove(hPool);
\r
391 allocationPools.add(new NeutronSubnet_IPAllocationPool(
\r
392 lPool.getPoolStart(), hPool.getPoolEnd()));
\r
396 public void setGatewayIPAllocated() {
\r
397 gatewayIPAssigned = true;
\r
400 public void resetGatewayIPAllocated() {
\r
401 gatewayIPAssigned = false;
\r