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