Fix for Bug 2290.
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / NeutronSubnet.java
index fbaa63a14800f163659b8fe1616cfca271598879..b0d9bf46d4e20ee851fd644b014161a1df8eefe3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright IBM Corporation, 2013.  All rights reserved.
+ * Copyright IBM Corporation and others, 2013.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -9,6 +9,8 @@
 package org.opendaylight.controller.networkconfig.neutron;
 
 import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.Inet6Address;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -20,11 +22,12 @@ import javax.xml.bind.annotation.XmlRootElement;
 
 import org.apache.commons.net.util.SubnetUtils;
 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
+import org.opendaylight.controller.configuration.ConfigurationObject;
 
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.NONE)
 
-public class NeutronSubnet implements Serializable {
+public class NeutronSubnet extends ConfigurationObject implements Serializable, INeutronObject {
     private static final long serialVersionUID = 1L;
 
     // See OpenStack Network API v2.0 Reference for description of
@@ -63,12 +66,18 @@ public class NeutronSubnet implements Serializable {
     @XmlElement (name="tenant_id")
     String tenantID;
 
+    @XmlElement (name="ipv6_address_mode", nillable=true)
+    String ipV6AddressMode;
+
+    @XmlElement (name="ipv6_ra_mode", nillable=true)
+    String ipV6RaMode;
+
     /* stores the OpenStackPorts associated with an instance
      * used to determine if that instance can be deleted.
      */
     List<NeutronPort> myPorts;
 
-    boolean gatewayIPAssigned;
+    Boolean gatewayIPAssigned;
 
     public NeutronSubnet() {
         myPorts = new ArrayList<NeutronPort>();
@@ -76,6 +85,8 @@ public class NeutronSubnet implements Serializable {
 
     public String getID() { return subnetUUID; }
 
+    public void setID(String id) { this.subnetUUID = id; }
+
     public String getSubnetUUID() {
         return subnetUUID;
     }
@@ -169,6 +180,14 @@ public class NeutronSubnet implements Serializable {
         this.tenantID = tenantID;
     }
 
+    public String getIpV6AddressMode() { return ipV6AddressMode; }
+
+    public void setIpV6AddressMode(String ipV6AddressMode) { this.ipV6AddressMode = ipV6AddressMode; }
+
+    public String getIpV6RaMode() { return ipV6RaMode; }
+
+    public void setIpV6RaMode(String ipV6RaMode) { this.ipV6RaMode = ipV6RaMode; }
+
     /**
      * This method copies selected fields from the object and returns them
      * as a new object, suitable for marshaling.
@@ -223,6 +242,12 @@ public class NeutronSubnet implements Serializable {
             if (s.equals("tenant_id")) {
                 ans.setTenantID(this.getTenantID());
             }
+            if (s.equals("ipv6_address_mode")) {
+                ans.setIpV6AddressMode(this.getIpV6AddressMode());
+            }
+            if (s.equals("ipv6_ra_mode")) {
+                ans.setIpV6RaMode(this.getIpV6RaMode());
+            }
         }
         return ans;
     }
@@ -232,16 +257,45 @@ public class NeutronSubnet implements Serializable {
      * a new subnet)
      */
     public boolean isValidCIDR() {
-        try {
-            SubnetUtils util = new SubnetUtils(cidr);
-            SubnetInfo info = util.getInfo();
-            if (!info.getNetworkAddress().equals(info.getAddress())) {
+        // fix for Bug 2290 - need to wrap the existing test as
+        // IPv4 because SubnetUtils doesn't support IPv6
+        if (ipVersion == 4) {
+            try {
+                SubnetUtils util = new SubnetUtils(cidr);
+                SubnetInfo info = util.getInfo();
+                if (!info.getNetworkAddress().equals(info.getAddress())) {
+                    return false;
+                }
+            } catch (Exception e) {
                 return false;
             }
-        } catch (Exception e) {
-            return false;
+            return true;
         }
-        return true;
+        if (ipVersion == 6) {
+            // fix for Bug2290 - this is custom code because no classes
+            // with ODL-friendly licenses have been found
+            // extract address (in front of /) and length (after /)
+            String[] parts = cidr.split("/");
+            if (parts.length != 2) {
+                return false;
+            }
+            try {
+                int length = Integer.parseInt(parts[1]);
+                //TODO?: limit check on length
+                // convert to byte array
+                byte[] addrBytes = ((Inet6Address) InetAddress.getByName(parts[0])).getAddress();
+                int i;
+                for (i=length; i<128; i++) { // offset is to ensure proper comparison
+                    if (((((int) addrBytes[i/8]) & 0x000000FF) & (1 << (7-(i%8)))) != 0) {
+                        return(false);
+                    }
+                }
+                return(true);
+            } catch (Exception e) {
+                return(false);
+            }
+        }
+        return false;
     }
 
     /* test to see if the gateway IP specified overlaps with specified
@@ -268,22 +322,26 @@ public class NeutronSubnet implements Serializable {
         }
         gatewayIPAssigned = false;
         dnsNameservers = new ArrayList<String>();
-        allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
-        hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
-        try {
-            SubnetUtils util = new SubnetUtils(cidr);
-            SubnetInfo info = util.getInfo();
-            if (gatewayIP == null) {
-                gatewayIP = info.getLowAddress();
-            }
-            if (allocationPools.size() < 1) {
-                NeutronSubnet_IPAllocationPool source =
-                    new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
-                            info.getHighAddress());
-                allocationPools = source.splitPool(gatewayIP);
+        if (hostRoutes == null) {
+            hostRoutes = new ArrayList<NeutronSubnet_HostRoute>();
+        }
+        if (allocationPools == null) {
+            allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();
+            try {
+                SubnetUtils util = new SubnetUtils(cidr);
+                SubnetInfo info = util.getInfo();
+                if (gatewayIP == null || ("").equals(gatewayIP)) {
+                    gatewayIP = info.getLowAddress();
+                }
+                if (allocationPools.size() < 1) {
+                    NeutronSubnet_IPAllocationPool source =
+                        new NeutronSubnet_IPAllocationPool(info.getLowAddress(),
+                                info.getHighAddress());
+                    allocationPools = source.splitPool(gatewayIP);
+                }
+            } catch (Exception e) {
+                return false;
             }
-        } catch (Exception e) {
-            return false;
         }
         return true;
     }
@@ -433,12 +491,17 @@ public class NeutronSubnet implements Serializable {
         gatewayIPAssigned = false;
     }
 
+    public Boolean getGatewayIPAllocated() {
+        return gatewayIPAssigned;
+    }
+
     @Override
     public String toString() {
         return "NeutronSubnet [subnetUUID=" + subnetUUID + ", networkUUID=" + networkUUID + ", name=" + name
                 + ", ipVersion=" + ipVersion + ", cidr=" + cidr + ", gatewayIP=" + gatewayIP + ", dnsNameservers="
                 + dnsNameservers + ", allocationPools=" + allocationPools + ", hostRoutes=" + hostRoutes
                 + ", enableDHCP=" + enableDHCP + ", tenantID=" + tenantID + ", myPorts=" + myPorts
-                + ", gatewayIPAssigned=" + gatewayIPAssigned + "]";
+                + ", gatewayIPAssigned=" + gatewayIPAssigned + ", ipv6AddressMode=" + ipV6AddressMode
+                + ", ipv6RaMode=" + ipV6RaMode + "]";
     }
 }