Bump MRI upstreams
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceTopologyAdapterTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
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;
18
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.NormalizedNode;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
54
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class NetconfDeviceTopologyAdapterTest {
57     private static BindingRuntimeContext RUNTIME_CONTEXT;
58
59     private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
60
61     @Mock
62     private WriteTransaction writeTx;
63     @Mock
64     private TransactionChain txChain;
65
66     private final String txIdent = "test transaction";
67
68     private TransactionChain transactionChain;
69
70     private DataBroker dataBroker;
71
72     private DOMDataBroker domDataBroker;
73
74     @BeforeClass
75     public static void beforeClass() {
76         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
77     }
78
79     @AfterClass
80     public static void afterClass() {
81         RUNTIME_CONTEXT = null;
82     }
83
84     @Before
85     public void setUp() throws Exception {
86         doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
87         doNothing().when(writeTx)
88                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
89         doNothing().when(writeTx)
90                 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetworkTopology.class));
91
92         doReturn(txIdent).when(writeTx).getIdentifier();
93
94         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
95         domDataBroker = customizer.getDOMDataBroker();
96         dataBroker = customizer.createDataBroker();
97         customizer.updateSchema(RUNTIME_CONTEXT);
98
99         transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
100             @Override
101             public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
102                     final Throwable cause) {
103
104             }
105
106             @Override
107             public void onTransactionChainSuccessful(final TransactionChain chain) {
108
109             }
110         });
111
112     }
113
114     @Test
115     public void testFailedDevice() throws Exception {
116
117         doReturn(emptyFluentFuture()).when(writeTx).commit();
118         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
119         adapter.setDeviceAsFailed(null);
120
121         verify(txChain, times(2)).newWriteOnlyTransaction();
122         verify(writeTx, times(1))
123                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
124         adapter.close();
125
126         adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
127         adapter.setDeviceAsFailed(null);
128
129         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
130             Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
131                     .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
132                     .get(5, TimeUnit.SECONDS);
133             return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
134                     == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
135         });
136     }
137
138     @Test
139     public void testDeviceUpdate() throws Exception {
140         doReturn(emptyFluentFuture()).when(writeTx).commit();
141
142         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
143         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
144
145         verify(txChain, times(2)).newWriteOnlyTransaction();
146         verify(writeTx, times(1))
147                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
148         verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
149
150     }
151
152     @Test
153     public void testDeviceAugmentedNodePresence() throws Exception {
154
155         Integer dataTestId = 474747;
156
157         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
158
159         QName netconfTestLeafQname = QName.create(
160                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
161
162         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
163                 .node(Topology.QNAME)
164                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
165                 .node(Node.QNAME)
166                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
167                 .node(netconfTestLeafQname).build();
168
169         NormalizedNode augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
170                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
171
172         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
173         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
174         wtx.commit().get(5, TimeUnit.SECONDS);
175
176         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
177         Optional<NormalizedNode> testNode = domDataBroker.newReadOnlyTransaction()
178                 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
179
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());
182
183         adapter.setDeviceAsFailed(null);
184         testNode = domDataBroker.newReadOnlyTransaction()
185                 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
186
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());
190     }
191
192     @Test
193     public void testRemoveDeviceConfiguration() throws Exception {
194         doReturn(emptyFluentFuture()).when(writeTx).commit();
195
196         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
197         adapter.close();
198
199         verify(txChain, times(2)).newWriteOnlyTransaction();
200         verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
201         verify(writeTx, times(2)).commit();
202     }
203 }