blob: 546ec64986e33e0a23c0e7c16872edc34f57b654 [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
18package net.onrc.onos.ofcontroller.devicemanager;
19
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 Hart96892d12014-03-26 20:21:29 -070024import net.onrc.onos.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 // ************
84
85 /**
86 * Create a new entity
87 *
88 * @param macAddress
89 * @param vlan
90 * @param ipv4Address
91 * @param switchDPID
92 * @param switchPort
93 * @param lastSeenTimestamp
94 */
95 public OnosDevice(MACAddress macAddress, Short vlan,
96 Integer ipv4Address, Long switchDPID, short switchPort,
97 Date lastSeenTimestamp) {
98 this.macAddress = macAddress;
99 this.ipv4Address = ipv4Address;
100 this.vlan = vlan;
101 this.switchDPID = switchDPID;
102 this.switchPort = switchPort;
103 this.lastSeenTimestamp = lastSeenTimestamp;
104 this.activeSince = lastSeenTimestamp;
105 }
106
107 // ***************
108 // Getters/Setters
109 // ***************
110
111 public MACAddress getMacAddress() {
112 return macAddress;
113 }
114
115 public Integer getIpv4Address() {
116 return ipv4Address;
117 }
TeruU80ce5062014-03-03 17:16:13 -0800118
119 public void setIpv4Address(Integer ipv4Address) {
120 this.ipv4Address = ipv4Address;
121 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800122
123 public Short getVlan() {
124 return vlan;
125 }
126
127 public Long getSwitchDPID() {
128 return switchDPID;
129 }
TeruU80ce5062014-03-03 17:16:13 -0800130
131 public void setSwitchDPID(long dpid) {
132 this.switchDPID = dpid;
133 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800134
135 public short getSwitchPort() {
136 return switchPort;
137 }
TeruU80ce5062014-03-03 17:16:13 -0800138
139 public void setSwitchPort(short port) {
140 this.switchPort = port;
141 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800142
143 public Date getLastSeenTimestamp() {
144 return lastSeenTimestamp;
145 }
146
Jonathan Hart02a59e42014-03-26 18:50:23 -0700147
Jonathan Hartd857ad62013-12-14 18:08:17 -0800148 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
149 if (activeSince == null ||
150 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
151 lastSeenTimestamp.getTime())
152 this.activeSince = lastSeenTimestamp;
153 this.lastSeenTimestamp = lastSeenTimestamp;
154 }
155
156 public Date getActiveSince() {
157 return activeSince;
158 }
159
160 public void setActiveSince(Date activeSince) {
161 this.activeSince = activeSince;
162 }
163
Jonathan Hartd857ad62013-12-14 18:08:17 -0800164 @Override
165 public int hashCode() {
166 if (hashCode != 0) return hashCode;
167 final int prime = 31;
168 hashCode = 1;
169 hashCode = prime * hashCode
170 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
TeruU80ce5062014-03-03 17:16:13 -0800171 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
172 hashCode = prime * hashCode + (int)switchDPID;
173 hashCode = prime * hashCode + (int)switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800174 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
175 return hashCode;
176 }
177
178 @Override
179 public boolean equals(Object obj) {
180 if (this == obj) return true;
181 if (obj == null) return false;
182 if (getClass() != obj.getClass()) return false;
TeruU80ce5062014-03-03 17:16:13 -0800183 OnosDevice other = (OnosDevice) obj;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800184 if (hashCode() != other.hashCode()) return false;
185 if (ipv4Address == null) {
186 if (other.ipv4Address != null) return false;
TeruU80ce5062014-03-03 17:16:13 -0800187 } else if (!ipv4Address.equals(other.ipv4Address)) return false;
188 if (macAddress == null) {
189 if (other.macAddress != null) return false;
190 } else if (!macAddress.equals(other.macAddress)) return false;
191 if(switchDPID != other.switchDPID) return false;
192 if (switchPort != other.switchPort) return false;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800193 if (vlan == null) {
194 if (other.vlan != null) return false;
195 } else if (!vlan.equals(other.vlan)) return false;
196 return true;
197 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800198
199 @Override
200 public String toString() {
201 StringBuilder builder = new StringBuilder();
202 builder.append("Entity [macAddress=");
203 builder.append(macAddress.toString());
204 builder.append(", ipv4Address=");
205 builder.append(IPv4.fromIPv4Address(ipv4Address==null ?
206 0 : ipv4Address.intValue()));
207 builder.append(", vlan=");
208 builder.append(vlan);
209 builder.append(", switchDPID=");
210 builder.append(switchDPID);
211 builder.append(", switchPort=");
212 builder.append(switchPort);
213 builder.append(", lastSeenTimestamp=");
214 builder.append(lastSeenTimestamp == null? "null" : lastSeenTimestamp.getTime());
215 builder.append(", activeSince=");
216 builder.append(activeSince == null? "null" : activeSince.getTime());
217 builder.append("]");
218 return builder.toString();
219 }
220
221 /*
222 @Override
223 public int compareTo(OnosDevice o) {
224 if (macAddress < o.macAddress) return -1;
225 if (macAddress > o.macAddress) return 1;
226
227 int r;
228 if (switchDPID == null)
229 r = o.switchDPID == null ? 0 : -1;
230 else if (o.switchDPID == null)
231 r = 1;
232 else
233 r = switchDPID.compareTo(o.switchDPID);
234 if (r != 0) return r;
235
236 if (switchPort == null)
237 r = o.switchPort == null ? 0 : -1;
238 else if (o.switchPort == null)
239 r = 1;
240 else
241 r = switchPort.compareTo(o.switchPort);
242 if (r != 0) return r;
243
244 if (ipv4Address == null)
245 r = o.ipv4Address == null ? 0 : -1;
246 else if (o.ipv4Address == null)
247 r = 1;
248 else
249 r = ipv4Address.compareTo(o.ipv4Address);
250 if (r != 0) return r;
251
252 if (vlan == null)
253 r = o.vlan == null ? 0 : -1;
254 else if (o.vlan == null)
255 r = 1;
256 else
257 r = vlan.compareTo(o.vlan);
258 if (r != 0) return r;
259
260 return 0;
261 }*/
262
263}