X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fswitchmanager%2Fapi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fswitchmanager%2FSubnet.java;h=e0842593c8cf25defbabdf078c814afb7ce2cc4d;hp=551f0722db2e1fd30c3e9d61bc17a5577cd07616;hb=c541f7868e6e2d654b8080b5426bb12a39bddf11;hpb=9e43cfabdc83df4c5db51ce6e22e0cecca12aa9a diff --git a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java index 551f0722db..e0842593c8 100644 --- a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java +++ b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java @@ -15,25 +15,20 @@ import java.net.UnknownHostException; import java.util.HashSet; import java.util.Set; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.opendaylight.controller.sal.core.NodeConnector; -import org.opendaylight.controller.switchmanager.Subnet; -import org.opendaylight.controller.switchmanager.SubnetConfig; - /** * The class describes subnet information including L3 address, vlan and set of * ports associated with the subnet. */ -public class Subnet implements Serializable { +public class Subnet implements Cloneable, Serializable { private static final long serialVersionUID = 1L; // Key fields private InetAddress networkAddress; private short subnetMaskLength; // Property fields private short vlan; - private Set nodeConnectors; + private final Set nodeConnectors; public Subnet(InetAddress ip, short maskLen, short vlan) { this.networkAddress = ip; @@ -43,9 +38,16 @@ public class Subnet implements Serializable { } public Subnet(SubnetConfig conf) { - networkAddress = conf.getIPnum(); + networkAddress = conf.getIPAddress(); subnetMaskLength = conf.getIPMaskLen(); - nodeConnectors = conf.getSubnetNodeConnectors(); + nodeConnectors = conf.getNodeConnectors(); + } + + public Subnet(Subnet subnet) { + networkAddress = subnet.networkAddress; + subnetMaskLength = subnet.subnetMaskLength; + vlan = subnet.vlan; + nodeConnectors = new HashSet(subnet.nodeConnectors); } /** @@ -54,12 +56,8 @@ public class Subnet implements Serializable { * @param sp Set of NodeConnectors to add to the subnet */ public void addNodeConnectors(Set sp) { - if (sp == null) { - return; - } - - for (NodeConnector p : sp) { - this.nodeConnectors.add(p); + if (sp != null) { + this.nodeConnectors.addAll(sp); } } @@ -158,16 +156,16 @@ public class Subnet implements Serializable { } public boolean isSubnetOf(InetAddress ip) { - if (ip == null) + 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; - else - return false; + boolean isSubnetOf = true; + if (((thisPrefix == null) || (otherPrefix == null)) || (!thisPrefix.equals(otherPrefix)) ) { + isSubnetOf = false; + } + return isSubnetOf; } public short getVlan() { @@ -179,18 +177,19 @@ public class Subnet implements Serializable { return this; } - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { - return new HashCodeBuilder().append(networkAddress).append( - subnetMaskLength).toHashCode(); + final int prime = 31; + int result = 1; + result = prime * result + + ((networkAddress == null) ? 0 : networkAddress.hashCode()); + result = prime * result + + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode()); + result = prime * result + subnetMaskLength; + result = prime * result + vlan; + return result; } - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object obj) { if (this == obj) { @@ -199,13 +198,31 @@ public class Subnet implements Serializable { if (obj == null) { return false; } - if (obj.getClass() != getClass()) { + if (getClass() != obj.getClass()) { return false; } Subnet other = (Subnet) obj; - // Check only equality for the key fields - return new EqualsBuilder().append(networkAddress, other.networkAddress) - .append(subnetMaskLength, other.subnetMaskLength).isEquals(); + if (networkAddress == null) { + if (other.networkAddress != null) { + return false; + } + } else if (!networkAddress.equals(other.networkAddress)) { + return false; + } + if (nodeConnectors == null) { + if (other.nodeConnectors != null) { + return false; + } + } else if (!nodeConnectors.equals(other.nodeConnectors)) { + return false; + } + if (subnetMaskLength != other.subnetMaskLength) { + return false; + } + if (vlan != other.vlan) { + return false; + } + return true; } /* (non-Javadoc) @@ -215,7 +232,7 @@ public class Subnet implements Serializable { public String toString() { return ("Subnet [networkAddress=" + networkAddress.getHostAddress() + "/" + subnetMaskLength - + ((vlan == 0) ? "" : (" vlan=" + vlan)) + " " + + ((vlan == 0) ? "" : (", vlan=" + vlan)) + ", " + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]"); } @@ -223,16 +240,14 @@ public class Subnet implements 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()) + .getClass()) { return true; + } if (this.isSubnetOf(otherSubnet.getNetworkAddress())) { return false; } @@ -241,4 +256,13 @@ public class Subnet implements Serializable { } return true; } + + /** + * Implement clonable interface + */ + @Override + public Subnet clone() { + return new Subnet(this); + } + }