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