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
9 package org.opendaylight.netconf.console.impl;
11 import static junit.framework.TestCase.assertFalse;
12 import static junit.framework.TestCase.assertNull;
13 import static junit.framework.TestCase.assertTrue;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
17 import com.google.common.collect.ImmutableList;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import org.awaitility.Awaitility;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.binding.test.ConcurrentDataBrokerTestCustomizer;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
34 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
35 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
58 public class NetconfCommandsImplTest {
60 private static final String NODE_ID = "NodeID";
61 private static final String IP = "192.168.1.1";
62 private static final int PORT = 1234;
63 private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
64 NetconfNodeConnectionStatus.ConnectionStatus.Connected;
65 private static final String CAP_PREFIX = "prefix";
67 private DataBroker dataBroker;
68 private SchemaContext schemaContext;
69 private NetconfCommandsImpl netconfCommands;
72 public void setUp() throws Exception {
73 schemaContext = YangParserTestUtils.parseYangResources(NetconfCommandsImplTest.class,
74 "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
75 "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang");
76 schemaContext.getModules();
78 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
79 dataBroker = customizer.createDataBroker();
80 customizer.updateSchema(schemaContext);
82 netconfCommands = new NetconfCommandsImpl(dataBroker);
86 public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
87 createTopology(LogicalDatastoreType.OPERATIONAL);
89 final Map<?, ?> map = netconfCommands.listDevices();
90 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
91 assertTrue(map.containsKey(NODE_ID));
93 final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
94 assertBaseNodeAttributes(mapNode);
98 public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
99 createTopology(LogicalDatastoreType.OPERATIONAL);
101 final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
102 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
103 assertTrue(mapCorrect.containsKey(NODE_ID));
105 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
107 final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
108 assertFalse(mapWrongPort.containsKey(NODE_ID));
110 final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
111 assertFalse(mapWrongIP.containsKey(NODE_ID));
113 final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
114 assertTrue(mapId.containsKey(NODE_ID));
115 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
119 public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
120 final NetconfNode netconfNode = new NetconfNodeBuilder()
121 .setPort(new PortNumber(7777)).setHost(HostBuilder.getDefaultInstance("10.10.1.1")).build();
123 createTopology(LogicalDatastoreType.CONFIGURATION);
124 netconfCommands.connectDevice(netconfNode, "netconf-ID");
126 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
127 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
128 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
129 final List<Node> nodes = topology.getNode();
130 if (nodes.size() != 2) {
134 final Optional<Node> storedNode = nodes.stream().filter(node ->
135 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
137 assertTrue(storedNode.isPresent());
139 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
140 assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
141 assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
145 netconfCommands.disconnectDevice("netconf-ID");
147 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
148 final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
149 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
150 final List<Node> nodesDeleted = topologyDeleted.getNode();
151 if (nodesDeleted.size() != 1) {
155 final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
156 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
158 assertFalse(storedNodeDeleted.isPresent());
164 public void testUpdateDevice() throws TimeoutException, InterruptedException, ExecutionException {
165 //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
166 createTopology(LogicalDatastoreType.OPERATIONAL);
167 createTopology(LogicalDatastoreType.CONFIGURATION);
169 final Map<String, String> update = new HashMap<>();
170 update.put(NetconfConsoleConstants.NETCONF_IP, "7.7.7.7");
171 update.put(NetconfConsoleConstants.TCP_ONLY, "true");
172 update.put(NetconfConsoleConstants.SCHEMALESS, "true");
174 netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
176 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
177 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
178 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
179 final List<Node> nodes = topology.getNode();
180 if (nodes.size() != 1) {
184 final Optional<Node> storedNode = nodes.stream().filter(node ->
185 node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
186 assertTrue(storedNode.isPresent());
188 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
189 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
195 public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException {
196 final List<Node> nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
197 assertNull(nodesNotExist);
198 createTopology(LogicalDatastoreType.OPERATIONAL);
199 final List<Node> nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
200 assertNotNull(nodes);
201 assertEquals(1, nodes.size());
204 private void createTopology(final LogicalDatastoreType dataStoreType)
205 throws TimeoutException, InterruptedException, ExecutionException {
206 final List<Node> nodes = new ArrayList<>();
207 final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
210 final Topology topology = new TopologyBuilder()
211 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
212 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName())).setNode(nodes).build();
214 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
215 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
216 writeTransaction.commit().get(2, TimeUnit.SECONDS);
219 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
220 final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
222 final Host host = HostBuilder.getDefaultInstance(ip);
223 final PortNumber port = new PortNumber(portNumber);
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();
232 final NetconfNode nn = new NetconfNodeBuilder().setConnectionStatus(cs).setHost(host).setPort(port)
233 .setAvailableCapabilities(avCaps).build();
234 final NodeId nodeId = new NodeId(nodeIdent);
235 final NodeKey nk = new NodeKey(nodeId);
236 final NodeBuilder nb = new NodeBuilder();
238 nb.setNodeId(nodeId);
239 nb.addAugmentation(NetconfNode.class, nn);
243 private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
244 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
245 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
246 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
247 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
249 assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
250 assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
251 assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
252 assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
255 private static void assertBaseNodeAttributesImmutableList(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));
261 assertEquals(ImmutableList.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
262 assertEquals(ImmutableList.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
263 assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
264 assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));