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