50036372afc041f106009834e676e7ab5f7037d5
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Host.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.sal.core;
11
12 import java.io.Serializable;
13 import java.net.InetAddress;
14
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
19
20 import org.opendaylight.controller.sal.packet.address.DataLinkAddress;
21
22 @XmlRootElement(name="host")
23 @XmlAccessorType(XmlAccessType.NONE)
24 public class Host implements Serializable {
25     private static final long serialVersionUID = 1L;
26     @XmlElement
27     private DataLinkAddress dataLayerAddress;
28     private InetAddress networkAddress;
29
30     public Host() {
31
32     }
33
34     /**
35      * Create an Host representation from the combination Data Link
36      * layer/Network layer address, both are needed to construct the
37      * object. Fake value can also be provided in case are not
38      * existent.
39      *
40      * @param dataLayerAddress Data Link Address for the host
41      * @param networkAddress Network Address for the host
42      *
43      * @return the constructed object
44      */
45     public Host(DataLinkAddress dataLayerAddress, InetAddress networkAddress)
46             throws ConstructionException {
47         if (dataLayerAddress == null) {
48             throw new ConstructionException("Passed null datalink address");
49         }
50         if (networkAddress == null) {
51             throw new ConstructionException("Passed null network address");
52         }
53         this.dataLayerAddress = dataLayerAddress;
54         this.networkAddress = networkAddress;
55     }
56
57     /**
58      * Copy constructor
59      *
60      * @param h Host to copy values from
61      *
62      * @return constructed copy
63      */
64     public Host(Host h) throws ConstructionException {
65         if (h == null) {
66             throw new ConstructionException("Passed null host");
67         }
68         this.dataLayerAddress = h.getDataLayerAddress();
69         this.networkAddress = h.getNetworkAddress();
70     }
71
72     /**
73      * @return the dataLayerAddress
74      */
75     public DataLinkAddress getDataLayerAddress() {
76         return this.dataLayerAddress;
77     }
78
79     /**
80      * @return the networkAddress
81      */
82     public InetAddress getNetworkAddress() {
83         return networkAddress;
84     }
85
86     @XmlElement(name = "networkAddress")
87     public String getNetworkAddressAsString() {
88         return networkAddress.getHostAddress();
89     }
90
91     @Override
92     public int hashCode() {
93         final int prime = 31;
94         int result = 1;
95         result = prime
96                 * result
97                 + ((dataLayerAddress == null) ? 0 : dataLayerAddress.hashCode());
98         result = prime * result
99                 + ((networkAddress == null) ? 0 : networkAddress.hashCode());
100         return result;
101     }
102
103     @Override
104     public boolean equals(Object obj) {
105         if (this == obj)
106             return true;
107         if (obj == null)
108             return false;
109         if (getClass() != obj.getClass())
110             return false;
111         Host other = (Host) obj;
112         if (dataLayerAddress == null) {
113             if (other.dataLayerAddress != null)
114                 return false;
115         } else if (!dataLayerAddress.equals(other.dataLayerAddress))
116             return false;
117         if (networkAddress == null) {
118             if (other.networkAddress != null)
119                 return false;
120         } else if (!networkAddress.equals(other.networkAddress))
121             return false;
122         return true;
123     }
124
125     @Override
126     public String toString() {
127         return "Host [dataLayerAddress=" + dataLayerAddress
128                 + ", networkAddress=" + networkAddress + "]";
129     }
130 }