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.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
18 import java.net.InetSocketAddress;
19 import java.util.Optional;
20 import java.util.concurrent.TimeUnit;
21 import org.awaitility.Awaitility;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.Transaction;
30 import org.opendaylight.mdsal.binding.api.TransactionChain;
31 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
32 import org.opendaylight.mdsal.binding.api.WriteTransaction;
33 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
34 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
35 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
39 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.augment.test.rev160808.Node1;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
53 public class NetconfDeviceTopologyAdapterTest {
54 private static BindingRuntimeContext RUNTIME_CONTEXT;
56 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
59 private WriteTransaction writeTx;
61 private TransactionChain txChain;
63 private NetconfNode data;
65 private final String txIdent = "test transaction";
67 private final String sessionIdForReporting = "netconf-test-session1";
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 MockitoAnnotations.initMocks(this);
88 doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
89 doNothing().when(writeTx)
90 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
91 doNothing().when(writeTx)
92 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
94 doReturn(txIdent).when(writeTx).getIdentifier();
96 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
97 domDataBroker = customizer.getDOMDataBroker();
98 dataBroker = customizer.createDataBroker();
99 customizer.updateSchema(RUNTIME_CONTEXT);
101 transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
103 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
104 final Throwable cause) {
109 public void onTransactionChainSuccessful(final TransactionChain chain) {
117 public void testFailedDevice() throws Exception {
119 doReturn(emptyFluentFuture()).when(writeTx).commit();
120 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
121 adapter.setDeviceAsFailed(null);
123 verify(txChain, times(2)).newWriteOnlyTransaction();
124 verify(writeTx, times(1))
125 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
128 adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
129 adapter.setDeviceAsFailed(null);
131 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
132 Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
133 .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
134 .get(5, TimeUnit.SECONDS);
135 return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
136 == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
141 public void testDeviceUpdate() throws Exception {
142 doReturn(emptyFluentFuture()).when(writeTx).commit();
144 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
145 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
147 verify(txChain, times(2)).newWriteOnlyTransaction();
148 verify(writeTx, times(1))
149 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
150 verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
155 public void testDeviceAugmentedNodePresence() throws Exception {
157 Integer dataTestId = 474747;
159 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
161 QName netconfTestLeafQname = QName.create(
162 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
164 YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
165 .node(Topology.QNAME)
166 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
168 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
169 .node(netconfTestLeafQname).build();
171 NormalizedNode<?, ?> augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
172 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
174 DOMDataTreeWriteTransaction wtx = domDataBroker.newWriteOnlyTransaction();
175 wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
176 wtx.commit().get(5, TimeUnit.SECONDS);
178 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
179 Optional<NormalizedNode<?, ?>> testNode = domDataBroker.newReadOnlyTransaction()
180 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
182 assertEquals("Augmented node data should be still present after device update.", true, testNode.isPresent());
183 assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().getValue());
185 adapter.setDeviceAsFailed(null);
186 testNode = domDataBroker.newReadOnlyTransaction()
187 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
189 assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
190 assertEquals("Augmented data should be the same as before failed device.",
191 dataTestId, testNode.get().getValue());
195 public void testRemoveDeviceConfiguration() throws Exception {
196 doReturn(emptyFluentFuture()).when(writeTx).commit();
198 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
201 verify(txChain, times(2)).newWriteOnlyTransaction();
202 verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
203 verify(writeTx, times(2)).commit();