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
8 package org.opendaylight.netconf.callhome.mount;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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 import org.opendaylight.yangtools.yang.common.Uint16;
25 private final IpAddress address;
26 private final PortNumber port;
28 ContextKey(final IpAddress address, final PortNumber port) {
29 this.address = requireNonNull(address);
30 this.port = requireNonNull(port);
34 public int hashCode() {
37 result = prime * result + address.hashCode();
38 result = prime * result + port.hashCode();
43 public boolean equals(final Object obj) {
50 if (getClass() != obj.getClass()) {
53 ContextKey other = (ContextKey) obj;
54 return address.equals(other.address) && port.equals(other.port);
57 IpAddress getIpAddress() {
61 PortNumber getPort() {
65 public static ContextKey from(final NetconfNode node) {
66 return new ContextKey(node.getHost().getIpAddress(), node.getPort());
69 public static ContextKey from(final SocketAddress remoteAddress) {
70 checkArgument(remoteAddress instanceof InetSocketAddress);
71 InetSocketAddress inetSocketAddr = (InetSocketAddress) remoteAddress;
72 InetAddress ipAddress = inetSocketAddr.getAddress();
74 final IpAddress yangIp;
75 if (ipAddress instanceof Inet4Address) {
76 yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv4AddressFor(ipAddress));
78 checkArgument(ipAddress instanceof Inet6Address);
79 yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv6AddressFor(ipAddress));
81 return new ContextKey(yangIp, new PortNumber(Uint16.valueOf(inetSocketAddr.getPort())));
85 public String toString() {
86 if (address.getIpv4Address() != null) {
87 return address.getIpv4Address().getValue() + ":" + port.getValue();
89 return address.getIpv6Address().getValue() + ":" + port.getValue();