Use base64 encoding for netconf device passwords
[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.rev231121.ConnectionOper.ConnectionStatus;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.oper.AvailableCapabilitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.oper.available.capabilities.AvailableCapability;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.oper.available.capabilities.AvailableCapabilityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.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;
47
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";
54
55     private static BindingRuntimeContext RUNTIME_CONTEXT =
56         BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
57
58     private final DataBroker dataBroker;
59     private final NetconfCommandsImpl netconfCommands;
60
61     NetconfCommandsImplTest() {
62         final var customizer = new ConcurrentDataBrokerTestCustomizer(true);
63         dataBroker = customizer.createDataBroker();
64         customizer.updateSchema(RUNTIME_CONTEXT);
65         netconfCommands = new NetconfCommandsImpl(dataBroker);
66     }
67
68     @Test
69     void testListDevice() throws Exception {
70         createTopology(LogicalDatastoreType.OPERATIONAL);
71
72         final var map = netconfCommands.listDevices();
73         // FIXME: WHAT?!
74         map.containsKey(NetconfConsoleConstants.NETCONF_ID);
75         assertTrue(map.containsKey(NODE_ID));
76
77         final var mapNode = map.get(NODE_ID);
78         assertBaseNodeAttributes(mapNode);
79     }
80
81     @Test
82     void testShowDevice() throws Exception {
83         createTopology(LogicalDatastoreType.OPERATIONAL);
84
85         final var mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
86         // FIXME: WHAT?!
87         mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
88         assertTrue(mapCorrect.containsKey(NODE_ID));
89
90         assertBaseNodeAttributesList(mapCorrect.get(NODE_ID));
91
92         final var mapWrongPort = netconfCommands.showDevice(IP, "1");
93         assertFalse(mapWrongPort.containsKey(NODE_ID));
94
95         final var mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
96         assertFalse(mapWrongIP.containsKey(NODE_ID));
97
98         final var mapId = netconfCommands.showDevice(NODE_ID);
99         assertTrue(mapId.containsKey(NODE_ID));
100         assertBaseNodeAttributesList(mapId.get(NODE_ID));
101     }
102
103     @Test
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"))))
108             .build();
109
110         createTopology(LogicalDatastoreType.CONFIGURATION);
111         netconfCommands.connectDevice(netconfNode, "netconf-ID");
112
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) {
118                 return false;
119             }
120
121             final var storedNode = nodes.stream()
122                 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
123                 .findFirst();
124
125             assertTrue(storedNode.isPresent());
126
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());
130             return true;
131         });
132
133         netconfCommands.disconnectDevice("netconf-ID");
134
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) {
140                 return false;
141             }
142
143             assertEquals(Optional.empty(), nodesDeleted.stream()
144                 .filter(node -> node.key().getNodeId().getValue().equals("netconf-ID"))
145                 .findFirst());
146             return true;
147         });
148     }
149
150     @Test
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);
155
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"));
160
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) {
166                 return false;
167             }
168
169             final var storedNode = nodes.stream()
170                 .filter(node -> node.key().getNodeId().getValue().equals(NODE_ID))
171                 .findFirst();
172             assertTrue(storedNode.isPresent());
173
174             final var storedNetconfNode = storedNode.orElseThrow().augmentation(NetconfNode.class);
175             assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
176             return true;
177         });
178     }
179
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))
186             .build();
187
188         final var writeTransaction = dataBroker.newWriteOnlyTransaction();
189         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
190         writeTransaction.commit().get(2, TimeUnit.SECONDS);
191     }
192
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")
205                         .build()))
206                     .build())
207                 .build())
208             .build();
209     }
210
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));
216
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));
221     }
222
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));
228
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));
233     }
234 }