2 * Copyright (c) 2016 Cisco Systems, Inc. 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.console.impl;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.TimeUnit;
18 import org.awaitility.Awaitility;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
22 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
23 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
26 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
27 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.ConnectionOper.ConnectionStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.connection.oper.AvailableCapabilitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.connection.oper.available.capabilities.AvailableCapability;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.connection.oper.available.capabilities.AvailableCapabilityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
45 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
46 import org.opendaylight.yangtools.yang.common.Uint16;
48 class NetconfCommandsImplTest {
49 private static final String NODE_ID = "NodeID";
50 private static final String IP = "192.168.1.1";
51 private static final int PORT = 1234;
52 private static final ConnectionStatus CONN_STATUS = ConnectionStatus.Connected;
53 private static final String CAP_PREFIX = "prefix";
55 private static BindingRuntimeContext RUNTIME_CONTEXT =
56 BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
58 private final DataBroker dataBroker;
59 private final NetconfCommandsImpl netconfCommands;
61 NetconfCommandsImplTest() {
62 final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
63 dataBroker = customizer.createDataBroker();
64 customizer.updateSchema(RUNTIME_CONTEXT);
65 netconfCommands = new NetconfCommandsImpl(dataBroker);
69 void testListDevice() throws Exception {
70 createTopology(LogicalDatastoreType.OPERATIONAL);
72 final var map = netconfCommands.listDevices();
74 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
75 assertTrue(map.containsKey(NODE_ID));
77 final var mapNode = map.get(NODE_ID);
78 assertBaseNodeAttributes(mapNode);
82 void testShowDevice() throws Exception {
83 createTopology(LogicalDatastoreType.OPERATIONAL);
85 final var mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
87 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
88 assertTrue(mapCorrect.containsKey(NODE_ID));
90 assertBaseNodeAttributesList(mapCorrect.get(NODE_ID));
92 final var mapWrongPort = netconfCommands.showDevice(IP, "1");
93 assertFalse(mapWrongPort.containsKey(NODE_ID));
95 final var mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
96 assertFalse(mapWrongIP.containsKey(NODE_ID));
98 final var mapId = netconfCommands.showDevice(NODE_ID);
99 assertTrue(mapId.containsKey(NODE_ID));
100 assertBaseNodeAttributesList(mapId.get(NODE_ID));
104 void testConnectDisconnectDevice() throws Exception {
105 final var netconfNode = new NetconfNodeBuilder()
106 .setPort(new PortNumber(Uint16.valueOf(7777)))
107 .setHost(new Host(new IpAddress(new Ipv4Address("10.10.1.1"))))
110 createTopology(LogicalDatastoreType.CONFIGURATION);
111 netconfCommands.connectDevice(netconfNode, "netconf-ID");
113 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
114 final var topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
115 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
116 final var nodes = topology.nonnullNode().values();
117 if (nodes.size() != 2) {
121 final var storedNode = nodes.stream()
122 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
125 assertTrue(storedNode.isPresent());
127 final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
128 assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
129 assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
133 netconfCommands.disconnectDevice("netconf-ID");
135 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
136 final var topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
137 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
138 final var nodesDeleted = topologyDeleted.nonnullNode().values();
139 if (nodesDeleted.size() != 1) {
143 assertEquals(Optional.empty(), nodesDeleted.stream()
144 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
151 void testUpdateDevice() throws Exception {
152 //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
153 createTopology(LogicalDatastoreType.OPERATIONAL);
154 createTopology(LogicalDatastoreType.CONFIGURATION);
156 netconfCommands.updateDevice(NODE_ID, "admin", "admin", Map.of(
157 NetconfConsoleConstants.NETCONF_IP, "7.7.7.7",
158 NetconfConsoleConstants.TCP_ONLY, "true",
159 NetconfConsoleConstants.SCHEMALESS, "true"));
161 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
162 final var topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
163 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
164 final var nodes = topology.nonnullNode().values();
165 if (nodes.size() != 1) {
169 final var storedNode = nodes.stream()
170 .filter(node -> node.key().getNodeId().getValue().equals(NODE_ID))
172 assertTrue(storedNode.isPresent());
174 final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
175 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
180 private void createTopology(final LogicalDatastoreType dataStoreType) throws Exception {
181 final var node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
182 final var topology = new TopologyBuilder()
183 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
184 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
185 .setNode(BindingMap.of(node))
188 final var writeTransaction = dataBroker.newWriteOnlyTransaction();
189 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
190 writeTransaction.commit().get(2, TimeUnit.SECONDS);
193 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
194 final ConnectionStatus cs, final String notificationCapabilityPrefix) {
195 return new NodeBuilder()
196 .setNodeId(new NodeId(nodeIdent))
197 .addAugmentation(new NetconfNodeBuilder()
198 .setConnectionStatus(cs)
199 .setHost(new Host(new IpAddress(new Ipv4Address(ip))))
200 .setPort(new PortNumber(Uint16.valueOf(portNumber)))
201 .setAvailableCapabilities(new AvailableCapabilitiesBuilder()
202 .setAvailableCapability(List.of(new AvailableCapabilityBuilder()
203 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
204 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1")
211 private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
212 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
213 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
214 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
215 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
217 assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
218 assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
219 assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
220 assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
223 private static void assertBaseNodeAttributesList(final Map<?, ?> mapNode) {
224 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
225 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
226 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
227 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
229 assertEquals(List.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
230 assertEquals(List.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
231 assertEquals(List.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
232 assertEquals(List.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));