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