blob: 796129a702b65f3fcd11d39d03509a0662819006 [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;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700104 if (lastSeenTimestamp != null) {
105 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
106 this.activeSince = new Date(lastSeenTimestamp.getTime());
107 } else {
108 this.lastSeenTimestamp = null;
109 this.activeSince = null;
110 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800111 }
112
113 // ***************
114 // Getters/Setters
115 // ***************
116
117 public MACAddress getMacAddress() {
118 return macAddress;
119 }
120
121 public Integer getIpv4Address() {
122 return ipv4Address;
123 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700124
TeruU80ce5062014-03-03 17:16:13 -0800125 public void setIpv4Address(Integer ipv4Address) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 this.ipv4Address = ipv4Address;
TeruU80ce5062014-03-03 17:16:13 -0800127 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800128
129 public Short getVlan() {
130 return vlan;
131 }
132
133 public Long getSwitchDPID() {
134 return switchDPID;
135 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700136
TeruU80ce5062014-03-03 17:16:13 -0800137 public void setSwitchDPID(long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 this.switchDPID = dpid;
TeruU80ce5062014-03-03 17:16:13 -0800139 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800140
141 public short getSwitchPort() {
142 return switchPort;
143 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700144
TeruU80ce5062014-03-03 17:16:13 -0800145 public void setSwitchPort(short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 this.switchPort = port;
TeruU80ce5062014-03-03 17:16:13 -0800147 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800148
149 public Date getLastSeenTimestamp() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700150 if (this.lastSeenTimestamp == null) {
151 return null;
152 }
153 return new Date(this.lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800154 }
155
Jonathan Hartd857ad62013-12-14 18:08:17 -0800156 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
157 if (activeSince == null ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 lastSeenTimestamp.getTime()) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700160 this.activeSince = new Date(lastSeenTimestamp.getTime());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700162 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800163 }
164
165 public Date getActiveSince() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700166 return new Date(this.activeSince.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800167 }
168
169 public void setActiveSince(Date activeSince) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700170 this.activeSince = new Date(activeSince.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800171 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700172
Jonathan Hartd857ad62013-12-14 18:08:17 -0800173 @Override
174 public int hashCode() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700175 if (hashCode != 0) {
176 return hashCode;
177 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800178 final int prime = 31;
179 hashCode = 1;
180 hashCode = prime * hashCode
Ray Milkey269ffb92014-04-03 14:43:30 -0700181 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
TeruU80ce5062014-03-03 17:16:13 -0800182 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 hashCode = prime * hashCode + (int) switchDPID;
184 hashCode = prime * hashCode + (int) switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800185 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
186 return hashCode;
187 }
188
189 @Override
190 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700191 if (this == obj) {
192 return true;
193 }
194 if (obj == null) {
195 return false;
196 }
197 if (getClass() != obj.getClass()) {
198 return false;
199 }
TeruU80ce5062014-03-03 17:16:13 -0800200 OnosDevice other = (OnosDevice) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700201 if (hashCode() != other.hashCode()) {
202 return false;
203 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800204 if (ipv4Address == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700205 if (other.ipv4Address != null) {
206 return false;
207 }
208 } else if (!ipv4Address.equals(other.ipv4Address)) {
209 return false;
210 }
TeruU80ce5062014-03-03 17:16:13 -0800211 if (macAddress == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700212 if (other.macAddress != null) {
213 return false;
214 }
215 } else if (!macAddress.equals(other.macAddress)) {
216 return false;
217 }
218 if (switchDPID != other.switchDPID) {
219 return false;
220 }
221 if (switchPort != other.switchPort) {
222 return false;
223 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800224 if (vlan == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700225 if (other.vlan != null) {
226 return false;
227 }
228 } else if (!vlan.equals(other.vlan)) {
229 return false;
230 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800231 return true;
232 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700233
Jonathan Hartd857ad62013-12-14 18:08:17 -0800234 @Override
235 public String toString() {
236 StringBuilder builder = new StringBuilder();
237 builder.append("Entity [macAddress=");
238 builder.append(macAddress.toString());
239 builder.append(", ipv4Address=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700240 builder.append(IPv4.fromIPv4Address(ipv4Address == null ?
241 0 : ipv4Address.intValue()));
Jonathan Hartd857ad62013-12-14 18:08:17 -0800242 builder.append(", vlan=");
243 builder.append(vlan);
244 builder.append(", switchDPID=");
245 builder.append(switchDPID);
246 builder.append(", switchPort=");
247 builder.append(switchPort);
248 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700249 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800250 builder.append(", activeSince=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700251 builder.append(activeSince == null ? "null" : activeSince.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800252 builder.append("]");
253 return builder.toString();
254 }
255
256 /*
257 @Override
258 public int compareTo(OnosDevice o) {
259 if (macAddress < o.macAddress) return -1;
260 if (macAddress > o.macAddress) return 1;
261
262 int r;
263 if (switchDPID == null)
264 r = o.switchDPID == null ? 0 : -1;
265 else if (o.switchDPID == null)
266 r = 1;
267 else
268 r = switchDPID.compareTo(o.switchDPID);
269 if (r != 0) return r;
270
271 if (switchPort == null)
272 r = o.switchPort == null ? 0 : -1;
273 else if (o.switchPort == null)
274 r = 1;
275 else
276 r = switchPort.compareTo(o.switchPort);
277 if (r != 0) return r;
278
279 if (ipv4Address == null)
280 r = o.ipv4Address == null ? 0 : -1;
281 else if (o.ipv4Address == null)
282 r = 1;
283 else
284 r = ipv4Address.compareTo(o.ipv4Address);
285 if (r != 0) return r;
286
287 if (vlan == null)
288 r = o.vlan == null ? 0 : -1;
289 else if (o.vlan == null)
290 r = 1;
291 else
292 r = vlan.compareTo(o.vlan);
293 if (r != 0) return r;
294
295 return 0;
296 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700297
Jonathan Hartd857ad62013-12-14 18:08:17 -0800298}