Remove blueprint from netconf-topology-singleton
[netconf.git] / apps / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / NetconfTopologyManagerTest.java
1 /*
2  * Copyright (c) 2016 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.topology.singleton.impl;
9
10 import static org.awaitility.Awaitility.await;
11 import static org.junit.Assert.assertEquals;
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.doThrow;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.reset;
18 import static org.mockito.Mockito.spy;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.DELETE;
23 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.SUBTREE_MODIFIED;
24 import static org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType.WRITE;
25
26 import akka.actor.ActorSystem;
27 import akka.util.Timeout;
28 import io.netty.util.concurrent.EventExecutor;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Objects;
33 import java.util.Set;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.TimeUnit;
37 import java.util.function.Function;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
45 import org.opendaylight.mdsal.binding.api.DataBroker;
46 import org.opendaylight.mdsal.binding.api.DataObjectModification;
47 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
48 import org.opendaylight.mdsal.binding.api.DataTreeModification;
49 import org.opendaylight.mdsal.binding.api.ReadTransaction;
50 import org.opendaylight.mdsal.binding.api.RpcProviderService;
51 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
52 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
53 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
54 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
55 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
56 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
57 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
58 import org.opendaylight.netconf.client.NetconfClientDispatcher;
59 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
60 import org.opendaylight.netconf.client.mdsal.impl.DefaultSchemaResourceManager;
61 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
62 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
63 import org.opendaylight.netconf.topology.spi.NetconfClientConfigurationBuilderFactory;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
75 import org.opendaylight.yangtools.concepts.ListenerRegistration;
76 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
77 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
78 import org.opendaylight.yangtools.yang.common.Uint16;
79 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
80
81 @RunWith(MockitoJUnitRunner.StrictStubs.class)
82 public class NetconfTopologyManagerTest extends AbstractBaseSchemasTest {
83     private static final Uint16 ACTOR_RESPONSE_WAIT_TIME = Uint16.valueOf(10);
84     private static final String TOPOLOGY_ID = "topologyID";
85
86     private NetconfTopologyManager netconfTopologyManager;
87
88     @Mock
89     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
90
91     @Mock
92     private ListenerRegistration<?> mockListenerReg;
93
94     private DataBroker dataBroker;
95
96     private final Map<InstanceIdentifier<Node>, Function<NetconfTopologySetup, NetconfTopologyContext>>
97             mockContextMap = new HashMap<>();
98
99     @Before
100     public void setUp() throws Exception {
101         AbstractDataBrokerTest dataBrokerTest = new AbstractDataBrokerTest() {
102             @Override
103             protected Set<YangModuleInfo> getModuleInfos() throws Exception {
104                 return Set.of(BindingRuntimeHelpers.getYangModuleInfo(NetworkTopology.class));
105             }
106         };
107
108         dataBrokerTest.setup();
109         dataBroker = spy(dataBrokerTest.getDataBroker());
110
111         final ScheduledExecutorService keepaliveExecutor = mock(ScheduledExecutorService.class);
112         final ExecutorService processingService = mock(ExecutorService.class);
113         final ActorSystem actorSystem = mock(ActorSystem.class);
114         final EventExecutor eventExecutor = mock(EventExecutor.class);
115         final NetconfClientDispatcher clientDispatcher = mock(NetconfClientDispatcher.class);
116         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
117         final AAAEncryptionService encryptionService = mock(AAAEncryptionService.class);
118         final DeviceActionFactory deviceActionFactory = mock(DeviceActionFactory.class);
119         final RpcProviderService rpcProviderService = mock(RpcProviderService.class);
120         final NetconfClientConfigurationBuilderFactory builderFactory =
121             mock(NetconfClientConfigurationBuilderFactory.class);
122
123         doNothing().when(mockListenerReg).close();
124         doReturn(mockListenerReg).when(dataBroker).registerDataTreeChangeListener(any(), any());
125
126         netconfTopologyManager = new NetconfTopologyManager(BASE_SCHEMAS, dataBroker, clusterSingletonServiceProvider,
127                 keepaliveExecutor, processingService, actorSystem, eventExecutor, clientDispatcher,
128                 mountPointService, encryptionService, rpcProviderService, deviceActionFactory,
129                 new DefaultSchemaResourceManager(new DefaultYangParserFactory()), builderFactory,
130                 TOPOLOGY_ID, Uint16.ZERO) {
131             @Override
132             protected NetconfTopologyContext newNetconfTopologyContext(final NetconfTopologySetup setup,
133                 final ServiceGroupIdentifier serviceGroupIdent, final Timeout actorResponseWaitTime,
134                 final DeviceActionFactory deviceActionFactory) {
135                 assertEquals(ACTOR_RESPONSE_WAIT_TIME.toJava(), actorResponseWaitTime.duration().toSeconds());
136                 return Objects.requireNonNull(mockContextMap.get(setup.getInstanceIdentifier()),
137                         "No mock context for " + setup.getInstanceIdentifier()).apply(setup);
138             }
139         };
140     }
141
142     @Test
143     public void testRegisterDataTreeChangeListener() throws Exception {
144         await().atMost(5, TimeUnit.SECONDS).until(() -> {
145             try (ReadTransaction readTx = dataBroker.newReadOnlyTransaction()) {
146                 return readTx.exists(LogicalDatastoreType.OPERATIONAL,
147                     NetconfTopologyUtils.createTopologyListPath(TOPOLOGY_ID)).get(3, TimeUnit.SECONDS);
148             }
149         });
150
151         // verify registration is called with right parameters
152
153         verify(dataBroker).registerDataTreeChangeListener(
154                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils
155                         .createTopologyListPath(TOPOLOGY_ID).child(Node.class)), netconfTopologyManager);
156
157         netconfTopologyManager.close();
158         verify(mockListenerReg).close();
159
160         netconfTopologyManager.close();
161         verifyNoMoreInteractions(mockListenerReg);
162     }
163
164     @SuppressWarnings("unchecked")
165     @Test
166     public void testOnDataTreeChanged() {
167         // Notify of 2 created Node objects.
168         final NodeId nodeId1 = new NodeId("node-id-1");
169         final InstanceIdentifier<Node> nodeInstanceId1 = NetconfTopologyUtils.createTopologyNodeListPath(
170                 new NodeKey(nodeId1), TOPOLOGY_ID);
171
172         final NodeId nodeId2 = new NodeId("node-id-2");
173         final InstanceIdentifier<Node> nodeInstanceId2 = NetconfTopologyUtils.createTopologyNodeListPath(
174                 new NodeKey(nodeId2), TOPOLOGY_ID);
175
176         final NetconfNode netconfNode1 = new NetconfNodeBuilder()
177                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
178                 .setPort(new PortNumber(Uint16.valueOf(1111)))
179                 .setActorResponseWaitTime(ACTOR_RESPONSE_WAIT_TIME)
180                 .build();
181         final Node node1 = new NodeBuilder().setNodeId(nodeId1).addAugmentation(netconfNode1).build();
182
183         final DataObjectModification<Node> dataObjectModification1 = mock(DataObjectModification.class);
184         doReturn(WRITE).when(dataObjectModification1).getModificationType();
185         doReturn(node1).when(dataObjectModification1).getDataAfter();
186         doReturn(InstanceIdentifier.IdentifiableItem.of(Node.class, new NodeKey(nodeId1)))
187                 .when(dataObjectModification1).getIdentifier();
188
189         final NetconfNode netconfNode2 = new NetconfNodeBuilder()
190                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
191                 .setPort(new PortNumber(Uint16.valueOf(2222)))
192                 .setActorResponseWaitTime(ACTOR_RESPONSE_WAIT_TIME)
193                 .build();
194         final Node node2 = new NodeBuilder().setNodeId(nodeId2).addAugmentation(netconfNode2).build();
195
196         final DataObjectModification<Node> dataObjectModification2 = mock(DataObjectModification.class);
197         doReturn(WRITE).when(dataObjectModification2).getModificationType();
198         doReturn(node2).when(dataObjectModification2).getDataAfter();
199         doReturn(InstanceIdentifier.IdentifiableItem.of(Node.class, new NodeKey(nodeId2)))
200                 .when(dataObjectModification2).getIdentifier();
201
202         final NetconfTopologyContext mockContext1 = mock(NetconfTopologyContext.class);
203         mockContextMap.put(nodeInstanceId1, setup -> {
204             assertEquals(node1, setup.getNode());
205             assertEquals(TOPOLOGY_ID, setup.getTopologyId());
206             return mockContext1;
207         });
208
209         final NetconfTopologyContext mockContext2 = mock(NetconfTopologyContext.class);
210         mockContextMap.put(nodeInstanceId2, setup -> {
211             assertEquals(node2, setup.getNode());
212             assertEquals(TOPOLOGY_ID, setup.getTopologyId());
213             return mockContext2;
214         });
215
216         ClusterSingletonServiceRegistration mockClusterRegistration1 = mock(ClusterSingletonServiceRegistration.class);
217         ClusterSingletonServiceRegistration mockClusterRegistration2 = mock(ClusterSingletonServiceRegistration.class);
218
219         doReturn(mockClusterRegistration1).when(clusterSingletonServiceProvider)
220                 .registerClusterSingletonService(mockContext1);
221         doReturn(mockClusterRegistration2).when(clusterSingletonServiceProvider)
222                 .registerClusterSingletonService(mockContext2);
223
224         netconfTopologyManager.onDataTreeChanged(List.of(
225                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
226                         nodeInstanceId1), dataObjectModification1),
227                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
228                         nodeInstanceId2), dataObjectModification2)));
229
230         verify(clusterSingletonServiceProvider).registerClusterSingletonService(mockContext1);
231         verify(clusterSingletonServiceProvider).registerClusterSingletonService(mockContext2);
232
233         // Notify of Node 1 replaced and Node 2 subtree modified.
234         mockContextMap.clear();
235
236         final NetconfNode updatedNetconfNode1 = new NetconfNodeBuilder(netconfNode1)
237                 .setPort(new PortNumber(Uint16.valueOf(33333))).build();
238         final Node updatedNode1 = new NodeBuilder().setNodeId(nodeId1).addAugmentation(updatedNetconfNode1).build();
239
240         doReturn(WRITE).when(dataObjectModification1).getModificationType();
241         doReturn(updatedNode1).when(dataObjectModification1).getDataAfter();
242
243         doReturn(SUBTREE_MODIFIED).when(dataObjectModification2).getModificationType();
244         doReturn(node2).when(dataObjectModification2).getDataAfter();
245
246         doNothing().when(mockContext1).refresh(any());
247         doNothing().when(mockContext2).refresh(any());
248
249         netconfTopologyManager.onDataTreeChanged(List.of(
250                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
251                         nodeInstanceId1), dataObjectModification1),
252                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
253                         nodeInstanceId2), dataObjectModification2)));
254
255         ArgumentCaptor<NetconfTopologySetup> mockContext1Setup = ArgumentCaptor.forClass(NetconfTopologySetup.class);
256         verify(mockContext1).refresh(mockContext1Setup.capture());
257         assertEquals(updatedNode1, mockContext1Setup.getValue().getNode());
258
259         verify(mockContext2).refresh(any());
260
261         verifyNoMoreInteractions(clusterSingletonServiceProvider);
262
263         // Notify of Node 1 deleted.
264         doReturn(DELETE).when(dataObjectModification1).getModificationType();
265
266         netconfTopologyManager.onDataTreeChanged(List.of(
267                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
268                         nodeInstanceId1), dataObjectModification1)));
269
270         verify(mockClusterRegistration1).close();
271         verify(mockContext1).close();
272         verifyNoMoreInteractions(clusterSingletonServiceProvider, mockClusterRegistration2, mockContext2);
273
274         // Notify of Node 1 created again.
275         reset(clusterSingletonServiceProvider);
276
277         final NetconfTopologyContext newMockContext1 = mock(NetconfTopologyContext.class);
278         final ClusterSingletonServiceRegistration newMockClusterRegistration1 =
279                 mock(ClusterSingletonServiceRegistration.class);
280
281         doThrow(new RuntimeException("mock error")).doReturn(newMockClusterRegistration1)
282                 .when(clusterSingletonServiceProvider).registerClusterSingletonService(newMockContext1);
283
284         doReturn(WRITE).when(dataObjectModification1).getModificationType();
285         doReturn(node1).when(dataObjectModification1).getDataAfter();
286
287         mockContextMap.put(nodeInstanceId1, setup -> {
288             assertEquals(node1, setup.getNode());
289             assertEquals(TOPOLOGY_ID, setup.getTopologyId());
290             return newMockContext1;
291         });
292
293         netconfTopologyManager.onDataTreeChanged(List.of(
294                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
295                         nodeInstanceId1), dataObjectModification1)));
296
297         verify(clusterSingletonServiceProvider, times(2)).registerClusterSingletonService(newMockContext1);
298         verifyNoMoreInteractions(mockClusterRegistration1, mockContext1, mockClusterRegistration2, mockContext2,
299                 newMockContext1, newMockClusterRegistration1, clusterSingletonServiceProvider);
300
301         // Test close.
302         netconfTopologyManager.close();
303
304         verify(newMockClusterRegistration1).close();
305         verify(newMockContext1).close();
306         verify(mockClusterRegistration2).close();
307         verify(mockContext2).close();
308
309         netconfTopologyManager.close();
310
311         verifyNoMoreInteractions(mockClusterRegistration1, mockContext1, mockClusterRegistration2, mockContext2,
312                 newMockContext1, newMockClusterRegistration1, clusterSingletonServiceProvider);
313     }
314
315     @Test
316     public void testClusterSingletonServiceRegistrationFailure() throws Exception {
317         final NodeId nodeId = new NodeId("node-id");
318         final InstanceIdentifier<Node> nodeInstanceId = NetconfTopologyUtils.createTopologyNodeListPath(
319                 new NodeKey(nodeId), TOPOLOGY_ID);
320
321         final Node node = new NodeBuilder()
322                 .setNodeId(nodeId)
323                 .addAugmentation(new NetconfNodeBuilder()
324                     .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
325                     .setPort(new PortNumber(Uint16.valueOf(10)))
326                     .setActorResponseWaitTime(ACTOR_RESPONSE_WAIT_TIME)
327                     .build())
328                 .build();
329
330         final DataObjectModification<Node> dataObjectModification = mock(DataObjectModification.class);
331         doReturn(WRITE).when(dataObjectModification).getModificationType();
332         doReturn(node).when(dataObjectModification).getDataAfter();
333         doReturn(InstanceIdentifier.IdentifiableItem.of(Node.class, new NodeKey(nodeId)))
334                 .when(dataObjectModification).getIdentifier();
335
336         final NetconfTopologyContext mockContext = mock(NetconfTopologyContext.class);
337         mockContextMap.put(nodeInstanceId, setup -> mockContext);
338
339         doThrow(new RuntimeException("mock error")).when(clusterSingletonServiceProvider)
340                 .registerClusterSingletonService(mockContext);
341
342         netconfTopologyManager.onDataTreeChanged(List.of(
343                 new CustomTreeModification(DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
344                         nodeInstanceId), dataObjectModification)));
345
346         verify(clusterSingletonServiceProvider, times(3)).registerClusterSingletonService(mockContext);
347         verify(mockContext).close();
348         verifyNoMoreInteractions(mockListenerReg);
349
350         netconfTopologyManager.close();
351         verifyNoMoreInteractions(mockContext);
352     }
353
354     static class CustomTreeModification  implements DataTreeModification<Node> {
355
356         private final DataTreeIdentifier<Node> rootPath;
357         private final DataObjectModification<Node> rootNode;
358
359         CustomTreeModification(final DataTreeIdentifier<Node> rootPath, final DataObjectModification<Node> rootNode) {
360             this.rootPath = rootPath;
361             this.rootNode = rootNode;
362         }
363
364         @Override
365         public DataTreeIdentifier<Node> getRootPath() {
366             return rootPath;
367         }
368
369         @Override
370         public DataObjectModification<Node> getRootNode() {
371             return rootNode;
372         }
373     }
374 }