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 Serializable {
25 private static final long serialVersionUID = 1L;
27 private InetAddress networkAddress;
28 private short subnetMaskLength;
31 private 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();
47 * Add NodeConnectors to a subnet
49 * @param sp Set of NodeConnectors to add to the subnet
51 public void addNodeConnectors(Set<NodeConnector> sp) {
56 for (NodeConnector p : sp) {
57 this.nodeConnectors.add(p);
62 * Delete NodeConnectors from subnet
64 * @param sp Set of NodeConnectors to add to the subnet
66 public void deleteNodeConnectors(Set<NodeConnector> sp) {
70 for (NodeConnector p : sp) {
71 this.nodeConnectors.remove(p);
76 * Return the list of NodeConnectors configured for this subnet,
77 * could be also an empty set in case of all the known
81 * @return The list of NodeConnectors attached to the subnet
83 public Set<NodeConnector> getNodeConnectors() {
84 return this.nodeConnectors;
88 * If the subnet has no node connectors attached to it then it
89 * means that is a whole L2 flat domain
92 * @return true if there are no node connectors configured for the
95 public boolean isFlatLayer2() {
96 return nodeConnectors.isEmpty();
103 * @return the Network Address part of the subnet
105 public InetAddress getNetworkAddress() {
106 return networkAddress;
110 * @param networkAddress the networkAddress to set
112 public Subnet setNetworkAddress(InetAddress networkAddress) {
113 this.networkAddress = networkAddress;
121 * @return the subnet mask length
123 public short getSubnetMaskLength() {
124 return this.subnetMaskLength;
127 public Subnet setSubnetMaskLength(short m) {
128 this.subnetMaskLength = m;
133 * returns the prefix of a given IP by applying this subnet's mask
135 private InetAddress getPrefixForAddress(InetAddress ip) {
136 int bytes = this.subnetMaskLength / 8;
137 int bits = this.subnetMaskLength % 8;
139 byte[] sn = ip.getAddress();
141 modifiedByte = (byte) (sn[bytes] >> (8 - bits));
142 sn[bytes] = (byte) (modifiedByte << (8 - bits));
145 for (; bytes < sn.length; bytes++) {
146 sn[bytes] = (byte) (0);
149 return InetAddress.getByAddress(sn);
150 } catch (UnknownHostException e) {
155 public boolean isSubnetOf(InetAddress ip) {
158 InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
159 InetAddress otherPrefix = getPrefixForAddress(ip);
160 if ((thisPrefix == null) || (otherPrefix == null))
162 if (thisPrefix.equals(otherPrefix))
168 public short getVlan() {
172 public Subnet setVlan(short i) {
178 public int hashCode() {
179 final int prime = 31;
181 result = prime * result
182 + ((networkAddress == null) ? 0 : networkAddress.hashCode());
183 result = prime * result
184 + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
185 result = prime * result + subnetMaskLength;
186 result = prime * result + vlan;
191 public boolean equals(Object obj) {
196 if (getClass() != obj.getClass())
198 Subnet other = (Subnet) obj;
199 if (networkAddress == null) {
200 if (other.networkAddress != null)
202 } else if (!networkAddress.equals(other.networkAddress))
204 if (nodeConnectors == null) {
205 if (other.nodeConnectors != null)
207 } else if (!nodeConnectors.equals(other.nodeConnectors))
209 if (subnetMaskLength != other.subnetMaskLength)
211 if (vlan != other.vlan)
217 * @see java.lang.Object#toString()
220 public String toString() {
221 return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
222 + "/" + subnetMaskLength
223 + ((vlan == 0) ? "" : (" vlan=" + vlan)) + " "
224 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
227 public boolean hasNodeConnector(NodeConnector p) {
231 if (this.isFlatLayer2()) {
234 return this.nodeConnectors.contains(p);
237 public boolean isMutualExclusive(Subnet otherSubnet) {
238 if (this.networkAddress.getClass() != otherSubnet.networkAddress
241 if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
244 if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {