3536d66090daf0a89bc15b7bda94b79c712ac216
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / ContextKey.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems and others.  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
9 package org.opendaylight.netconf.callhome.mount;
10
11 import com.google.common.base.Objects;
12 import com.google.common.base.Preconditions;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.InetSocketAddress;
17 import java.net.SocketAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
22
23 class ContextKey {
24
25     private final IpAddress address;
26     private final PortNumber port;
27
28     ContextKey(IpAddress address, PortNumber port) {
29         this.address = Preconditions.checkNotNull(address);
30         this.port = Preconditions.checkNotNull(port);
31     }
32
33     @Override
34     public int hashCode() {
35         final int prime = 31;
36         int result = 1;
37         result = prime * result + address.hashCode();
38         result = prime * result + port.hashCode();
39         return result;
40     }
41
42     @Override
43     public boolean equals(Object obj) {
44         if (this == obj) {
45             return true;
46         }
47         if (obj == null) {
48             return false;
49         }
50         if (getClass() != obj.getClass()) {
51             return false;
52         }
53         ContextKey other = (ContextKey) obj;
54         return Objects.equal(address, other.address) && Objects.equal(port, other.port);
55     }
56
57     IpAddress getIpAddress() {
58         return address;
59     }
60
61     PortNumber getPort() {
62         return port;
63     }
64
65     public static ContextKey from(NetconfNode node) {
66         return new ContextKey(node.getHost().getIpAddress(), node.getPort());
67     }
68
69     public static ContextKey from(SocketAddress remoteAddress) {
70         Preconditions.checkArgument(remoteAddress instanceof InetSocketAddress);
71         InetSocketAddress inetSocketAddr = (InetSocketAddress) remoteAddress;
72         InetAddress ipAddress = inetSocketAddr.getAddress();
73
74         final IpAddress yangIp;
75         if (ipAddress instanceof Inet4Address) {
76             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv4AddressFor(ipAddress));
77         } else {
78             Preconditions.checkArgument(ipAddress instanceof Inet6Address);
79             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv6AddressFor(ipAddress));
80         }
81         return new ContextKey(yangIp, new PortNumber(inetSocketAddr.getPort()));
82     }
83
84     @Override
85     public String toString() {
86         if (address.getIpv4Address() != null) {
87             return address.getIpv4Address().getValue() + ":" + port.getValue();
88         }
89         return address.getIpv6Address().getValue() + ":" + port.getValue();
90     }
91 }