39b4f9bf9ca1bd388ac6b2ac24aedac99710b5de
[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 java.net.InetSocketAddress;
13 import java.util.Objects;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29
30 public final class RemoteDeviceId {
31     private static final String DEFAULT_TOPOLOGY_NAME = TopologyNetconf.QNAME.getLocalName();
32     private static final KeyedInstanceIdentifier<Topology, TopologyKey> DEFAULT_TOPOLOGY_IID =
33             InstanceIdentifier.create(NetworkTopology.class)
34             .child(Topology.class, new TopologyKey(new TopologyId(DEFAULT_TOPOLOGY_NAME)));
35     private static final YangInstanceIdentifier DEFAULT_TOPOLOGY_NODE = YangInstanceIdentifier.builder()
36             .node(NetworkTopology.QNAME).node(Topology.QNAME)
37             .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), DEFAULT_TOPOLOGY_NAME)
38             .node(Node.QNAME)
39             .build();
40     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id").intern();
41
42     private final String name;
43     private final NodeKey key;
44     private final YangInstanceIdentifier topologyPath;
45     private final KeyedInstanceIdentifier<Node, NodeKey> topologyBindingPath;
46     private InetSocketAddress address;
47     private Host host;
48
49     private RemoteDeviceId(final String name) {
50         this.name = requireNonNull(name);
51         this.topologyPath = DEFAULT_TOPOLOGY_NODE
52                 .node(NodeIdentifierWithPredicates.of(Node.QNAME, NODE_ID_QNAME, name));
53         this.key = new NodeKey(new NodeId(name));
54         this.topologyBindingPath = DEFAULT_TOPOLOGY_IID.child(Node.class, key);
55     }
56
57     public RemoteDeviceId(final String name, final InetSocketAddress address) {
58         this(name);
59         this.address = address;
60         this.host = buildHost();
61     }
62
63     private Host buildHost() {
64         return HostBuilder.getDefaultInstance(address.getHostString());
65     }
66
67     public String getName() {
68         return name;
69     }
70
71     public NodeKey getBindingKey() {
72         return key;
73     }
74
75     public InstanceIdentifier<Node> getTopologyBindingPath() {
76         return topologyBindingPath;
77     }
78
79     public YangInstanceIdentifier getTopologyPath() {
80         return topologyPath;
81     }
82
83     public InetSocketAddress getAddress() {
84         return address;
85     }
86
87     public Host getHost() {
88         return host;
89     }
90
91     @Override
92     public String toString() {
93         return "RemoteDevice{" + name + '}';
94     }
95
96     @Override
97     public boolean equals(final Object obj) {
98         if (this == obj) {
99             return true;
100         }
101         if (!(obj instanceof RemoteDeviceId)) {
102             return false;
103         }
104         final RemoteDeviceId that = (RemoteDeviceId) obj;
105         return name.equals(that.name) && topologyBindingPath.equals(that.topologyBindingPath);
106     }
107
108     @Override
109     public int hashCode() {
110         return Objects.hash(name, topologyBindingPath);
111     }
112 }