Bump upstreams for Silicon
[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 com.google.common.collect.ImmutableMap;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 import org.awaitility.Awaitility;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
35 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
36 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
39 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
40 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
60 import org.opendaylight.yangtools.yang.common.Uint16;
61
62 public class NetconfCommandsImplTest {
63
64     private static final String NODE_ID = "NodeID";
65     private static final String IP = "192.168.1.1";
66     private static final int PORT = 1234;
67     private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
68             NetconfNodeConnectionStatus.ConnectionStatus.Connected;
69     private static final String CAP_PREFIX = "prefix";
70
71     private static BindingRuntimeContext RUNTIME_CONTEXT;
72
73     private DataBroker dataBroker;
74     private NetconfCommandsImpl netconfCommands;
75
76     @BeforeClass
77     public static void beforeClass() {
78         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
79     }
80
81     @AfterClass
82     public static void afterClass() {
83         RUNTIME_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(RUNTIME_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 Collection<Node> nodes = topology.nonnullNode().values();
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 Collection<Node> nodesDeleted = topologyDeleted.nonnullNode().values();
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 Collection<Node> nodes = topology.nonnullNode().values();
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 Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
218
219         final Topology topology = new TopologyBuilder()
220                 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
221                 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
222                 .setNode(ImmutableMap.of(node.key(), node)).build();
223
224         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
225         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
226         writeTransaction.commit().get(2, TimeUnit.SECONDS);
227     }
228
229     private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
230             final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
231
232         final Host host = HostBuilder.getDefaultInstance(ip);
233         final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
234
235         final List<AvailableCapability> avCapList = new ArrayList<>();
236         avCapList.add(new AvailableCapabilityBuilder()
237                 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
238                 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1").build());
239         final AvailableCapabilities avCaps =
240                 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
241
242         final NodeId nodeId = new NodeId(nodeIdent);
243         final NodeKey nk = new NodeKey(nodeId);
244         return new NodeBuilder()
245                 .withKey(nk)
246                 .setNodeId(nodeId)
247                 .addAugmentation(new NetconfNodeBuilder()
248                     .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
249                 .build();
250     }
251
252     private static void assertBaseNodeAttributes(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(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
259         assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
260         assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
261         assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
262     }
263
264     private static void assertBaseNodeAttributesImmutableList(final Map<?, ?> mapNode) {
265         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
266         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
267         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
268         assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
269
270         assertEquals(ImmutableList.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
271         assertEquals(ImmutableList.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
272         assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
273         assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));
274     }
275 }