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.opendaylight.controller.sal.core.NodeConnector;
21 * The class describes subnet information including L3 address, vlan and set of
22 * ports associated with the subnet.
24 public class Subnet implements Cloneable, Serializable {
25 private static final long serialVersionUID = 1L;
27 private InetAddress networkAddress;
28 private short subnetMaskLength;
31 private final Set<NodeConnector> nodeConnectors;
33 public Subnet(InetAddress ip, short maskLen, short vlan) {
34 this.networkAddress = ip;
35 this.subnetMaskLength = maskLen;
37 this.nodeConnectors = new HashSet<NodeConnector>();
40 public Subnet(SubnetConfig conf) {
41 networkAddress = conf.getIPnum();
42 subnetMaskLength = conf.getIPMaskLen();
43 nodeConnectors = conf.getSubnetNodeConnectors();
46 public Subnet(Subnet subnet) {
47 networkAddress = subnet.networkAddress;
48 subnetMaskLength = subnet.subnetMaskLength;
50 nodeConnectors = new HashSet<NodeConnector>(subnet.nodeConnectors);
54 * Add NodeConnectors to a subnet
56 * @param sp Set of NodeConnectors to add to the subnet
58 public void addNodeConnectors(Set<NodeConnector> sp) {
63 for (NodeConnector p : sp) {
64 this.nodeConnectors.add(p);
69 * Delete NodeConnectors from subnet
71 * @param sp Set of NodeConnectors to add to the subnet
73 public void deleteNodeConnectors(Set<NodeConnector> sp) {
77 for (NodeConnector p : sp) {
78 this.nodeConnectors.remove(p);
83 * Return the list of NodeConnectors configured for this subnet,
84 * could be also an empty set in case of all the known
88 * @return The list of NodeConnectors attached to the subnet
90 public Set<NodeConnector> getNodeConnectors() {
91 return this.nodeConnectors;
95 * If the subnet has no node connectors attached to it then it
96 * means that is a whole L2 flat domain
99 * @return true if there are no node connectors configured for the
102 public boolean isFlatLayer2() {
103 return nodeConnectors.isEmpty();
110 * @return the Network Address part of the subnet
112 public InetAddress getNetworkAddress() {
113 return networkAddress;
117 * @param networkAddress the networkAddress to set
119 public Subnet setNetworkAddress(InetAddress networkAddress) {
120 this.networkAddress = networkAddress;
128 * @return the subnet mask length
130 public short getSubnetMaskLength() {
131 return this.subnetMaskLength;
134 public Subnet setSubnetMaskLength(short m) {
135 this.subnetMaskLength = m;
140 * returns the prefix of a given IP by applying this subnet's mask
142 private InetAddress getPrefixForAddress(InetAddress ip) {
143 int bytes = this.subnetMaskLength / 8;
144 int bits = this.subnetMaskLength % 8;
146 byte[] sn = ip.getAddress();
148 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
149 sn[bytes] = (byte) (modifiedByte << (8 - bits));
152 for (; bytes < sn.length; bytes++) {
153 sn[bytes] = (byte) (0);
156 return InetAddress.getByAddress(sn);
157 } catch (UnknownHostException e) {
162 public boolean isSubnetOf(InetAddress ip) {
165 InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
166 InetAddress otherPrefix = getPrefixForAddress(ip);
167 if ((thisPrefix == null) || (otherPrefix == null))
169 if (thisPrefix.equals(otherPrefix))
175 public short getVlan() {
179 public Subnet setVlan(short i) {
185 public int hashCode() {
186 final int prime = 31;
188 result = prime * result
189 + ((networkAddress == null) ? 0 : networkAddress.hashCode());
190 result = prime * result
191 + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
192 result = prime * result + subnetMaskLength;
193 result = prime * result + vlan;
198 public boolean equals(Object obj) {
203 if (getClass() != obj.getClass())
205 Subnet other = (Subnet) obj;
206 if (networkAddress == null) {
207 if (other.networkAddress != null)
209 } else if (!networkAddress.equals(other.networkAddress))
211 if (nodeConnectors == null) {
212 if (other.nodeConnectors != null)
214 } else if (!nodeConnectors.equals(other.nodeConnectors))
216 if (subnetMaskLength != other.subnetMaskLength)
218 if (vlan != other.vlan)
224 * @see java.lang.Object#toString()
227 public String toString() {
228 return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
229 + "/" + subnetMaskLength
230 + ((vlan == 0) ? "" : (", vlan=" + vlan)) + ", "
231 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
234 public boolean hasNodeConnector(NodeConnector p) {
238 if (this.isFlatLayer2()) {
241 return this.nodeConnectors.contains(p);
244 public boolean isMutualExclusive(Subnet otherSubnet) {
245 if (this.networkAddress.getClass() != otherSubnet.networkAddress
248 if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
251 if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {
258 * Implement clonable interface
261 public Subnet clone() {
262 return new Subnet(this);