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