3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.switchmanager;
12 import java.io.Serializable;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.HashSet;
18 import org.apache.commons.lang3.builder.EqualsBuilder;
19 import org.apache.commons.lang3.builder.HashCodeBuilder;
20 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.switchmanager.Subnet;
23 import org.opendaylight.controller.switchmanager.SubnetConfig;
26 * The class describes subnet information including L3 address, vlan and set of
27 * ports associated with the subnet.
29 public class Subnet implements Serializable {
30 private static final long serialVersionUID = 1L;
32 private InetAddress networkAddress;
33 private short subnetMaskLength;
36 private Set<NodeConnector> nodeConnectors;
38 public Subnet(InetAddress ip, short maskLen, short vlan) {
39 this.networkAddress = ip;
40 this.subnetMaskLength = maskLen;
42 this.nodeConnectors = new HashSet<NodeConnector>();
45 public Subnet(SubnetConfig conf) {
46 networkAddress = conf.getIPnum();
47 subnetMaskLength = conf.getIPMaskLen();
48 nodeConnectors = conf.getSubnetNodeConnectors();
52 * Add NodeConnectors to a subnet
54 * @param sp Set of NodeConnectors to add to the subnet
56 public void addNodeConnectors(Set<NodeConnector> sp) {
61 for (NodeConnector p : sp) {
62 this.nodeConnectors.add(p);
67 * Delete NodeConnectors from subnet
69 * @param sp Set of NodeConnectors to add to the subnet
71 public void deleteNodeConnectors(Set<NodeConnector> sp) {
75 for (NodeConnector p : sp) {
76 this.nodeConnectors.remove(p);
81 * Return the list of NodeConnectors configured for this subnet,
82 * could be also an empty set in case of all the known
86 * @return The list of NodeConnectors attached to the subnet
88 public Set<NodeConnector> getNodeConnectors() {
89 return this.nodeConnectors;
93 * If the subnet has no node connectors attached to it then it
94 * means that is a whole L2 flat domain
97 * @return true if there are no node connectors configured for the
100 public boolean isFlatLayer2() {
101 return nodeConnectors.isEmpty();
108 * @return the Network Address part of the subnet
110 public InetAddress getNetworkAddress() {
111 return networkAddress;
115 * @param networkAddress the networkAddress to set
117 public Subnet setNetworkAddress(InetAddress networkAddress) {
118 this.networkAddress = networkAddress;
126 * @return the subnet mask length
128 public short getSubnetMaskLength() {
129 return this.subnetMaskLength;
132 public Subnet setSubnetMaskLength(short m) {
133 this.subnetMaskLength = m;
138 * returns the prefix of a given IP by applying this subnet's mask
140 private InetAddress getPrefixForAddress(InetAddress ip) {
141 int bytes = this.subnetMaskLength / 8;
142 int bits = this.subnetMaskLength % 8;
144 byte[] sn = ip.getAddress();
146 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
147 sn[bytes] = (byte) (modifiedByte << (8 - bits));
150 for (; bytes < sn.length; bytes++) {
151 sn[bytes] = (byte) (0);
154 return InetAddress.getByAddress(sn);
155 } catch (UnknownHostException e) {
160 public boolean isSubnetOf(InetAddress ip) {
163 InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
164 InetAddress otherPrefix = getPrefixForAddress(ip);
165 if ((thisPrefix == null) || (otherPrefix == null))
167 if (thisPrefix.equals(otherPrefix))
173 public short getVlan() {
177 public Subnet setVlan(short i) {
183 * @see java.lang.Object#hashCode()
186 public int hashCode() {
187 return new HashCodeBuilder().append(networkAddress).append(
188 subnetMaskLength).toHashCode();
192 * @see java.lang.Object#equals(java.lang.Object)
195 public boolean equals(Object obj) {
202 if (obj.getClass() != getClass()) {
205 Subnet other = (Subnet) obj;
206 // Check only equality for the key fields
207 return new EqualsBuilder().append(networkAddress, other.networkAddress)
208 .append(subnetMaskLength, other.subnetMaskLength).isEquals();
212 * @see java.lang.Object#toString()
215 public String toString() {
216 return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
217 + "/" + subnetMaskLength
218 + ((vlan == 0) ? "" : (" vlan=" + vlan)) + " "
219 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
222 public boolean hasNodeConnector(NodeConnector p) {
226 if (this.isFlatLayer2()) {
229 return this.nodeConnectors.contains(p);
232 public boolean isMutualExclusive(Subnet otherSubnet) {
233 if (this.networkAddress.getClass() != otherSubnet.networkAddress
236 if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
239 if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {