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.rev240611.ConnectionOper.ConnectionStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.connection.oper.AvailableCapabilitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.connection.oper.available.capabilities.AvailableCapability;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.connection.oper.available.capabilities.AvailableCapabilityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.LoginPwUnencryptedBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240611.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240611.NetconfNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240611.NetconfNodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev240611.network.topology.topology.topology.types.TopologyNetconf;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
47 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
48 import org.opendaylight.yangtools.yang.common.Uint16;
50 class NetconfCommandsImplTest {
51 private static final String NODE_ID = "NodeID";
52 private static final String IP = "192.168.1.1";
53 private static final int PORT = 1234;
54 private static final ConnectionStatus CONN_STATUS = ConnectionStatus.Connected;
55 private static final String CAP_PREFIX = "prefix";
57 private static BindingRuntimeContext RUNTIME_CONTEXT =
58 BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
60 private final DataBroker dataBroker;
61 private final NetconfCommandsImpl netconfCommands;
63 NetconfCommandsImplTest() {
64 final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
65 dataBroker = customizer.createDataBroker();
66 customizer.updateSchema(RUNTIME_CONTEXT);
67 netconfCommands = new NetconfCommandsImpl(dataBroker);
71 void testListDevice() throws Exception {
72 createTopology(LogicalDatastoreType.OPERATIONAL);
74 final var map = netconfCommands.listDevices();
76 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
77 assertTrue(map.containsKey(NODE_ID));
79 final var mapNode = map.get(NODE_ID);
80 assertBaseNodeAttributes(mapNode);
84 void testShowDevice() throws Exception {
85 createTopology(LogicalDatastoreType.OPERATIONAL);
87 final var mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
89 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
90 assertTrue(mapCorrect.containsKey(NODE_ID));
92 assertBaseNodeAttributesList(mapCorrect.get(NODE_ID));
94 final var mapWrongPort = netconfCommands.showDevice(IP, "1");
95 assertFalse(mapWrongPort.containsKey(NODE_ID));
97 final var mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
98 assertFalse(mapWrongIP.containsKey(NODE_ID));
100 final var mapId = netconfCommands.showDevice(NODE_ID);
101 assertTrue(mapId.containsKey(NODE_ID));
102 assertBaseNodeAttributesList(mapId.get(NODE_ID));
106 void testConnectDisconnectDevice() throws Exception {
107 final var netconfNode = new NetconfNodeBuilder()
108 .setPort(new PortNumber(Uint16.valueOf(7777)))
109 .setHost(new Host(new IpAddress(new Ipv4Address("10.10.1.1"))))
110 .setCredentials(new LoginPwUnencryptedBuilder()
111 .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
112 .setUsername("testuser")
113 .setPassword("testpassword")
118 createTopology(LogicalDatastoreType.CONFIGURATION);
119 netconfCommands.connectDevice(netconfNode, "netconf-ID");
121 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
122 final var topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
123 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
124 final var nodes = topology.nonnullNode().values();
125 if (nodes.size() != 2) {
129 final var storedNode = nodes.stream()
130 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
133 assertTrue(storedNode.isPresent());
135 final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
136 assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
137 assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
141 netconfCommands.disconnectDevice("netconf-ID");
143 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
144 final var topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
145 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
146 final var nodesDeleted = topologyDeleted.nonnullNode().values();
147 if (nodesDeleted.size() != 1) {
151 assertEquals(Optional.empty(), nodesDeleted.stream()
152 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
159 void testUpdateDevice() throws Exception {
160 //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
161 createTopology(LogicalDatastoreType.OPERATIONAL);
162 createTopology(LogicalDatastoreType.CONFIGURATION);
164 netconfCommands.updateDevice(NODE_ID, "admin", "admin", Map.of(
165 NetconfConsoleConstants.NETCONF_IP, "7.7.7.7",
166 NetconfConsoleConstants.TCP_ONLY, "true",
167 NetconfConsoleConstants.SCHEMALESS, "true"));
169 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
170 final var topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
171 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
172 final var nodes = topology.nonnullNode().values();
173 if (nodes.size() != 1) {
177 final var storedNode = nodes.stream()
178 .filter(node -> node.key().getNodeId().getValue().equals(NODE_ID))
180 assertTrue(storedNode.isPresent());
182 final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
183 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
188 private void createTopology(final LogicalDatastoreType dataStoreType) throws Exception {
189 final var node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
190 final var topology = new TopologyBuilder()
191 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
192 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
193 .setNode(BindingMap.of(node))
196 final var writeTransaction = dataBroker.newWriteOnlyTransaction();
197 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
198 writeTransaction.commit().get(2, TimeUnit.SECONDS);
201 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
202 final ConnectionStatus cs, final String notificationCapabilityPrefix) {
203 return new NodeBuilder()
204 .setNodeId(new NodeId(nodeIdent))
205 .addAugmentation(new NetconfNodeBuilder()
206 .setConnectionStatus(cs)
207 .setHost(new Host(new IpAddress(new Ipv4Address(ip))))
208 .setPort(new PortNumber(Uint16.valueOf(portNumber)))
209 .setAvailableCapabilities(new AvailableCapabilitiesBuilder()
210 .setAvailableCapability(List.of(new AvailableCapabilityBuilder()
211 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
212 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1")
219 private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
220 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
221 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
222 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
223 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
225 assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
226 assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
227 assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
228 assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
231 private static void assertBaseNodeAttributesList(final Map<?, ?> mapNode) {
232 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
233 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
234 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
235 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
237 assertEquals(List.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
238 assertEquals(List.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
239 assertEquals(List.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
240 assertEquals(List.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));