Get/remove inactive hosts
[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.net.InetAddress;
37 import java.util.Date;
38
39 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
40 import org.opendaylight.controller.sal.core.NodeConnector;
41 import org.opendaylight.controller.sal.utils.NetUtils;
42
43 /**
44  * An entity on the network is a visible trace of a device that corresponds to a
45  * packet received from a particular interface on the edge of a network, with a
46  * particular VLAN tag, and a particular MAC address, along with any other
47  * packet characteristics we might want to consider as helpful for
48  * disambiguating devices.
49  *
50  * Entities are the most basic element of devices; devices consist of one or
51  * more entities. Entities are immutable once created, except for the last seen
52  * timestamp.
53  *
54  * @author readams
55  *
56  */
57 public class Entity implements Comparable<Entity> {
58     /**
59      * Timeout for computing {@link Entity#activeSince}.
60      *
61      * @see {@link Entity#activeSince}
62      */
63     protected static int ACTIVITY_TIMEOUT = 30000;
64
65     /**
66      * The MAC address associated with this entity
67      */
68     protected long macAddress;
69
70     /**
71      * The IP address associated with this entity, or null if no IP learned from
72      * the network observation associated with this entity
73      */
74     protected Integer ipv4Address;
75
76     /**
77      * The VLAN tag on this entity, or null if untagged
78      */
79     protected Short vlan;
80
81     /**
82      * The attachment point for this entity
83      */
84     NodeConnector port;
85
86     /**
87      * The last time we observed this entity on the network
88      */
89     protected Date lastSeenTimestamp;
90
91     /**
92      * The time between {@link Entity#activeSince} and
93      * {@link Entity#lastSeenTimestamp} is a period of activity for this entity
94      * where it was observed repeatedly. If, when the entity is observed, the is
95      * longer ago than the activity timeout, {@link Entity#lastSeenTimestamp}
96      * and {@link Entity#activeSince} will be set to the current time.
97      */
98     protected Date activeSince;
99
100     private int hashCode = 0;
101
102     // ************
103     // Constructors
104     // ************
105
106     /**
107      * Create a new entity
108      *
109      * @param macAddress
110      * @param vlan
111      * @param ipv4Address
112      * @param switchDPID
113      * @param switchPort
114      * @param lastSeenTimestamp
115      */
116     public Entity(long macAddress, Short vlan, Integer ipv4Address,
117             NodeConnector port, Date lastSeenTimestamp) {
118         this.macAddress = macAddress;
119         this.ipv4Address = ipv4Address;
120         this.vlan = vlan;
121         this.port = port;
122         this.lastSeenTimestamp = lastSeenTimestamp;
123         this.activeSince = lastSeenTimestamp;
124     }
125
126     // ***************
127     // Getters/Setters
128     // ***************
129
130     // @JsonSerialize(using=MACSerializer.class)
131     public long getMacAddress() {
132         return macAddress;
133     }
134
135     // @JsonSerialize(using=IPv4Serializer.class)
136     public Integer getIpv4Address() {
137         return ipv4Address;
138     }
139
140     public Short getVlan() {
141         return vlan;
142     }
143
144     public NodeConnector getPort() {
145         return port;
146     }
147
148     // @JsonIgnore
149     public boolean hasSwitchPort() {
150         return port != null;
151     }
152
153     public Date getLastSeenTimestamp() {
154         return lastSeenTimestamp;
155     }
156
157     /**
158      * Set the last seen timestamp and also update {@link Entity#activeSince} if
159      * appropriate
160      *
161      * @param lastSeenTimestamp
162      *            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) < lastSeenTimestamp
168                         .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
181     @Override
182     public int hashCode() {
183         if (hashCode != 0)
184             return hashCode;
185         final int prime = 31;
186         hashCode = 1;
187         hashCode = prime * hashCode
188                 + ((ipv4Address == null) ? 0 : ipv4Address.hashCode());
189         hashCode = prime * hashCode + (int) (macAddress ^ (macAddress >>> 32));
190         hashCode = prime * hashCode + ((port == null) ? 0 : port.hashCode());
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)
198             return true;
199         if (obj == null)
200             return false;
201         if (getClass() != obj.getClass())
202             return false;
203         Entity other = (Entity) obj;
204         if (ipv4Address == null) {
205             if (other.ipv4Address != null)
206                 return false;
207         } else if (!ipv4Address.equals(other.ipv4Address))
208             return false;
209         if (macAddress != other.macAddress)
210             return false;
211         if (port == null) {
212             if (other.port != null)
213                 return false;
214         } else if (!port.equals(other.port))
215             return false;
216         if (vlan == null) {
217             if (other.vlan != null)
218                 return false;
219         } else if (!vlan.equals(other.vlan))
220             return false;
221         return true;
222     }
223
224     public HostNodeConnector toHostNodeConnector() {
225         try {
226             NodeConnector n = this.getPort();
227             InetAddress ip = InetAddress.getByAddress(NetUtils.intToByteArray4(this.getIpv4Address()));
228             byte[] macAddr = NetUtils.longToByteArray6(this.getMacAddress());
229             HostNodeConnector nc = new HostNodeConnector(macAddr, ip, n,
230                     (short) 0);
231             return nc;
232         } catch (Exception e) {
233             return null;
234         }
235     }
236
237     @Override
238     public String toString() {
239         return "Entity [macAddress=" + macAddress + ", ipv4Address="
240                 + ipv4Address + ", vlan=" + vlan + ", port=" + port + "]";
241     }
242
243     @SuppressWarnings({ "rawtypes", "unchecked" })
244     @Override
245     public int compareTo(Entity o) {
246         int r;
247         if (port == null)
248             r = o.port == null ? 0 : -1;
249         else if (o.port == null)
250             r = 1;
251         else {
252             // XXX - the node id is only defined as an object rather
253             // than something useful. We're just going to have to
254             // blindly cast to Comparable and hope it works.
255             Comparable switchId = (Comparable) port.getNode().getID();
256             Comparable oswitchId = (Comparable) o.port.getNode().getID();
257             r = switchId.compareTo(oswitchId);
258             if (r != 0)
259                 return r;
260
261             Comparable portId = (Comparable) port.getID();
262             Comparable oportId = (Comparable) o.port.getID();
263             r = portId.compareTo(oportId);
264         }
265         return r;
266     }
267
268 }