Merge "Bug 1327 : Fixing a few Connection handling issues" into topic/schema
[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 public class OvsdbConnectionInfo {
18     public enum ConnectionType {
19         ACTIVE, PASSIVE
20     }
21
22     private Channel channel;
23     private ConnectionType type;
24
25     public OvsdbConnectionInfo(Channel channel, ConnectionType type) {
26         this.channel = channel;
27         this.type = type;
28     }
29
30     public InetAddress getRemoteAddress() {
31         return ((InetSocketAddress)channel.remoteAddress()).getAddress();
32     }
33     public int getRemotePort() {
34         return ((InetSocketAddress)channel.remoteAddress()).getPort();
35     }
36
37     public InetAddress getLocalAddress() {
38         return ((InetSocketAddress)channel.localAddress()).getAddress();
39     }
40     public int getLocalPort() {
41         return ((InetSocketAddress)channel.localAddress()).getPort();
42     }
43
44     public ConnectionType getType() {
45         return type;
46     }
47
48     @Override
49     public int hashCode() {
50         final int prime = 31;
51         int result = 1;
52         result = prime * result + ((channel == null) ? 0 : getRemoteAddress().hashCode());
53         result = prime * result + ((type == null) ? 0 : getRemotePort());
54         result = prime * result + ((channel == null) ? 0 : getLocalAddress().hashCode());
55         result = prime * result + ((type == null) ? 0 : getLocalPort());
56         return result;
57     }
58
59     @Override
60     public boolean equals(Object obj) {
61         if (this == obj)
62             return true;
63         if (obj == null)
64             return false;
65         if (getClass() != obj.getClass())
66             return false;
67         OvsdbConnectionInfo other = (OvsdbConnectionInfo) obj;
68         if (channel == null) {
69             if (other.channel != null)
70                 return false;
71         } else if (!getRemoteAddress().equals(other.getRemoteAddress())) {
72             return false;
73         } else if (!getLocalAddress().equals(other.getLocalAddress())) {
74             return false;
75         } else if (getRemotePort() != other.getRemotePort()) {
76             return false;
77         } else if (getLocalPort() != other.getLocalPort()) {
78             return false;
79         }
80         if (type != other.type)
81             return false;
82         return true;
83     }
84
85     @Override
86     public String toString() {
87         return "ConnectionInfo [Remote-address=" + this.getRemoteAddress().getHostAddress() +
88                              ", Remote-port=" + this.getRemotePort() +
89                              ", Local-address" + this.getLocalAddress().getHostAddress() +
90                              ", Local-port=" + this.getLocalPort() +
91                              ", type=" + type + "]";
92     }
93 }