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
154 public Boolean getEnableDHCP() { return enableDHCP; }
\r
156 public void setEnableDHCP(Boolean newValue) {
\r
157 enableDHCP = newValue;
\r
160 public String getTenantID() {
\r
164 public void setTenantID(String tenantID) {
\r
165 this.tenantID = tenantID;
\r
169 * This method copies selected fields from the object and returns them
\r
170 * as a new object, suitable for marshaling.
\r
173 * List of attributes to be extracted
\r
174 * @return an OpenStackSubnets object with only the selected fields
\r
178 public NeutronSubnet extractFields(List<String> fields) {
\r
179 NeutronSubnet ans = new NeutronSubnet();
\r
180 Iterator<String> i = fields.iterator();
\r
181 while (i.hasNext()) {
\r
182 String s = i.next();
\r
183 if (s.equals("id")) {
\r
184 ans.setSubnetUUID(this.getSubnetUUID());
\r
186 if (s.equals("network_id")) {
\r
187 ans.setNetworkUUID(this.getNetworkUUID());
\r
189 if (s.equals("name")) {
\r
190 ans.setName(this.getName());
\r
192 if (s.equals("ip_version")) {
\r
193 ans.setIpVersion(this.getIpVersion());
\r
195 if (s.equals("cidr")) {
\r
196 ans.setCidr(this.getCidr());
\r
198 if (s.equals("gateway_ip")) {
\r
199 ans.setGatewayIP(this.getGatewayIP());
\r
201 if (s.equals("dns_nameservers")) {
\r
202 List<String> nsList = new ArrayList<String>();
\r
203 nsList.addAll(this.getDnsNameservers());
\r
204 ans.setDnsNameservers(nsList);
\r
206 if (s.equals("allocation_pools")) {
\r
207 List<NeutronSubnet_IPAllocationPool> aPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
\r
208 aPools.addAll(this.getAllocationPools());
\r
209 ans.setAllocationPools(aPools);
\r
211 if (s.equals("host_routes")) {
\r
212 List<NeutronSubnet_HostRoute> hRoutes = new ArrayList<NeutronSubnet_HostRoute>();
\r
213 hRoutes.addAll(this.getHostRoutes());
\r
214 ans.setHostRoutes(hRoutes);
\r
216 if (s.equals("enable_dhcp")) {
\r
217 ans.setEnableDHCP(this.getEnableDHCP());
\r
219 if (s.equals("tenant_id")) {
\r
220 ans.setTenantID(this.getTenantID());
\r
226 /* test to see if the cidr address used to define this subnet
\r
227 * is a valid network address (an necessary condition when creating
\r
230 public boolean isValidCIDR() {
\r
232 SubnetUtils util = new SubnetUtils(cidr);
\r
233 SubnetInfo info = util.getInfo();
\r
234 if (!info.getNetworkAddress().equals(info.getAddress())) {
\r
237 } catch (Exception e) {
\r
243 /* test to see if the gateway IP specified overlaps with specified
\r
244 * allocation pools (an error condition when creating a new subnet
\r
245 * or assigning a gateway IP)
\r
247 public boolean gatewayIP_Pool_overlap() {
\r
248 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
249 while (i.hasNext()) {
\r
250 NeutronSubnet_IPAllocationPool pool = i.next();
\r
251 if (pool.contains(gatewayIP)) {
\r
258 public boolean initDefaults() {
\r
259 if (enableDHCP == null) {
\r
262 if (ipVersion == null) {
\r
265 gatewayIPAssigned = false;
\r
266 dnsNameservers = new ArrayList<String>();
\r
267 allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
\r
268 hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
\r
270 SubnetUtils util = new SubnetUtils(cidr);
\r
271 SubnetInfo info = util.getInfo();
\r
272 if (gatewayIP == null) {
\r
273 gatewayIP = info.getLowAddress();
\r
275 if (allocationPools.size() < 1) {
\r
276 NeutronSubnet_IPAllocationPool source =
\r
277 new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
\r
278 info.getHighAddress());
\r
279 allocationPools = source.splitPool(gatewayIP);
\r
281 } catch (Exception e) {
\r
287 public List<NeutronPort> getPortsInSubnet() {
\r
291 public void addPort(NeutronPort port) {
\r
295 public void removePort(NeutronPort port) {
\r
296 myPorts.remove(port);
\r
299 /* this method tests to see if the supplied IPv4 address
\r
300 * is valid for this subnet or not
\r
302 public boolean isValidIP(String ipAddress) {
\r
304 SubnetUtils util = new SubnetUtils(cidr);
\r
305 SubnetInfo info = util.getInfo();
\r
306 return info.isInRange(ipAddress);
\r
307 } catch (Exception e) {
\r
312 /* test to see if the supplied IPv4 address is part of one of the
\r
313 * available allocation pools or not
\r
315 public boolean isIPInUse(String ipAddress) {
\r
316 if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned ) {
\r
319 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
320 while (i.hasNext()) {
\r
321 NeutronSubnet_IPAllocationPool pool = i.next();
\r
322 if (pool.contains(ipAddress)) {
\r
329 /* method to get the lowest available address of the subnet.
\r
330 * go through all the allocation pools and keep the lowest of their
\r
333 public String getLowAddr() {
\r
335 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
336 while (i.hasNext()) {
\r
337 NeutronSubnet_IPAllocationPool pool = i.next();
\r
339 ans = pool.getPoolStart();
\r
342 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <
\r
343 NeutronSubnet_IPAllocationPool.convert(ans)) {
\r
344 ans = pool.getPoolStart();
\r
351 * allocate the parameter address. Because this uses an iterator to
\r
352 * check the instance's list of allocation pools and we want to modify
\r
353 * pools while the iterator is being used, it is necessary to
\r
354 * build a new list of allocation pools and replace the list when
\r
355 * finished (otherwise a split will cause undefined iterator behavior.
\r
357 public void allocateIP(String ipAddress) {
\r
358 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
359 List<NeutronSubnet_IPAllocationPool> newList = new ArrayList<NeutronSubnet_IPAllocationPool>(); // we have to modify a separate list
\r
360 while (i.hasNext()) {
\r
361 NeutronSubnet_IPAllocationPool pool = i.next();
\r
362 /* if the pool contains a single address element and we are allocating it
\r
363 * then we don't need to copy the pool over. Otherwise, we need to possibly
\r
364 * split the pool and add both pieces to the new list
\r
366 if (!(pool.getPoolEnd().equalsIgnoreCase(ipAddress) &&
\r
367 pool.getPoolStart().equalsIgnoreCase(ipAddress))) {
\r
368 if (pool.contains(ipAddress)) {
\r
369 List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);
\r
370 newList.addAll(pools);
\r
376 allocationPools = newList;
\r
380 * release an IP address back to the subnet. Although an iterator
\r
381 * is used, the list is not modified until the iterator is complete, so
\r
382 * an extra list is not necessary.
\r
384 public void releaseIP(String ipAddress) {
\r
385 NeutronSubnet_IPAllocationPool lPool = null;
\r
386 NeutronSubnet_IPAllocationPool hPool = null;
\r
387 Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();
\r
388 long sIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
\r
389 //look for lPool where ipAddr - 1 is high address
\r
390 //look for hPool where ipAddr + 1 is low address
\r
391 while (i.hasNext()) {
\r
392 NeutronSubnet_IPAllocationPool pool = i.next();
\r
393 long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());
\r
394 long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());
\r
395 if (sIP+1 == lIP) {
\r
398 if (sIP-1 == hIP) {
\r
402 //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high
\r
403 if (lPool == null && hPool == null) {
\r
404 allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));
\r
406 //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr
\r
407 if (lPool == null && hPool != null) {
\r
408 hPool.setPoolStart(ipAddress);
\r
410 //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr
\r
411 if (lPool != null && hPool == null) {
\r
412 lPool.setPoolEnd(ipAddress);
\r
414 //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool
\r
415 // where low address = lPool.low address and high address = hPool.high Address
\r
416 if (lPool != null && hPool != null) {
\r
417 allocationPools.remove(lPool);
\r
418 allocationPools.remove(hPool);
\r
419 allocationPools.add(new NeutronSubnet_IPAllocationPool(
\r
420 lPool.getPoolStart(), hPool.getPoolEnd()));
\r
424 public void setGatewayIPAllocated() {
\r
425 gatewayIPAssigned = true;
\r
428 public void resetGatewayIPAllocated() {
\r
429 gatewayIPAssigned = false;
\r