2 * Copyright IBM Corporation and others, 2013. All rights reserved.
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
9 package org.opendaylight.controller.networkconfig.neutron;
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
21 import org.apache.commons.net.util.SubnetUtils;
22 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
23 import org.opendaylight.controller.configuration.ConfigurationObject;
26 @XmlAccessorType(XmlAccessType.NONE)
28 public class NeutronSubnet extends ConfigurationObject implements Serializable {
29 private static final long serialVersionUID = 1L;
31 // See OpenStack Network API v2.0 Reference for description of
32 // annotated attributes
34 @XmlElement (name="id")
37 @XmlElement (name="network_id")
40 @XmlElement (name="name")
43 @XmlElement (defaultValue="4", name="ip_version")
46 @XmlElement (name="cidr")
49 @XmlElement (name="gateway_ip")
52 @XmlElement (name="dns_nameservers")
53 List<String> dnsNameservers;
55 @XmlElement (name="allocation_pools")
56 List<NeutronSubnet_IPAllocationPool> allocationPools;
58 @XmlElement (name="host_routes")
59 List<NeutronSubnet_HostRoute> hostRoutes;
61 @XmlElement (defaultValue="true", name="enable_dhcp")
64 @XmlElement (name="tenant_id")
67 @XmlElement (name="ipv6_address_mode", nillable=true)
68 String ipV6AddressMode;
70 @XmlElement (name="ipv6_ra_mode", nillable=true)
73 /* stores the OpenStackPorts associated with an instance
74 * used to determine if that instance can be deleted.
76 List<NeutronPort> myPorts;
78 boolean gatewayIPAssigned;
80 public NeutronSubnet() {
81 myPorts = new ArrayList<NeutronPort>();
84 public String getID() { return subnetUUID; }
86 public String getSubnetUUID() {
90 public void setSubnetUUID(String subnetUUID) {
91 this.subnetUUID = subnetUUID;
94 public String getNetworkUUID() {
98 public void setNetworkUUID(String networkUUID) {
99 this.networkUUID = networkUUID;
102 public String getName() {
106 public void setName(String name) {
110 public Integer getIpVersion() {
114 public void setIpVersion(Integer ipVersion) {
115 this.ipVersion = ipVersion;
118 public String getCidr() {
122 public void setCidr(String cidr) {
126 public String getGatewayIP() {
130 public void setGatewayIP(String gatewayIP) {
131 this.gatewayIP = gatewayIP;
134 public List<String> getDnsNameservers() {
135 return dnsNameservers;
138 public void setDnsNameservers(List<String> dnsNameservers) {
139 this.dnsNameservers = dnsNameservers;
142 public List<NeutronSubnet_IPAllocationPool> getAllocationPools() {
143 return allocationPools;
146 public void setAllocationPools(List<NeutronSubnet_IPAllocationPool> allocationPools) {
147 this.allocationPools = allocationPools;
150 public List<NeutronSubnet_HostRoute> getHostRoutes() {
154 public void setHostRoutes(List<NeutronSubnet_HostRoute> hostRoutes) {
155 this.hostRoutes = hostRoutes;
158 public boolean isEnableDHCP() {
159 if (enableDHCP == null) {
165 public Boolean getEnableDHCP() { return enableDHCP; }
167 public void setEnableDHCP(Boolean newValue) {
168 enableDHCP = newValue;
171 public String getTenantID() {
175 public void setTenantID(String tenantID) {
176 this.tenantID = tenantID;
179 public String getIpV6AddressMode() { return ipV6AddressMode; }
181 public void setIpV6AddressMode(String ipV6AddressMode) { this.ipV6AddressMode = ipV6AddressMode; }
183 public String getIpV6RaMode() { return ipV6RaMode; }
185 public void setIpV6RaMode(String ipV6RaMode) { this.ipV6RaMode = ipV6RaMode; }
188 * This method copies selected fields from the object and returns them
189 * as a new object, suitable for marshaling.
192 * List of attributes to be extracted
193 * @return an OpenStackSubnets object with only the selected fields
197 public NeutronSubnet extractFields(List<String> fields) {
198 NeutronSubnet ans = new NeutronSubnet();
199 Iterator<String> i = fields.iterator();
200 while (i.hasNext()) {
202 if (s.equals("id")) {
203 ans.setSubnetUUID(this.getSubnetUUID());
205 if (s.equals("network_id")) {
206 ans.setNetworkUUID(this.getNetworkUUID());
208 if (s.equals("name")) {
209 ans.setName(this.getName());
211 if (s.equals("ip_version")) {
212 ans.setIpVersion(this.getIpVersion());
214 if (s.equals("cidr")) {
215 ans.setCidr(this.getCidr());
217 if (s.equals("gateway_ip")) {
218 ans.setGatewayIP(this.getGatewayIP());
220 if (s.equals("dns_nameservers")) {
221 List<String> nsList = new ArrayList<String>();
222 nsList.addAll(this.getDnsNameservers());
223 ans.setDnsNameservers(nsList);
225 if (s.equals("allocation_pools")) {
226 List<NeutronSubnet_IPAllocationPool> aPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
227 aPools.addAll(this.getAllocationPools());
228 ans.setAllocationPools(aPools);
230 if (s.equals("host_routes")) {
231 List<NeutronSubnet_HostRoute> hRoutes = new ArrayList<NeutronSubnet_HostRoute>();
232 hRoutes.addAll(this.getHostRoutes());
233 ans.setHostRoutes(hRoutes);
235 if (s.equals("enable_dhcp")) {
236 ans.setEnableDHCP(this.getEnableDHCP());
238 if (s.equals("tenant_id")) {
239 ans.setTenantID(this.getTenantID());
241 if (s.equals("ipv6_address_mode")) {
242 ans.setIpV6AddressMode(this.getIpV6AddressMode());
244 if (s.equals("ipv6_ra_mode")) {
245 ans.setIpV6RaMode(this.getIpV6RaMode());
251 /* test to see if the cidr address used to define this subnet
252 * is a valid network address (an necessary condition when creating
255 public boolean isValidCIDR() {
257 SubnetUtils util = new SubnetUtils(cidr);
258 SubnetInfo info = util.getInfo();
259 if (!info.getNetworkAddress().equals(info.getAddress())) {
262 } catch (Exception e) {
268 /* test to see if the gateway IP specified overlaps with specified
269 * allocation pools (an error condition when creating a new subnet
270 * or assigning a gateway IP)
272 public boolean gatewayIP_Pool_overlap() {
273 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
274 while (i.hasNext()) {
275 NeutronSubnet_IPAllocationPool pool = i.next();
276 if (pool.contains(gatewayIP)) {
283 public boolean initDefaults() {
284 if (enableDHCP == null) {
287 if (ipVersion == null) {
290 gatewayIPAssigned = false;
291 dnsNameservers = new ArrayList<String>();
292 if (hostRoutes == null) {
293 hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
295 if (allocationPools == null) {
296 allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
298 SubnetUtils util = new SubnetUtils(cidr);
299 SubnetInfo info = util.getInfo();
300 if (gatewayIP == null) {
301 gatewayIP = info.getLowAddress();
303 if (allocationPools.size() < 1) {
304 NeutronSubnet_IPAllocationPool source =
305 new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
306 info.getHighAddress());
307 allocationPools = source.splitPool(gatewayIP);
309 } catch (Exception e) {
316 public List<NeutronPort> getPortsInSubnet() {
320 public void addPort(NeutronPort port) {
324 public void removePort(NeutronPort port) {
325 myPorts.remove(port);
328 /* this method tests to see if the supplied IPv4 address
329 * is valid for this subnet or not
331 public boolean isValidIP(String ipAddress) {
333 SubnetUtils util = new SubnetUtils(cidr);
334 SubnetInfo info = util.getInfo();
335 return info.isInRange(ipAddress);
336 } catch (Exception e) {
341 /* test to see if the supplied IPv4 address is part of one of the
342 * available allocation pools or not
344 public boolean isIPInUse(String ipAddress) {
345 if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned ) {
348 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
349 while (i.hasNext()) {
350 NeutronSubnet_IPAllocationPool pool = i.next();
351 if (pool.contains(ipAddress)) {
358 /* method to get the lowest available address of the subnet.
359 * go through all the allocation pools and keep the lowest of their
362 public String getLowAddr() {
364 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
365 while (i.hasNext()) {
366 NeutronSubnet_IPAllocationPool pool = i.next();
368 ans = pool.getPoolStart();
371 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <
372 NeutronSubnet_IPAllocationPool.convert(ans)) {
373 ans = pool.getPoolStart();
380 * allocate the parameter address. Because this uses an iterator to
381 * check the instance's list of allocation pools and we want to modify
382 * pools while the iterator is being used, it is necessary to
383 * build a new list of allocation pools and replace the list when
384 * finished (otherwise a split will cause undefined iterator behavior.
386 public void allocateIP(String ipAddress) {
387 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
388 List<NeutronSubnet_IPAllocationPool> newList = new ArrayList<NeutronSubnet_IPAllocationPool>(); // we have to modify a separate list
389 while (i.hasNext()) {
390 NeutronSubnet_IPAllocationPool pool = i.next();
391 /* if the pool contains a single address element and we are allocating it
392 * then we don't need to copy the pool over. Otherwise, we need to possibly
393 * split the pool and add both pieces to the new list
395 if (!(pool.getPoolEnd().equalsIgnoreCase(ipAddress) &&
396 pool.getPoolStart().equalsIgnoreCase(ipAddress))) {
397 if (pool.contains(ipAddress)) {
398 List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);
399 newList.addAll(pools);
405 allocationPools = newList;
409 * release an IP address back to the subnet. Although an iterator
410 * is used, the list is not modified until the iterator is complete, so
411 * an extra list is not necessary.
413 public void releaseIP(String ipAddress) {
414 NeutronSubnet_IPAllocationPool lPool = null;
415 NeutronSubnet_IPAllocationPool hPool = null;
416 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
417 long sIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
418 //look for lPool where ipAddr - 1 is high address
419 //look for hPool where ipAddr + 1 is low address
420 while (i.hasNext()) {
421 NeutronSubnet_IPAllocationPool pool = i.next();
422 long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());
423 long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());
431 //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high
432 if (lPool == null && hPool == null) {
433 allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));
435 //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr
436 if (lPool == null && hPool != null) {
437 hPool.setPoolStart(ipAddress);
439 //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr
440 if (lPool != null && hPool == null) {
441 lPool.setPoolEnd(ipAddress);
443 //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool
444 // where low address = lPool.low address and high address = hPool.high Address
445 if (lPool != null && hPool != null) {
446 allocationPools.remove(lPool);
447 allocationPools.remove(hPool);
448 allocationPools.add(new NeutronSubnet_IPAllocationPool(
449 lPool.getPoolStart(), hPool.getPoolEnd()));
453 public void setGatewayIPAllocated() {
454 gatewayIPAssigned = true;
457 public void resetGatewayIPAllocated() {
458 gatewayIPAssigned = false;
462 public String toString() {
463 return "NeutronSubnet [subnetUUID=" + subnetUUID + ", networkUUID=" + networkUUID + ", name=" + name
464 + ", ipVersion=" + ipVersion + ", cidr=" + cidr + ", gatewayIP=" + gatewayIP + ", dnsNameservers="
465 + dnsNameservers + ", allocationPools=" + allocationPools + ", hostRoutes=" + hostRoutes
466 + ", enableDHCP=" + enableDHCP + ", tenantID=" + tenantID + ", myPorts=" + myPorts
467 + ", gatewayIPAssigned=" + gatewayIPAssigned + ", ipv6AddressMode=" + ipV6AddressMode
468 + ", ipv6RaMode=" + ipV6RaMode + "]";