Bug 2192 - Part 3 - Make ovsdb library not depend on ovsdb.utils.config
[ovsdb.git] / library / src / main / java / org / opendaylight / ovsdb / lib / OvsdbConnectionInfo.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.lib;
12
13 import io.netty.channel.Channel;
14
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
20 import javax.xml.bind.annotation.XmlTransient;
21
22 @XmlRootElement(name="Connection")
23 public class OvsdbConnectionInfo {
24     public enum ConnectionType {
25         ACTIVE, PASSIVE
26     }
27
28     @XmlTransient
29     private Channel channel;
30     @XmlTransient
31     private ConnectionType type;
32
33     public OvsdbConnectionInfo(Channel channel, ConnectionType type) {
34         this.channel = channel;
35         this.type = type;
36     }
37
38     @XmlElement(name="remoteAddress")
39     public InetAddress getRemoteAddress() {
40         return ((InetSocketAddress)channel.remoteAddress()).getAddress();
41     }
42     @XmlElement(name="remotePort")
43     public int getRemotePort() {
44         return ((InetSocketAddress)channel.remoteAddress()).getPort();
45     }
46     @XmlElement(name="localAddress")
47     public InetAddress getLocalAddress() {
48         return ((InetSocketAddress)channel.localAddress()).getAddress();
49     }
50     @XmlElement(name="localPort")
51     public int getLocalPort() {
52         return ((InetSocketAddress)channel.localAddress()).getPort();
53     }
54     @XmlElement(name="connectionType")
55     public ConnectionType getType() {
56         return type;
57     }
58
59     @Override
60     public int hashCode() {
61         final int prime = 31;
62         int result = 1;
63         result = prime * result + ((channel == null) ? 0 : getRemoteAddress().hashCode());
64         result = prime * result + ((type == null) ? 0 : getRemotePort());
65         result = prime * result + ((channel == null) ? 0 : getLocalAddress().hashCode());
66         result = prime * result + ((type == null) ? 0 : getLocalPort());
67         return result;
68     }
69
70     @Override
71     public boolean equals(Object obj) {
72         if (this == obj)
73             return true;
74         if (obj == null)
75             return false;
76         if (getClass() != obj.getClass())
77             return false;
78         OvsdbConnectionInfo other = (OvsdbConnectionInfo) obj;
79         if (channel == null) {
80             if (other.channel != null)
81                 return false;
82         } else if (!getRemoteAddress().equals(other.getRemoteAddress())) {
83             return false;
84         } else if (!getLocalAddress().equals(other.getLocalAddress())) {
85             return false;
86         } else if (getRemotePort() != other.getRemotePort()) {
87             return false;
88         } else if (getLocalPort() != other.getLocalPort()) {
89             return false;
90         }
91         if (type != other.type)
92             return false;
93         return true;
94     }
95
96     @Override
97     public String toString() {
98         return "ConnectionInfo [Remote-address=" + this.getRemoteAddress().getHostAddress() +
99                              ", Remote-port=" + this.getRemotePort() +
100                              ", Local-address" + this.getLocalAddress().getHostAddress() +
101                              ", Local-port=" + this.getLocalPort() +
102                              ", type=" + type + "]";
103     }
104 }