2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.console.impl;
10 import static junit.framework.TestCase.assertFalse;
11 import static junit.framework.TestCase.assertTrue;
12 import static org.junit.Assert.assertEquals;
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;
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;
60 public class NetconfCommandsImplTest {
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";
69 private static BindingRuntimeContext RUNTIME_CONTEXT;
71 private DataBroker dataBroker;
72 private NetconfCommandsImpl netconfCommands;
75 public static void beforeClass() {
76 RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
80 public static void afterClass() {
81 RUNTIME_CONTEXT = null;
85 public void setUp() throws Exception {
86 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
87 dataBroker = customizer.createDataBroker();
88 customizer.updateSchema(RUNTIME_CONTEXT);
90 netconfCommands = new NetconfCommandsImpl(dataBroker);
94 public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
95 createTopology(LogicalDatastoreType.OPERATIONAL);
97 final Map<?, ?> map = netconfCommands.listDevices();
98 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
99 assertTrue(map.containsKey(NODE_ID));
101 final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
102 assertBaseNodeAttributes(mapNode);
106 public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
107 createTopology(LogicalDatastoreType.OPERATIONAL);
109 final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
110 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
111 assertTrue(mapCorrect.containsKey(NODE_ID));
113 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
115 final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
116 assertFalse(mapWrongPort.containsKey(NODE_ID));
118 final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
119 assertFalse(mapWrongIP.containsKey(NODE_ID));
121 final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
122 assertTrue(mapId.containsKey(NODE_ID));
123 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
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"))
132 createTopology(LogicalDatastoreType.CONFIGURATION);
133 netconfCommands.connectDevice(netconfNode, "netconf-ID");
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) {
143 final Optional<Node> storedNode = nodes.stream().filter(node ->
144 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
146 assertTrue(storedNode.isPresent());
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());
154 netconfCommands.disconnectDevice("netconf-ID");
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) {
164 final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
165 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
167 assertFalse(storedNodeDeleted.isPresent());
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);
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");
183 netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
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) {
193 final Optional<Node> storedNode = nodes.stream().filter(node ->
194 node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
195 assertTrue(storedNode.isPresent());
197 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
198 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
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);
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();
212 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
213 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
214 writeTransaction.commit().get(2, TimeUnit.SECONDS);
217 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
218 final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
220 final Host host = HostBuilder.getDefaultInstance(ip);
221 final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
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();
230 final NodeId nodeId = new NodeId(nodeIdent);
231 final NodeKey nk = new NodeKey(nodeId);
232 return new NodeBuilder()
235 .addAugmentation(new NetconfNodeBuilder()
236 .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
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));
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));
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));
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));