blob: 157d52f37828006f44d193daa753b3ced67e438f [file] [log] [blame]
Jonathan Hartd857ad62013-12-14 18:08:17 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 l* Copyright 2011,2012, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Jonathan Hartd857ad62013-12-14 18:08:17 -080017
Jonathan Hart23701d12014-04-03 10:45:48 -070018package net.onrc.onos.core.devicemanager;
Jonathan Hartd857ad62013-12-14 18:08:17 -080019
TeruU80ce5062014-03-03 17:16:13 -080020import java.io.Serializable;
Jonathan Hartd857ad62013-12-14 18:08:17 -080021import java.util.Date;
22
Jonathan Hartd857ad62013-12-14 18:08:17 -080023import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070024import net.onrc.onos.core.packet.IPv4;
Jonathan Hartd857ad62013-12-14 18:08:17 -080025
26/**
27 * An entity on the network is a visible trace of a device that corresponds
28 * to a packet received from a particular interface on the edge of a network,
29 * with a particular VLAN tag, and a particular MAC address, along with any
30 * other packet characteristics we might want to consider as helpful for
31 * disambiguating devices.
Ray Milkey269ffb92014-04-03 14:43:30 -070032 * <p/>
Jonathan Hartd857ad62013-12-14 18:08:17 -080033 * Entities are the most basic element of devices; devices consist of one or
34 * more entities. Entities are immutable once created, except for the last
35 * seen timestamp.
Jonathan Hartd857ad62013-12-14 18:08:17 -080036 *
Ray Milkey269ffb92014-04-03 14:43:30 -070037 * @author readams
Jonathan Hartd857ad62013-12-14 18:08:17 -080038 */
TeruU80ce5062014-03-03 17:16:13 -080039public class OnosDevice implements Serializable { //implements Comparable<OnosDevice> {
Jonathan Hart96892d12014-03-26 20:21:29 -070040
Ray Milkey94b41b52014-04-10 11:13:06 -070041 private static final int ACTIVITY_TIMEOUT = 30000;
Ray Milkey269ffb92014-04-03 14:43:30 -070042
Jonathan Hartd857ad62013-12-14 18:08:17 -080043 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070044 * The MAC address associated with this entity.
Jonathan Hartd857ad62013-12-14 18:08:17 -080045 */
46 private MACAddress macAddress;
Ray Milkey269ffb92014-04-03 14:43:30 -070047
Jonathan Hartd857ad62013-12-14 18:08:17 -080048 /**
49 * The IP address associated with this entity, or null if no IP learned
Ray Milkeyb41100a2014-04-10 10:42:15 -070050 * from the network observation associated with this entity.
Jonathan Hartd857ad62013-12-14 18:08:17 -080051 */
52 private Integer ipv4Address;
Ray Milkey269ffb92014-04-03 14:43:30 -070053
Jonathan Hartd857ad62013-12-14 18:08:17 -080054 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070055 * The VLAN tag on this entity, or null if untagged.
Jonathan Hartd857ad62013-12-14 18:08:17 -080056 */
57 private Short vlan;
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Jonathan Hartd857ad62013-12-14 18:08:17 -080059 /**
60 * The DPID of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070061 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080062 */
63 private long switchDPID;
Ray Milkey269ffb92014-04-03 14:43:30 -070064
Jonathan Hartd857ad62013-12-14 18:08:17 -080065 /**
66 * The port number of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070067 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080068 */
69 private short switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070070
Jonathan Hartd857ad62013-12-14 18:08:17 -080071 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070072 * The last time we observed this entity on the network.
Jonathan Hartd857ad62013-12-14 18:08:17 -080073 */
74 private Date lastSeenTimestamp;
75
Jonathan Hartd857ad62013-12-14 18:08:17 -080076 private Date activeSince;
Ray Milkey269ffb92014-04-03 14:43:30 -070077
Jonathan Hartd857ad62013-12-14 18:08:17 -080078 private int hashCode = 0;
79
80 // ************
81 // Constructors
82 // ************
Ray Milkey269ffb92014-04-03 14:43:30 -070083 protected OnosDevice() {
84 }
85
Jonathan Hartd857ad62013-12-14 18:08:17 -080086 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070087 * Create a new entity.
Ray Milkey269ffb92014-04-03 14:43:30 -070088 *
Jonathan Hartd857ad62013-12-14 18:08:17 -080089 * @param macAddress
90 * @param vlan
91 * @param ipv4Address
92 * @param switchDPID
93 * @param switchPort
94 * @param lastSeenTimestamp
95 */
Ray Milkey269ffb92014-04-03 14:43:30 -070096 public OnosDevice(MACAddress macAddress, Short vlan,
97 Integer ipv4Address, Long switchDPID, short switchPort,
98 Date lastSeenTimestamp) {
Jonathan Hartd857ad62013-12-14 18:08:17 -080099 this.macAddress = macAddress;
100 this.ipv4Address = ipv4Address;
101 this.vlan = vlan;
102 this.switchDPID = switchDPID;
103 this.switchPort = switchPort;
104 this.lastSeenTimestamp = lastSeenTimestamp;
105 this.activeSince = lastSeenTimestamp;
106 }
107
108 // ***************
109 // Getters/Setters
110 // ***************
111
112 public MACAddress getMacAddress() {
113 return macAddress;
114 }
115
116 public Integer getIpv4Address() {
117 return ipv4Address;
118 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700119
TeruU80ce5062014-03-03 17:16:13 -0800120 public void setIpv4Address(Integer ipv4Address) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 this.ipv4Address = ipv4Address;
TeruU80ce5062014-03-03 17:16:13 -0800122 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800123
124 public Short getVlan() {
125 return vlan;
126 }
127
128 public Long getSwitchDPID() {
129 return switchDPID;
130 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700131
TeruU80ce5062014-03-03 17:16:13 -0800132 public void setSwitchDPID(long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 this.switchDPID = dpid;
TeruU80ce5062014-03-03 17:16:13 -0800134 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800135
136 public short getSwitchPort() {
137 return switchPort;
138 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700139
TeruU80ce5062014-03-03 17:16:13 -0800140 public void setSwitchPort(short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 this.switchPort = port;
TeruU80ce5062014-03-03 17:16:13 -0800142 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800143
144 public Date getLastSeenTimestamp() {
145 return lastSeenTimestamp;
146 }
147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148
Jonathan Hartd857ad62013-12-14 18:08:17 -0800149 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
150 if (activeSince == null ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
Ray Milkeyb29e6262014-04-09 16:02:14 -0700152 lastSeenTimestamp.getTime()) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800153 this.activeSince = lastSeenTimestamp;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700154 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800155 this.lastSeenTimestamp = lastSeenTimestamp;
156 }
157
158 public Date getActiveSince() {
159 return activeSince;
160 }
161
162 public void setActiveSince(Date activeSince) {
163 this.activeSince = activeSince;
164 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700165
Jonathan Hartd857ad62013-12-14 18:08:17 -0800166 @Override
167 public int hashCode() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700168 if (hashCode != 0) {
169 return hashCode;
170 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800171 final int prime = 31;
172 hashCode = 1;
173 hashCode = prime * hashCode
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
TeruU80ce5062014-03-03 17:16:13 -0800175 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 hashCode = prime * hashCode + (int) switchDPID;
177 hashCode = prime * hashCode + (int) switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800178 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
179 return hashCode;
180 }
181
182 @Override
183 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 if (this == obj) {
185 return true;
186 }
187 if (obj == null) {
188 return false;
189 }
190 if (getClass() != obj.getClass()) {
191 return false;
192 }
TeruU80ce5062014-03-03 17:16:13 -0800193 OnosDevice other = (OnosDevice) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700194 if (hashCode() != other.hashCode()) {
195 return false;
196 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800197 if (ipv4Address == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700198 if (other.ipv4Address != null) {
199 return false;
200 }
201 } else if (!ipv4Address.equals(other.ipv4Address)) {
202 return false;
203 }
TeruU80ce5062014-03-03 17:16:13 -0800204 if (macAddress == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700205 if (other.macAddress != null) {
206 return false;
207 }
208 } else if (!macAddress.equals(other.macAddress)) {
209 return false;
210 }
211 if (switchDPID != other.switchDPID) {
212 return false;
213 }
214 if (switchPort != other.switchPort) {
215 return false;
216 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800217 if (vlan == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700218 if (other.vlan != null) {
219 return false;
220 }
221 } else if (!vlan.equals(other.vlan)) {
222 return false;
223 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800224 return true;
225 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700226
Jonathan Hartd857ad62013-12-14 18:08:17 -0800227 @Override
228 public String toString() {
229 StringBuilder builder = new StringBuilder();
230 builder.append("Entity [macAddress=");
231 builder.append(macAddress.toString());
232 builder.append(", ipv4Address=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 builder.append(IPv4.fromIPv4Address(ipv4Address == null ?
234 0 : ipv4Address.intValue()));
Jonathan Hartd857ad62013-12-14 18:08:17 -0800235 builder.append(", vlan=");
236 builder.append(vlan);
237 builder.append(", switchDPID=");
238 builder.append(switchDPID);
239 builder.append(", switchPort=");
240 builder.append(switchPort);
241 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700242 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800243 builder.append(", activeSince=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700244 builder.append(activeSince == null ? "null" : activeSince.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800245 builder.append("]");
246 return builder.toString();
247 }
248
249 /*
250 @Override
251 public int compareTo(OnosDevice o) {
252 if (macAddress < o.macAddress) return -1;
253 if (macAddress > o.macAddress) return 1;
254
255 int r;
256 if (switchDPID == null)
257 r = o.switchDPID == null ? 0 : -1;
258 else if (o.switchDPID == null)
259 r = 1;
260 else
261 r = switchDPID.compareTo(o.switchDPID);
262 if (r != 0) return r;
263
264 if (switchPort == null)
265 r = o.switchPort == null ? 0 : -1;
266 else if (o.switchPort == null)
267 r = 1;
268 else
269 r = switchPort.compareTo(o.switchPort);
270 if (r != 0) return r;
271
272 if (ipv4Address == null)
273 r = o.ipv4Address == null ? 0 : -1;
274 else if (o.ipv4Address == null)
275 r = 1;
276 else
277 r = ipv4Address.compareTo(o.ipv4Address);
278 if (r != 0) return r;
279
280 if (vlan == null)
281 r = o.vlan == null ? 0 : -1;
282 else if (o.vlan == null)
283 r = 1;
284 else
285 r = vlan.compareTo(o.vlan);
286 if (r != 0) return r;
287
288 return 0;
289 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700290
Jonathan Hartd857ad62013-12-14 18:08:17 -0800291}