Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / 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 junit.framework.TestCase.assertFalse;
11 import static junit.framework.TestCase.assertNull;
12 import static junit.framework.TestCase.assertTrue;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import com.google.common.collect.ImmutableList;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.awaitility.Awaitility;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
33 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
34 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
56
57 public class NetconfCommandsImplTest {
58
59     private static final String NODE_ID = "NodeID";
60     private static final String IP = "192.168.1.1";
61     private static final int PORT = 1234;
62     private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
63             NetconfNodeConnectionStatus.ConnectionStatus.Connected;
64     private static final String CAP_PREFIX = "prefix";
65
66     private DataBroker dataBroker;
67     private SchemaContext schemaContext;
68     private NetconfCommandsImpl netconfCommands;
69
70     @Before
71     public void setUp() throws Exception {
72         schemaContext = YangParserTestUtils.parseYangResources(NetconfCommandsImplTest.class,
73             "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
74             "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang");
75         schemaContext.getModules();
76
77         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
78         dataBroker = customizer.createDataBroker();
79         customizer.updateSchema(schemaContext);
80
81         netconfCommands = new NetconfCommandsImpl(dataBroker);
82     }
83
84     @Test
85     public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
86         createTopology(LogicalDatastoreType.OPERATIONAL);
87
88         final Map<?, ?> map = netconfCommands.listDevices();
89         map.containsKey(NetconfConsoleConstants.NETCONF_ID);
90         assertTrue(map.containsKey(NODE_ID));
91
92         final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
93         assertBaseNodeAttributes(mapNode);
94     }
95
96     @Test
97     public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
98         createTopology(LogicalDatastoreType.OPERATIONAL);
99
100         final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
101         mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
102         assertTrue(mapCorrect.containsKey(NODE_ID));
103
104         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
105
106         final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
107         assertFalse(mapWrongPort.containsKey(NODE_ID));
108
109         final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
110         assertFalse(mapWrongIP.containsKey(NODE_ID));
111
112         final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
113         assertTrue(mapId.containsKey(NODE_ID));
114         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
115     }
116
117     @Test
118     public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
119         final NetconfNode netconfNode = new NetconfNodeBuilder()
120                 .setPort(new PortNumber(7777)).setHost(HostBuilder.getDefaultInstance("10.10.1.1")).build();
121
122         createTopology(LogicalDatastoreType.CONFIGURATION);
123         netconfCommands.connectDevice(netconfNode, "netconf-ID");
124
125         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
126             final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
127                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
128             final List<Node> nodes = topology.getNode();
129             if (nodes.size() != 2) {
130                 return false;
131             }
132
133             final Optional<Node> storedNode = nodes.stream().filter(node ->
134                     node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
135
136             assertTrue(storedNode.isPresent());
137
138             NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
139             assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
140             assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
141             return true;
142         });
143
144         netconfCommands.disconnectDevice("netconf-ID");
145
146         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
147             final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
148                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
149             final List<Node> nodesDeleted = topologyDeleted.getNode();
150             if (nodesDeleted.size() != 1) {
151                 return false;
152             }
153
154             final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
155                     node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
156
157             assertFalse(storedNodeDeleted.isPresent());
158             return true;
159         });
160     }
161
162     @Test
163     public void testUpdateDevice() throws TimeoutException, InterruptedException, ExecutionException {
164         //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
165         createTopology(LogicalDatastoreType.OPERATIONAL);
166         createTopology(LogicalDatastoreType.CONFIGURATION);
167
168         final Map<String, String> update = new HashMap<>();
169         update.put(NetconfConsoleConstants.NETCONF_IP, "7.7.7.7");
170         update.put(NetconfConsoleConstants.TCP_ONLY, "true");
171         update.put(NetconfConsoleConstants.SCHEMALESS, "true");
172
173         netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
174
175         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
176             final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
177                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
178             final List<Node> nodes = topology.getNode();
179             if (nodes.size() != 1) {
180                 return false;
181             }
182
183             final Optional<Node> storedNode = nodes.stream().filter(node ->
184                     node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
185             assertTrue(storedNode.isPresent());
186
187             NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
188             assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
189             return true;
190         });
191     }
192
193     @Test
194     public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException {
195         final List<Node> nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
196         assertNull(nodesNotExist);
197         createTopology(LogicalDatastoreType.OPERATIONAL);
198         final List<Node> nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
199         assertNotNull(nodes);
200         assertEquals(1, nodes.size());
201     }
202
203     private void createTopology(final LogicalDatastoreType dataStoreType)
204             throws TimeoutException, InterruptedException, ExecutionException {
205         final List<Node> nodes = new ArrayList<>();
206         final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
207         nodes.add(node);
208
209         final Topology topology = new TopologyBuilder()
210                 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
211                 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName())).setNode(nodes).build();
212
213         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
214         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
215         writeTransaction.commit().get(2, TimeUnit.SECONDS);
216     }
217
218     private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
219             final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
220
221         final Host host = HostBuilder.getDefaultInstance(ip);
222         final PortNumber port = new PortNumber(portNumber);
223
224         final List<AvailableCapability> avCapList = new ArrayList<>();
225         avCapList.add(new AvailableCapabilityBuilder()
226                 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
227                 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1").build());
228         final AvailableCapabilities avCaps =
229                 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
230
231         final NetconfNode nn = new NetconfNodeBuilder().setConnectionStatus(cs).setHost(host).setPort(port)
232                 .setAvailableCapabilities(avCaps).build();
233         final NodeId nodeId = new NodeId(nodeIdent);
234         final NodeKey nk = new NodeKey(nodeId);
235         final NodeBuilder nb = new NodeBuilder();
236         nb.withKey(nk);
237         nb.setNodeId(nodeId);
238         nb.addAugmentation(NetconfNode.class, nn);
239         return nb.build();
240     }
241
242     private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
243         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
244         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
245         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
246         assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
247
248         assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
249         assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
250         assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
251         assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
252     }
253
254     private static void assertBaseNodeAttributesImmutableList(final Map<?, ?> mapNode) {
255         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
256         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
257         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
258         assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
259
260         assertEquals(ImmutableList.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
261         assertEquals(ImmutableList.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
262         assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
263         assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));
264     }
265 }