2 * Copyright (c) 2011,2012 Big Switch Networks, Inc.
4 * Licensed under the Eclipse Public License, Version 1.0 (the
5 * "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
8 * http://www.eclipse.org/legal/epl-v10.html
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 * This file incorporates work covered by the following copyright and
19 * Originally created by David Erickson, Stanford University
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the
23 * License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing,
28 * software distributed under the License is distributed on an "AS
29 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30 * express or implied. See the License for the specific language
31 * governing permissions and limitations under the License.
34 package org.opendaylight.controller.hosttracker;
36 import java.util.Date;
38 import org.opendaylight.controller.sal.core.NodeConnector;
41 * An entity on the network is a visible trace of a device that corresponds to a
42 * packet received from a particular interface on the edge of a network, with a
43 * particular VLAN tag, and a particular MAC address, along with any other
44 * packet characteristics we might want to consider as helpful for
45 * disambiguating devices.
47 * Entities are the most basic element of devices; devices consist of one or
48 * more entities. Entities are immutable once created, except for the last seen
54 public class Entity implements Comparable<Entity> {
56 * Timeout for computing {@link Entity#activeSince}.
58 * @see {@link Entity#activeSince}
60 protected static int ACTIVITY_TIMEOUT = 30000;
63 * The MAC address associated with this entity
65 protected long macAddress;
68 * The IP address associated with this entity, or null if no IP learned from
69 * the network observation associated with this entity
71 protected Integer ipv4Address;
74 * The VLAN tag on this entity, or null if untagged
79 * The attachment point for this entity
84 * The last time we observed this entity on the network
86 protected Date lastSeenTimestamp;
89 * The time between {@link Entity#activeSince} and
90 * {@link Entity#lastSeenTimestamp} is a period of activity for this entity
91 * where it was observed repeatedly. If, when the entity is observed, the is
92 * longer ago than the activity timeout, {@link Entity#lastSeenTimestamp}
93 * and {@link Entity#activeSince} will be set to the current time.
95 protected Date activeSince;
97 private int hashCode = 0;
104 * Create a new entity
111 * @param lastSeenTimestamp
113 public Entity(long macAddress, Short vlan, Integer ipv4Address,
114 NodeConnector port, Date lastSeenTimestamp) {
115 this.macAddress = macAddress;
116 this.ipv4Address = ipv4Address;
119 this.lastSeenTimestamp = lastSeenTimestamp;
120 this.activeSince = lastSeenTimestamp;
127 // @JsonSerialize(using=MACSerializer.class)
128 public long getMacAddress() {
132 // @JsonSerialize(using=IPv4Serializer.class)
133 public Integer getIpv4Address() {
137 public Short getVlan() {
141 public NodeConnector getPort() {
146 public boolean hasSwitchPort() {
150 public Date getLastSeenTimestamp() {
151 return lastSeenTimestamp;
155 * Set the last seen timestamp and also update {@link Entity#activeSince} if
158 * @param lastSeenTimestamp
159 * the new last seen timestamp
160 * @see {@link Entity#activeSince}
162 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
163 if (activeSince == null
164 || (activeSince.getTime() + ACTIVITY_TIMEOUT) < lastSeenTimestamp
166 this.activeSince = lastSeenTimestamp;
167 this.lastSeenTimestamp = lastSeenTimestamp;
170 public Date getActiveSince() {
174 public void setActiveSince(Date activeSince) {
175 this.activeSince = activeSince;
179 public int hashCode() {
182 final int prime = 31;
184 hashCode = prime * hashCode
185 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
186 hashCode = prime * hashCode + (int) (macAddress ^ (macAddress >>> 32));
187 hashCode = prime * hashCode + ((port == null) ? 0 : port.hashCode());
188 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
193 public boolean equals(Object obj) {
198 if (getClass() != obj.getClass())
200 Entity other = (Entity) obj;
201 if (ipv4Address == null) {
202 if (other.ipv4Address != null)
204 } else if (!ipv4Address.equals(other.ipv4Address))
206 if (macAddress != other.macAddress)
209 if (other.port != null)
211 } else if (!port.equals(other.port))
214 if (other.vlan != null)
216 } else if (!vlan.equals(other.vlan))
222 public String toString() {
223 return "Entity [macAddress=" + macAddress + ", ipv4Address="
224 + ipv4Address + ", vlan=" + vlan + ", port=" + port + "]";
227 @SuppressWarnings({ "rawtypes", "unchecked" })
229 public int compareTo(Entity o) {
232 r = o.port == null ? 0 : -1;
233 else if (o.port == null)
236 // XXX - the node id is only defined as an object rather
237 // than something useful. We're just going to have to
238 // blindly cast to Comparable and hope it works.
239 Comparable switchId = (Comparable) port.getNode().getID();
240 Comparable oswitchId = (Comparable) o.port.getNode().getID();
241 r = switchId.compareTo(oswitchId);
245 Comparable portId = (Comparable) port.getID();
246 Comparable oportId = (Comparable) o.port.getID();
247 r = portId.compareTo(oportId);