- Application is no longer blocked when programming hundreds of flows. The Barrier...
[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     @SuppressWarnings("unused")
47     private HostNodeConnector() {
48     }
49
50     public HostNodeConnector(InetAddress ip) throws ConstructionException {
51         this(ip, null);
52     }
53
54     public HostNodeConnector(InetAddress ip, NodeConnector nc)
55             throws ConstructionException {
56         this(new EthernetAddress(new byte[] { (byte) 0x00, (byte) 0x00,
57                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }), ip, nc,
58                 (short) 0);
59     }
60
61     public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
62             short vlan) throws ConstructionException {
63         this(new EthernetAddress(mac.clone()), ip, nc, vlan);
64     }
65
66     public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
67             NodeConnector nc, short vlan) throws ConstructionException {
68         super(eaddr, naddr);
69         this.nodeConnector = nc;
70         this.vlan = vlan;
71     }
72
73     /**
74      * @return the NodeConnector
75      */
76     public NodeConnector getnodeConnector() {
77         return this.nodeConnector;
78     }
79
80     /**
81      * @return the Node
82      */
83     public Node getnodeconnectorNode() {
84         return this.nodeConnector.getNode();
85     }
86
87     /**
88      * @return the NodeId
89      */
90     public Long getnodeconnectornodeId() {
91         return (Long) this.nodeConnector.getNode().getID();
92     }
93
94     /**
95      * @return the port
96      */
97     public Short getnodeconnectorportId() {
98         return (Short) this.nodeConnector.getID();
99     }
100
101     /**
102      * @return the DataLayerAddress
103      */
104     public byte[] getDataLayerAddressBytes() {
105         byte[] macaddr = null;
106         if (getDataLayerAddress() instanceof EthernetAddress) {
107             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
108             macaddr = e.getValue();
109         }
110         return macaddr;
111     }
112
113     /**
114      * @return the vlan
115      */
116     public short getVlan() {
117         return this.vlan;
118     }
119
120     public boolean isStaticHost() {
121         return this.staticHost;
122     }
123
124     public HostNodeConnector setStaticHost(boolean statically_learned) {
125         this.staticHost = statically_learned;
126         return this;
127     }
128
129     public HostNodeConnector initArpSendCountDown() {
130         this.arpSendCountDown = 24;
131         return this;
132     }
133
134     public short getArpSendCountDown() {
135         return (this.arpSendCountDown);
136     }
137
138     public HostNodeConnector setArpSendCountDown(short cntdown) {
139         this.arpSendCountDown = cntdown;
140         return this;
141     }
142
143     /* (non-Javadoc)
144      * @see java.lang.Object#hashCode()
145      */
146     @Override
147     public int hashCode() {
148         return HashCodeBuilder.reflectionHashCode(this);
149     }
150
151     /* (non-Javadoc)
152      * @see java.lang.Object#equals(java.lang.Object)
153      */
154     @Override
155     public boolean equals(Object obj) {
156         return EqualsBuilder.reflectionEquals(this, obj);
157     }
158
159     public boolean equalsByIP(InetAddress networkAddress) {
160         return (this.getNetworkAddress().equals(networkAddress));
161     }
162
163     public boolean isRewriteEnabled() {
164         byte[] emptyArray = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
165                 (byte) 0x00, (byte) 0x00, (byte) 0x00 };
166         byte[] macaddr = null;
167         if (getDataLayerAddress() instanceof EthernetAddress) {
168             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
169             macaddr = e.getValue();
170         }
171         if (macaddr == null)
172             return false;
173         return !Arrays.equals(emptyArray, macaddr);
174     }
175
176     /* (non-Javadoc)
177      * @see java.lang.Object#toString()
178      */
179     @Override
180     public String toString() {
181         return "HostNodeConnector[" + ReflectionToStringBuilder.toString(this)
182                 + "]";
183     }
184
185     public boolean isV4Host() {
186         return (getNetworkAddress() instanceof Inet4Address);
187     }
188
189     public boolean isV6Host() {
190         return (getNetworkAddress() instanceof Inet6Address);
191     }
192
193     public String toJson() {
194         return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
195                 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
196                 + nodeConnector.toString() + "\"," + "\"static\":\""
197                 + String.valueOf(isStaticHost()) + "\"}";
198     }
199
200 }