7c98e9505b28b1b51098a9d9c9c3e29ab9af6a2c
[controller.git] / opendaylight / hosttracker_new / api / src / main / java / org / opendaylight / controller / hosttracker / Entity.java
1 /*
2  * Copyright (c) 2011,2012 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  *
16  * This file incorporates work covered by the following copyright and
17  * permission notice:
18  *
19  *    Originally created by David Erickson, Stanford University
20  *
21  *    Licensed under the Apache License, Version 2.0 (the "License");
22  *    you may not use this file except in compliance with the
23  *    License. You may obtain a copy of the License at
24  *
25  *         http://www.apache.org/licenses/LICENSE-2.0
26  *
27  *    Unless required by applicable law or agreed to in writing,
28  *    software distributed under the License is distributed on an "AS
29  *    IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30  *    express or implied. See the License for the specific language
31  *    governing permissions and limitations under the License.
32  */
33
34 package org.opendaylight.controller.hosttracker;
35
36 import java.util.Date;
37
38 import org.opendaylight.controller.sal.core.NodeConnector;
39
40 /**
41  * An entity on the network is a visible trace of a device that corresponds to a
42  * packet received from a particular interface on the edge of a network, with a
43  * particular VLAN tag, and a particular MAC address, along with any other
44  * packet characteristics we might want to consider as helpful for
45  * disambiguating devices.
46  *
47  * Entities are the most basic element of devices; devices consist of one or
48  * more entities. Entities are immutable once created, except for the last seen
49  * timestamp.
50  *
51  * @author readams
52  *
53  */
54 public class Entity implements Comparable<Entity> {
55     /**
56      * Timeout for computing {@link Entity#activeSince}.
57      *
58      * @see {@link Entity#activeSince}
59      */
60     protected static int ACTIVITY_TIMEOUT = 30000;
61
62     /**
63      * The MAC address associated with this entity
64      */
65     protected long macAddress;
66
67     /**
68      * The IP address associated with this entity, or null if no IP learned from
69      * the network observation associated with this entity
70      */
71     protected Integer ipv4Address;
72
73     /**
74      * The VLAN tag on this entity, or null if untagged
75      */
76     protected Short vlan;
77
78     /**
79      * The attachment point for this entity
80      */
81     NodeConnector port;
82
83     /**
84      * The last time we observed this entity on the network
85      */
86     protected Date lastSeenTimestamp;
87
88     /**
89      * The time between {@link Entity#activeSince} and
90      * {@link Entity#lastSeenTimestamp} is a period of activity for this entity
91      * where it was observed repeatedly. If, when the entity is observed, the is
92      * longer ago than the activity timeout, {@link Entity#lastSeenTimestamp}
93      * and {@link Entity#activeSince} will be set to the current time.
94      */
95     protected Date activeSince;
96
97     private int hashCode = 0;
98
99     // ************
100     // Constructors
101     // ************
102
103     /**
104      * Create a new entity
105      *
106      * @param macAddress
107      * @param vlan
108      * @param ipv4Address
109      * @param switchDPID
110      * @param switchPort
111      * @param lastSeenTimestamp
112      */
113     public Entity(long macAddress, Short vlan, Integer ipv4Address,
114             NodeConnector port, Date lastSeenTimestamp) {
115         this.macAddress = macAddress;
116         this.ipv4Address = ipv4Address;
117         this.vlan = vlan;
118         this.port = port;
119         this.lastSeenTimestamp = lastSeenTimestamp;
120         this.activeSince = lastSeenTimestamp;
121     }
122
123     // ***************
124     // Getters/Setters
125     // ***************
126
127     // @JsonSerialize(using=MACSerializer.class)
128     public long getMacAddress() {
129         return macAddress;
130     }
131
132     // @JsonSerialize(using=IPv4Serializer.class)
133     public Integer getIpv4Address() {
134         return ipv4Address;
135     }
136
137     public Short getVlan() {
138         return vlan;
139     }
140
141     public NodeConnector getPort() {
142         return port;
143     }
144
145     // @JsonIgnore
146     public boolean hasSwitchPort() {
147         return port != null;
148     }
149
150     public Date getLastSeenTimestamp() {
151         return lastSeenTimestamp;
152     }
153
154     /**
155      * Set the last seen timestamp and also update {@link Entity#activeSince} if
156      * appropriate
157      *
158      * @param lastSeenTimestamp
159      *            the new last seen timestamp
160      * @see {@link Entity#activeSince}
161      */
162     public void setLastSeenTimestamp(Date lastSeenTimestamp) {
163         if (activeSince == null
164                 || (activeSince.getTime() + ACTIVITY_TIMEOUT) < lastSeenTimestamp
165                         .getTime())
166             this.activeSince = lastSeenTimestamp;
167         this.lastSeenTimestamp = lastSeenTimestamp;
168     }
169
170     public Date getActiveSince() {
171         return activeSince;
172     }
173
174     public void setActiveSince(Date activeSince) {
175         this.activeSince = activeSince;
176     }
177
178     @Override
179     public int hashCode() {
180         if (hashCode != 0)
181             return hashCode;
182         final int prime = 31;
183         hashCode = 1;
184         hashCode = prime * hashCode
185                 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
186         hashCode = prime * hashCode + (int) (macAddress ^ (macAddress >>> 32));
187         hashCode = prime * hashCode + ((port == null) ? 0 : port.hashCode());
188         hashCode = prime * hashCode + ((vlan == null) ? 0 : vlan.hashCode());
189         return hashCode;
190     }
191
192     @Override
193     public boolean equals(Object obj) {
194         if (this == obj)
195             return true;
196         if (obj == null)
197             return false;
198         if (getClass() != obj.getClass())
199             return false;
200         Entity other = (Entity) obj;
201         if (ipv4Address == null) {
202             if (other.ipv4Address != null)
203                 return false;
204         } else if (!ipv4Address.equals(other.ipv4Address))
205             return false;
206         if (macAddress != other.macAddress)
207             return false;
208         if (port == null) {
209             if (other.port != null)
210                 return false;
211         } else if (!port.equals(other.port))
212             return false;
213         if (vlan == null) {
214             if (other.vlan != null)
215                 return false;
216         } else if (!vlan.equals(other.vlan))
217             return false;
218         return true;
219     }
220
221     @Override
222     public String toString() {
223         return "Entity [macAddress=" + macAddress + ", ipv4Address="
224                 + ipv4Address + ", vlan=" + vlan + ", port=" + port + "]";
225     }
226
227     @SuppressWarnings({ "rawtypes", "unchecked" })
228     @Override
229     public int compareTo(Entity o) {
230         int r;
231         if (port == null)
232             r = o.port == null ? 0 : -1;
233         else if (o.port == null)
234             r = 1;
235         else {
236             // XXX - the node id is only defined as an object rather
237             // than something useful. We're just going to have to
238             // blindly cast to Comparable and hope it works.
239             Comparable switchId = (Comparable) port.getNode().getID();
240             Comparable oswitchId = (Comparable) o.port.getNode().getID();
241             r = switchId.compareTo(oswitchId);
242             if (r != 0)
243                 return r;
244
245             Comparable portId = (Comparable) port.getID();
246             Comparable oportId = (Comparable) o.port.getID();
247             r = portId.compareTo(oportId);
248         }
249         return r;
250     }
251
252 }