blob: adac7871af6e455216034e29ba4f7bcc3be67d65 [file] [log] [blame]
Jonathan Hartd857ad62013-12-14 18:08:17 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 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 Hartd857ad62013-12-14 18:08:17 -080017
Jonathan Hart23701d12014-04-03 10:45:48 -070018package net.onrc.onos.core.devicemanager;
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;
22
Jonathan Hartd857ad62013-12-14 18:08:17 -080023import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070024import net.onrc.onos.core.packet.IPv4;
Jonathan Hartd857ad62013-12-14 18:08:17 -080025
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 Milkey269ffb92014-04-03 14:43:30 -070032 * <p/>
Jonathan Hartd857ad62013-12-14 18:08:17 -080033 * 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 Hartd857ad62013-12-14 18:08:17 -080036 *
Ray Milkey269ffb92014-04-03 14:43:30 -070037 * @author readams
Jonathan Hartd857ad62013-12-14 18:08:17 -080038 */
TeruU80ce5062014-03-03 17:16:13 -080039public class OnosDevice implements Serializable { //implements Comparable<OnosDevice> {
Jonathan Hart96892d12014-03-26 20:21:29 -070040
Jonathan Hartd857ad62013-12-14 18:08:17 -080041 private static int ACTIVITY_TIMEOUT = 30000;
Ray Milkey269ffb92014-04-03 14:43:30 -070042
Jonathan Hartd857ad62013-12-14 18:08:17 -080043 /**
44 * The MAC address associated with this entity
45 */
46 private MACAddress macAddress;
Ray Milkey269ffb92014-04-03 14:43:30 -070047
Jonathan Hartd857ad62013-12-14 18:08:17 -080048 /**
49 * The IP address associated with this entity, or null if no IP learned
50 * from the network observation associated with this entity
51 */
52 private Integer ipv4Address;
Ray Milkey269ffb92014-04-03 14:43:30 -070053
Jonathan Hartd857ad62013-12-14 18:08:17 -080054 /**
55 * The VLAN tag on this entity, or null if untagged
56 */
57 private Short vlan;
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Jonathan Hartd857ad62013-12-14 18:08:17 -080059 /**
60 * The DPID of the switch for the ingress point for this entity,
61 * or null if not present
62 */
63 private long switchDPID;
Ray Milkey269ffb92014-04-03 14:43:30 -070064
Jonathan Hartd857ad62013-12-14 18:08:17 -080065 /**
66 * The port number of the switch for the ingress point for this entity,
67 * or null if not present
68 */
69 private short switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070070
Jonathan Hartd857ad62013-12-14 18:08:17 -080071 /**
72 * The last time we observed this entity on the network
73 */
74 private Date lastSeenTimestamp;
75
Jonathan Hartd857ad62013-12-14 18:08:17 -080076 private Date activeSince;
Ray Milkey269ffb92014-04-03 14:43:30 -070077
Jonathan Hartd857ad62013-12-14 18:08:17 -080078 private int hashCode = 0;
79
80 // ************
81 // Constructors
82 // ************
Ray Milkey269ffb92014-04-03 14:43:30 -070083 protected OnosDevice() {
84 }
85
Jonathan Hartd857ad62013-12-14 18:08:17 -080086 /**
87 * Create a new entity
Ray Milkey269ffb92014-04-03 14:43:30 -070088 *
Jonathan Hartd857ad62013-12-14 18:08:17 -080089 * @param macAddress
90 * @param vlan
91 * @param ipv4Address
92 * @param switchDPID
93 * @param switchPort
94 * @param lastSeenTimestamp
95 */
Ray Milkey269ffb92014-04-03 14:43:30 -070096 public OnosDevice(MACAddress macAddress, Short vlan,
97 Integer ipv4Address, Long switchDPID, short switchPort,
98 Date lastSeenTimestamp) {
Jonathan Hartd857ad62013-12-14 18:08:17 -080099 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 Milkey269ffb92014-04-03 14:43:30 -0700119
TeruU80ce5062014-03-03 17:16:13 -0800120 public void setIpv4Address(Integer ipv4Address) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 this.ipv4Address = ipv4Address;
TeruU80ce5062014-03-03 17:16:13 -0800122 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800123
124 public Short getVlan() {
125 return vlan;
126 }
127
128 public Long getSwitchDPID() {
129 return switchDPID;
130 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700131
TeruU80ce5062014-03-03 17:16:13 -0800132 public void setSwitchDPID(long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 this.switchDPID = dpid;
TeruU80ce5062014-03-03 17:16:13 -0800134 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800135
136 public short getSwitchPort() {
137 return switchPort;
138 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700139
TeruU80ce5062014-03-03 17:16:13 -0800140 public void setSwitchPort(short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 this.switchPort = port;
TeruU80ce5062014-03-03 17:16:13 -0800142 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800143
144 public Date getLastSeenTimestamp() {
145 return lastSeenTimestamp;
146 }
147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148
Jonathan Hartd857ad62013-12-14 18:08:17 -0800149 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
150 if (activeSince == null ||
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 (activeSince.getTime() + ACTIVITY_TIMEOUT) <
152 lastSeenTimestamp.getTime())
Jonathan Hartd857ad62013-12-14 18:08:17 -0800153 this.activeSince = lastSeenTimestamp;
154 this.lastSeenTimestamp = lastSeenTimestamp;
155 }
156
157 public Date getActiveSince() {
158 return activeSince;
159 }
160
161 public void setActiveSince(Date activeSince) {
162 this.activeSince = activeSince;
163 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700164
Jonathan Hartd857ad62013-12-14 18:08:17 -0800165 @Override
166 public int hashCode() {
167 if (hashCode != 0) return hashCode;
168 final int prime = 31;
169 hashCode = 1;
170 hashCode = prime * hashCode
Ray Milkey269ffb92014-04-03 14:43:30 -0700171 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
TeruU80ce5062014-03-03 17:16:13 -0800172 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 hashCode = prime * hashCode + (int) switchDPID;
174 hashCode = prime * hashCode + (int) switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800175 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
176 return hashCode;
177 }
178
179 @Override
180 public boolean equals(Object obj) {
181 if (this == obj) return true;
182 if (obj == null) return false;
183 if (getClass() != obj.getClass()) return false;
TeruU80ce5062014-03-03 17:16:13 -0800184 OnosDevice other = (OnosDevice) obj;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800185 if (hashCode() != other.hashCode()) return false;
186 if (ipv4Address == null) {
187 if (other.ipv4Address != null) return false;
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 } else if (!ipv4Address.equals(other.ipv4Address)) return false;
TeruU80ce5062014-03-03 17:16:13 -0800189 if (macAddress == null) {
190 if (other.macAddress != null) return false;
191 } else if (!macAddress.equals(other.macAddress)) return false;
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 if (switchDPID != other.switchDPID) return false;
TeruU80ce5062014-03-03 17:16:13 -0800193 if (switchPort != other.switchPort) return false;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800194 if (vlan == null) {
195 if (other.vlan != null) return false;
196 } else if (!vlan.equals(other.vlan)) return false;
197 return true;
198 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700199
Jonathan Hartd857ad62013-12-14 18:08:17 -0800200 @Override
201 public String toString() {
202 StringBuilder builder = new StringBuilder();
203 builder.append("Entity [macAddress=");
204 builder.append(macAddress.toString());
205 builder.append(", ipv4Address=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700206 builder.append(IPv4.fromIPv4Address(ipv4Address == null ?
207 0 : ipv4Address.intValue()));
Jonathan Hartd857ad62013-12-14 18:08:17 -0800208 builder.append(", vlan=");
209 builder.append(vlan);
210 builder.append(", switchDPID=");
211 builder.append(switchDPID);
212 builder.append(", switchPort=");
213 builder.append(switchPort);
214 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800216 builder.append(", activeSince=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 builder.append(activeSince == null ? "null" : activeSince.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800218 builder.append("]");
219 return builder.toString();
220 }
221
222 /*
223 @Override
224 public int compareTo(OnosDevice o) {
225 if (macAddress < o.macAddress) return -1;
226 if (macAddress > o.macAddress) return 1;
227
228 int r;
229 if (switchDPID == null)
230 r = o.switchDPID == null ? 0 : -1;
231 else if (o.switchDPID == null)
232 r = 1;
233 else
234 r = switchDPID.compareTo(o.switchDPID);
235 if (r != 0) return r;
236
237 if (switchPort == null)
238 r = o.switchPort == null ? 0 : -1;
239 else if (o.switchPort == null)
240 r = 1;
241 else
242 r = switchPort.compareTo(o.switchPort);
243 if (r != 0) return r;
244
245 if (ipv4Address == null)
246 r = o.ipv4Address == null ? 0 : -1;
247 else if (o.ipv4Address == null)
248 r = 1;
249 else
250 r = ipv4Address.compareTo(o.ipv4Address);
251 if (r != 0) return r;
252
253 if (vlan == null)
254 r = o.vlan == null ? 0 : -1;
255 else if (o.vlan == null)
256 r = 1;
257 else
258 r = vlan.compareTo(o.vlan);
259 if (r != 0) return r;
260
261 return 0;
262 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700263
Jonathan Hartd857ad62013-12-14 18:08:17 -0800264}