blob: 45452e04b5b34ce07a307a132a005f6aefef3cef [file] [log] [blame]
Jonathan Hartd857ad62013-12-14 18:08:17 -08001/**
Jonathan Hartac15d932014-06-01 22:59:35 -07002 * Copyright 2011,2012, Big Switch Networks, Inc.
Ray Milkey269ffb92014-04-03 14:43:30 -07003 * 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 **/
Jonathan Hartd857ad62013-12-14 18:08:17 -080017
Jonathan Hart03102132014-07-01 23:22:04 -070018package net.onrc.onos.core.hostmanager;
Jonathan Hartd857ad62013-12-14 18:08:17 -080019
TeruU80ce5062014-03-03 17:16:13 -080020import java.io.Serializable;
Jonathan Hartd857ad62013-12-14 18:08:17 -080021import java.util.Date;
TeruU5d2c9392014-06-09 20:02:02 -070022import java.util.Objects;
Jonathan Hartd857ad62013-12-14 18:08:17 -080023
Jonathan Hartd857ad62013-12-14 18:08:17 -080024import net.floodlightcontroller.util.MACAddress;
25
TeruU5d2c9392014-06-09 20:02:02 -070026
27
Jonathan Hartd857ad62013-12-14 18:08:17 -080028/**
29 * An entity on the network is a visible trace of a device that corresponds
30 * to a packet received from a particular interface on the edge of a network,
31 * with a particular VLAN tag, and a particular MAC address, along with any
32 * other packet characteristics we might want to consider as helpful for
33 * disambiguating devices.
Ray Milkey269ffb92014-04-03 14:43:30 -070034 * <p/>
Jonathan Hartd857ad62013-12-14 18:08:17 -080035 * Entities are the most basic element of devices; devices consist of one or
36 * more entities. Entities are immutable once created, except for the last
37 * seen timestamp.
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070038 * <!-- CHECKSTYLE IGNORE WriteTag FOR NEXT 1 LINES -->
Ray Milkey269ffb92014-04-03 14:43:30 -070039 * @author readams
Jonathan Hartd857ad62013-12-14 18:08:17 -080040 */
Jonathan Hart03102132014-07-01 23:22:04 -070041public class Host implements Serializable {
Jonathan Hart96892d12014-03-26 20:21:29 -070042
Jonathan Hartac15d932014-06-01 22:59:35 -070043 private static final long serialVersionUID = 1L;
Ray Milkey269ffb92014-04-03 14:43:30 -070044
Jonathan Hartd857ad62013-12-14 18:08:17 -080045 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070046 * The MAC address associated with this entity.
Jonathan Hartd857ad62013-12-14 18:08:17 -080047 */
48 private MACAddress macAddress;
Ray Milkey269ffb92014-04-03 14:43:30 -070049
Jonathan Hartd857ad62013-12-14 18:08:17 -080050 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070051 * The VLAN tag on this entity, or null if untagged.
Jonathan Hartd857ad62013-12-14 18:08:17 -080052 */
53 private Short vlan;
Ray Milkey269ffb92014-04-03 14:43:30 -070054
Jonathan Hartd857ad62013-12-14 18:08:17 -080055 /**
56 * The DPID of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070057 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080058 */
TeruU5d2c9392014-06-09 20:02:02 -070059 private Long switchDPID;
Ray Milkey269ffb92014-04-03 14:43:30 -070060
Jonathan Hartd857ad62013-12-14 18:08:17 -080061 /**
62 * The port number of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070063 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080064 */
TeruU5d2c9392014-06-09 20:02:02 -070065 private Long switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070066
Jonathan Hartd857ad62013-12-14 18:08:17 -080067 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070068 * The last time we observed this entity on the network.
Jonathan Hartd857ad62013-12-14 18:08:17 -080069 */
70 private Date lastSeenTimestamp;
71
Jonathan Hartd857ad62013-12-14 18:08:17 -080072 private int hashCode = 0;
73
74 // ************
75 // Constructors
76 // ************
Jonathan Hart03102132014-07-01 23:22:04 -070077 protected Host() {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
79
Jonathan Hartd857ad62013-12-14 18:08:17 -080080 /**
Jonathan Hart03102132014-07-01 23:22:04 -070081 * Create a new host and its information.
Ray Milkey269ffb92014-04-03 14:43:30 -070082 *
Jonathan Hart03102132014-07-01 23:22:04 -070083 * @param macAddress mac address of this host
84 * @param vlan vlan ID of this host
85 * @param switchDPID switch DPID where the host is attached
86 * @param switchPort port number where the host is attached
87 * @param lastSeenTimestamp last packet-in time of this host
Jonathan Hartd857ad62013-12-14 18:08:17 -080088 */
Jonathan Hart03102132014-07-01 23:22:04 -070089 public Host(MACAddress macAddress, Short vlan, Long switchDPID,
TeruU5d2c9392014-06-09 20:02:02 -070090 Long switchPort, Date lastSeenTimestamp) {
Jonathan Hartd857ad62013-12-14 18:08:17 -080091 this.macAddress = macAddress;
Jonathan Hartd857ad62013-12-14 18:08:17 -080092 this.vlan = vlan;
93 this.switchDPID = switchDPID;
94 this.switchPort = switchPort;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070095 if (lastSeenTimestamp != null) {
96 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070097 } else {
98 this.lastSeenTimestamp = null;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070099 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800100 }
101
102 // ***************
103 // Getters/Setters
104 // ***************
105
106 public MACAddress getMacAddress() {
107 return macAddress;
108 }
109
Jonathan Hartd857ad62013-12-14 18:08:17 -0800110 public Short getVlan() {
111 return vlan;
112 }
113
114 public Long getSwitchDPID() {
115 return switchDPID;
116 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700117
TeruU5d2c9392014-06-09 20:02:02 -0700118 public Long getSwitchPort() {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800119 return switchPort;
120 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700121
Jonathan Hartd857ad62013-12-14 18:08:17 -0800122 public Date getLastSeenTimestamp() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700123 if (this.lastSeenTimestamp == null) {
124 return null;
125 }
126 return new Date(this.lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800127 }
128
Jonathan Hartd857ad62013-12-14 18:08:17 -0800129 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700130 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800131 }
132
Jonathan Hartd857ad62013-12-14 18:08:17 -0800133 @Override
134 public int hashCode() {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800135 final int prime = 31;
136 hashCode = 1;
TeruU80ce5062014-03-03 17:16:13 -0800137 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
TeruU5d2c9392014-06-09 20:02:02 -0700138 hashCode = prime * hashCode + ((switchDPID == null) ? 0 : switchDPID.hashCode());
139 hashCode = prime * hashCode + ((switchPort == null) ? 0 : switchPort.hashCode());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800140 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
141 return hashCode;
142 }
143
144 @Override
145 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700146 if (this == obj) {
147 return true;
148 }
149 if (obj == null) {
150 return false;
151 }
152 if (getClass() != obj.getClass()) {
153 return false;
154 }
Jonathan Hart03102132014-07-01 23:22:04 -0700155 Host other = (Host) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700156 if (hashCode() != other.hashCode()) {
157 return false;
158 }
TeruU5d2c9392014-06-09 20:02:02 -0700159 if (!Objects.equals(macAddress, other.macAddress)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700160 return false;
161 }
TeruU5d2c9392014-06-09 20:02:02 -0700162 if (!Objects.equals(switchDPID, other.switchDPID)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700163 return false;
164 }
TeruU5d2c9392014-06-09 20:02:02 -0700165 if (!Objects.equals(switchPort, other.switchPort)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700166 return false;
167 }
TeruU5d2c9392014-06-09 20:02:02 -0700168 if (!Objects.equals(vlan, other.vlan)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 return false;
170 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800171 return true;
172 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700173
Jonathan Hartd857ad62013-12-14 18:08:17 -0800174 @Override
175 public String toString() {
176 StringBuilder builder = new StringBuilder();
Jonathan Hart03102132014-07-01 23:22:04 -0700177 builder.append("Host [macAddress=");
Jonathan Hartd857ad62013-12-14 18:08:17 -0800178 builder.append(macAddress.toString());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800179 builder.append(", vlan=");
180 builder.append(vlan);
181 builder.append(", switchDPID=");
182 builder.append(switchDPID);
183 builder.append(", switchPort=");
184 builder.append(switchPort);
185 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800187 builder.append("]");
188 return builder.toString();
189 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800190}