blob: 6ba350144fe172caa9f71ba17bf29a2a5a724dae [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 /**
Sangho Shin2f263692014-09-15 14:09:41 -070051 * The IP address associated with this entity.
52 */
53 private int ipAddress;
54
55
56 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070057 * The VLAN tag on this entity, or null if untagged.
Jonathan Hartd857ad62013-12-14 18:08:17 -080058 */
59 private Short vlan;
Ray Milkey269ffb92014-04-03 14:43:30 -070060
Jonathan Hartd857ad62013-12-14 18:08:17 -080061 /**
62 * The DPID 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 switchDPID;
Ray Milkey269ffb92014-04-03 14:43:30 -070066
Jonathan Hartd857ad62013-12-14 18:08:17 -080067 /**
68 * The port number of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070069 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080070 */
TeruU5d2c9392014-06-09 20:02:02 -070071 private Long switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070072
Jonathan Hartd857ad62013-12-14 18:08:17 -080073 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070074 * The last time we observed this entity on the network.
Jonathan Hartd857ad62013-12-14 18:08:17 -080075 */
76 private Date lastSeenTimestamp;
77
Jonathan Hartd857ad62013-12-14 18:08:17 -080078 private int hashCode = 0;
79
80 // ************
81 // Constructors
82 // ************
Jonathan Hart03102132014-07-01 23:22:04 -070083 protected Host() {
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
85
Jonathan Hartd857ad62013-12-14 18:08:17 -080086 /**
Jonathan Hart03102132014-07-01 23:22:04 -070087 * Create a new host and its information.
Ray Milkey269ffb92014-04-03 14:43:30 -070088 *
Jonathan Hart03102132014-07-01 23:22:04 -070089 * @param macAddress mac address of this host
Sangho Shin2f263692014-09-15 14:09:41 -070090 * @param ipAddress ip address of this host (if any)
Jonathan Hart03102132014-07-01 23:22:04 -070091 * @param vlan vlan ID of this host
92 * @param switchDPID switch DPID where the host is attached
93 * @param switchPort port number where the host is attached
94 * @param lastSeenTimestamp last packet-in time of this host
Jonathan Hartd857ad62013-12-14 18:08:17 -080095 */
Sangho Shin2f263692014-09-15 14:09:41 -070096 public Host(MACAddress macAddress, int ipAddress, Short vlan, Long switchDPID,
TeruU5d2c9392014-06-09 20:02:02 -070097 Long switchPort, Date lastSeenTimestamp) {
Jonathan Hartd857ad62013-12-14 18:08:17 -080098 this.macAddress = macAddress;
Sangho Shin2f263692014-09-15 14:09:41 -070099 this.ipAddress = ipAddress;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800100 this.vlan = vlan;
101 this.switchDPID = switchDPID;
102 this.switchPort = switchPort;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700103 if (lastSeenTimestamp != null) {
104 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700105 } else {
106 this.lastSeenTimestamp = null;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700107 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800108 }
109
110 // ***************
111 // Getters/Setters
112 // ***************
113
114 public MACAddress getMacAddress() {
115 return macAddress;
116 }
117
Sangho Shin2f263692014-09-15 14:09:41 -0700118 public int getIpAddress() {
119 return ipAddress;
120 }
121
Jonathan Hartd857ad62013-12-14 18:08:17 -0800122 public Short getVlan() {
123 return vlan;
124 }
125
126 public Long getSwitchDPID() {
127 return switchDPID;
128 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700129
TeruU5d2c9392014-06-09 20:02:02 -0700130 public Long getSwitchPort() {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800131 return switchPort;
132 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700133
Jonathan Hartd857ad62013-12-14 18:08:17 -0800134 public Date getLastSeenTimestamp() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700135 if (this.lastSeenTimestamp == null) {
136 return null;
137 }
138 return new Date(this.lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800139 }
140
Jonathan Hartd857ad62013-12-14 18:08:17 -0800141 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700142 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800143 }
144
Jonathan Hartd857ad62013-12-14 18:08:17 -0800145 @Override
146 public int hashCode() {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800147 final int prime = 31;
148 hashCode = 1;
TeruU80ce5062014-03-03 17:16:13 -0800149 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
Sangho Shin2f263692014-09-15 14:09:41 -0700150 hashCode = prime * hashCode + ipAddress;
TeruU5d2c9392014-06-09 20:02:02 -0700151 hashCode = prime * hashCode + ((switchDPID == null) ? 0 : switchDPID.hashCode());
152 hashCode = prime * hashCode + ((switchPort == null) ? 0 : switchPort.hashCode());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800153 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
154 return hashCode;
155 }
156
157 @Override
158 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 if (this == obj) {
160 return true;
161 }
162 if (obj == null) {
163 return false;
164 }
165 if (getClass() != obj.getClass()) {
166 return false;
167 }
Jonathan Hart03102132014-07-01 23:22:04 -0700168 Host other = (Host) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 if (hashCode() != other.hashCode()) {
170 return false;
171 }
TeruU5d2c9392014-06-09 20:02:02 -0700172 if (!Objects.equals(macAddress, other.macAddress)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700173 return false;
174 }
Sangho Shin2f263692014-09-15 14:09:41 -0700175 if (!Objects.equals(ipAddress, other.ipAddress)) {
176 return false;
177 }
TeruU5d2c9392014-06-09 20:02:02 -0700178 if (!Objects.equals(switchDPID, other.switchDPID)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700179 return false;
180 }
TeruU5d2c9392014-06-09 20:02:02 -0700181 if (!Objects.equals(switchPort, other.switchPort)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 return false;
183 }
TeruU5d2c9392014-06-09 20:02:02 -0700184 if (!Objects.equals(vlan, other.vlan)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700185 return false;
186 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800187 return true;
188 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700189
Jonathan Hartd857ad62013-12-14 18:08:17 -0800190 @Override
191 public String toString() {
192 StringBuilder builder = new StringBuilder();
Jonathan Hart03102132014-07-01 23:22:04 -0700193 builder.append("Host [macAddress=");
Jonathan Hartd857ad62013-12-14 18:08:17 -0800194 builder.append(macAddress.toString());
Saurav Dasf7c057c2014-10-13 16:17:10 -0700195 builder.append(", ipAddress=");
Sangho Shin2f263692014-09-15 14:09:41 -0700196 builder.append(ipAddress);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800197 builder.append(", vlan=");
198 builder.append(vlan);
199 builder.append(", switchDPID=");
200 builder.append(switchDPID);
201 builder.append(", switchPort=");
202 builder.append(switchPort);
203 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800205 builder.append("]");
206 return builder.toString();
207 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800208}