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 transient InetAddress subnetPrefix;
29 private short subnetMaskLength;
32 private final Set<NodeConnector> nodeConnectors;
34 public Subnet(InetAddress ip, short maskLen, short vlan) {
35 this.networkAddress = ip;
36 this.subnetMaskLength = maskLen;
38 this.nodeConnectors = new HashSet<NodeConnector>();
41 public Subnet(SubnetConfig conf) {
42 networkAddress = conf.getIPAddress();
43 subnetMaskLength = conf.getIPMaskLen();
44 nodeConnectors = conf.getNodeConnectors();
47 public Subnet(Subnet subnet) {
48 networkAddress = subnet.networkAddress;
49 subnetMaskLength = subnet.subnetMaskLength;
51 nodeConnectors = new HashSet<NodeConnector>(subnet.nodeConnectors);
55 * Add NodeConnectors to a subnet
57 * @param sp Set of NodeConnectors to add to the subnet
59 public void addNodeConnectors(Set<NodeConnector> sp) {
61 this.nodeConnectors.addAll(sp);
66 * Delete NodeConnectors from subnet
68 * @param sp Set of NodeConnectors to add to the subnet
70 public void deleteNodeConnectors(Set<NodeConnector> sp) {
74 for (NodeConnector p : sp) {
75 this.nodeConnectors.remove(p);
80 * Return the list of NodeConnectors configured for this subnet,
81 * could be also an empty set in case of all the known
85 * @return The list of NodeConnectors attached to the subnet
87 public Set<NodeConnector> getNodeConnectors() {
88 return this.nodeConnectors;
92 * If the subnet has no node connectors attached to it then it
93 * means that is a whole L2 flat domain
96 * @return true if there are no node connectors configured for the
99 public boolean isFlatLayer2() {
100 return nodeConnectors.isEmpty();
107 * @return the Network Address part of the subnet
109 public InetAddress getNetworkAddress() {
110 return networkAddress;
114 * @param networkAddress the networkAddress to set
116 public Subnet setNetworkAddress(InetAddress networkAddress) {
117 this.networkAddress = networkAddress;
118 this.subnetPrefix = null;
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) {
164 if(subnetPrefix == null) {
165 subnetPrefix = getPrefixForAddress(this.networkAddress);
167 InetAddress otherPrefix = getPrefixForAddress(ip);
168 boolean isSubnetOf = true;
169 if (((subnetPrefix == null) || (otherPrefix == null)) || (!subnetPrefix.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) {
205 if (getClass() != obj.getClass()) {
208 Subnet other = (Subnet) obj;
209 if (networkAddress == null) {
210 if (other.networkAddress != null) {
213 } else if (!networkAddress.equals(other.networkAddress)) {
216 if (nodeConnectors == null) {
217 if (other.nodeConnectors != null) {
220 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
223 if (subnetMaskLength != other.subnetMaskLength) {
226 if (vlan != other.vlan) {
233 * @see java.lang.Object#toString()
236 public String toString() {
237 return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
238 + "/" + subnetMaskLength
239 + ((vlan == 0) ? "" : (", vlan=" + vlan)) + ", "
240 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
243 public boolean hasNodeConnector(NodeConnector p) {
247 return isFlatLayer2() || nodeConnectors.contains(p);
250 public boolean isMutualExclusive(Subnet otherSubnet) {
251 return !(isSubnetOf(otherSubnet.getNetworkAddress()) || otherSubnet.isSubnetOf(getNetworkAddress()));
255 * Implement clonable interface
258 public Subnet clone() {
259 return new Subnet(this);