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 com.google.common.util.concurrent.MoreExecutors;
19 import java.util.ArrayList;
20 import java.util.EnumMap;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import javassist.ClassPool;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
34 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
37 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
38 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
39 import org.opendaylight.controller.sal.core.api.model.SchemaService;
40 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
41 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
42 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
43 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
44 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
45 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
46 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
47 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
48 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
49 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
50 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
70 import org.opendaylight.yangtools.concepts.ListenerRegistration;
71 import org.opendaylight.yangtools.yang.model.api.Module;
72 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
73 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
74 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
76 public class NetconfCommandsImplTest {
78 private static final String NODE_ID = "NodeID";
79 private static final String IP = "192.168.1.1";
80 private static final int PORT = 1234;
81 private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
82 NetconfNodeConnectionStatus.ConnectionStatus.Connected;
83 private static final String CAP_PREFIX = "prefix";
85 private DataBroker dataBroker;
86 private SchemaContext schemaContext;
87 private NetconfCommandsImpl netconfCommands;
90 public void setUp() throws Exception {
91 schemaContext = YangParserTestUtils.parseYangResources(NetconfCommandsImplTest.class,
92 "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
93 "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang");
94 schemaContext.getModules();
95 final SchemaService schemaService = createSchemaService();
97 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
98 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
100 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
101 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
102 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
104 final DOMDataBroker domDataBroker =
105 new SerializedDOMDataBroker(datastores, MoreExecutors.newDirectExecutorService());
107 final ClassPool pool = ClassPool.getDefault();
108 final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
109 final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
110 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
112 .onBindingRuntimeContextUpdated(BindingRuntimeContext.create(moduleInfoBackedContext, schemaContext));
114 final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
115 final BindingToNormalizedNodeCodec bindingToNormalized =
116 new BindingToNormalizedNodeCodec(loading, codecRegistry);
117 bindingToNormalized.onGlobalContextUpdated(schemaContext);
118 dataBroker = new BindingDOMDataBrokerAdapter(domDataBroker, bindingToNormalized);
120 netconfCommands = new NetconfCommandsImpl(dataBroker);
124 public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
125 createTopology(LogicalDatastoreType.OPERATIONAL);
127 final Map<?, ?> map = netconfCommands.listDevices();
128 map.containsKey(NetconfConsoleConstants.NETCONF_ID);
129 assertTrue(map.containsKey(NODE_ID));
131 final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
132 assertBaseNodeAttributes(mapNode);
136 public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
137 createTopology(LogicalDatastoreType.OPERATIONAL);
139 final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
140 mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
141 assertTrue(mapCorrect.containsKey(NODE_ID));
143 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
145 final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
146 assertFalse(mapWrongPort.containsKey(NODE_ID));
148 final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
149 assertFalse(mapWrongIP.containsKey(NODE_ID));
151 final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
152 assertTrue(mapId.containsKey(NODE_ID));
153 assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
157 public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
158 final NetconfNode netconfNode = new NetconfNodeBuilder()
159 .setPort(new PortNumber(7777)).setHost(HostBuilder.getDefaultInstance("10.10.1.1")).build();
161 createTopology(LogicalDatastoreType.CONFIGURATION);
162 netconfCommands.connectDevice(netconfNode, "netconf-ID");
163 NetconfConsoleUtils.waitForUpdate("10.10.1.1");
165 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
166 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
167 final List<Node> nodes = topology.getNode();
168 assertEquals(2, nodes.size());
170 final Optional<Node> storedNode = nodes.stream().filter(node ->
171 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
173 assertTrue(storedNode.isPresent());
175 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
176 assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
177 assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
179 netconfCommands.disconnectDevice("netconf-ID");
181 final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
182 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
183 final List<Node> nodesDeleted = topologyDeleted.getNode();
184 assertEquals(1, nodesDeleted.size());
186 final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
187 node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
189 assertFalse(storedNodeDeleted.isPresent());
193 public void testUpdateDevice() throws TimeoutException, InterruptedException, ExecutionException {
194 //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
195 createTopology(LogicalDatastoreType.OPERATIONAL);
196 createTopology(LogicalDatastoreType.CONFIGURATION);
198 final Map<String, String> update = new HashMap<>();
199 update.put(NetconfConsoleConstants.NETCONF_IP, "7.7.7.7");
200 update.put(NetconfConsoleConstants.TCP_ONLY, "true");
201 update.put(NetconfConsoleConstants.SCHEMALESS, "true");
203 netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
204 NetconfConsoleUtils.waitForUpdate("7.7.7.7");
206 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
207 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
208 final List<Node> nodes = topology.getNode();
209 assertEquals(1, nodes.size());
211 final Optional<Node> storedNode = nodes.stream().filter(node ->
212 node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
213 assertTrue(storedNode.isPresent());
215 NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
216 assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
220 public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException {
221 final List<Node> nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
222 assertNull(nodesNotExist);
223 createTopology(LogicalDatastoreType.OPERATIONAL);
224 final List<Node> nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
225 assertNotNull(nodes);
226 assertEquals(1, nodes.size());
229 private void createTopology(final LogicalDatastoreType dataStoreType)
230 throws TimeoutException, InterruptedException, ExecutionException {
231 final List<Node> nodes = new ArrayList<>();
232 final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
235 final Topology topology = new TopologyBuilder()
236 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
237 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName())).setNode(nodes).build();
239 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
240 writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
241 writeTransaction.commit().get(2, TimeUnit.SECONDS);
244 private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
245 final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
247 final Host host = HostBuilder.getDefaultInstance(ip);
248 final PortNumber port = new PortNumber(portNumber);
250 final List<AvailableCapability> avCapList = new ArrayList<>();
251 avCapList.add(new AvailableCapabilityBuilder()
252 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
253 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1").build());
254 final AvailableCapabilities avCaps =
255 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
257 final NetconfNode nn = new NetconfNodeBuilder().setConnectionStatus(cs).setHost(host).setPort(port)
258 .setAvailableCapabilities(avCaps).build();
259 final NodeId nodeId = new NodeId(nodeIdent);
260 final NodeKey nk = new NodeKey(nodeId);
261 final NodeBuilder nb = new NodeBuilder();
263 nb.setNodeId(nodeId);
264 nb.addAugmentation(NetconfNode.class, nn);
268 private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
269 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
270 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
271 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
272 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
274 assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
275 assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
276 assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
277 assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
280 private static void assertBaseNodeAttributesImmutableList(final Map<?, ?> mapNode) {
281 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
282 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
283 assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
284 assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
286 assertEquals(ImmutableList.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
287 assertEquals(ImmutableList.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
288 assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
289 assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));
292 private SchemaService createSchemaService() {
293 return new SchemaService() {
296 public void addModule(final Module module) {
300 public void removeModule(final Module module) {
305 public SchemaContext getSessionContext() {
306 return schemaContext;
310 public SchemaContext getGlobalContext() {
311 return schemaContext;
315 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
316 final SchemaContextListener listener) {
317 listener.onGlobalContextUpdated(getGlobalContext());
318 return new ListenerRegistration<SchemaContextListener>() {
320 public void close() {
325 public SchemaContextListener getInstance() {