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.net.InetAddress;
13 import java.net.Inet6Address;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
23 import org.apache.commons.net.util.SubnetUtils;
24 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
27 @XmlAccessorType(XmlAccessType.NONE)
29 public class NeutronSubnet implements Serializable, INeutronObject {
30 private static final long serialVersionUID = 1L;
32 // See OpenStack Network API v2.0 Reference for description of
33 // annotated attributes
35 @XmlElement (name="id")
38 @XmlElement (name="network_id")
41 @XmlElement (name="name")
44 @XmlElement (defaultValue="4", name="ip_version")
47 @XmlElement (name="cidr")
50 @XmlElement (name="gateway_ip")
53 @XmlElement (name="dns_nameservers")
54 List<String> dnsNameservers;
56 @XmlElement (name="allocation_pools")
57 List<NeutronSubnet_IPAllocationPool> allocationPools;
59 @XmlElement (name="host_routes")
60 List<NeutronSubnet_HostRoute> hostRoutes;
62 @XmlElement (defaultValue="true", name="enable_dhcp")
65 @XmlElement (name="tenant_id")
68 @XmlElement (name="ipv6_address_mode", nillable=true)
69 String ipV6AddressMode;
71 @XmlElement (name="ipv6_ra_mode", nillable=true)
74 /* stores the OpenStackPorts associated with an instance
75 * used to determine if that instance can be deleted.
77 List<NeutronPort> myPorts;
79 Boolean gatewayIPAssigned;
81 public NeutronSubnet() {
82 myPorts = new ArrayList<NeutronPort>();
85 public String getID() { return subnetUUID; }
87 public void setID(String id) { this.subnetUUID = id; }
89 public String getSubnetUUID() {
93 public void setSubnetUUID(String subnetUUID) {
94 this.subnetUUID = subnetUUID;
97 public String getNetworkUUID() {
101 public void setNetworkUUID(String networkUUID) {
102 this.networkUUID = networkUUID;
105 public String getName() {
109 public void setName(String name) {
113 public Integer getIpVersion() {
117 public void setIpVersion(Integer ipVersion) {
118 this.ipVersion = ipVersion;
121 public String getCidr() {
125 public void setCidr(String cidr) {
129 public String getGatewayIP() {
133 public void setGatewayIP(String gatewayIP) {
134 this.gatewayIP = gatewayIP;
137 public List<String> getDnsNameservers() {
138 return dnsNameservers;
141 public void setDnsNameservers(List<String> dnsNameservers) {
142 this.dnsNameservers = dnsNameservers;
145 public List<NeutronSubnet_IPAllocationPool> getAllocationPools() {
146 return allocationPools;
149 public void setAllocationPools(List<NeutronSubnet_IPAllocationPool> allocationPools) {
150 this.allocationPools = allocationPools;
153 public List<NeutronSubnet_HostRoute> getHostRoutes() {
157 public void setHostRoutes(List<NeutronSubnet_HostRoute> hostRoutes) {
158 this.hostRoutes = hostRoutes;
161 public boolean isEnableDHCP() {
162 if (enableDHCP == null) {
168 public Boolean getEnableDHCP() { return enableDHCP; }
170 public void setEnableDHCP(Boolean newValue) {
171 enableDHCP = newValue;
174 public String getTenantID() {
178 public void setTenantID(String tenantID) {
179 this.tenantID = tenantID;
182 public String getIpV6AddressMode() { return ipV6AddressMode; }
184 public void setIpV6AddressMode(String ipV6AddressMode) { this.ipV6AddressMode = ipV6AddressMode; }
186 public String getIpV6RaMode() { return ipV6RaMode; }
188 public void setIpV6RaMode(String ipV6RaMode) { this.ipV6RaMode = ipV6RaMode; }
191 * This method copies selected fields from the object and returns them
192 * as a new object, suitable for marshaling.
195 * List of attributes to be extracted
196 * @return an OpenStackSubnets object with only the selected fields
200 public NeutronSubnet extractFields(List<String> fields) {
201 NeutronSubnet ans = new NeutronSubnet();
202 Iterator<String> i = fields.iterator();
203 while (i.hasNext()) {
205 if (s.equals("id")) {
206 ans.setSubnetUUID(this.getSubnetUUID());
208 if (s.equals("network_id")) {
209 ans.setNetworkUUID(this.getNetworkUUID());
211 if (s.equals("name")) {
212 ans.setName(this.getName());
214 if (s.equals("ip_version")) {
215 ans.setIpVersion(this.getIpVersion());
217 if (s.equals("cidr")) {
218 ans.setCidr(this.getCidr());
220 if (s.equals("gateway_ip")) {
221 ans.setGatewayIP(this.getGatewayIP());
223 if (s.equals("dns_nameservers")) {
224 List<String> nsList = new ArrayList<String>();
225 nsList.addAll(this.getDnsNameservers());
226 ans.setDnsNameservers(nsList);
228 if (s.equals("allocation_pools")) {
229 List<NeutronSubnet_IPAllocationPool> aPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
230 aPools.addAll(this.getAllocationPools());
231 ans.setAllocationPools(aPools);
233 if (s.equals("host_routes")) {
234 List<NeutronSubnet_HostRoute> hRoutes = new ArrayList<NeutronSubnet_HostRoute>();
235 hRoutes.addAll(this.getHostRoutes());
236 ans.setHostRoutes(hRoutes);
238 if (s.equals("enable_dhcp")) {
239 ans.setEnableDHCP(this.getEnableDHCP());
241 if (s.equals("tenant_id")) {
242 ans.setTenantID(this.getTenantID());
244 if (s.equals("ipv6_address_mode")) {
245 ans.setIpV6AddressMode(this.getIpV6AddressMode());
247 if (s.equals("ipv6_ra_mode")) {
248 ans.setIpV6RaMode(this.getIpV6RaMode());
254 /* test to see if the cidr address used to define this subnet
255 * is a valid network address (an necessary condition when creating
258 public boolean isValidCIDR() {
259 // fix for Bug 2290 - need to wrap the existing test as
260 // IPv4 because SubnetUtils doesn't support IPv6
261 if (ipVersion == 4) {
263 SubnetUtils util = new SubnetUtils(cidr);
264 SubnetInfo info = util.getInfo();
265 if (!info.getNetworkAddress().equals(info.getAddress())) {
268 } catch (Exception e) {
273 if (ipVersion == 6) {
274 // fix for Bug2290 - this is custom code because no classes
275 // with ODL-friendly licenses have been found
276 // extract address (in front of /) and length (after /)
277 String[] parts = cidr.split("/");
278 if (parts.length != 2) {
282 int length = Integer.parseInt(parts[1]);
283 //TODO?: limit check on length
284 // convert to byte array
285 byte[] addrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
287 for (i=length; i<128; i++) { // offset is to ensure proper comparison
288 if (((((int) addrBytes[i/8]) & 0x000000FF) & (1 << (7-(i%8)))) != 0) {
293 } catch (Exception e) {
300 /* test to see if the gateway IP specified overlaps with specified
301 * allocation pools (an error condition when creating a new subnet
302 * or assigning a gateway IP)
304 public boolean gatewayIP_Pool_overlap() {
305 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
306 while (i.hasNext()) {
307 NeutronSubnet_IPAllocationPool pool = i.next();
308 if (pool.contains(gatewayIP)) {
315 public boolean initDefaults() {
316 if (enableDHCP == null) {
319 if (ipVersion == null) {
322 gatewayIPAssigned = false;
323 dnsNameservers = new ArrayList<String>();
324 if (hostRoutes == null) {
325 hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
327 if (allocationPools == null) {
328 allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
330 SubnetUtils util = new SubnetUtils(cidr);
331 SubnetInfo info = util.getInfo();
332 if (gatewayIP == null || ("").equals(gatewayIP)) {
333 gatewayIP = info.getLowAddress();
335 if (allocationPools.size() < 1) {
336 NeutronSubnet_IPAllocationPool source =
337 new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
338 info.getHighAddress());
339 allocationPools = source.splitPool(gatewayIP);
341 } catch (Exception e) {
348 public List<NeutronPort> getPortsInSubnet() {
352 public void addPort(NeutronPort port) {
356 public void removePort(NeutronPort port) {
357 myPorts.remove(port);
360 /* this method tests to see if the supplied IPv4 address
361 * is valid for this subnet or not
363 public boolean isValidIP(String ipAddress) {
365 SubnetUtils util = new SubnetUtils(cidr);
366 SubnetInfo info = util.getInfo();
367 return info.isInRange(ipAddress);
368 } catch (Exception e) {
373 /* test to see if the supplied IPv4 address is part of one of the
374 * available allocation pools or not
376 public boolean isIPInUse(String ipAddress) {
377 if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned ) {
380 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
381 while (i.hasNext()) {
382 NeutronSubnet_IPAllocationPool pool = i.next();
383 if (pool.contains(ipAddress)) {
390 /* method to get the lowest available address of the subnet.
391 * go through all the allocation pools and keep the lowest of their
394 public String getLowAddr() {
396 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
397 while (i.hasNext()) {
398 NeutronSubnet_IPAllocationPool pool = i.next();
400 ans = pool.getPoolStart();
403 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <
404 NeutronSubnet_IPAllocationPool.convert(ans)) {
405 ans = pool.getPoolStart();
412 * allocate the parameter address. Because this uses an iterator to
413 * check the instance's list of allocation pools and we want to modify
414 * pools while the iterator is being used, it is necessary to
415 * build a new list of allocation pools and replace the list when
416 * finished (otherwise a split will cause undefined iterator behavior.
418 public void allocateIP(String ipAddress) {
419 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
420 List<NeutronSubnet_IPAllocationPool> newList = new ArrayList<NeutronSubnet_IPAllocationPool>(); // we have to modify a separate list
421 while (i.hasNext()) {
422 NeutronSubnet_IPAllocationPool pool = i.next();
423 /* if the pool contains a single address element and we are allocating it
424 * then we don't need to copy the pool over. Otherwise, we need to possibly
425 * split the pool and add both pieces to the new list
427 if (!(pool.getPoolEnd().equalsIgnoreCase(ipAddress) &&
428 pool.getPoolStart().equalsIgnoreCase(ipAddress))) {
429 if (pool.contains(ipAddress)) {
430 List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);
431 newList.addAll(pools);
437 allocationPools = newList;
441 * release an IP address back to the subnet. Although an iterator
442 * is used, the list is not modified until the iterator is complete, so
443 * an extra list is not necessary.
445 public void releaseIP(String ipAddress) {
446 NeutronSubnet_IPAllocationPool lPool = null;
447 NeutronSubnet_IPAllocationPool hPool = null;
448 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
449 long sIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
450 //look for lPool where ipAddr - 1 is high address
451 //look for hPool where ipAddr + 1 is low address
452 while (i.hasNext()) {
453 NeutronSubnet_IPAllocationPool pool = i.next();
454 long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());
455 long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());
463 //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high
464 if (lPool == null && hPool == null) {
465 allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));
467 //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr
468 if (lPool == null && hPool != null) {
469 hPool.setPoolStart(ipAddress);
471 //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr
472 if (lPool != null && hPool == null) {
473 lPool.setPoolEnd(ipAddress);
475 //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool
476 // where low address = lPool.low address and high address = hPool.high Address
477 if (lPool != null && hPool != null) {
478 allocationPools.remove(lPool);
479 allocationPools.remove(hPool);
480 allocationPools.add(new NeutronSubnet_IPAllocationPool(
481 lPool.getPoolStart(), hPool.getPoolEnd()));
485 public void setGatewayIPAllocated() {
486 gatewayIPAssigned = true;
489 public void resetGatewayIPAllocated() {
490 gatewayIPAssigned = false;
493 public Boolean getGatewayIPAllocated() {
494 return gatewayIPAssigned;
498 public String toString() {
499 return "NeutronSubnet [subnetUUID=" + subnetUUID + ", networkUUID=" + networkUUID + ", name=" + name
500 + ", ipVersion=" + ipVersion + ", cidr=" + cidr + ", gatewayIP=" + gatewayIP + ", dnsNameservers="
501 + dnsNameservers + ", allocationPools=" + allocationPools + ", hostRoutes=" + hostRoutes
502 + ", enableDHCP=" + enableDHCP + ", tenantID=" + tenantID + ", myPorts=" + myPorts
503 + ", gatewayIPAssigned=" + gatewayIPAssigned + ", ipv6AddressMode=" + ipV6AddressMode
504 + ", ipv6RaMode=" + ipV6RaMode + "]";