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