Convert to using requireNonNull()
[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 package org.opendaylight.netconf.callhome.mount;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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     private final IpAddress address;
25     private final PortNumber port;
26
27     ContextKey(final IpAddress address, final PortNumber port) {
28         this.address = requireNonNull(address);
29         this.port = requireNonNull(port);
30     }
31
32     @Override
33     public int hashCode() {
34         final int prime = 31;
35         int result = 1;
36         result = prime * result + address.hashCode();
37         result = prime * result + port.hashCode();
38         return result;
39     }
40
41     @Override
42     public boolean equals(final Object obj) {
43         if (this == obj) {
44             return true;
45         }
46         if (obj == null) {
47             return false;
48         }
49         if (getClass() != obj.getClass()) {
50             return false;
51         }
52         ContextKey other = (ContextKey) obj;
53         return address.equals(other.address) && port.equals(other.port);
54     }
55
56     IpAddress getIpAddress() {
57         return address;
58     }
59
60     PortNumber getPort() {
61         return port;
62     }
63
64     public static ContextKey from(final NetconfNode node) {
65         return new ContextKey(node.getHost().getIpAddress(), node.getPort());
66     }
67
68     public static ContextKey from(final SocketAddress remoteAddress) {
69         checkArgument(remoteAddress instanceof InetSocketAddress);
70         InetSocketAddress inetSocketAddr = (InetSocketAddress) remoteAddress;
71         InetAddress ipAddress = inetSocketAddr.getAddress();
72
73         final IpAddress yangIp;
74         if (ipAddress instanceof Inet4Address) {
75             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv4AddressFor(ipAddress));
76         } else {
77             checkArgument(ipAddress instanceof Inet6Address);
78             yangIp = new IpAddress(IetfInetUtil.INSTANCE.ipv6AddressFor(ipAddress));
79         }
80         return new ContextKey(yangIp, new PortNumber(inetSocketAddr.getPort()));
81     }
82
83     @Override
84     public String toString() {
85         if (address.getIpv4Address() != null) {
86             return address.getIpv4Address().getValue() + ":" + port.getValue();
87         }
88         return address.getIpv6Address().getValue() + ":" + port.getValue();
89     }
90 }