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