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.net.InetAddress;
37 import java.util.Date;
39 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.utils.NetUtils;
44 * An entity on the network is a visible trace of a device that corresponds to a
45 * packet received from a particular interface on the edge of a network, with a
46 * particular VLAN tag, and a particular MAC address, along with any other
47 * packet characteristics we might want to consider as helpful for
48 * disambiguating devices.
50 * Entities are the most basic element of devices; devices consist of one or
51 * more entities. Entities are immutable once created, except for the last seen
57 public class Entity implements Comparable<Entity> {
59 * Timeout for computing {@link Entity#activeSince}.
61 * @see {@link Entity#activeSince}
63 protected static int ACTIVITY_TIMEOUT = 30000;
66 * The MAC address associated with this entity
68 protected long macAddress;
71 * The IP address associated with this entity, or null if no IP learned from
72 * the network observation associated with this entity
74 protected Integer ipv4Address;
77 * The VLAN tag on this entity, or null if untagged
82 * The attachment point for this entity
87 * The last time we observed this entity on the network
89 protected Date lastSeenTimestamp;
92 * The time between {@link Entity#activeSince} and
93 * {@link Entity#lastSeenTimestamp} is a period of activity for this entity
94 * where it was observed repeatedly. If, when the entity is observed, the is
95 * longer ago than the activity timeout, {@link Entity#lastSeenTimestamp}
96 * and {@link Entity#activeSince} will be set to the current time.
98 protected Date activeSince;
100 private int hashCode = 0;
107 * Create a new entity
114 * @param lastSeenTimestamp
116 public Entity(long macAddress, Short vlan, Integer ipv4Address,
117 NodeConnector port, Date lastSeenTimestamp) {
118 this.macAddress = macAddress;
119 this.ipv4Address = ipv4Address;
122 this.lastSeenTimestamp = lastSeenTimestamp;
123 this.activeSince = lastSeenTimestamp;
130 // @JsonSerialize(using=MACSerializer.class)
131 public long getMacAddress() {
135 // @JsonSerialize(using=IPv4Serializer.class)
136 public Integer getIpv4Address() {
140 public Short getVlan() {
144 public NodeConnector getPort() {
149 public boolean hasSwitchPort() {
153 public Date getLastSeenTimestamp() {
154 return lastSeenTimestamp;
158 * Set the last seen timestamp and also update {@link Entity#activeSince} if
161 * @param lastSeenTimestamp
162 * the new last seen timestamp
163 * @see {@link Entity#activeSince}
165 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
166 if (activeSince == null
167 || (activeSince.getTime() + ACTIVITY_TIMEOUT) < lastSeenTimestamp
169 this.activeSince = lastSeenTimestamp;
171 this.lastSeenTimestamp = lastSeenTimestamp;
174 public Date getActiveSince() {
178 public void setActiveSince(Date activeSince) {
179 this.activeSince = activeSince;
183 public int hashCode() {
187 final int prime = 31;
189 hashCode = prime * hashCode
190 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
191 hashCode = prime * hashCode + (int) (macAddress ^ (macAddress >>> 32));
192 hashCode = prime * hashCode + ((port == null) ? 0 : port.hashCode());
193 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
198 public boolean equals(Object obj) {
205 if (getClass() != obj.getClass()) {
208 Entity other = (Entity) obj;
209 if (ipv4Address == null) {
210 if (other.ipv4Address != null) {
213 } else if (!ipv4Address.equals(other.ipv4Address)) {
216 if (macAddress != other.macAddress) {
220 if (other.port != null) {
223 } else if (!port.equals(other.port)) {
227 if (other.vlan != null) {
230 } else if (!vlan.equals(other.vlan)) {
236 public HostNodeConnector toHostNodeConnector() {
238 NodeConnector n = this.getPort();
239 InetAddress ip = InetAddress.getByAddress(NetUtils.intToByteArray4(this.getIpv4Address()));
240 byte[] macAddr = NetUtils.longToByteArray6(this.getMacAddress());
241 HostNodeConnector nc = new HostNodeConnector(macAddr, ip, n,
244 } catch (Exception e) {
250 public String toString() {
251 return "Entity [macAddress=" + macAddress + ", ipv4Address="
252 + ipv4Address + ", vlan=" + vlan + ", port=" + port + "]";
255 @SuppressWarnings({ "rawtypes", "unchecked" })
257 public int compareTo(Entity o) {
260 r = o.port == null ? 0 : -1;
262 else if (o.port == null) {
266 // XXX - the node id is only defined as an object rather
267 // than something useful. We're just going to have to
268 // blindly cast to Comparable and hope it works.
269 Comparable switchId = (Comparable) port.getNode().getID();
270 Comparable oswitchId = (Comparable) o.port.getNode().getID();
271 r = switchId.compareTo(oswitchId);
276 Comparable portId = (Comparable) port.getID();
277 Comparable oportId = (Comparable) o.port.getID();
278 r = portId.compareTo(oportId);