blob: 34d2c38079c72a4555308bb320a109a7686dbcd7 [file] [log] [blame]
Jonathan Hartd857ad62013-12-14 18:08:17 -08001/**
2* 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**/
17
18package net.onrc.onos.ofcontroller.devicemanager;
19
20import java.util.Date;
21
22import net.floodlightcontroller.devicemanager.internal.Entity;
23import net.floodlightcontroller.packet.IPv4;
24import net.floodlightcontroller.util.MACAddress;
25
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 */
40public class OnosDevice { //implements Comparable<OnosDevice> {
41 /**
42 * Timeout for computing {@link Entity#activeSince}.
43 * @see {@link Entity#activeSince}
44 */
45 private static int ACTIVITY_TIMEOUT = 30000;
46
47 /**
48 * The MAC address associated with this entity
49 */
50 private MACAddress macAddress;
51
52 /**
53 * The IP address associated with this entity, or null if no IP learned
54 * from the network observation associated with this entity
55 */
56 private Integer ipv4Address;
57
58 /**
59 * The VLAN tag on this entity, or null if untagged
60 */
61 private Short vlan;
62
63 /**
64 * The DPID of the switch for the ingress point for this entity,
65 * or null if not present
66 */
67 private long switchDPID;
68
69 /**
70 * The port number of the switch for the ingress point for this entity,
71 * or null if not present
72 */
73 private short switchPort;
74
75 /**
76 * The last time we observed this entity on the network
77 */
78 private Date lastSeenTimestamp;
79
80 /**
81 * The time between {@link Entity#activeSince} and
82 * {@link Entity#lastSeenTimestamp} is a period of activity for this
83 * entity where it was observed repeatedly. If, when the entity is
84 * observed, the is longer ago than the activity timeout,
85 * {@link Entity#lastSeenTimestamp} and {@link Entity#activeSince} will
86 * be set to the current time.
87 */
88 private Date activeSince;
89
90 private int hashCode = 0;
91
92 // ************
93 // Constructors
94 // ************
95
96 /**
97 * Create a new entity
98 *
99 * @param macAddress
100 * @param vlan
101 * @param ipv4Address
102 * @param switchDPID
103 * @param switchPort
104 * @param lastSeenTimestamp
105 */
106 public OnosDevice(MACAddress macAddress, Short vlan,
107 Integer ipv4Address, Long switchDPID, short switchPort,
108 Date lastSeenTimestamp) {
109 this.macAddress = macAddress;
110 this.ipv4Address = ipv4Address;
111 this.vlan = vlan;
112 this.switchDPID = switchDPID;
113 this.switchPort = switchPort;
114 this.lastSeenTimestamp = lastSeenTimestamp;
115 this.activeSince = lastSeenTimestamp;
116 }
117
118 // ***************
119 // Getters/Setters
120 // ***************
121
122 public MACAddress getMacAddress() {
123 return macAddress;
124 }
125
126 public Integer getIpv4Address() {
127 return ipv4Address;
128 }
129
130 public Short getVlan() {
131 return vlan;
132 }
133
134 public Long getSwitchDPID() {
135 return switchDPID;
136 }
137
138 public short getSwitchPort() {
139 return switchPort;
140 }
141
142 public Date getLastSeenTimestamp() {
143 return lastSeenTimestamp;
144 }
145
146 /**
147 * Set the last seen timestamp and also update {@link Entity#activeSince}
148 * if appropriate
149 * @param lastSeenTimestamp the new last seen timestamp
150 * @see {@link Entity#activeSince}
151 */
152 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
153 if (activeSince == null ||
154 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
155 lastSeenTimestamp.getTime())
156 this.activeSince = lastSeenTimestamp;
157 this.lastSeenTimestamp = lastSeenTimestamp;
158 }
159
160 public Date getActiveSince() {
161 return activeSince;
162 }
163
164 public void setActiveSince(Date activeSince) {
165 this.activeSince = activeSince;
166 }
167
168 /*
169 @Override
170 public int hashCode() {
171 if (hashCode != 0) return hashCode;
172 final int prime = 31;
173 hashCode = 1;
174 hashCode = prime * hashCode
175 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
176 //hashCode = prime * hashCode + (int) (macAddress ^ (macAddress >>> 32));
177 hashCode = prime * macAddress.hashCode();
178 hashCode = prime * hashCode + (int) (switchDPID ^ (switchDPID >>> 32));
179 hashCode = prime * hashCode + switchPort;
180 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
181 return hashCode;
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (this == obj) return true;
187 if (obj == null) return false;
188 if (getClass() != obj.getClass()) return false;
189 Entity other = (Entity) obj;
190 if (hashCode() != other.hashCode()) return false;
191 if (ipv4Address == null) {
192 if (other.ipv4Address != null) return false;
193 } else if (!ipv4Address.equals(other.ipv4Address)) return false;
194 if (macAddress != other.macAddress) return false;
195 if (switchDPID == null) {
196 if (other.switchDPID != null) return false;
197 } else if (!switchDPID.equals(other.switchDPID)) return false;
198 if (switchPort == null) {
199 if (other.switchPort != null) return false;
200 } else if (!switchPort.equals(other.switchPort)) return false;
201 if (vlan == null) {
202 if (other.vlan != null) return false;
203 } else if (!vlan.equals(other.vlan)) return false;
204 return true;
205 }
206 */
207
208
209
210 @Override
211 public String toString() {
212 StringBuilder builder = new StringBuilder();
213 builder.append("Entity [macAddress=");
214 builder.append(macAddress.toString());
215 builder.append(", ipv4Address=");
216 builder.append(IPv4.fromIPv4Address(ipv4Address==null ?
217 0 : ipv4Address.intValue()));
218 builder.append(", vlan=");
219 builder.append(vlan);
220 builder.append(", switchDPID=");
221 builder.append(switchDPID);
222 builder.append(", switchPort=");
223 builder.append(switchPort);
224 builder.append(", lastSeenTimestamp=");
225 builder.append(lastSeenTimestamp == null? "null" : lastSeenTimestamp.getTime());
226 builder.append(", activeSince=");
227 builder.append(activeSince == null? "null" : activeSince.getTime());
228 builder.append("]");
229 return builder.toString();
230 }
231
232 /*
233 @Override
234 public int compareTo(OnosDevice o) {
235 if (macAddress < o.macAddress) return -1;
236 if (macAddress > o.macAddress) return 1;
237
238 int r;
239 if (switchDPID == null)
240 r = o.switchDPID == null ? 0 : -1;
241 else if (o.switchDPID == null)
242 r = 1;
243 else
244 r = switchDPID.compareTo(o.switchDPID);
245 if (r != 0) return r;
246
247 if (switchPort == null)
248 r = o.switchPort == null ? 0 : -1;
249 else if (o.switchPort == null)
250 r = 1;
251 else
252 r = switchPort.compareTo(o.switchPort);
253 if (r != 0) return r;
254
255 if (ipv4Address == null)
256 r = o.ipv4Address == null ? 0 : -1;
257 else if (o.ipv4Address == null)
258 r = 1;
259 else
260 r = ipv4Address.compareTo(o.ipv4Address);
261 if (r != 0) return r;
262
263 if (vlan == null)
264 r = o.vlan == null ? 0 : -1;
265 else if (o.vlan == null)
266 r = 1;
267 else
268 r = vlan.compareTo(o.vlan);
269 if (r != 0) return r;
270
271 return 0;
272 }*/
273
274}