2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.hosttracker.hostAware;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.Arrays;
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;
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;
27 @XmlRootElement(name = "host")
28 @XmlAccessorType(XmlAccessType.NONE)
29 public class HostNodeConnector extends Host {
30 private static final long serialVersionUID = 1L;
32 private NodeConnector nodeConnector;
36 private boolean staticHost;
37 private short arpSendCountDown;
40 * Private constructor used for JAXB mapping
42 @SuppressWarnings("unused")
43 private HostNodeConnector() {
46 public HostNodeConnector(InetAddress ip) throws ConstructionException {
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,
57 public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
58 short vlan) throws ConstructionException {
59 this(new EthernetAddress(mac.clone()), ip, nc, vlan);
62 public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
63 NodeConnector nc, short vlan) throws ConstructionException {
65 this.nodeConnector = nc;
70 * @return the NodeConnector
72 public NodeConnector getnodeConnector() {
73 return this.nodeConnector;
79 public Node getnodeconnectorNode() {
80 return this.nodeConnector.getNode();
84 * @return the DataLayerAddress
86 public byte[] getDataLayerAddressBytes() {
87 byte[] macaddr = null;
88 if (getDataLayerAddress() instanceof EthernetAddress) {
89 EthernetAddress e = (EthernetAddress) getDataLayerAddress();
90 macaddr = e.getValue();
98 public short getVlan() {
102 public boolean isStaticHost() {
103 return this.staticHost;
106 public HostNodeConnector setStaticHost(boolean statically_learned) {
107 this.staticHost = statically_learned;
111 public HostNodeConnector initArpSendCountDown() {
112 this.arpSendCountDown = 24;
116 public short getArpSendCountDown() {
117 return (this.arpSendCountDown);
120 public HostNodeConnector setArpSendCountDown(short cntdown) {
121 this.arpSendCountDown = cntdown;
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;
137 public boolean equals(Object obj) {
141 if (!super.equals(obj)) {
144 if (getClass() != obj.getClass()) {
147 HostNodeConnector other = (HostNodeConnector) obj;
148 if (nodeConnector == null) {
149 if (other.nodeConnector != null) {
152 } else if (!nodeConnector.equals(other.nodeConnector)) {
155 if (staticHost != other.staticHost) {
158 if (vlan != other.vlan) {
164 public boolean equalsByIP(InetAddress networkAddress) {
165 return (this.getNetworkAddress().equals(networkAddress));
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();
176 if (macaddr == null) {
179 return !Arrays.equals(emptyArray, macaddr);
185 * @see java.lang.Object#toString()
188 public String toString() {
189 StringBuilder builder = new StringBuilder();
190 builder.append("HostNodeConnector [");
191 if (nodeConnector != null) {
192 builder.append("nodeConnector=")
193 .append(nodeConnector)
196 builder.append("vlan=")
198 .append(", staticHost=")
200 .append(", arpSendCountDown=")
201 .append(arpSendCountDown)
203 return builder.toString();
206 public boolean isV4Host() {
207 return (getNetworkAddress() instanceof Inet4Address);
210 public boolean isV6Host() {
211 return (getNetworkAddress() instanceof Inet6Address);
214 public String toJson() {
215 return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
216 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
217 + nodeConnector.toString() + "\"," + "\"static\":\""
218 + String.valueOf(isStaticHost()) + "\"}";