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