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.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
38 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52 public class NetconfDeviceTopologyAdapterTest {
53 private static SchemaContext SCHEMA_CONTEXT;
55 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
58 private WriteTransaction writeTx;
60 private TransactionChain txChain;
62 private NetconfNode data;
64 private final String txIdent = "test transaction";
66 private final String sessionIdForReporting = "netconf-test-session1";
68 private TransactionChain transactionChain;
70 private DataBroker dataBroker;
72 private DOMDataBroker domDataBroker;
75 public static void beforeClass() {
76 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResources(NetconfDeviceTopologyAdapterTest.class,
77 "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
78 "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang",
79 "/schemas/network-topology-augment-test@2016-08-08.yang");
83 public static void afterClass() {
84 SCHEMA_CONTEXT = null;
88 public void setUp() throws Exception {
89 MockitoAnnotations.initMocks(this);
90 doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
91 doNothing().when(writeTx)
92 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
93 doNothing().when(writeTx)
94 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
96 doReturn(txIdent).when(writeTx).getIdentifier();
98 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
99 domDataBroker = customizer.getDOMDataBroker();
100 dataBroker = customizer.createDataBroker();
101 customizer.updateSchema(SCHEMA_CONTEXT);
103 transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
105 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
106 final Throwable cause) {
111 public void onTransactionChainSuccessful(final TransactionChain chain) {
119 public void testFailedDevice() throws Exception {
121 doReturn(emptyFluentFuture()).when(writeTx).commit();
122 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
123 adapter.setDeviceAsFailed(null);
125 verify(txChain, times(2)).newWriteOnlyTransaction();
126 verify(writeTx, times(1))
127 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
130 adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
131 adapter.setDeviceAsFailed(null);
133 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
134 Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
135 .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
136 .get(5, TimeUnit.SECONDS);
137 return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
138 == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
143 public void testDeviceUpdate() throws Exception {
144 doReturn(emptyFluentFuture()).when(writeTx).commit();
146 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
147 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
149 verify(txChain, times(2)).newWriteOnlyTransaction();
150 verify(writeTx, times(1))
151 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
152 verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
157 public void testDeviceAugmentedNodePresence() throws Exception {
159 Integer dataTestId = 474747;
161 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
163 QName netconfTestLeafQname = QName.create(
164 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
166 YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
167 .node(Topology.QNAME)
168 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
170 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
171 .node(netconfTestLeafQname).build();
173 NormalizedNode<?, ?> augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
174 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
176 DOMDataTreeWriteTransaction wtx = domDataBroker.newWriteOnlyTransaction();
177 wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
178 wtx.commit().get(5, TimeUnit.SECONDS);
180 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
181 Optional<NormalizedNode<?, ?>> testNode = domDataBroker.newReadOnlyTransaction()
182 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
184 assertEquals("Augmented node data should be still present after device update.", true, testNode.isPresent());
185 assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().getValue());
187 adapter.setDeviceAsFailed(null);
188 testNode = domDataBroker.newReadOnlyTransaction()
189 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
191 assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
192 assertEquals("Augmented data should be the same as before failed device.",
193 dataTestId, testNode.get().getValue());
197 public void testRemoveDeviceConfiguration() throws Exception {
198 doReturn(emptyFluentFuture()).when(writeTx).commit();
200 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
203 verify(txChain, times(2)).newWriteOnlyTransaction();
204 verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
205 verify(writeTx, times(2)).commit();