2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.topology.spi;
10 import java.net.InetSocketAddress;
11 import java.util.ArrayList;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
15 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
16 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.available.capabilities.AvailableCapability.CapabilityOrigin;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 * Utility methods to work with {@link NetconfNode} information.
36 public final class NetconfNodeUtils {
37 // FIXME: extract all of this to users, as they are in control of topology-id
38 @Deprecated(forRemoval = true)
39 public static final String DEFAULT_TOPOLOGY_NAME = TopologyNetconf.QNAME.getLocalName();
41 // FIXME: extract this into caller and pass to constructor
42 @Deprecated(forRemoval = true)
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)));
47 private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id").intern();
48 // FIXME: push this out to callers
49 private static final YangInstanceIdentifier DEFAULT_TOPOLOGY_NODE = YangInstanceIdentifier.builder()
50 .node(NetworkTopology.QNAME).node(Topology.QNAME)
51 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), DEFAULT_TOPOLOGY_NAME)
55 private NetconfNodeUtils() {
60 * Create an {@link InetSocketAddress} targeting a particular {@link NetconfNode}.
62 * @param node A {@link NetconfNode}
63 * @return A {@link InetSocketAddress}
64 * @throws NullPointerException if {@code node} is {@code null}
66 public static @NonNull InetSocketAddress toInetSocketAddress(final NetconfNode node) {
67 final var host = node.requireHost();
68 final int port = node.requirePort().getValue().toJava();
69 final var ipAddress = host.getIpAddress();
70 return ipAddress != null ? new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressFor(ipAddress), port)
71 : new InetSocketAddress(host.getDomainName().getValue(), port);
74 public static @NonNull RemoteDeviceId toRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
75 return new RemoteDeviceId(nodeId.getValue(), toInetSocketAddress(node));
79 * Extract {@link UserPreferences} from na {@link NetconfNode}.
81 * @param node A {@link NetconfNode}
82 * @return {@link UserPreferences}, potentially {@code null}
83 * @throws NullPointerException if {@code node} is {@code null}
84 * @throws IllegalArgumentException there are any non-module capabilities
86 public static @Nullable UserPreferences extractUserCapabilities(final NetconfNode node) {
87 final var moduleCaps = node.getYangModuleCapabilities();
88 final var nonModuleCaps = node.getNonModuleCapabilities();
90 if (moduleCaps == null && nonModuleCaps == null) {
91 // if none of yang-module-capabilities or non-module-capabilities is specified
95 final var capabilities = new ArrayList<String>();
96 final boolean overrideYangModuleCaps;
97 if (moduleCaps != null) {
98 capabilities.addAll(moduleCaps.getCapability());
99 overrideYangModuleCaps = moduleCaps.getOverride();
101 overrideYangModuleCaps = false;
104 //non-module capabilities should not exist in yang module capabilities
105 final var sessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
106 final var nonModulePrefs = sessionPreferences.nonModuleCaps();
107 if (!nonModulePrefs.isEmpty()) {
108 throw new IllegalArgumentException("List yang-module-capabilities/capability should contain only module "
109 + "based capabilities. Non-module capabilities used: " + nonModulePrefs);
112 final boolean overrideNonModuleCaps;
113 if (nonModuleCaps != null) {
114 capabilities.addAll(nonModuleCaps.getCapability());
115 overrideNonModuleCaps = nonModuleCaps.getOverride();
117 overrideNonModuleCaps = false;
120 return new UserPreferences(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined),
121 overrideYangModuleCaps, overrideNonModuleCaps);
124 @Deprecated(forRemoval = true)
125 public static @NonNull YangInstanceIdentifier defaultTopologyMountPath(final RemoteDeviceId id) {
126 return DEFAULT_TOPOLOGY_NODE.node(NodeIdentifierWithPredicates.of(Node.QNAME, NODE_ID_QNAME, id.name()));