blob: 4ff17a31d0c9f6fe1e9a5266995794d30a191e86 [file] [log] [blame]
Jonathan Hartd857ad62013-12-14 18:08:17 -08001/**
Jonathan Hart96892d12014-03-26 20:21:29 -07002l* Copyright 2011,2012, Big Switch Networks, Inc.
Jonathan Hartd857ad62013-12-14 18:08:17 -08003* 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**/
17
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.
32 *
33 * 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.
36 *
37 * @author readams
38 *
39 */
TeruU80ce5062014-03-03 17:16:13 -080040public class OnosDevice implements Serializable { //implements Comparable<OnosDevice> {
Jonathan Hart96892d12014-03-26 20:21:29 -070041
Jonathan Hartd857ad62013-12-14 18:08:17 -080042 private static int ACTIVITY_TIMEOUT = 30000;
43
44 /**
45 * The MAC address associated with this entity
46 */
47 private MACAddress macAddress;
48
49 /**
50 * The IP address associated with this entity, or null if no IP learned
51 * from the network observation associated with this entity
52 */
53 private Integer ipv4Address;
54
55 /**
56 * The VLAN tag on this entity, or null if untagged
57 */
58 private Short vlan;
59
60 /**
61 * The DPID of the switch for the ingress point for this entity,
62 * or null if not present
63 */
64 private long switchDPID;
65
66 /**
67 * The port number of the switch for the ingress point for this entity,
68 * or null if not present
69 */
70 private short switchPort;
71
72 /**
73 * The last time we observed this entity on the network
74 */
75 private Date lastSeenTimestamp;
76
Jonathan Hartd857ad62013-12-14 18:08:17 -080077 private Date activeSince;
78
79 private int hashCode = 0;
80
81 // ************
82 // Constructors
83 // ************
TeruU7feef8a2014-04-03 00:15:49 -070084 protected OnosDevice() {}
85
Jonathan Hartd857ad62013-12-14 18:08:17 -080086 /**
87 * Create a new entity
88 *
89 * @param macAddress
90 * @param vlan
91 * @param ipv4Address
92 * @param switchDPID
93 * @param switchPort
94 * @param lastSeenTimestamp
95 */
96 public OnosDevice(MACAddress macAddress, Short vlan,
97 Integer ipv4Address, Long switchDPID, short switchPort,
98 Date lastSeenTimestamp) {
99 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 }
TeruU80ce5062014-03-03 17:16:13 -0800119
120 public void setIpv4Address(Integer ipv4Address) {
121 this.ipv4Address = ipv4Address;
122 }
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 }
TeruU80ce5062014-03-03 17:16:13 -0800131
132 public void setSwitchDPID(long dpid) {
133 this.switchDPID = dpid;
134 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800135
136 public short getSwitchPort() {
137 return switchPort;
138 }
TeruU80ce5062014-03-03 17:16:13 -0800139
140 public void setSwitchPort(short port) {
141 this.switchPort = port;
142 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800143
144 public Date getLastSeenTimestamp() {
145 return lastSeenTimestamp;
146 }
147
Jonathan Hart02a59e42014-03-26 18:50:23 -0700148
Jonathan Hartd857ad62013-12-14 18:08:17 -0800149 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
150 if (activeSince == null ||
151 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
152 lastSeenTimestamp.getTime())
153 this.activeSince = lastSeenTimestamp;
154 this.lastSeenTimestamp = lastSeenTimestamp;
155 }
156
157 public Date getActiveSince() {
158 return activeSince;
159 }
160
161 public void setActiveSince(Date activeSince) {
162 this.activeSince = activeSince;
163 }
164
Jonathan Hartd857ad62013-12-14 18:08:17 -0800165 @Override
166 public int hashCode() {
167 if (hashCode != 0) return hashCode;
168 final int prime = 31;
169 hashCode = 1;
170 hashCode = prime * hashCode
171 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
TeruU80ce5062014-03-03 17:16:13 -0800172 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
173 hashCode = prime * hashCode + (int)switchDPID;
174 hashCode = prime * hashCode + (int)switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800175 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
176 return hashCode;
177 }
178
179 @Override
180 public boolean equals(Object obj) {
181 if (this == obj) return true;
182 if (obj == null) return false;
183 if (getClass() != obj.getClass()) return false;
TeruU80ce5062014-03-03 17:16:13 -0800184 OnosDevice other = (OnosDevice) obj;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800185 if (hashCode() != other.hashCode()) return false;
186 if (ipv4Address == null) {
187 if (other.ipv4Address != null) return false;
TeruU80ce5062014-03-03 17:16:13 -0800188 } else if (!ipv4Address.equals(other.ipv4Address)) return false;
189 if (macAddress == null) {
190 if (other.macAddress != null) return false;
191 } else if (!macAddress.equals(other.macAddress)) return false;
192 if(switchDPID != other.switchDPID) return false;
193 if (switchPort != other.switchPort) return false;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800194 if (vlan == null) {
195 if (other.vlan != null) return false;
196 } else if (!vlan.equals(other.vlan)) return false;
197 return true;
198 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800199
200 @Override
201 public String toString() {
202 StringBuilder builder = new StringBuilder();
203 builder.append("Entity [macAddress=");
204 builder.append(macAddress.toString());
205 builder.append(", ipv4Address=");
206 builder.append(IPv4.fromIPv4Address(ipv4Address==null ?
207 0 : ipv4Address.intValue()));
208 builder.append(", vlan=");
209 builder.append(vlan);
210 builder.append(", switchDPID=");
211 builder.append(switchDPID);
212 builder.append(", switchPort=");
213 builder.append(switchPort);
214 builder.append(", lastSeenTimestamp=");
215 builder.append(lastSeenTimestamp == null? "null" : lastSeenTimestamp.getTime());
216 builder.append(", activeSince=");
217 builder.append(activeSince == null? "null" : activeSince.getTime());
218 builder.append("]");
219 return builder.toString();
220 }
221
222 /*
223 @Override
224 public int compareTo(OnosDevice o) {
225 if (macAddress < o.macAddress) return -1;
226 if (macAddress > o.macAddress) return 1;
227
228 int r;
229 if (switchDPID == null)
230 r = o.switchDPID == null ? 0 : -1;
231 else if (o.switchDPID == null)
232 r = 1;
233 else
234 r = switchDPID.compareTo(o.switchDPID);
235 if (r != 0) return r;
236
237 if (switchPort == null)
238 r = o.switchPort == null ? 0 : -1;
239 else if (o.switchPort == null)
240 r = 1;
241 else
242 r = switchPort.compareTo(o.switchPort);
243 if (r != 0) return r;
244
245 if (ipv4Address == null)
246 r = o.ipv4Address == null ? 0 : -1;
247 else if (o.ipv4Address == null)
248 r = 1;
249 else
250 r = ipv4Address.compareTo(o.ipv4Address);
251 if (r != 0) return r;
252
253 if (vlan == null)
254 r = o.vlan == null ? 0 : -1;
255 else if (o.vlan == null)
256 r = 1;
257 else
258 r = vlan.compareTo(o.vlan);
259 if (r != 0) return r;
260
261 return 0;
262 }*/
263
264}