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