Five more Equals/HashCode/StringBuilder replacements
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / Subnet.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.switchmanager;
11
12 import java.io.Serializable;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.opendaylight.controller.sal.core.NodeConnector;
19
20 /**
21  * The class describes subnet information including L3 address, vlan and set of
22  * ports associated with the subnet.
23  */
24 public class Subnet implements Serializable {
25     private static final long serialVersionUID = 1L;
26     // Key fields
27     private InetAddress networkAddress;
28     private short subnetMaskLength;
29     // Property fields
30     private short vlan;
31     private Set<NodeConnector> nodeConnectors;
32
33     public Subnet(InetAddress ip, short maskLen, short vlan) {
34         this.networkAddress = ip;
35         this.subnetMaskLength = maskLen;
36         this.vlan = vlan;
37         this.nodeConnectors = new HashSet<NodeConnector>();
38     }
39
40     public Subnet(SubnetConfig conf) {
41         networkAddress = conf.getIPnum();
42         subnetMaskLength = conf.getIPMaskLen();
43         nodeConnectors = conf.getSubnetNodeConnectors();
44     }
45
46     /**
47      * Add NodeConnectors to a subnet
48      *
49      * @param sp Set of NodeConnectors to add to the subnet
50      */
51     public void addNodeConnectors(Set<NodeConnector> sp) {
52         if (sp == null) {
53             return;
54         }
55
56         for (NodeConnector p : sp) {
57             this.nodeConnectors.add(p);
58         }
59     }
60
61     /**
62      * Delete NodeConnectors from subnet
63      *
64      * @param sp Set of NodeConnectors to add to the subnet
65      */
66     public void deleteNodeConnectors(Set<NodeConnector> sp) {
67         if (sp == null) {
68             return;
69         }
70         for (NodeConnector p : sp) {
71             this.nodeConnectors.remove(p);
72         }
73     }
74
75     /**
76      * Return the list of NodeConnectors configured for this subnet,
77      * could be also an empty set in case of all the known
78      * nodeconnectors.
79      *
80      *
81      * @return The list of NodeConnectors attached to the subnet
82      */
83     public Set<NodeConnector> getNodeConnectors() {
84         return this.nodeConnectors;
85     }
86
87     /**
88      * If the subnet has no node connectors attached to it then it
89      * means that is a whole L2 flat domain
90      *
91      *
92      * @return true if there are no node connectors configured for the
93      * subnet else false
94      */
95     public boolean isFlatLayer2() {
96         return nodeConnectors.isEmpty();
97     }
98
99     /**
100      * getter method
101      *
102      *
103      * @return the Network Address part of the subnet
104      */
105     public InetAddress getNetworkAddress() {
106         return networkAddress;
107     }
108
109     /**
110      * @param networkAddress the networkAddress to set
111      */
112     public Subnet setNetworkAddress(InetAddress networkAddress) {
113         this.networkAddress = networkAddress;
114         return this;
115     }
116
117     /**
118      * getter method
119      *
120      *
121      * @return the subnet mask length
122      */
123     public short getSubnetMaskLength() {
124         return this.subnetMaskLength;
125     }
126
127     public Subnet setSubnetMaskLength(short m) {
128         this.subnetMaskLength = m;
129         return this;
130     }
131
132     /*
133      * returns the prefix of a given IP by applying this subnet's mask
134      */
135     private InetAddress getPrefixForAddress(InetAddress ip) {
136         int bytes = this.subnetMaskLength / 8;
137         int bits = this.subnetMaskLength % 8;
138         byte modifiedByte;
139         byte[] sn = ip.getAddress();
140         if (bits > 0) {
141             modifiedByte = (byte) (sn[bytes] >> (8 - bits));
142             sn[bytes] = (byte) (modifiedByte << (8 - bits));
143             bytes++;
144         }
145         for (; bytes < sn.length; bytes++) {
146             sn[bytes] = (byte) (0);
147         }
148         try {
149             return InetAddress.getByAddress(sn);
150         } catch (UnknownHostException e) {
151             return null;
152         }
153     }
154
155     public boolean isSubnetOf(InetAddress ip) {
156         if (ip == null)
157             return false;
158         InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
159         InetAddress otherPrefix = getPrefixForAddress(ip);
160         if ((thisPrefix == null) || (otherPrefix == null))
161             return false;
162         if (thisPrefix.equals(otherPrefix))
163             return true;
164         else
165             return false;
166     }
167
168     public short getVlan() {
169         return this.vlan;
170     }
171
172     public Subnet setVlan(short i) {
173         this.vlan = i;
174         return this;
175     }
176
177     @Override
178     public int hashCode() {
179         final int prime = 31;
180         int result = 1;
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;
187         return result;
188     }
189
190     @Override
191     public boolean equals(Object obj) {
192         if (this == obj)
193             return true;
194         if (obj == null)
195             return false;
196         if (getClass() != obj.getClass())
197             return false;
198         Subnet other = (Subnet) obj;
199         if (networkAddress == null) {
200             if (other.networkAddress != null)
201                 return false;
202         } else if (!networkAddress.equals(other.networkAddress))
203             return false;
204         if (nodeConnectors == null) {
205             if (other.nodeConnectors != null)
206                 return false;
207         } else if (!nodeConnectors.equals(other.nodeConnectors))
208             return false;
209         if (subnetMaskLength != other.subnetMaskLength)
210             return false;
211         if (vlan != other.vlan)
212             return false;
213         return true;
214     }
215
216     /* (non-Javadoc)
217      * @see java.lang.Object#toString()
218      */
219     @Override
220     public String toString() {
221         return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
222                 + "/" + subnetMaskLength
223                 + ((vlan == 0) ? "" : (" vlan=" + vlan)) + " "
224                 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
225     }
226
227     public boolean hasNodeConnector(NodeConnector p) {
228         if (p == null) {
229             return false;
230         }
231         if (this.isFlatLayer2()) {
232             return true;
233         }
234         return this.nodeConnectors.contains(p);
235     }
236
237     public boolean isMutualExclusive(Subnet otherSubnet) {
238         if (this.networkAddress.getClass() != otherSubnet.networkAddress
239                 .getClass())
240             return true;
241         if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
242             return false;
243         }
244         if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {
245             return false;
246         }
247         return true;
248     }
249 }