Merge "Modified construction of built-in yang types."
[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 DataLayerAddress
87      */
88     public byte[] getDataLayerAddressBytes() {
89         byte[] macaddr = null;
90         if (getDataLayerAddress() instanceof EthernetAddress) {
91             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
92             macaddr = e.getValue();
93         }
94         return macaddr;
95     }
96
97     /**
98      * @return the vlan
99      */
100     public short getVlan() {
101         return this.vlan;
102     }
103
104     public boolean isStaticHost() {
105         return this.staticHost;
106     }
107
108     public HostNodeConnector setStaticHost(boolean statically_learned) {
109         this.staticHost = statically_learned;
110         return this;
111     }
112
113     public HostNodeConnector initArpSendCountDown() {
114         this.arpSendCountDown = 24;
115         return this;
116     }
117
118     public short getArpSendCountDown() {
119         return (this.arpSendCountDown);
120     }
121
122     public HostNodeConnector setArpSendCountDown(short cntdown) {
123         this.arpSendCountDown = cntdown;
124         return this;
125     }
126
127     @Override
128     public int hashCode() {
129         final int prime = 31;
130         int result = super.hashCode();
131         result = prime * result
132                 + ((nodeConnector == null) ? 0 : nodeConnector.hashCode());
133         result = prime * result + (staticHost ? 1231 : 1237);
134         result = prime * result + vlan;
135         return result;
136     }
137
138     @Override
139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         if (!super.equals(obj))
143             return false;
144         if (getClass() != obj.getClass())
145             return false;
146         HostNodeConnector other = (HostNodeConnector) obj;
147         if (nodeConnector == null) {
148             if (other.nodeConnector != null)
149                 return false;
150         } else if (!nodeConnector.equals(other.nodeConnector))
151             return false;
152         if (staticHost != other.staticHost)
153             return false;
154         if (vlan != other.vlan)
155             return false;
156         return true;
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 }