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) {
60 this.nodeConnectors.addAll(sp);
65 * Delete NodeConnectors from subnet
67 * @param sp Set of NodeConnectors to add to the subnet
69 public void deleteNodeConnectors(Set<NodeConnector> sp) {
73 for (NodeConnector p : sp) {
74 this.nodeConnectors.remove(p);
79 * Return the list of NodeConnectors configured for this subnet,
80 * could be also an empty set in case of all the known
84 * @return The list of NodeConnectors attached to the subnet
86 public Set<NodeConnector> getNodeConnectors() {
87 return this.nodeConnectors;
91 * If the subnet has no node connectors attached to it then it
92 * means that is a whole L2 flat domain
95 * @return true if there are no node connectors configured for the
98 public boolean isFlatLayer2() {
99 return nodeConnectors.isEmpty();
106 * @return the Network Address part of the subnet
108 public InetAddress getNetworkAddress() {
109 return networkAddress;
113 * @param networkAddress the networkAddress to set
115 public Subnet setNetworkAddress(InetAddress networkAddress) {
116 this.networkAddress = networkAddress;
124 * @return the subnet mask length
126 public short getSubnetMaskLength() {
127 return this.subnetMaskLength;
130 public Subnet setSubnetMaskLength(short m) {
131 this.subnetMaskLength = m;
136 * returns the prefix of a given IP by applying this subnet's mask
138 private InetAddress getPrefixForAddress(InetAddress ip) {
139 int bytes = this.subnetMaskLength / 8;
140 int bits = this.subnetMaskLength % 8;
142 byte[] sn = ip.getAddress();
144 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
145 sn[bytes] = (byte) (modifiedByte << (8 - bits));
148 for (; bytes < sn.length; bytes++) {
149 sn[bytes] = (byte) (0);
152 return InetAddress.getByAddress(sn);
153 } catch (UnknownHostException e) {
158 public boolean isSubnetOf(InetAddress ip) {
162 InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
163 InetAddress otherPrefix = getPrefixForAddress(ip);
164 if ((thisPrefix == null) || (otherPrefix == null)) {
167 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);