Remove RemoteDeviceId.getTopologyBindingPath()
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / util / RemoteDeviceId.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.sal.connect.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.net.InetSocketAddress;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.DomainName;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
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.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
30
31 public final class RemoteDeviceId {
32     // FIXME: extract all of this to users, as they are in control of topology-id
33     private static final String DEFAULT_TOPOLOGY_NAME = TopologyNetconf.QNAME.getLocalName();
34     private static final YangInstanceIdentifier DEFAULT_TOPOLOGY_NODE = YangInstanceIdentifier.builder()
35             .node(NetworkTopology.QNAME).node(Topology.QNAME)
36             .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), DEFAULT_TOPOLOGY_NAME)
37             .node(Node.QNAME)
38             .build();
39     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id").intern();
40
41     // FIXME: extract this into caller and pass to constructor
42     @VisibleForTesting
43     public static final KeyedInstanceIdentifier<Topology, TopologyKey> DEFAULT_TOPOLOGY_IID =
44         InstanceIdentifier.create(NetworkTopology.class)
45         .child(Topology.class, new TopologyKey(new TopologyId(DEFAULT_TOPOLOGY_NAME)));
46
47     private final @NonNull String name;
48     private final @NonNull YangInstanceIdentifier topologyPath;
49
50     private InetSocketAddress address;
51     private Host host;
52
53     private RemoteDeviceId(final String name) {
54         this.name = requireNonNull(name);
55         topologyPath = DEFAULT_TOPOLOGY_NODE.node(NodeIdentifierWithPredicates.of(Node.QNAME, NODE_ID_QNAME, name));
56     }
57
58     public RemoteDeviceId(final String name, final InetSocketAddress address) {
59         this(name);
60         this.address = address;
61
62         final var addr = address.getAddress();
63         host = addr != null ? new Host(IetfInetUtil.INSTANCE.ipAddressFor(addr))
64             : new Host(new DomainName(address.getHostString()));
65     }
66
67     public @NonNull String getName() {
68         return name;
69     }
70
71     public @NonNull YangInstanceIdentifier getTopologyPath() {
72         return topologyPath;
73     }
74
75     public InetSocketAddress getAddress() {
76         return address;
77     }
78
79     public Host getHost() {
80         return host;
81     }
82
83     @Override
84     public String toString() {
85         return "RemoteDevice{" + name + '}';
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         return this == obj || obj instanceof RemoteDeviceId other
91             && name.equals(other.name) && topologyPath.equals(other.topologyPath);
92     }
93
94     @Override
95     public int hashCode() {
96         return Objects.hash(name, topologyPath);
97     }
98 }