d18070d222b58e035208d52580fc56aea1ac74a6
[controller.git] / opendaylight / hosttracker / 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.EqualsBuilder;
23 import org.apache.commons.lang3.builder.HashCodeBuilder;
24 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
25 import org.opendaylight.controller.sal.core.ConstructionException;
26 import org.opendaylight.controller.sal.core.Host;
27 import org.opendaylight.controller.sal.core.Node;
28 import org.opendaylight.controller.sal.core.NodeConnector;
29 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
30
31 @XmlRootElement(name="host")
32 @XmlAccessorType(XmlAccessType.NONE)
33 public class HostNodeConnector extends Host {
34     private static final long serialVersionUID = 1L;
35     @XmlElement
36     private NodeConnector nodeConnector;
37     @XmlElement
38     private short vlan;
39     @XmlElement
40     private boolean staticHost;
41     private transient short arpSendCountDown;
42
43     /**
44      * Private constructor used for JAXB mapping
45      */
46     private HostNodeConnector() {
47     }
48
49     public HostNodeConnector(InetAddress ip) throws ConstructionException {
50         this(ip, null);
51     }
52
53     public HostNodeConnector(InetAddress ip, NodeConnector nc)
54             throws ConstructionException {
55         this(new EthernetAddress(new byte[] { (byte) 0x00, (byte) 0x00,
56                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }), ip, nc,
57                 (short) 0);
58     }
59
60     public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
61             short vlan) throws ConstructionException {
62         this(new EthernetAddress(mac.clone()), ip, nc, vlan);
63     }
64
65     public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
66             NodeConnector nc, short vlan) throws ConstructionException {
67         super(eaddr, naddr);
68         this.nodeConnector = nc;
69         this.vlan = vlan;
70     }
71
72     /**
73      * @return the NodeConnector
74      */
75     public NodeConnector getnodeConnector() {
76         return this.nodeConnector;
77     }
78
79     /**
80      * @return the Node
81      */
82     public Node getnodeconnectorNode() {
83         return this.nodeConnector.getNode();
84     }
85
86     /**
87      * @return the NodeId
88      */
89     public Long getnodeconnectornodeId() {
90         return (Long) this.nodeConnector.getNode().getID();
91     }
92
93     /**
94      * @return the port
95      */
96     public Short getnodeconnectorportId() {
97         return (Short) this.nodeConnector.getID();
98     }
99
100     /**
101      * @return the DataLayerAddress
102      */
103     public byte[] getDataLayerAddressBytes() {
104         byte[] macaddr = null;
105         if (getDataLayerAddress() instanceof EthernetAddress) {
106             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
107             macaddr = e.getValue();
108         }
109         return macaddr;
110     }
111
112     /**
113      * @return the vlan
114      */
115     public short getVlan() {
116         return this.vlan;
117     }
118
119     public boolean isStaticHost() {
120         return this.staticHost;
121     }
122
123     public HostNodeConnector setStaticHost(boolean statically_learned) {
124         this.staticHost = statically_learned;
125         return this;
126     }
127
128     public HostNodeConnector initArpSendCountDown() {
129         this.arpSendCountDown = 24;
130         return this;
131     }
132
133     public short getArpSendCountDown() {
134         return (this.arpSendCountDown);
135     }
136
137     public HostNodeConnector setArpSendCountDown(short cntdown) {
138         this.arpSendCountDown = cntdown;
139         return this;
140     }
141
142     /* (non-Javadoc)
143      * @see java.lang.Object#hashCode()
144      */
145     @Override
146     public int hashCode() {
147         return HashCodeBuilder.reflectionHashCode(this);
148     }
149
150     /* (non-Javadoc)
151      * @see java.lang.Object#equals(java.lang.Object)
152      */
153     @Override
154     public boolean equals(Object obj) {
155         return EqualsBuilder.reflectionEquals(this, obj);
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     /* (non-Javadoc)
176      * @see java.lang.Object#toString()
177      */
178     @Override
179     public String toString() {
180         return "HostNodeConnector[" + ReflectionToStringBuilder.toString(this)
181                 + "]";
182     }
183
184     public boolean isV4Host() {
185         return (getNetworkAddress() instanceof Inet4Address);
186     }
187
188     public boolean isV6Host() {
189         return (getNetworkAddress() instanceof Inet6Address);
190     }
191
192     public String toJson() {
193         return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
194                 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
195                 + nodeConnector.toString() + "\"," + "\"static\":\""
196                 + String.valueOf(isStaticHost()) + "\"}";
197     }
198
199 }