2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
9 package org.opendaylight.netconf.callhome.mount;
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;
25 private final IpAddress address;
26 private final PortNumber port;
28 public ContextKey(IpAddress address, PortNumber port) {
29 this.address = Preconditions.checkNotNull(address);
30 this.port = Preconditions.checkNotNull(port);
34 public int hashCode() {
37 result = prime * result + address.hashCode();
38 result = prime * result + port.hashCode();
42 public boolean equals(Object obj) {
47 if (getClass() != obj.getClass())
49 ContextKey other = (ContextKey) obj;
50 return Objects.equal(address, other.address) && Objects.equal(port, other.port);
53 public static ContextKey from(NetconfNode node) {
54 return new ContextKey(node.getHost().getIpAddress(), node.getPort());
57 IpAddress getIpAddress() {
61 PortNumber getPort() {
65 public static ContextKey from(SocketAddress remoteAddress) {
66 Preconditions.checkArgument(remoteAddress instanceof InetSocketAddress);
67 InetSocketAddress inetSocketAddr = (InetSocketAddress) remoteAddress;
68 InetAddress ipAddress = inetSocketAddr.getAddress();
70 final IpAddress yangIp;
71 if(ipAddress instanceof Inet4Address) {
72 yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv4AddressFor(ipAddress));
74 Preconditions.checkArgument(ipAddress instanceof Inet6Address);
75 yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv6AddressFor(ipAddress));
77 return new ContextKey(yangIp, new PortNumber(inetSocketAddr.getPort()));
81 public String toString() {
82 if(address.getIpv4Address() != null) {
83 return address.getIpv4Address().getValue() + ":" + port.getValue();
85 return address.getIpv6Address().getValue() + ":" + port.getValue();