Bump upstreams
[netconf.git] / apps / 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.client.mdsal.UserPreferences;
15 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
16 import org.opendaylight.netconf.client.mdsal.api.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.device.rev230430.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;
32
33 /**
34  * Utility methods to work with {@link NetconfNode} information.
35  */
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();
40
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)));
46
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)
52         .node(Node.QNAME)
53         .build();
54
55     private NetconfNodeUtils() {
56         // Hidden on purpose
57     }
58
59     /**
60      * Create an {@link InetSocketAddress} targeting a particular {@link NetconfNode}.
61      *
62      * @param node A {@link NetconfNode}
63      * @return A {@link InetSocketAddress}
64      * @throws NullPointerException if {@code node} is {@code null}
65      */
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.inetAddressFor(ipAddress), port)
71             : new InetSocketAddress(host.getDomainName().getValue(), port);
72     }
73
74     public static @NonNull RemoteDeviceId toRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
75         return new RemoteDeviceId(nodeId.getValue(), toInetSocketAddress(node));
76     }
77
78     /**
79      * Extract {@link UserPreferences} from na {@link NetconfNode}.
80      *
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
85      */
86     public static @Nullable UserPreferences extractUserCapabilities(final NetconfNode node) {
87         final var moduleCaps = node.getYangModuleCapabilities();
88         final var nonModuleCaps = node.getNonModuleCapabilities();
89
90         if (moduleCaps == null && nonModuleCaps == null) {
91             // if none of yang-module-capabilities or non-module-capabilities is specified
92             return null;
93         }
94
95         final var capabilities = new ArrayList<String>();
96         final boolean overrideYangModuleCaps;
97         if (moduleCaps != null) {
98             capabilities.addAll(moduleCaps.getCapability());
99             overrideYangModuleCaps = moduleCaps.getOverride();
100         } else {
101             overrideYangModuleCaps = false;
102         }
103
104         //non-module capabilities should not exist in yang module capabilities
105         final var sessionPreferences = NetconfSessionPreferences.fromStrings(capabilities,
106             CapabilityOrigin.DeviceAdvertised, node.getSessionId());
107         final var nonModulePrefs = sessionPreferences.nonModuleCaps();
108         if (!nonModulePrefs.isEmpty()) {
109             throw new IllegalArgumentException("List yang-module-capabilities/capability should contain only module "
110                 + "based capabilities. Non-module capabilities used: " + nonModulePrefs);
111         }
112
113         final boolean overrideNonModuleCaps;
114         if (nonModuleCaps != null) {
115             capabilities.addAll(nonModuleCaps.getCapability());
116             overrideNonModuleCaps = nonModuleCaps.getOverride();
117         } else {
118             overrideNonModuleCaps = false;
119         }
120
121         // FIXME: UserPreferences is constructor parameter of NetconfDeviceCommunicator and NetconfSessionPreferences
122         // are created in NetconfDeviceCommunicator#onSessionUp from session. What are we doing here?
123         // IMO we should rework UserPreferences and NetconfSessionPreferences and this method.
124         return new UserPreferences(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined,
125                 node.getSessionId()), overrideYangModuleCaps, overrideNonModuleCaps);
126     }
127
128     @Deprecated(forRemoval = true)
129     public static @NonNull YangInstanceIdentifier defaultTopologyMountPath(final RemoteDeviceId id) {
130         return DEFAULT_TOPOLOGY_NODE.node(NodeIdentifierWithPredicates.of(Node.QNAME, NODE_ID_QNAME, id.name()));
131     }
132 }