Merge "BUG-1422 Introduce Call-Home functionality for the NETCONF Topology."
[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     public 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     @Override
42     public boolean equals(Object obj) {
43         if (this == obj)
44             return true;
45         if (obj == null)
46             return false;
47         if (getClass() != obj.getClass())
48             return false;
49         ContextKey other = (ContextKey) obj;
50         return Objects.equal(address, other.address) && Objects.equal(port, other.port);
51     }
52
53     public static ContextKey from(NetconfNode node) {
54         return new ContextKey(node.getHost().getIpAddress(), node.getPort());
55     }
56
57     IpAddress getIpAddress() {
58         return address;
59     }
60
61     PortNumber getPort() {
62         return port;
63     }
64
65     public static ContextKey from(SocketAddress remoteAddress) {
66         Preconditions.checkArgument(remoteAddress instanceof InetSocketAddress);
67         InetSocketAddress inetSocketAddr = (InetSocketAddress) remoteAddress;
68         InetAddress ipAddress = inetSocketAddr.getAddress();
69
70         final IpAddress yangIp;
71         if(ipAddress instanceof Inet4Address) {
72             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv4AddressFor(ipAddress));
73         } else {
74             Preconditions.checkArgument(ipAddress instanceof Inet6Address);
75             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv6AddressFor(ipAddress));
76         }
77         return new ContextKey(yangIp, new PortNumber(inetSocketAddr.getPort()));
78     }
79
80     @Override
81     public String toString() {
82         if(address.getIpv4Address() != null) {
83             return address.getIpv4Address().getValue() + ":" + port.getValue();
84         }
85         return address.getIpv6Address().getValue() + ":" + port.getValue();
86     }
87 }