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.topology.spi;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import com.google.common.util.concurrent.Futures;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
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.TransactionChain;
32 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.common.api.CommitInfo;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCapabilities;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.Empty;
48 import org.opendaylight.yangtools.yang.common.Uint32;
50 @RunWith(MockitoJUnitRunner.StrictStubs.class)
51 public class NetconfDeviceTopologyAdapterTest {
52 private static final KeyedInstanceIdentifier<Topology, TopologyKey> TEST_TOPOLOGY_ID =
53 // FIXME: do not use this constant
54 NetconfNodeUtils.DEFAULT_TOPOLOGY_IID;
55 private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
58 private DataBroker mockBroker;
60 private TransactionChain mockChain;
62 private WriteTransaction mockTx;
64 private ArgumentCaptor<TransactionChainListener> listeners;
66 private NetconfDeviceTopologyAdapter adapter;
69 public void before() {
70 doReturn(mockTx).when(mockChain).newWriteOnlyTransaction();
72 doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
74 doReturn("test transaction").when(mockTx).getIdentifier();
75 doReturn(CommitInfo.emptyFluentFuture()).when(mockTx).commit();
77 doReturn(mockChain).when(mockBroker).createMergingTransactionChain(listeners.capture());
78 adapter = new NetconfDeviceTopologyAdapter(mockBroker, TEST_TOPOLOGY_ID, id);
82 public void replaceChainIfFailed() {
83 doNothing().when(mockChain).close();
84 doReturn("mockChain").when(mockChain).toString();
85 adapter.onTransactionChainFailed(mockChain, mockTx, new Exception("chain failed"));
86 verify(mockBroker, times(2)).createMergingTransactionChain(any());
90 public void testFailedDevice() {
92 doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
93 any(NetconfNode.class));
95 adapter.setDeviceAsFailed(null);
97 verify(mockChain, times(2)).newWriteOnlyTransaction();
98 // FIXME: LogicationDataStoreStype, concrete identifier, concreate (or captured/asserted) data
99 verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
104 public void testDeviceUpdate() throws Exception {
105 // FIXME: exact match
106 doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
107 any(NetconfNode.class));
108 adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty(), new SessionIdType(Uint32.ONE));
110 verify(mockChain, times(2)).newWriteOnlyTransaction();
111 verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
112 verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
116 public void testRemoveDeviceConfiguration() throws Exception {
117 // FIXME: exact match
118 doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
119 doNothing().when(mockChain).close();
121 final var future = adapter.shutdown();
122 verify(mockChain, times(2)).newWriteOnlyTransaction();
123 verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
124 TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
125 verify(mockTx, times(2)).commit();
126 verify(mockChain).close();
128 assertFalse(future.isDone());
131 assertSame(future, adapter.shutdown());
134 listeners.getValue().onTransactionChainSuccessful(mockChain);
135 assertSame(Empty.value(), Futures.getDone(future));
139 public void testShutdownCompletion() throws Exception {
140 // FIXME: exact match
141 doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
142 doNothing().when(mockChain).close();
144 final var future = adapter.shutdown();
145 verify(mockChain, times(2)).newWriteOnlyTransaction();
146 verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
147 TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
148 verify(mockTx, times(2)).commit();
149 verify(mockChain).close();
151 assertFalse(future.isDone());
154 assertSame(future, adapter.shutdown());
157 final var cause = new Throwable();
158 listeners.getValue().onTransactionChainFailed(mockChain, mockTx, cause);
159 final var ex = assertThrows(ExecutionException.class, () -> Futures.getDone(future));
160 assertSame(cause, ex.getCause());