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 com.google.common.base.Optional;
19 import java.net.InetSocketAddress;
20 import java.util.concurrent.TimeUnit;
21 import org.awaitility.Awaitility;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.binding.test.ConcurrentDataBrokerTestCustomizer;
30 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
35 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
36 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
37 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
51 public class NetconfDeviceTopologyAdapterTest {
53 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
56 private WriteTransaction writeTx;
58 private BindingTransactionChain txChain;
60 private NetconfNode data;
62 private final String txIdent = "test transaction";
64 private SchemaContext schemaContext = null;
65 private final String sessionIdForReporting = "netconf-test-session1";
67 private BindingTransactionChain transactionChain;
69 private DataBroker dataBroker;
71 private DOMDataBroker domDataBroker;
74 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
76 doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
77 doNothing().when(writeTx)
78 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
79 doNothing().when(writeTx)
80 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
82 doReturn(txIdent).when(writeTx).getIdentifier();
84 this.schemaContext = YangParserTestUtils.parseYangResources(NetconfDeviceTopologyAdapterTest.class,
85 "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
86 "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang",
87 "/schemas/network-topology-augment-test@2016-08-08.yang");
88 schemaContext.getModules();
90 ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
91 domDataBroker = customizer.getDOMDataBroker();
92 dataBroker = customizer.createDataBroker();
93 customizer.updateSchema(schemaContext);
95 transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
97 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
98 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
103 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
111 public void testFailedDevice() throws Exception {
113 doReturn(emptyFluentFuture()).when(writeTx).commit();
114 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
115 adapter.setDeviceAsFailed(null);
117 verify(txChain, times(2)).newWriteOnlyTransaction();
118 verify(writeTx, times(1))
119 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
122 adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
123 adapter.setDeviceAsFailed(null);
125 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
126 Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
127 .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
128 .checkedGet(5, TimeUnit.SECONDS);
129 return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
130 == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
135 public void testDeviceUpdate() throws Exception {
136 doReturn(emptyFluentFuture()).when(writeTx).commit();
138 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
139 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
141 verify(txChain, times(2)).newWriteOnlyTransaction();
142 verify(writeTx, times(1))
143 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
144 verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
149 public void testDeviceAugmentedNodePresence() throws Exception {
151 Integer dataTestId = 474747;
153 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
155 QName netconfTestLeafQname = QName.create(
156 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
158 YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
159 .node(Topology.QNAME)
160 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
162 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
163 .node(netconfTestLeafQname).build();
165 NormalizedNode<?, ?> augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
166 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
168 DOMDataWriteTransaction wtx = domDataBroker.newWriteOnlyTransaction();
169 wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
170 wtx.commit().get(5, TimeUnit.SECONDS);
172 adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
173 Optional<NormalizedNode<?, ?>> testNode = domDataBroker.newReadOnlyTransaction()
174 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
176 assertEquals("Augmented node data should be still present after device update.", true, testNode.isPresent());
177 assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().getValue());
179 adapter.setDeviceAsFailed(null);
180 testNode = domDataBroker.newReadOnlyTransaction()
181 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
183 assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
184 assertEquals("Augmented data should be the same as before failed device.",
185 dataTestId, testNode.get().getValue());
189 public void testRemoveDeviceConfiguration() throws Exception {
190 doReturn(emptyFluentFuture()).when(writeTx).commit();
192 NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
195 verify(txChain, times(2)).newWriteOnlyTransaction();
196 verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
197 verify(writeTx, times(2)).commit();