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