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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
23 * Utility methods to work with {@link NetconfNode} information.
25 public final class NetconfNodeUtils {
26 private NetconfNodeUtils() {
31 * Create an {@link InetSocketAddress} targeting a particular {@link NetconfNode}.
33 * @param node A {@link NetconfNode}
34 * @return A {@link InetSocketAddress}
35 * @throws NullPointerException if {@code node} is {@code null}
37 public static @NonNull InetSocketAddress toInetSocketAddress(final NetconfNode node) {
38 final var host = node.requireHost();
39 final int port = node.requirePort().getValue().toJava();
40 final var ipAddress = host.getIpAddress();
41 return ipAddress != null ? new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressFor(ipAddress), port)
42 : new InetSocketAddress(host.getDomainName().getValue(), port);
45 public static @NonNull RemoteDeviceId toRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
46 return new RemoteDeviceId(nodeId.getValue(), toInetSocketAddress(node));
50 * Extract {@link UserPreferences} from na {@link NetconfNode}.
52 * @param node A {@link NetconfNode}
53 * @return {@link UserPreferences}, potentially {@code null}
54 * @throws NullPointerException if {@code node} is {@code null}
55 * @throws IllegalArgumentException there are any non-module capabilities
57 public static @Nullable UserPreferences extractUserCapabilities(final NetconfNode node) {
58 final var moduleCaps = node.getYangModuleCapabilities();
59 final var nonModuleCaps = node.getNonModuleCapabilities();
61 if (moduleCaps == null && nonModuleCaps == null) {
62 // if none of yang-module-capabilities or non-module-capabilities is specified
66 final var capabilities = new ArrayList<String>();
67 final boolean overrideYangModuleCaps;
68 if (moduleCaps != null) {
69 capabilities.addAll(moduleCaps.getCapability());
70 overrideYangModuleCaps = moduleCaps.getOverride();
72 overrideYangModuleCaps = false;
75 //non-module capabilities should not exist in yang module capabilities
76 final var sessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
77 final var nonModulePrefs = sessionPreferences.nonModuleCaps();
78 if (!nonModulePrefs.isEmpty()) {
79 throw new IllegalArgumentException("List yang-module-capabilities/capability should contain only module "
80 + "based capabilities. Non-module capabilities used: " + nonModulePrefs);
83 final boolean overrideNonModuleCaps;
84 if (nonModuleCaps != null) {
85 capabilities.addAll(nonModuleCaps.getCapability());
86 overrideNonModuleCaps = nonModuleCaps.getOverride();
88 overrideNonModuleCaps = false;
91 return new UserPreferences(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined),
92 overrideYangModuleCaps, overrideNonModuleCaps);