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<NodeConnector> nodeConnectors;
+ private final Set<NodeConnector> nodeConnectors;
public Subnet(InetAddress ip, short maskLen, short vlan) {
this.networkAddress = ip;
nodeConnectors = conf.getSubnetNodeConnectors();
}
+ public Subnet(Subnet subnet) {
+ networkAddress = subnet.networkAddress;
+ subnetMaskLength = subnet.subnetMaskLength;
+ vlan = subnet.vlan;
+ nodeConnectors = new HashSet<NodeConnector>(subnet.nodeConnectors);
+ }
+
/**
* Add NodeConnectors to a subnet
*
* @param sp Set of NodeConnectors to add to the subnet
*/
public void addNodeConnectors(Set<NodeConnector> sp) {
- if (sp == null) {
- return;
- }
-
- for (NodeConnector p : sp) {
- this.nodeConnectors.add(p);
+ if (sp != null) {
+ this.nodeConnectors.addAll(sp);
}
}
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) {
+ if (this == obj)
return true;
- }
- if (obj == null) {
+ 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)
public String toString() {
return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
+ "/" + subnetMaskLength
- + ((vlan == 0) ? "" : (" vlan=" + vlan)) + " "
+ + ((vlan == 0) ? "" : (", vlan=" + vlan)) + ", "
+ ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
}
}
return true;
}
+
+ /**
+ * Implement clonable interface
+ */
+ @Override
+ public Subnet clone() {
+ return new Subnet(this);
+ }
+
}