2 * Copyright (c) 2015 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.sal.connect.netconf.sal;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
19 import java.net.InetSocketAddress;
20 import java.util.Optional;
21 import java.util.concurrent.TimeUnit;
22 import org.awaitility.Awaitility;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.Transaction;
32 import org.opendaylight.mdsal.binding.api.TransactionChain;
33 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
34 import org.opendaylight.mdsal.binding.api.WriteTransaction;
35 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
36 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
37 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
41 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
42 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
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.NetconfNodeConnectionStatus;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.augment.test.rev160808.Node1;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class NetconfDeviceTopologyAdapterTest {
58 private static BindingRuntimeContext RUNTIME_CONTEXT;
60 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
63 private WriteTransaction writeTx;
65 private TransactionChain txChain;
67 private final String txIdent = "test transaction";
69 private TransactionChain transactionChain;
71 private DataBroker dataBroker;
73 private DOMDataBroker domDataBroker;
76 public static void beforeClass() {
77 RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
81 public static void afterClass() {
82 RUNTIME_CONTEXT = null;
86 public void setUp() throws Exception {
87 doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
88 doNothing().when(writeTx)
89 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
90 doNothing().when(writeTx)
91 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetworkTopology.class));
93 doReturn(txIdent).when(writeTx).getIdentifier();
95 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
96 domDataBroker = customizer.getDOMDataBroker();
97 dataBroker = customizer.createDataBroker();
98 customizer.updateSchema(RUNTIME_CONTEXT);
100 transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
102 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
103 final Throwable cause) {
108 public void onTransactionChainSuccessful(final TransactionChain chain) {
116 public void testFailedDevice() throws Exception {
118 doReturn(emptyFluentFuture()).when(writeTx).commit();
119 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
120 adapter.setDeviceAsFailed(null);
122 verify(txChain, times(2)).newWriteOnlyTransaction();
123 verify(writeTx, times(1))
124 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
127 adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
128 adapter.setDeviceAsFailed(null);
130 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
131 Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
132 .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
133 .get(5, TimeUnit.SECONDS);
134 return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
135 == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
140 public void testDeviceUpdate() throws Exception {
141 doReturn(emptyFluentFuture()).when(writeTx).commit();
143 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
144 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
146 verify(txChain, times(2)).newWriteOnlyTransaction();
147 verify(writeTx, times(1))
148 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
149 verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
154 public void testDeviceAugmentedNodePresence() throws Exception {
156 Integer dataTestId = 474747;
158 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
160 QName netconfTestLeafQname = QName.create(
161 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
163 YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
164 .node(Topology.QNAME)
165 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
167 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
168 .node(netconfTestLeafQname).build();
170 LeafNode augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
172 DOMDataTreeWriteTransaction wtx = domDataBroker.newWriteOnlyTransaction();
173 wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
174 wtx.commit().get(5, TimeUnit.SECONDS);
176 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
177 Optional<NormalizedNode> testNode = domDataBroker.newReadOnlyTransaction()
178 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
180 assertTrue("Augmented node data should be still present after device update.", testNode.isPresent());
181 assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().body());
183 adapter.setDeviceAsFailed(null);
184 testNode = domDataBroker.newReadOnlyTransaction()
185 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
187 assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
188 assertEquals("Augmented data should be the same as before failed device.",
189 dataTestId, testNode.get().body());
193 public void testRemoveDeviceConfiguration() throws Exception {
194 doReturn(emptyFluentFuture()).when(writeTx).commit();
196 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
199 verify(txChain, times(2)).newWriteOnlyTransaction();
200 verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
201 verify(writeTx, times(2)).commit();