Merge "BUG 2509 : Removing all journal entries from a Followers in-memory journal...
[controller.git] / opendaylight / adsal / hosttracker_new / api / src / main / java / org / opendaylight / controller / hosttracker / hostAware / HostNodeConnector.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.hosttracker.hostAware;
10
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.Arrays;
15
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;
20
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;
27
28 @XmlRootElement(name = "host")
29 @XmlAccessorType(XmlAccessType.NONE)
30 public class HostNodeConnector extends Host {
31     private static final long serialVersionUID = 1L;
32     @XmlElement
33     private NodeConnector nodeConnector;
34     @XmlElement
35     private short vlan;
36     @XmlElement
37     private boolean staticHost;
38     private transient short arpSendCountDown;
39
40     /**
41      * Private constructor used for JAXB mapping
42      */
43     @SuppressWarnings("unused")
44     private HostNodeConnector() {
45     }
46
47     public HostNodeConnector(InetAddress ip) throws ConstructionException {
48         this(ip, null);
49     }
50
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,
55                 (short) 0);
56     }
57
58     public HostNodeConnector(byte[] mac, InetAddress ip, NodeConnector nc,
59             short vlan) throws ConstructionException {
60         this(new EthernetAddress(mac.clone()), ip, nc, vlan);
61     }
62
63     public HostNodeConnector(EthernetAddress eaddr, InetAddress naddr,
64             NodeConnector nc, short vlan) throws ConstructionException {
65         super(eaddr, naddr);
66         this.nodeConnector = nc;
67         this.vlan = vlan;
68     }
69
70     /**
71      * @return the NodeConnector
72      */
73     public NodeConnector getnodeConnector() {
74         return this.nodeConnector;
75     }
76
77     /**
78      * @return the Node
79      */
80     public Node getnodeconnectorNode() {
81         return this.nodeConnector.getNode();
82     }
83
84     /**
85      * @return the NodeId
86      */
87     public Long getnodeconnectornodeId() {
88         return (Long) this.nodeConnector.getNode().getID();
89     }
90
91     /**
92      * @return the port
93      */
94     public Short getnodeconnectorportId() {
95         return (Short) this.nodeConnector.getID();
96     }
97
98     /**
99      * @return the DataLayerAddress
100      */
101     public byte[] getDataLayerAddressBytes() {
102         byte[] macaddr = null;
103         if (getDataLayerAddress() instanceof EthernetAddress) {
104             EthernetAddress e = (EthernetAddress) getDataLayerAddress();
105             macaddr = e.getValue();
106         }
107         return macaddr;
108     }
109
110     /**
111      * @return the vlan
112      */
113     public short getVlan() {
114         return this.vlan;
115     }
116
117     public boolean isStaticHost() {
118         return this.staticHost;
119     }
120
121     public HostNodeConnector setStaticHost(boolean statically_learned) {
122         this.staticHost = statically_learned;
123         return this;
124     }
125
126     public HostNodeConnector initArpSendCountDown() {
127         this.arpSendCountDown = 24;
128         return this;
129     }
130
131     public short getArpSendCountDown() {
132         return (this.arpSendCountDown);
133     }
134
135     public HostNodeConnector setArpSendCountDown(short cntdown) {
136         this.arpSendCountDown = cntdown;
137         return this;
138     }
139
140     @Override
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;
148         return result;
149     }
150
151     @Override
152     public boolean equals(Object obj) {
153         if (this == obj) {
154             return true;
155         }
156         if (!super.equals(obj)) {
157             return false;
158         }
159         if (getClass() != obj.getClass()) {
160             return false;
161         }
162         HostNodeConnector other = (HostNodeConnector) obj;
163         if (nodeConnector == null) {
164             if (other.nodeConnector != null) {
165                 return false;
166             }
167         } else if (!nodeConnector.equals(other.nodeConnector)) {
168             return false;
169         }
170         if (staticHost != other.staticHost) {
171             return false;
172         }
173         if (vlan != other.vlan) {
174             return false;
175         }
176         return true;
177     }
178
179     public boolean equalsByIP(InetAddress networkAddress) {
180         return (this.getNetworkAddress().equals(networkAddress));
181     }
182
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();
190         }
191         if (macaddr == null) {
192             return false;
193         }
194         return !Arrays.equals(emptyArray, macaddr);
195     }
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see java.lang.Object#toString()
201      */
202     @Override
203     public String toString() {
204         return "HostNodeConnector[" + ReflectionToStringBuilder.toString(this)
205                 + "]";
206     }
207
208     public boolean isV4Host() {
209         return (getNetworkAddress() instanceof Inet4Address);
210     }
211
212     public boolean isV6Host() {
213         return (getNetworkAddress() instanceof Inet6Address);
214     }
215
216     public String toJson() {
217         return "{\"host\":\"" + super.toString() + "\", " + "\"vlan\":\""
218                 + String.valueOf(vlan) + "\",\"NodeConnector\":\""
219                 + nodeConnector.toString() + "\"," + "\"static\":\""
220                 + String.valueOf(isStaticHost()) + "\"}";
221     }
222
223 }