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.assertNull;
12 import static junit.framework.TestCase.assertTrue;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
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;
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;
62 public class NetconfCommandsImplTest {
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";
71 private static EffectiveModelContext SCHEMA_CONTEXT;
73 private DataBroker dataBroker;
74 private NetconfCommandsImpl netconfCommands;
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");
84 public static void afterClass() {
85 SCHEMA_CONTEXT = null;
89 public void setUp() throws Exception {
90 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
91 dataBroker = customizer.createDataBroker();
92 customizer.updateSchema(SCHEMA_CONTEXT);
94 netconfCommands = new NetconfCommandsImpl(dataBroker);
98 public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
99 createTopology(LogicalDatastoreType.OPERATIONAL);
101 final Map<?, ?> map = netconfCommands.listDevices();
102 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
103 assertTrue(map.containsKey(NODE_ID));
105 final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
106 assertBaseNodeAttributes(mapNode);
110 public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
111 createTopology(LogicalDatastoreType.OPERATIONAL);
113 final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
114 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
115 assertTrue(mapCorrect.containsKey(NODE_ID));
117 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
119 final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
120 assertFalse(mapWrongPort.containsKey(NODE_ID));
122 final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
123 assertFalse(mapWrongIP.containsKey(NODE_ID));
125 final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
126 assertTrue(mapId.containsKey(NODE_ID));
127 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
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"))
136 createTopology(LogicalDatastoreType.CONFIGURATION);
137 netconfCommands.connectDevice(netconfNode, "netconf-ID");
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) {
147 final Optional<Node> storedNode = nodes.stream().filter(node ->
148 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
150 assertTrue(storedNode.isPresent());
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());
158 netconfCommands.disconnectDevice("netconf-ID");
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) {
168 final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
169 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
171 assertFalse(storedNodeDeleted.isPresent());
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);
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");
187 netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
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) {
197 final Optional<Node> storedNode = nodes.stream().filter(node ->
198 node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
199 assertTrue(storedNode.isPresent());
201 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
202 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
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());
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);
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();
226 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
227 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
228 writeTransaction.commit().get(2, TimeUnit.SECONDS);
231 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
232 final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
234 final Host host = HostBuilder.getDefaultInstance(ip);
235 final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
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();
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();
250 nb.setNodeId(nodeId);
251 nb.addAugmentation(NetconfNode.class, nn);
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));
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));
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));
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));