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.apache.commons.lang3.builder.ReflectionToStringBuilder;
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;
28 @XmlRootElement(name = "host")
29 @XmlAccessorType(XmlAccessType.NONE)
30 public class HostNodeConnector extends Host {
31 private static final long serialVersionUID = 1L;
33 private NodeConnector nodeConnector;
37 private boolean staticHost;
38 private transient short arpSendCountDown;
41 * Private constructor used for JAXB mapping
43 @SuppressWarnings("unused")
44 private HostNodeConnector() {
47 public HostNodeConnector(InetAddress ip) throws ConstructionException {
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,
58 public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
59 short vlan) throws ConstructionException {
60 this(new EthernetAddress(mac.clone()), ip, nc, vlan);
63 public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
64 NodeConnector nc, short vlan) throws ConstructionException {
66 this.nodeConnector = nc;
71 * @return the NodeConnector
73 public NodeConnector getnodeConnector() {
74 return this.nodeConnector;
80 public Node getnodeconnectorNode() {
81 return this.nodeConnector.getNode();
87 public Long getnodeconnectornodeId() {
88 return (Long) this.nodeConnector.getNode().getID();
94 public Short getnodeconnectorportId() {
95 return (Short) this.nodeConnector.getID();
99 * @return the DataLayerAddress
101 public byte[] getDataLayerAddressBytes() {
102 byte[] macaddr = null;
103 if (getDataLayerAddress() instanceof EthernetAddress) {
104 EthernetAddress e = (EthernetAddress) getDataLayerAddress();
105 macaddr = e.getValue();
113 public short getVlan() {
117 public boolean isStaticHost() {
118 return this.staticHost;
121 public HostNodeConnector setStaticHost(boolean statically_learned) {
122 this.staticHost = statically_learned;
126 public HostNodeConnector initArpSendCountDown() {
127 this.arpSendCountDown = 24;
131 public short getArpSendCountDown() {
132 return (this.arpSendCountDown);
135 public HostNodeConnector setArpSendCountDown(short cntdown) {
136 this.arpSendCountDown = cntdown;
141 public int hashCode() {
142 final int prime = 31;
143 int result = super.hashCode();
144 result = prime * result
145 + ((nodeConnector == null) ? 0 : nodeConnector.hashCode());
146 result = prime * result + (staticHost ? 1231 : 1237);
147 result = prime * result + vlan;
152 public boolean equals(Object obj) {
156 if (!super.equals(obj)) {
159 if (getClass() != obj.getClass()) {
162 HostNodeConnector other = (HostNodeConnector) obj;
163 if (nodeConnector == null) {
164 if (other.nodeConnector != null) {
167 } else if (!nodeConnector.equals(other.nodeConnector)) {
170 if (staticHost != other.staticHost) {
173 if (vlan != other.vlan) {
179 public boolean equalsByIP(InetAddress networkAddress) {
180 return (this.getNetworkAddress().equals(networkAddress));
183 public boolean isRewriteEnabled() {
184 byte[] emptyArray = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
185 (byte) 0x00, (byte) 0x00, (byte) 0x00 };
186 byte[] macaddr = null;
187 if (getDataLayerAddress() instanceof EthernetAddress) {
188 EthernetAddress e = (EthernetAddress) getDataLayerAddress();
189 macaddr = e.getValue();
191 if (macaddr == null) {
194 return !Arrays.equals(emptyArray, macaddr);
200 * @see java.lang.Object#toString()
203 public String toString() {
204 return "HostNodeConnector[" + ReflectionToStringBuilder.toString(this)
208 public boolean isV4Host() {
209 return (getNetworkAddress() instanceof Inet4Address);
212 public boolean isV6Host() {
213 return (getNetworkAddress() instanceof Inet6Address);
216 public String toJson() {
217 return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
218 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
219 + nodeConnector.toString() + "\"," + "\"static\":\""
220 + String.valueOf(isStaticHost()) + "\"}";