blob: 17ce3b967ee3f7235964ed3c90a1e973979189bd [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 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;
24
25/**
26 * An entity on the network is a visible trace of a device that corresponds
27 * to a packet received from a particular interface on the edge of a network,
28 * with a particular VLAN tag, and a particular MAC address, along with any
29 * other packet characteristics we might want to consider as helpful for
30 * disambiguating devices.
Ray Milkey269ffb92014-04-03 14:43:30 -070031 * <p/>
Jonathan Hartd857ad62013-12-14 18:08:17 -080032 * Entities are the most basic element of devices; devices consist of one or
33 * more entities. Entities are immutable once created, except for the last
34 * seen timestamp.
Jonathan Hartd857ad62013-12-14 18:08:17 -080035 *
Ray Milkey269ffb92014-04-03 14:43:30 -070036 * @author readams
Jonathan Hartd857ad62013-12-14 18:08:17 -080037 */
Jonathan Hartac15d932014-06-01 22:59:35 -070038public class OnosDevice implements Serializable {
Jonathan Hart96892d12014-03-26 20:21:29 -070039
Jonathan Hartac15d932014-06-01 22:59:35 -070040 private static final long serialVersionUID = 1L;
Ray Milkey269ffb92014-04-03 14:43:30 -070041
Jonathan Hartd857ad62013-12-14 18:08:17 -080042 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070043 * The MAC address associated with this entity.
Jonathan Hartd857ad62013-12-14 18:08:17 -080044 */
45 private MACAddress macAddress;
Ray Milkey269ffb92014-04-03 14:43:30 -070046
Jonathan Hartd857ad62013-12-14 18:08:17 -080047 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070048 * The VLAN tag on this entity, or null if untagged.
Jonathan Hartd857ad62013-12-14 18:08:17 -080049 */
50 private Short vlan;
Ray Milkey269ffb92014-04-03 14:43:30 -070051
Jonathan Hartd857ad62013-12-14 18:08:17 -080052 /**
53 * The DPID of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070054 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080055 */
56 private long switchDPID;
Ray Milkey269ffb92014-04-03 14:43:30 -070057
Jonathan Hartd857ad62013-12-14 18:08:17 -080058 /**
59 * The port number of the switch for the ingress point for this entity,
Ray Milkeyb41100a2014-04-10 10:42:15 -070060 * or null if not present.
Jonathan Hartd857ad62013-12-14 18:08:17 -080061 */
62 private short switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070063
Jonathan Hartd857ad62013-12-14 18:08:17 -080064 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070065 * The last time we observed this entity on the network.
Jonathan Hartd857ad62013-12-14 18:08:17 -080066 */
67 private Date lastSeenTimestamp;
68
Jonathan Hartd857ad62013-12-14 18:08:17 -080069 private int hashCode = 0;
70
71 // ************
72 // Constructors
73 // ************
Ray Milkey269ffb92014-04-03 14:43:30 -070074 protected OnosDevice() {
75 }
76
Jonathan Hartd857ad62013-12-14 18:08:17 -080077 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070078 * Create a new entity.
Ray Milkey269ffb92014-04-03 14:43:30 -070079 *
Jonathan Hartd857ad62013-12-14 18:08:17 -080080 * @param macAddress
81 * @param vlan
82 * @param ipv4Address
83 * @param switchDPID
84 * @param switchPort
85 * @param lastSeenTimestamp
86 */
Jonathan Hartac15d932014-06-01 22:59:35 -070087 public OnosDevice(MACAddress macAddress, Short vlan, Long switchDPID,
88 short switchPort, Date lastSeenTimestamp) {
Jonathan Hartd857ad62013-12-14 18:08:17 -080089 this.macAddress = macAddress;
Jonathan Hartd857ad62013-12-14 18:08:17 -080090 this.vlan = vlan;
91 this.switchDPID = switchDPID;
92 this.switchPort = switchPort;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070093 if (lastSeenTimestamp != null) {
94 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070095 } else {
96 this.lastSeenTimestamp = null;
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070097 }
Jonathan Hartd857ad62013-12-14 18:08:17 -080098 }
99
100 // ***************
101 // Getters/Setters
102 // ***************
103
104 public MACAddress getMacAddress() {
105 return macAddress;
106 }
107
Jonathan Hartd857ad62013-12-14 18:08:17 -0800108 public Short getVlan() {
109 return vlan;
110 }
111
112 public Long getSwitchDPID() {
113 return switchDPID;
114 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700115
TeruU80ce5062014-03-03 17:16:13 -0800116 public void setSwitchDPID(long dpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 this.switchDPID = dpid;
TeruU80ce5062014-03-03 17:16:13 -0800118 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800119
120 public short getSwitchPort() {
121 return switchPort;
122 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700123
TeruU80ce5062014-03-03 17:16:13 -0800124 public void setSwitchPort(short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 this.switchPort = port;
TeruU80ce5062014-03-03 17:16:13 -0800126 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800127
128 public Date getLastSeenTimestamp() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700129 if (this.lastSeenTimestamp == null) {
130 return null;
131 }
132 return new Date(this.lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800133 }
134
Jonathan Hartd857ad62013-12-14 18:08:17 -0800135 public void setLastSeenTimestamp(Date lastSeenTimestamp) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -0700136 this.lastSeenTimestamp = new Date(lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800137 }
138
Jonathan Hartd857ad62013-12-14 18:08:17 -0800139 @Override
140 public int hashCode() {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700141 if (hashCode != 0) {
142 return hashCode;
143 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800144 final int prime = 31;
145 hashCode = 1;
TeruU80ce5062014-03-03 17:16:13 -0800146 hashCode = prime * hashCode + (int) (macAddress.toLong() ^ (macAddress.toLong() >>> 32));
Ray Milkey269ffb92014-04-03 14:43:30 -0700147 hashCode = prime * hashCode + (int) switchDPID;
148 hashCode = prime * hashCode + (int) switchPort;
Jonathan Hartd857ad62013-12-14 18:08:17 -0800149 hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
150 return hashCode;
151 }
152
153 @Override
154 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700155 if (this == obj) {
156 return true;
157 }
158 if (obj == null) {
159 return false;
160 }
161 if (getClass() != obj.getClass()) {
162 return false;
163 }
TeruU80ce5062014-03-03 17:16:13 -0800164 OnosDevice other = (OnosDevice) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700165 if (hashCode() != other.hashCode()) {
166 return false;
167 }
TeruU80ce5062014-03-03 17:16:13 -0800168 if (macAddress == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700169 if (other.macAddress != null) {
170 return false;
171 }
172 } else if (!macAddress.equals(other.macAddress)) {
173 return false;
174 }
175 if (switchDPID != other.switchDPID) {
176 return false;
177 }
178 if (switchPort != other.switchPort) {
179 return false;
180 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800181 if (vlan == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 if (other.vlan != null) {
183 return false;
184 }
185 } else if (!vlan.equals(other.vlan)) {
186 return false;
187 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800188 return true;
189 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700190
Jonathan Hartd857ad62013-12-14 18:08:17 -0800191 @Override
192 public String toString() {
193 StringBuilder builder = new StringBuilder();
194 builder.append("Entity [macAddress=");
195 builder.append(macAddress.toString());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800196 builder.append(", vlan=");
197 builder.append(vlan);
198 builder.append(", switchDPID=");
199 builder.append(switchDPID);
200 builder.append(", switchPort=");
201 builder.append(switchPort);
202 builder.append(", lastSeenTimestamp=");
Ray Milkey269ffb92014-04-03 14:43:30 -0700203 builder.append(lastSeenTimestamp == null ? "null" : lastSeenTimestamp.getTime());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800204 builder.append("]");
205 return builder.toString();
206 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800207}