Prevent NPE for Credentials
[netconf.git] / apps / netconf-console / src / test / java / org / opendaylight / netconf / console / impl / NetconfCommandsImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.console.impl;
9
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;
13
14 import java.util.List;
15 import java.util.Map;
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;
49
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";
56
57     private static BindingRuntimeContext RUNTIME_CONTEXT =
58         BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
59
60     private final DataBroker dataBroker;
61     private final NetconfCommandsImpl netconfCommands;
62
63     NetconfCommandsImplTest() {
64         final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
65         dataBroker = customizer.createDataBroker();
66         customizer.updateSchema(RUNTIME_CONTEXT);
67         netconfCommands = new NetconfCommandsImpl(dataBroker);
68     }
69
70     @Test
71     void testListDevice() throws Exception {
72         createTopology(LogicalDatastoreType.OPERATIONAL);
73
74         final var map = netconfCommands.listDevices();
75         // FIXME: WHAT?!
76         map.containsKey(NetconfConsoleConstants.NETCONF_ID);
77         assertTrue(map.containsKey(NODE_ID));
78
79         final var mapNode = map.get(NODE_ID);
80         assertBaseNodeAttributes(mapNode);
81     }
82
83     @Test
84     void testShowDevice() throws Exception {
85         createTopology(LogicalDatastoreType.OPERATIONAL);
86
87         final var mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
88         // FIXME: WHAT?!
89         mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
90         assertTrue(mapCorrect.containsKey(NODE_ID));
91
92         assertBaseNodeAttributesList(mapCorrect.get(NODE_ID));
93
94         final var mapWrongPort = netconfCommands.showDevice(IP, "1");
95         assertFalse(mapWrongPort.containsKey(NODE_ID));
96
97         final var mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
98         assertFalse(mapWrongIP.containsKey(NODE_ID));
99
100         final var mapId = netconfCommands.showDevice(NODE_ID);
101         assertTrue(mapId.containsKey(NODE_ID));
102         assertBaseNodeAttributesList(mapId.get(NODE_ID));
103     }
104
105     @Test
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")
114                     .build())
115                 .build())
116             .build();
117
118         createTopology(LogicalDatastoreType.CONFIGURATION);
119         netconfCommands.connectDevice(netconfNode, "netconf-ID");
120
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) {
126                 return false;
127             }
128
129             final var storedNode = nodes.stream()
130                 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
131                 .findFirst();
132
133             assertTrue(storedNode.isPresent());
134
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());
138             return true;
139         });
140
141         netconfCommands.disconnectDevice("netconf-ID");
142
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) {
148                 return false;
149             }
150
151             assertEquals(Optional.empty(), nodesDeleted.stream()
152                 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
153                 .findFirst());
154             return true;
155         });
156     }
157
158     @Test
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);
163
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"));
168
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) {
174                 return false;
175             }
176
177             final var storedNode = nodes.stream()
178                 .filter(node -> node.key().getNodeId().getValue().equals(NODE_ID))
179                 .findFirst();
180             assertTrue(storedNode.isPresent());
181
182             final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
183             assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
184             return true;
185         });
186     }
187
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))
194             .build();
195
196         final var writeTransaction = dataBroker.newWriteOnlyTransaction();
197         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
198         writeTransaction.commit().get(2, TimeUnit.SECONDS);
199     }
200
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")
213                         .build()))
214                     .build())
215                 .build())
216             .build();
217     }
218
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));
224
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));
229     }
230
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));
236
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));
241     }
242 }