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