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.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;
61 public class NetconfCommandsImplTest {
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";
70 private static BindingRuntimeContext RUNTIME_CONTEXT;
72 private DataBroker dataBroker;
73 private NetconfCommandsImpl netconfCommands;
76 public static void beforeClass() {
77 RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
81 public static void afterClass() {
82 RUNTIME_CONTEXT = null;
86 public void setUp() throws Exception {
87 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
88 dataBroker = customizer.createDataBroker();
89 customizer.updateSchema(RUNTIME_CONTEXT);
91 netconfCommands = new NetconfCommandsImpl(dataBroker);
95 public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
96 createTopology(LogicalDatastoreType.OPERATIONAL);
98 final Map<?, ?> map = netconfCommands.listDevices();
99 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
100 assertTrue(map.containsKey(NODE_ID));
102 final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
103 assertBaseNodeAttributes(mapNode);
107 public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
108 createTopology(LogicalDatastoreType.OPERATIONAL);
110 final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
111 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
112 assertTrue(mapCorrect.containsKey(NODE_ID));
114 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
116 final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
117 assertFalse(mapWrongPort.containsKey(NODE_ID));
119 final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
120 assertFalse(mapWrongIP.containsKey(NODE_ID));
122 final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
123 assertTrue(mapId.containsKey(NODE_ID));
124 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
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"))))
134 createTopology(LogicalDatastoreType.CONFIGURATION);
135 netconfCommands.connectDevice(netconfNode, "netconf-ID");
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) {
145 final Optional<Node> storedNode = nodes.stream().filter(node ->
146 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
148 assertTrue(storedNode.isPresent());
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());
156 netconfCommands.disconnectDevice("netconf-ID");
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) {
166 final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
167 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
169 assertFalse(storedNodeDeleted.isPresent());
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);
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");
185 netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
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) {
195 final Optional<Node> storedNode = nodes.stream().filter(node ->
196 node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
197 assertTrue(storedNode.isPresent());
199 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
200 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
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);
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();
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 = new Host(new IpAddress(new Ipv4Address(ip)));
223 final PortNumber port = new PortNumber(Uint16.valueOf(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 NodeId nodeId = new NodeId(nodeIdent);
233 final NodeKey nk = new NodeKey(nodeId);
234 return new NodeBuilder()
237 .addAugmentation(new NetconfNodeBuilder()
238 .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
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));
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));
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));
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));