HostTracker immediately ages out hosts on coordinator
[controller.git] / opendaylight / hosttracker / api / src / main / java / org / opendaylight / controller / hosttracker / hostAware / HostNodeConnector.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.hosttracker.hostAware;
10
11 import java.io.Serializable;
12 import java.net.Inet4Address;
13 import java.net.Inet6Address;
14 import java.net.InetAddress;
15 import java.util.Arrays;
16
17 import javax.xml.bind.annotation.XmlAccessType;
18 import javax.xml.bind.annotation.XmlAccessorType;
19 import javax.xml.bind.annotation.XmlElement;
20 import javax.xml.bind.annotation.XmlRootElement;
21
22 import org.opendaylight.controller.sal.core.ConstructionException;
23 import org.opendaylight.controller.sal.core.Host;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.core.NodeConnector;
26 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
27
28 @XmlRootElement(name = "host")
29 @XmlAccessorType(XmlAccessType.NONE)
30 public class HostNodeConnector extends Host implements Serializable {
31     private static final long serialVersionUID = 1L;
32     @XmlElement
33     private NodeConnector nodeConnector;
34     @XmlElement
35     private short vlan;
36     @XmlElement
37     private boolean staticHost;
38     private short arpSendCountDown;
39
40     /**
41      * Private constructor used for JAXB mapping
42      */
43     @SuppressWarnings("unused")
44     private HostNodeConnector() {
45     }
46
47     public HostNodeConnector(InetAddress ip) throws ConstructionException {
48         this(ip, null);
49     }
50
51     public HostNodeConnector(InetAddress ip, NodeConnector nc)
52             throws ConstructionException {
53         this(new EthernetAddress(new byte[] { (byte) 0x00, (byte) 0x00,
54                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }), ip, nc,
55                 (short) 0);
56     }
57
58     public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
59             short vlan) throws ConstructionException {
60         this(new EthernetAddress(mac.clone()), ip, nc, vlan);
61     }
62
63     public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
64             NodeConnector nc, short vlan) throws ConstructionException {
65         super(eaddr, naddr);
66         this.nodeConnector = nc;
67         this.vlan = vlan;
68     }
69
70     /**
71      * @return the NodeConnector
72      */
73     public NodeConnector getnodeConnector() {
74         return this.nodeConnector;
75     }
76
77     /**
78      * @return the Node
79      */
80     public Node getnodeconnectorNode() {
81         return this.nodeConnector.getNode();
82     }
83
84     /**
85      * @return the DataLayerAddress
86      */
87     public byte[] getDataLayerAddressBytes() {
88         byte[] macaddr = null;
89         if (getDataLayerAddress() instanceof EthernetAddress) {
90             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
91             macaddr = e.getValue();
92         }
93         return macaddr;
94     }
95
96     /**
97      * @return the vlan
98      */
99     public short getVlan() {
100         return this.vlan;
101     }
102
103     public boolean isStaticHost() {
104         return this.staticHost;
105     }
106
107     public HostNodeConnector setStaticHost(boolean statically_learned) {
108         this.staticHost = statically_learned;
109         return this;
110     }
111
112     public HostNodeConnector initArpSendCountDown() {
113         this.arpSendCountDown = 24;
114         return this;
115     }
116
117     public short getArpSendCountDown() {
118         return (this.arpSendCountDown);
119     }
120
121     public HostNodeConnector setArpSendCountDown(short cntdown) {
122         this.arpSendCountDown = cntdown;
123         return this;
124     }
125
126     @Override
127     public int hashCode() {
128         final int prime = 31;
129         int result = super.hashCode();
130         result = prime * result
131                 + ((nodeConnector == null) ? 0 : nodeConnector.hashCode());
132         result = prime * result + (staticHost ? 1231 : 1237);
133         result = prime * result + vlan;
134         return result;
135     }
136
137     @Override
138     public boolean equals(Object obj) {
139         if (this == obj)
140             return true;
141         if (!super.equals(obj))
142             return false;
143         if (getClass() != obj.getClass())
144             return false;
145         HostNodeConnector other = (HostNodeConnector) obj;
146         if (nodeConnector == null) {
147             if (other.nodeConnector != null)
148                 return false;
149         } else if (!nodeConnector.equals(other.nodeConnector))
150             return false;
151         if (staticHost != other.staticHost)
152             return false;
153         if (vlan != other.vlan)
154             return false;
155         return true;
156     }
157
158     public boolean equalsByIP(InetAddress networkAddress) {
159         return (this.getNetworkAddress().equals(networkAddress));
160     }
161
162     public boolean isRewriteEnabled() {
163         byte[] emptyArray = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
164                 (byte) 0x00, (byte) 0x00, (byte) 0x00 };
165         byte[] macaddr = null;
166         if (getDataLayerAddress() instanceof EthernetAddress) {
167             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
168             macaddr = e.getValue();
169         }
170         if (macaddr == null)
171             return false;
172         return !Arrays.equals(emptyArray, macaddr);
173     }
174
175     @Override
176     public String toString() {
177         return "HostNodeConnector [nodeConnector=" + nodeConnector + ", vlan="
178                 + vlan + ", staticHost=" + staticHost + "]";
179     }
180
181     public boolean isV4Host() {
182         return (getNetworkAddress() instanceof Inet4Address);
183     }
184
185     public boolean isV6Host() {
186         return (getNetworkAddress() instanceof Inet6Address);
187     }
188
189     public String toJson() {
190         return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
191                 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
192                 + nodeConnector.toString() + "\"," + "\"static\":\""
193                 + String.valueOf(isStaticHost()) + "\"}";
194     }
195
196 }