Gracefully stop HT threads when the bundle is being stopped (cache terminated exception)
[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.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.Arrays;
15
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
20
21 import org.opendaylight.controller.sal.core.ConstructionException;
22 import org.opendaylight.controller.sal.core.Host;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
26
27 @XmlRootElement(name = "host")
28 @XmlAccessorType(XmlAccessType.NONE)
29 public class HostNodeConnector extends Host {
30     private static final long serialVersionUID = 1L;
31     @XmlElement
32     private NodeConnector nodeConnector;
33     @XmlElement
34     private short vlan;
35     @XmlElement
36     private boolean staticHost;
37     private short arpSendCountDown;
38
39     /**
40      * Private constructor used for JAXB mapping
41      */
42     @SuppressWarnings("unused")
43     private HostNodeConnector() {
44     }
45
46     public HostNodeConnector(InetAddress ip) throws ConstructionException {
47         this(ip, null);
48     }
49
50     public HostNodeConnector(InetAddress ip, NodeConnector nc)
51             throws ConstructionException {
52         this(new EthernetAddress(new byte[] { (byte) 0x00, (byte) 0x00,
53                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }), ip, nc,
54                 (short) 0);
55     }
56
57     public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
58             short vlan) throws ConstructionException {
59         this(new EthernetAddress(mac.clone()), ip, nc, vlan);
60     }
61
62     public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
63             NodeConnector nc, short vlan) throws ConstructionException {
64         super(eaddr, naddr);
65         this.nodeConnector = nc;
66         this.vlan = vlan;
67     }
68
69     /**
70      * @return the NodeConnector
71      */
72     public NodeConnector getnodeConnector() {
73         return this.nodeConnector;
74     }
75
76     /**
77      * @return the Node
78      */
79     public Node getnodeconnectorNode() {
80         return this.nodeConnector.getNode();
81     }
82
83     /**
84      * @return the DataLayerAddress
85      */
86     public byte[] getDataLayerAddressBytes() {
87         byte[] macaddr = null;
88         if (getDataLayerAddress() instanceof EthernetAddress) {
89             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
90             macaddr = e.getValue();
91         }
92         return macaddr;
93     }
94
95     /**
96      * @return the vlan
97      */
98     public short getVlan() {
99         return this.vlan;
100     }
101
102     public boolean isStaticHost() {
103         return this.staticHost;
104     }
105
106     public HostNodeConnector setStaticHost(boolean statically_learned) {
107         this.staticHost = statically_learned;
108         return this;
109     }
110
111     public HostNodeConnector initArpSendCountDown() {
112         this.arpSendCountDown = 24;
113         return this;
114     }
115
116     public short getArpSendCountDown() {
117         return (this.arpSendCountDown);
118     }
119
120     public HostNodeConnector setArpSendCountDown(short cntdown) {
121         this.arpSendCountDown = cntdown;
122         return this;
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = super.hashCode();
129         result = prime * result
130                 + ((nodeConnector == null) ? 0 : nodeConnector.hashCode());
131         result = prime * result + (staticHost ? 1231 : 1237);
132         result = prime * result + vlan;
133         return result;
134     }
135
136     @Override
137     public boolean equals(Object obj) {
138         if (this == obj) {
139             return true;
140         }
141         if (!super.equals(obj)) {
142             return false;
143         }
144         if (getClass() != obj.getClass()) {
145             return false;
146         }
147         HostNodeConnector other = (HostNodeConnector) obj;
148         if (nodeConnector == null) {
149             if (other.nodeConnector != null) {
150                 return false;
151             }
152         } else if (!nodeConnector.equals(other.nodeConnector)) {
153             return false;
154         }
155         if (staticHost != other.staticHost) {
156             return false;
157         }
158         if (vlan != other.vlan) {
159             return false;
160         }
161         return true;
162     }
163
164     public boolean equalsByIP(InetAddress networkAddress) {
165         return (this.getNetworkAddress().equals(networkAddress));
166     }
167
168     public boolean isRewriteEnabled() {
169         byte[] emptyArray = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
170                 (byte) 0x00, (byte) 0x00, (byte) 0x00 };
171         byte[] macaddr = null;
172         if (getDataLayerAddress() instanceof EthernetAddress) {
173             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
174             macaddr = e.getValue();
175         }
176         if (macaddr == null) {
177             return false;
178         }
179         return !Arrays.equals(emptyArray, macaddr);
180     }
181
182     @Override
183     public String toString() {
184         return "HostNodeConnector [nodeConnector=" + nodeConnector + ", vlan="
185                 + vlan + ", staticHost=" + staticHost + "]";
186     }
187
188     public boolean isV4Host() {
189         return (getNetworkAddress() instanceof Inet4Address);
190     }
191
192     public boolean isV6Host() {
193         return (getNetworkAddress() instanceof Inet6Address);
194     }
195
196     public String toJson() {
197         return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
198                 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
199                 + nodeConnector.toString() + "\"," + "\"static\":\""
200                 + String.valueOf(isStaticHost()) + "\"}";
201     }
202
203 }