Do not reference individual builders
[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.LeafNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class NetconfDeviceTopologyAdapterTest {
58     private static BindingRuntimeContext RUNTIME_CONTEXT;
59
60     private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
61
62     @Mock
63     private WriteTransaction writeTx;
64     @Mock
65     private TransactionChain txChain;
66
67     private final String txIdent = "test transaction";
68
69     private TransactionChain transactionChain;
70
71     private DataBroker dataBroker;
72
73     private DOMDataBroker domDataBroker;
74
75     @BeforeClass
76     public static void beforeClass() {
77         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(NetconfNode.class, Node1.class);
78     }
79
80     @AfterClass
81     public static void afterClass() {
82         RUNTIME_CONTEXT = null;
83     }
84
85     @Before
86     public void setUp() throws Exception {
87         doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
88         doNothing().when(writeTx)
89                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
90         doNothing().when(writeTx)
91                 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetworkTopology.class));
92
93         doReturn(txIdent).when(writeTx).getIdentifier();
94
95         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
96         domDataBroker = customizer.getDOMDataBroker();
97         dataBroker = customizer.createDataBroker();
98         customizer.updateSchema(RUNTIME_CONTEXT);
99
100         transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
101             @Override
102             public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
103                     final Throwable cause) {
104
105             }
106
107             @Override
108             public void onTransactionChainSuccessful(final TransactionChain chain) {
109
110             }
111         });
112
113     }
114
115     @Test
116     public void testFailedDevice() throws Exception {
117
118         doReturn(emptyFluentFuture()).when(writeTx).commit();
119         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
120         adapter.setDeviceAsFailed(null);
121
122         verify(txChain, times(2)).newWriteOnlyTransaction();
123         verify(writeTx, times(1))
124                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
125         adapter.close();
126
127         adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
128         adapter.setDeviceAsFailed(null);
129
130         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
131             Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
132                     .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
133                     .get(5, TimeUnit.SECONDS);
134             return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
135                     == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
136         });
137     }
138
139     @Test
140     public void testDeviceUpdate() throws Exception {
141         doReturn(emptyFluentFuture()).when(writeTx).commit();
142
143         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
144         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
145
146         verify(txChain, times(2)).newWriteOnlyTransaction();
147         verify(writeTx, times(1))
148                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
149         verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
150
151     }
152
153     @Test
154     public void testDeviceAugmentedNodePresence() throws Exception {
155
156         Integer dataTestId = 474747;
157
158         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
159
160         QName netconfTestLeafQname = QName.create(
161                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
162
163         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
164                 .node(Topology.QNAME)
165                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
166                 .node(Node.QNAME)
167                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
168                 .node(netconfTestLeafQname).build();
169
170         LeafNode augmentNode = ImmutableNodes.leafNode(netconfTestLeafQname, dataTestId);
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 }