Create extractUserCapabilities()
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / spi / NetconfNodeUtils.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.topology.spi;
9
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.netconf.listener.NetconfSessionPreferences;
15 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
16 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
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.node.topology.rev150114.NetconfNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability.CapabilityOrigin;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21
22 /**
23  * Utility methods to work with {@link NetconfNode} information.
24  */
25 public final class NetconfNodeUtils {
26     private NetconfNodeUtils() {
27         // Hidden on purpose
28     }
29
30     /**
31      * Create an {@link InetSocketAddress} targeting a particular {@link NetconfNode}.
32      *
33      * @param node A {@link NetconfNode}
34      * @return A {@link InetSocketAddress}
35      * @throws NullPointerException if {@code node} is {@code null}
36      */
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);
43     }
44
45     public static @NonNull RemoteDeviceId toRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
46         return new RemoteDeviceId(nodeId.getValue(), toInetSocketAddress(node));
47     }
48
49     /**
50      * Extract {@link UserPreferences} from na {@link NetconfNode}.
51      *
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
56      */
57     public static @Nullable UserPreferences extractUserCapabilities(final NetconfNode node) {
58         final var moduleCaps = node.getYangModuleCapabilities();
59         final var nonModuleCaps = node.getNonModuleCapabilities();
60
61         if (moduleCaps == null && nonModuleCaps == null) {
62             // if none of yang-module-capabilities or non-module-capabilities is specified
63             return null;
64         }
65
66         final var capabilities = new ArrayList<String>();
67         final boolean overrideYangModuleCaps;
68         if (moduleCaps != null) {
69             capabilities.addAll(moduleCaps.getCapability());
70             overrideYangModuleCaps = moduleCaps.getOverride();
71         } else {
72             overrideYangModuleCaps = false;
73         }
74
75         //non-module capabilities should not exist in yang module capabilities
76         final var sessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
77         final var nonModulePrefs = sessionPreferences.getNonModuleCaps();
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);
81         }
82
83         final boolean overrideNonModuleCaps;
84         if (nonModuleCaps != null) {
85             capabilities.addAll(nonModuleCaps.getCapability());
86             overrideNonModuleCaps = nonModuleCaps.getOverride();
87         } else {
88             overrideNonModuleCaps = false;
89         }
90
91         return new UserPreferences(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined),
92             overrideYangModuleCaps, overrideNonModuleCaps);
93     }
94 }