2 * Copyright IBM Corporation, 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 /* stores the OpenStackPorts associated with an instance
68 * used to determine if that instance can be deleted.
70 List<NeutronPort> myPorts;
72 boolean gatewayIPAssigned;
74 public NeutronSubnet() {
75 myPorts = new ArrayList<NeutronPort>();
78 public String getID() { return subnetUUID; }
80 public String getSubnetUUID() {
84 public void setSubnetUUID(String subnetUUID) {
85 this.subnetUUID = subnetUUID;
88 public String getNetworkUUID() {
92 public void setNetworkUUID(String networkUUID) {
93 this.networkUUID = networkUUID;
96 public String getName() {
100 public void setName(String name) {
104 public Integer getIpVersion() {
108 public void setIpVersion(Integer ipVersion) {
109 this.ipVersion = ipVersion;
112 public String getCidr() {
116 public void setCidr(String cidr) {
120 public String getGatewayIP() {
124 public void setGatewayIP(String gatewayIP) {
125 this.gatewayIP = gatewayIP;
128 public List<String> getDnsNameservers() {
129 return dnsNameservers;
132 public void setDnsNameservers(List<String> dnsNameservers) {
133 this.dnsNameservers = dnsNameservers;
136 public List<NeutronSubnet_IPAllocationPool> getAllocationPools() {
137 return allocationPools;
140 public void setAllocationPools(List<NeutronSubnet_IPAllocationPool> allocationPools) {
141 this.allocationPools = allocationPools;
144 public List<NeutronSubnet_HostRoute> getHostRoutes() {
148 public void setHostRoutes(List<NeutronSubnet_HostRoute> hostRoutes) {
149 this.hostRoutes = hostRoutes;
152 public boolean isEnableDHCP() {
153 if (enableDHCP == null) {
159 public Boolean getEnableDHCP() { return enableDHCP; }
161 public void setEnableDHCP(Boolean newValue) {
162 enableDHCP = newValue;
165 public String getTenantID() {
169 public void setTenantID(String tenantID) {
170 this.tenantID = tenantID;
174 * This method copies selected fields from the object and returns them
175 * as a new object, suitable for marshaling.
178 * List of attributes to be extracted
179 * @return an OpenStackSubnets object with only the selected fields
183 public NeutronSubnet extractFields(List<String> fields) {
184 NeutronSubnet ans = new NeutronSubnet();
185 Iterator<String> i = fields.iterator();
186 while (i.hasNext()) {
188 if (s.equals("id")) {
189 ans.setSubnetUUID(this.getSubnetUUID());
191 if (s.equals("network_id")) {
192 ans.setNetworkUUID(this.getNetworkUUID());
194 if (s.equals("name")) {
195 ans.setName(this.getName());
197 if (s.equals("ip_version")) {
198 ans.setIpVersion(this.getIpVersion());
200 if (s.equals("cidr")) {
201 ans.setCidr(this.getCidr());
203 if (s.equals("gateway_ip")) {
204 ans.setGatewayIP(this.getGatewayIP());
206 if (s.equals("dns_nameservers")) {
207 List<String> nsList = new ArrayList<String>();
208 nsList.addAll(this.getDnsNameservers());
209 ans.setDnsNameservers(nsList);
211 if (s.equals("allocation_pools")) {
212 List<NeutronSubnet_IPAllocationPool> aPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
213 aPools.addAll(this.getAllocationPools());
214 ans.setAllocationPools(aPools);
216 if (s.equals("host_routes")) {
217 List<NeutronSubnet_HostRoute> hRoutes = new ArrayList<NeutronSubnet_HostRoute>();
218 hRoutes.addAll(this.getHostRoutes());
219 ans.setHostRoutes(hRoutes);
221 if (s.equals("enable_dhcp")) {
222 ans.setEnableDHCP(this.getEnableDHCP());
224 if (s.equals("tenant_id")) {
225 ans.setTenantID(this.getTenantID());
231 /* test to see if the cidr address used to define this subnet
232 * is a valid network address (an necessary condition when creating
235 public boolean isValidCIDR() {
237 SubnetUtils util = new SubnetUtils(cidr);
238 SubnetInfo info = util.getInfo();
239 if (!info.getNetworkAddress().equals(info.getAddress())) {
242 } catch (Exception e) {
248 /* test to see if the gateway IP specified overlaps with specified
249 * allocation pools (an error condition when creating a new subnet
250 * or assigning a gateway IP)
252 public boolean gatewayIP_Pool_overlap() {
253 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
254 while (i.hasNext()) {
255 NeutronSubnet_IPAllocationPool pool = i.next();
256 if (pool.contains(gatewayIP)) {
263 public boolean initDefaults() {
264 if (enableDHCP == null) {
267 if (ipVersion == null) {
270 gatewayIPAssigned = false;
271 dnsNameservers = new ArrayList<String>();
272 if (hostRoutes == null) {
273 hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
275 if (allocationPools == null) {
276 allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
278 SubnetUtils util = new SubnetUtils(cidr);
279 SubnetInfo info = util.getInfo();
280 if (gatewayIP == null) {
281 gatewayIP = info.getLowAddress();
283 if (allocationPools.size() < 1) {
284 NeutronSubnet_IPAllocationPool source =
285 new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
286 info.getHighAddress());
287 allocationPools = source.splitPool(gatewayIP);
289 } catch (Exception e) {
296 public List<NeutronPort> getPortsInSubnet() {
300 public void addPort(NeutronPort port) {
304 public void removePort(NeutronPort port) {
305 myPorts.remove(port);
308 /* this method tests to see if the supplied IPv4 address
309 * is valid for this subnet or not
311 public boolean isValidIP(String ipAddress) {
313 SubnetUtils util = new SubnetUtils(cidr);
314 SubnetInfo info = util.getInfo();
315 return info.isInRange(ipAddress);
316 } catch (Exception e) {
321 /* test to see if the supplied IPv4 address is part of one of the
322 * available allocation pools or not
324 public boolean isIPInUse(String ipAddress) {
325 if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned ) {
328 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
329 while (i.hasNext()) {
330 NeutronSubnet_IPAllocationPool pool = i.next();
331 if (pool.contains(ipAddress)) {
338 /* method to get the lowest available address of the subnet.
339 * go through all the allocation pools and keep the lowest of their
342 public String getLowAddr() {
344 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
345 while (i.hasNext()) {
346 NeutronSubnet_IPAllocationPool pool = i.next();
348 ans = pool.getPoolStart();
351 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <
352 NeutronSubnet_IPAllocationPool.convert(ans)) {
353 ans = pool.getPoolStart();
360 * allocate the parameter address. Because this uses an iterator to
361 * check the instance's list of allocation pools and we want to modify
362 * pools while the iterator is being used, it is necessary to
363 * build a new list of allocation pools and replace the list when
364 * finished (otherwise a split will cause undefined iterator behavior.
366 public void allocateIP(String ipAddress) {
367 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
368 List<NeutronSubnet_IPAllocationPool> newList = new ArrayList<NeutronSubnet_IPAllocationPool>(); // we have to modify a separate list
369 while (i.hasNext()) {
370 NeutronSubnet_IPAllocationPool pool = i.next();
371 /* if the pool contains a single address element and we are allocating it
372 * then we don't need to copy the pool over. Otherwise, we need to possibly
373 * split the pool and add both pieces to the new list
375 if (!(pool.getPoolEnd().equalsIgnoreCase(ipAddress) &&
376 pool.getPoolStart().equalsIgnoreCase(ipAddress))) {
377 if (pool.contains(ipAddress)) {
378 List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);
379 newList.addAll(pools);
385 allocationPools = newList;
389 * release an IP address back to the subnet. Although an iterator
390 * is used, the list is not modified until the iterator is complete, so
391 * an extra list is not necessary.
393 public void releaseIP(String ipAddress) {
394 NeutronSubnet_IPAllocationPool lPool = null;
395 NeutronSubnet_IPAllocationPool hPool = null;
396 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
397 long sIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
398 //look for lPool where ipAddr - 1 is high address
399 //look for hPool where ipAddr + 1 is low address
400 while (i.hasNext()) {
401 NeutronSubnet_IPAllocationPool pool = i.next();
402 long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());
403 long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());
411 //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high
412 if (lPool == null && hPool == null) {
413 allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));
415 //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr
416 if (lPool == null && hPool != null) {
417 hPool.setPoolStart(ipAddress);
419 //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr
420 if (lPool != null && hPool == null) {
421 lPool.setPoolEnd(ipAddress);
423 //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool
424 // where low address = lPool.low address and high address = hPool.high Address
425 if (lPool != null && hPool != null) {
426 allocationPools.remove(lPool);
427 allocationPools.remove(hPool);
428 allocationPools.add(new NeutronSubnet_IPAllocationPool(
429 lPool.getPoolStart(), hPool.getPoolEnd()));
433 public void setGatewayIPAllocated() {
434 gatewayIPAssigned = true;
437 public void resetGatewayIPAllocated() {
438 gatewayIPAssigned = false;
442 public String toString() {
443 return "NeutronSubnet [subnetUUID=" + subnetUUID + ", networkUUID=" + networkUUID + ", name=" + name
444 + ", ipVersion=" + ipVersion + ", cidr=" + cidr + ", gatewayIP=" + gatewayIP + ", dnsNameservers="
445 + dnsNameservers + ", allocationPools=" + allocationPools + ", hostRoutes=" + hostRoutes
446 + ", enableDHCP=" + enableDHCP + ", tenantID=" + tenantID + ", myPorts=" + myPorts
447 + ", gatewayIPAssigned=" + gatewayIPAssigned + "]";