Merge topic 'checkstyle'
[controller.git] / opendaylight / adsal / hosttracker / api / src / main / java / org / opendaylight / controller / hosttracker / IPMacHostId.java
1 /*
2  * Copyright IBM Corporation, 2013.  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 package org.opendaylight.controller.hosttracker;
9
10 import java.io.Serializable;
11 import java.net.InetAddress;
12
13 /*
14  * IP + Mac key class implementation using the marker interface IHostId
15  * @author Deepak Udapudi
16  */
17
18 import org.opendaylight.controller.sal.packet.address.DataLinkAddress;
19
20 public class IPMacHostId implements IHostId, Serializable {
21
22     private static final long serialVersionUID = 1L;
23     private InetAddress ipAddress;
24     private DataLinkAddress macAddr;
25
26     public IPMacHostId(InetAddress ipAddress, DataLinkAddress macAddr) {
27         super();
28         this.ipAddress = ipAddress;
29         this.macAddr = macAddr;
30     }
31
32     public InetAddress getIpAddress() {
33         return ipAddress;
34     }
35
36     public void setIpAddress(InetAddress ipAddress) {
37         this.ipAddress = ipAddress;
38     }
39
40     public DataLinkAddress getMacAddr() {
41         return macAddr;
42     }
43
44     public void setMacAddr(DataLinkAddress macAddr) {
45         this.macAddr = macAddr;
46     }
47
48     @Override
49     public int hashCode() {
50         final int prime = 31;
51         int result = 1;
52         result = prime * result + ((ipAddress == null) ? 0 : ipAddress.hashCode());
53         result = prime * result + ((macAddr == null) ? 0 : macAddr.hashCode());
54         return result;
55     }
56
57     @Override
58     public boolean equals(Object obj) {
59         if (this == obj) {
60             return true;
61         }
62         if (obj == null) {
63             return false;
64         }
65         if (getClass() != obj.getClass()) {
66             return false;
67         }
68         IPMacHostId other = (IPMacHostId) obj;
69         if (ipAddress == null) {
70             if (other.ipAddress != null) {
71                 return false;
72             }
73         } else if (!ipAddress.equals(other.ipAddress)) {
74             return false;
75         }
76         if (macAddr == null) {
77             if (other.macAddr != null) {
78                 return false;
79             }
80         } else if (!macAddr.equals(other.macAddr)) {
81             return false;
82         }
83         return true;
84     }
85
86     public static IHostId fromIPAndMac(InetAddress ip, DataLinkAddress mac) {
87         return new IPMacHostId(ip, mac);
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see java.lang.Object#toString()
94      */
95     @Override
96     public String toString() {
97         StringBuilder builder = new StringBuilder();
98         builder.append("IP=[");
99         if (this.ipAddress != null) {
100             builder.append(this.ipAddress.getHostAddress());
101         }
102         builder.append("]")
103                .append("MAC=[");
104         if (this.macAddr != null) {
105             builder.append(this.macAddr.toString());
106         }
107         builder.append("]");
108         return (builder.toString());
109     }
110 }