f5490860d652cec6e171bc32d99f7228f2569bcc
[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.apache.commons.lang3.builder.HashCodeBuilder;
21 import org.apache.commons.lang3.builder.EqualsBuilder;
22 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
23 import org.opendaylight.controller.sal.packet.address.DataLinkAddress;
24
25 @XmlRootElement(name="host")
26 @XmlAccessorType(XmlAccessType.NONE)
27 public class Host implements Serializable {
28     private static final long serialVersionUID = 1L;
29     @XmlElement
30     private DataLinkAddress dataLayerAddress;
31     private InetAddress networkAddress;
32
33     public Host() {
34
35     }
36
37     /**
38      * Create an Host representation from the combination Data Link
39      * layer/Network layer address, both are needed to construct the
40      * object. Fake value can also be provided in case are not
41      * existent.
42      *
43      * @param dataLayerAddress Data Link Address for the host
44      * @param networkAddress Network Address for the host
45      *
46      * @return the constructed object
47      */
48     public Host(DataLinkAddress dataLayerAddress, InetAddress networkAddress)
49             throws ConstructionException {
50         if (dataLayerAddress == null) {
51             throw new ConstructionException("Passed null datalink address");
52         }
53         if (networkAddress == null) {
54             throw new ConstructionException("Passed null network address");
55         }
56         this.dataLayerAddress = dataLayerAddress;
57         this.networkAddress = networkAddress;
58     }
59
60     /**
61      * Copy constructor
62      *
63      * @param h Host to copy values from
64      *
65      * @return constructed copy
66      */
67     public Host(Host h) throws ConstructionException {
68         if (h == null) {
69             throw new ConstructionException("Passed null host");
70         }
71         this.dataLayerAddress = h.getDataLayerAddress();
72         this.networkAddress = h.getNetworkAddress();
73     }
74
75     /**
76      * @return the dataLayerAddress
77      */
78     public DataLinkAddress getDataLayerAddress() {
79         return this.dataLayerAddress;
80     }
81
82     /**
83      * @return the networkAddress
84      */
85     public InetAddress getNetworkAddress() {
86         return networkAddress;
87     }
88
89     @XmlElement(name = "networkAddress")
90     public String getNetworkAddressAsString() {
91         return networkAddress.getHostAddress();
92     }
93
94     @Override
95     public int hashCode() {
96         return HashCodeBuilder.reflectionHashCode(this);
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         return EqualsBuilder.reflectionEquals(this, obj);
102     }
103
104     @Override
105     public String toString() {
106         return "Host[" + ReflectionToStringBuilder.toString(this) + "]";
107     }
108 }