Optimization - calculate the subnet prefix only once.
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / Subnet.java
index 1deda7c9d0d73ff3250dbcbcb2d08c215f624954..2794109c242bc420677b1e6209bc72921a8a7f6d 100644 (file)
@@ -25,6 +25,7 @@ public class Subnet implements Cloneable, Serializable {
     private static final long serialVersionUID = 1L;
     // Key fields
     private InetAddress networkAddress;
+    private transient InetAddress subnetPrefix;
     private short subnetMaskLength;
     // Property fields
     private short vlan;
@@ -38,9 +39,9 @@ public class Subnet implements Cloneable, Serializable {
     }
 
     public Subnet(SubnetConfig conf) {
-        networkAddress = conf.getIPnum();
+        networkAddress = conf.getIPAddress();
         subnetMaskLength = conf.getIPMaskLen();
-        nodeConnectors = conf.getSubnetNodeConnectors();
+        nodeConnectors = conf.getNodeConnectors();
     }
 
     public Subnet(Subnet subnet) {
@@ -114,6 +115,7 @@ public class Subnet implements Cloneable, Serializable {
      */
     public Subnet setNetworkAddress(InetAddress networkAddress) {
         this.networkAddress = networkAddress;
+        this.subnetPrefix = null;
         return this;
     }
 
@@ -159,17 +161,15 @@ public class Subnet implements Cloneable, Serializable {
         if (ip == null) {
             return false;
         }
-        InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
-        InetAddress otherPrefix = getPrefixForAddress(ip);
-        if ((thisPrefix == null) || (otherPrefix == null)) {
-            return false;
-        }
-        if (thisPrefix.equals(otherPrefix)) {
-            return true;
+        if(subnetPrefix == null) {
+            subnetPrefix = getPrefixForAddress(this.networkAddress);
         }
-        else {
-            return false;
+        InetAddress otherPrefix = getPrefixForAddress(ip);
+        boolean isSubnetOf = true;
+        if (((subnetPrefix == null) || (otherPrefix == null)) || (!subnetPrefix.equals(otherPrefix)) ) {
+            isSubnetOf = false;
         }
+        return isSubnetOf;
     }
 
     public short getVlan() {
@@ -196,27 +196,36 @@ public class Subnet implements Cloneable, Serializable {
 
     @Override
     public boolean equals(Object obj) {
-        if (this == obj)
+        if (this == obj) {
             return true;
-        if (obj == null)
+        }
+        if (obj == null) {
             return false;
-        if (getClass() != obj.getClass())
+        }
+        if (getClass() != obj.getClass()) {
             return false;
+        }
         Subnet other = (Subnet) obj;
         if (networkAddress == null) {
-            if (other.networkAddress != null)
+            if (other.networkAddress != null) {
                 return false;
-        } else if (!networkAddress.equals(other.networkAddress))
+            }
+        } else if (!networkAddress.equals(other.networkAddress)) {
             return false;
+        }
         if (nodeConnectors == null) {
-            if (other.nodeConnectors != null)
+            if (other.nodeConnectors != null) {
                 return false;
-        } else if (!nodeConnectors.equals(other.nodeConnectors))
+            }
+        } else if (!nodeConnectors.equals(other.nodeConnectors)) {
             return false;
-        if (subnetMaskLength != other.subnetMaskLength)
+        }
+        if (subnetMaskLength != other.subnetMaskLength) {
             return false;
-        if (vlan != other.vlan)
+        }
+        if (vlan != other.vlan) {
             return false;
+        }
         return true;
     }
 
@@ -235,23 +244,11 @@ public class Subnet implements Cloneable, Serializable {
         if (p == null) {
             return false;
         }
-        if (this.isFlatLayer2()) {
-            return true;
-        }
-        return this.nodeConnectors.contains(p);
+        return isFlatLayer2() || nodeConnectors.contains(p);
     }
 
     public boolean isMutualExclusive(Subnet otherSubnet) {
-        if (this.networkAddress.getClass() != otherSubnet.networkAddress
-                .getClass())
-            return true;
-        if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
-            return false;
-        }
-        if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {
-            return false;
-        }
-        return true;
+        return !(isSubnetOf(otherSubnet.getNetworkAddress()) || otherSubnet.isSubnetOf(getNetworkAddress()));
     }
 
     /**