Bump upstreams for Silicon
[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.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;
17
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.binding.runtime.api.BindingRuntimeContext;
35 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
39 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.augment.test.rev160808.Node1;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
52
53 public class NetconfDeviceTopologyAdapterTest {
54     private static BindingRuntimeContext RUNTIME_CONTEXT;
55
56     private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
57
58     @Mock
59     private WriteTransaction writeTx;
60     @Mock
61     private TransactionChain txChain;
62     @Mock
63     private NetconfNode data;
64
65     private final String txIdent = "test transaction";
66
67     private final String sessionIdForReporting = "netconf-test-session1";
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         MockitoAnnotations.initMocks(this);
88         doReturn(writeTx).when(txChain).newWriteOnlyTransaction();
89         doNothing().when(writeTx)
90                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
91         doNothing().when(writeTx)
92                 .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(NetconfNode.class));
93
94         doReturn(txIdent).when(writeTx).getIdentifier();
95
96         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
97         domDataBroker = customizer.getDOMDataBroker();
98         dataBroker = customizer.createDataBroker();
99         customizer.updateSchema(RUNTIME_CONTEXT);
100
101         transactionChain = dataBroker.createTransactionChain(new TransactionChainListener() {
102             @Override
103             public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
104                     final Throwable cause) {
105
106             }
107
108             @Override
109             public void onTransactionChainSuccessful(final TransactionChain chain) {
110
111             }
112         });
113
114     }
115
116     @Test
117     public void testFailedDevice() throws Exception {
118
119         doReturn(emptyFluentFuture()).when(writeTx).commit();
120         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
121         adapter.setDeviceAsFailed(null);
122
123         verify(txChain, times(2)).newWriteOnlyTransaction();
124         verify(writeTx, times(1))
125                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
126         adapter.close();
127
128         adapter = new NetconfDeviceTopologyAdapter(id, transactionChain); //not a mock
129         adapter.setDeviceAsFailed(null);
130
131         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
132             Optional<NetconfNode> netconfNode = dataBroker.newReadWriteTransaction()
133                     .read(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath().augmentation(NetconfNode.class))
134                     .get(5, TimeUnit.SECONDS);
135             return netconfNode.isPresent() && netconfNode.get().getConnectionStatus()
136                     == NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
137         });
138     }
139
140     @Test
141     public void testDeviceUpdate() throws Exception {
142         doReturn(emptyFluentFuture()).when(writeTx).commit();
143
144         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
145         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
146
147         verify(txChain, times(2)).newWriteOnlyTransaction();
148         verify(writeTx, times(1))
149                 .put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
150         verify(writeTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
151
152     }
153
154     @Test
155     public void testDeviceAugmentedNodePresence() throws Exception {
156
157         Integer dataTestId = 474747;
158
159         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, transactionChain);
160
161         QName netconfTestLeafQname = QName.create(
162                 "urn:TBD:params:xml:ns:yang:network-topology-augment-test", "2016-08-08", "test-id").intern();
163
164         YangInstanceIdentifier pathToAugmentedLeaf = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
165                 .node(Topology.QNAME)
166                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "topology-netconf")
167                 .node(Node.QNAME)
168                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test")
169                 .node(netconfTestLeafQname).build();
170
171         NormalizedNode<?, ?> augmentNode = ImmutableLeafNodeBuilder.create().withValue(dataTestId)
172                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(netconfTestLeafQname)).build();
173
174         DOMDataTreeWriteTransaction wtx =  domDataBroker.newWriteOnlyTransaction();
175         wtx.put(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf, augmentNode);
176         wtx.commit().get(5, TimeUnit.SECONDS);
177
178         adapter.updateDeviceData(true, new NetconfDeviceCapabilities());
179         Optional<NormalizedNode<?, ?>> testNode = domDataBroker.newReadOnlyTransaction()
180                 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
181
182         assertEquals("Augmented node data should be still present after device update.", true, testNode.isPresent());
183         assertEquals("Augmented data should be the same as before update node.", dataTestId, testNode.get().getValue());
184
185         adapter.setDeviceAsFailed(null);
186         testNode = domDataBroker.newReadOnlyTransaction()
187                 .read(LogicalDatastoreType.OPERATIONAL, pathToAugmentedLeaf).get(2, TimeUnit.SECONDS);
188
189         assertEquals("Augmented node data should be still present after device failed.", true, testNode.isPresent());
190         assertEquals("Augmented data should be the same as before failed device.",
191                 dataTestId, testNode.get().getValue());
192     }
193
194     @Test
195     public void testRemoveDeviceConfiguration() throws Exception {
196         doReturn(emptyFluentFuture()).when(writeTx).commit();
197
198         NetconfDeviceTopologyAdapter adapter = new NetconfDeviceTopologyAdapter(id, txChain);
199         adapter.close();
200
201         verify(txChain, times(2)).newWriteOnlyTransaction();
202         verify(writeTx).delete(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath());
203         verify(writeTx, times(2)).commit();
204     }
205
206 }