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