Merge "Maping of union leaf a bits leaf YANG type to JAVA inner classes."
[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 Cloneable, 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 final 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     public Subnet(Subnet subnet) {
47         networkAddress = subnet.networkAddress;
48         subnetMaskLength = subnet.subnetMaskLength;
49         vlan = subnet.vlan;
50         nodeConnectors = new HashSet<NodeConnector>(subnet.nodeConnectors);
51     }
52
53     /**
54      * Add NodeConnectors to a subnet
55      *
56      * @param sp Set of NodeConnectors to add to the subnet
57      */
58     public void addNodeConnectors(Set<NodeConnector> sp) {
59         if (sp == null) {
60             return;
61         }
62
63         for (NodeConnector p : sp) {
64             this.nodeConnectors.add(p);
65         }
66     }
67
68     /**
69      * Delete NodeConnectors from subnet
70      *
71      * @param sp Set of NodeConnectors to add to the subnet
72      */
73     public void deleteNodeConnectors(Set<NodeConnector> sp) {
74         if (sp == null) {
75             return;
76         }
77         for (NodeConnector p : sp) {
78             this.nodeConnectors.remove(p);
79         }
80     }
81
82     /**
83      * Return the list of NodeConnectors configured for this subnet,
84      * could be also an empty set in case of all the known
85      * nodeconnectors.
86      *
87      *
88      * @return The list of NodeConnectors attached to the subnet
89      */
90     public Set<NodeConnector> getNodeConnectors() {
91         return this.nodeConnectors;
92     }
93
94     /**
95      * If the subnet has no node connectors attached to it then it
96      * means that is a whole L2 flat domain
97      *
98      *
99      * @return true if there are no node connectors configured for the
100      * subnet else false
101      */
102     public boolean isFlatLayer2() {
103         return nodeConnectors.isEmpty();
104     }
105
106     /**
107      * getter method
108      *
109      *
110      * @return the Network Address part of the subnet
111      */
112     public InetAddress getNetworkAddress() {
113         return networkAddress;
114     }
115
116     /**
117      * @param networkAddress the networkAddress to set
118      */
119     public Subnet setNetworkAddress(InetAddress networkAddress) {
120         this.networkAddress = networkAddress;
121         return this;
122     }
123
124     /**
125      * getter method
126      *
127      *
128      * @return the subnet mask length
129      */
130     public short getSubnetMaskLength() {
131         return this.subnetMaskLength;
132     }
133
134     public Subnet setSubnetMaskLength(short m) {
135         this.subnetMaskLength = m;
136         return this;
137     }
138
139     /*
140      * returns the prefix of a given IP by applying this subnet's mask
141      */
142     private InetAddress getPrefixForAddress(InetAddress ip) {
143         int bytes = this.subnetMaskLength / 8;
144         int bits = this.subnetMaskLength % 8;
145         byte modifiedByte;
146         byte[] sn = ip.getAddress();
147         if (bits > 0) {
148             modifiedByte = (byte) (sn[bytes] >> (8 - bits));
149             sn[bytes] = (byte) (modifiedByte << (8 - bits));
150             bytes++;
151         }
152         for (; bytes < sn.length; bytes++) {
153             sn[bytes] = (byte) (0);
154         }
155         try {
156             return InetAddress.getByAddress(sn);
157         } catch (UnknownHostException e) {
158             return null;
159         }
160     }
161
162     public boolean isSubnetOf(InetAddress ip) {
163         if (ip == null)
164             return false;
165         InetAddress thisPrefix = getPrefixForAddress(this.networkAddress);
166         InetAddress otherPrefix = getPrefixForAddress(ip);
167         if ((thisPrefix == null) || (otherPrefix == null))
168             return false;
169         if (thisPrefix.equals(otherPrefix))
170             return true;
171         else
172             return false;
173     }
174
175     public short getVlan() {
176         return this.vlan;
177     }
178
179     public Subnet setVlan(short i) {
180         this.vlan = i;
181         return this;
182     }
183
184     @Override
185     public int hashCode() {
186         final int prime = 31;
187         int result = 1;
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;
194         return result;
195     }
196
197     @Override
198     public boolean equals(Object obj) {
199         if (this == obj)
200             return true;
201         if (obj == null)
202             return false;
203         if (getClass() != obj.getClass())
204             return false;
205         Subnet other = (Subnet) obj;
206         if (networkAddress == null) {
207             if (other.networkAddress != null)
208                 return false;
209         } else if (!networkAddress.equals(other.networkAddress))
210             return false;
211         if (nodeConnectors == null) {
212             if (other.nodeConnectors != null)
213                 return false;
214         } else if (!nodeConnectors.equals(other.nodeConnectors))
215             return false;
216         if (subnetMaskLength != other.subnetMaskLength)
217             return false;
218         if (vlan != other.vlan)
219             return false;
220         return true;
221     }
222
223     /* (non-Javadoc)
224      * @see java.lang.Object#toString()
225      */
226     @Override
227     public String toString() {
228         return ("Subnet [networkAddress=" + networkAddress.getHostAddress()
229                 + "/" + subnetMaskLength
230                 + ((vlan == 0) ? "" : (", vlan=" + vlan)) + ", "
231                 + ((isFlatLayer2()) ? "{[*, *]}" : nodeConnectors.toString()) + "]");
232     }
233
234     public boolean hasNodeConnector(NodeConnector p) {
235         if (p == null) {
236             return false;
237         }
238         if (this.isFlatLayer2()) {
239             return true;
240         }
241         return this.nodeConnectors.contains(p);
242     }
243
244     public boolean isMutualExclusive(Subnet otherSubnet) {
245         if (this.networkAddress.getClass() != otherSubnet.networkAddress
246                 .getClass())
247             return true;
248         if (this.isSubnetOf(otherSubnet.getNetworkAddress())) {
249             return false;
250         }
251         if (otherSubnet.isSubnetOf(this.getNetworkAddress())) {
252             return false;
253         }
254         return true;
255     }
256
257     /**
258      * Implement clonable interface
259      */
260     @Override
261     public Subnet clone() {
262         return new Subnet(this);
263     }
264
265 }