Bump upstream versions
[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.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
59 import org.opendaylight.yangtools.yang.common.Uint16;
60
61 public class NetconfCommandsImplTest {
62
63     private static final String NODE_ID = "NodeID";
64     private static final String IP = "192.168.1.1";
65     private static final int PORT = 1234;
66     private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
67             NetconfNodeConnectionStatus.ConnectionStatus.Connected;
68     private static final String CAP_PREFIX = "prefix";
69
70     private static BindingRuntimeContext RUNTIME_CONTEXT;
71
72     private DataBroker dataBroker;
73     private NetconfCommandsImpl netconfCommands;
74
75     @BeforeClass
76     public static void beforeClass() {
77         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
78     }
79
80     @AfterClass
81     public static void afterClass() {
82         RUNTIME_CONTEXT = null;
83     }
84
85     @Before
86     public void setUp() throws Exception {
87         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
88         dataBroker = customizer.createDataBroker();
89         customizer.updateSchema(RUNTIME_CONTEXT);
90
91         netconfCommands = new NetconfCommandsImpl(dataBroker);
92     }
93
94     @Test
95     public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
96         createTopology(LogicalDatastoreType.OPERATIONAL);
97
98         final Map<?, ?> map = netconfCommands.listDevices();
99         map.containsKey(NetconfConsoleConstants.NETCONF_ID);
100         assertTrue(map.containsKey(NODE_ID));
101
102         final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
103         assertBaseNodeAttributes(mapNode);
104     }
105
106     @Test
107     public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
108         createTopology(LogicalDatastoreType.OPERATIONAL);
109
110         final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
111         mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
112         assertTrue(mapCorrect.containsKey(NODE_ID));
113
114         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
115
116         final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
117         assertFalse(mapWrongPort.containsKey(NODE_ID));
118
119         final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
120         assertFalse(mapWrongIP.containsKey(NODE_ID));
121
122         final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
123         assertTrue(mapId.containsKey(NODE_ID));
124         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
125     }
126
127     @Test
128     public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
129         final NetconfNode netconfNode = new NetconfNodeBuilder()
130                 .setPort(new PortNumber(Uint16.valueOf(7777)))
131                 .setHost(new Host(new IpAddress(new Ipv4Address("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     private void createTopology(final LogicalDatastoreType dataStoreType)
206             throws TimeoutException, InterruptedException, ExecutionException {
207         final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
208
209         final Topology topology = new TopologyBuilder()
210                 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
211                 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
212                 .setNode(ImmutableMap.of(node.key(), node)).build();
213
214         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
215         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
216         writeTransaction.commit().get(2, TimeUnit.SECONDS);
217     }
218
219     private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
220             final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
221
222         final Host host = new Host(new IpAddress(new Ipv4Address(ip)));
223         final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
224
225         final List<AvailableCapability> avCapList = new ArrayList<>();
226         avCapList.add(new AvailableCapabilityBuilder()
227                 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
228                 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1").build());
229         final AvailableCapabilities avCaps =
230                 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
231
232         final NodeId nodeId = new NodeId(nodeIdent);
233         final NodeKey nk = new NodeKey(nodeId);
234         return new NodeBuilder()
235                 .withKey(nk)
236                 .setNodeId(nodeId)
237                 .addAugmentation(new NetconfNodeBuilder()
238                     .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
239                 .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 }