Merge "Bug 8197: Deregister schema sources on actor stop"
[netconf.git] / netconf / 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
9 package org.opendaylight.netconf.topology.singleton.impl;
10
11 import static junit.framework.TestCase.assertFalse;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.MockitoAnnotations.initMocks;
21 import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.DELETE;
22 import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.WRITE;
23
24 import com.google.common.util.concurrent.Futures;
25 import io.netty.util.concurrent.EventExecutor;
26 import java.lang.reflect.Field;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Map;
30 import javax.annotation.Nonnull;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.controller.cluster.ActorSystemProvider;
35 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
36 import org.opendaylight.controller.config.threadpool.ThreadPool;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
39 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
40 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
41 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
44 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
45 import org.opendaylight.controller.sal.core.api.Broker;
46 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
47 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
48 import org.opendaylight.netconf.client.NetconfClientDispatcher;
49 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.topology.singleton.config.rev170419.Config;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.topology.singleton.config.rev170419.ConfigBuilder;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
62 import org.opendaylight.yangtools.yang.binding.Identifier;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64
65 public class NetconfTopologyManagerTest {
66
67     private final String topologyId = "topologyID";
68     private NetconfTopologyManager netconfTopologyManager;
69
70     @Mock
71     private DataBroker dataBroker;
72
73     @Mock
74     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
75
76     @Before
77     public void setUp() {
78         initMocks(this);
79
80         final RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class);
81         final BindingAwareBroker bindingAwareBroker = mock(BindingAwareBroker.class);
82         final ScheduledThreadPool keepaliveExecutor = mock(ScheduledThreadPool.class);
83         final ThreadPool processingExecutor = mock(ThreadPool.class);
84         final Broker domBroker = mock(Broker.class);
85         final ActorSystemProvider actorSystemProvider = mock(ActorSystemProvider.class);
86         final EventExecutor eventExecutor = mock(EventExecutor.class);
87         final NetconfClientDispatcher clientDispatcher = mock(NetconfClientDispatcher.class);
88
89         final Config config = new ConfigBuilder().setWriteTransactionIdleTimeout(0).build();
90         netconfTopologyManager = new NetconfTopologyManager(dataBroker, rpcProviderRegistry,
91                 clusterSingletonServiceProvider, bindingAwareBroker, keepaliveExecutor, processingExecutor, domBroker,
92                 actorSystemProvider, eventExecutor, clientDispatcher, topologyId, config);
93     }
94
95     @Test
96     public void testWriteConfiguration() throws Exception {
97
98         final ClusterSingletonServiceRegistration clusterRegistration = mock(ClusterSingletonServiceRegistration.class);
99
100         final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts");
101         fieldContexts.setAccessible(true);
102         @SuppressWarnings("unchecked")
103         final Map<InstanceIdentifier<Node>, NetconfTopologyContext> contexts =
104                 (Map<InstanceIdentifier<Node>, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager);
105
106         final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations");
107         fieldClusterRegistrations.setAccessible(true);
108         @SuppressWarnings("unchecked")
109         final Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration> clusterRegistrations =
110                 (Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration>)
111                         fieldClusterRegistrations.get(netconfTopologyManager);
112
113         final Collection<DataTreeModification<Node>> changes = new ArrayList<>();
114
115         final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
116                 new NodeKey(new NodeId("node-id-1")),"topology-1");
117
118         final InstanceIdentifier<Node> instanceIdentifierDiferent = NetconfTopologyUtils.createTopologyNodeListPath(
119                 new NodeKey(new NodeId("node-id-2")),"topology-2");
120
121         final DataTreeIdentifier<Node> rootIdentifier =
122                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
123
124         final DataTreeIdentifier<Node> rootIdentifierDifferent =
125                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierDiferent);
126
127         @SuppressWarnings("unchecked")
128         final DataObjectModification<Node> objectModification = mock(DataObjectModification.class);
129
130         final NetconfNode netconfNode = new NetconfNodeBuilder()
131                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
132                 .setPort(new PortNumber(9999))
133                 .setReconnectOnChangedSchema(true)
134                 .setDefaultRequestTimeoutMillis(1000L)
135                 .setBetweenAttemptsTimeoutMillis(100)
136                 .setSchemaless(false)
137                 .setTcpOnly(false)
138                 .setActorResponseWaitTime(10)
139                 .build();
140         final Node node = new NodeBuilder().setNodeId(new NodeId("node-id"))
141                 .addAugmentation(NetconfNode.class, netconfNode).build();
142
143         final Identifier key = new NodeKey(new NodeId("node-id"));
144
145         @SuppressWarnings("unchecked")
146         final InstanceIdentifier.IdentifiableItem<Node, NodeKey> pathArgument =
147                 new InstanceIdentifier.IdentifiableItem(Node.class, key);
148
149
150         // testing WRITE on two identical rootIdentifiers and one different
151
152         changes.add(new CustomTreeModification(rootIdentifier, objectModification));
153         changes.add(new CustomTreeModification(rootIdentifier, objectModification));
154         changes.add(new CustomTreeModification(rootIdentifierDifferent, objectModification));
155
156         doReturn(WRITE).when(objectModification).getModificationType();
157         doReturn(node).when(objectModification).getDataAfter();
158         doReturn(pathArgument).when(objectModification).getIdentifier();
159         doReturn(clusterRegistration).when(clusterSingletonServiceProvider).registerClusterSingletonService(any());
160
161         netconfTopologyManager.onDataTreeChanged(changes);
162
163         verify(clusterSingletonServiceProvider, times(2)).registerClusterSingletonService(any());
164
165         // only two created contexts
166         assertEquals(2, contexts.size());
167         assertTrue(contexts.containsKey(rootIdentifier.getRootIdentifier()));
168         assertTrue(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
169
170         // only two created cluster registrations
171         assertEquals(2, contexts.size());
172         assertTrue(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
173         assertTrue(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
174
175         // after delete there should be no context and clustered registrations
176         doReturn(DELETE).when(objectModification).getModificationType();
177
178         doNothing().when(clusterRegistration).close();
179
180         netconfTopologyManager.onDataTreeChanged(changes);
181
182         verify(clusterRegistration, times(2)).close();
183
184         // empty map of contexts
185         assertTrue(contexts.isEmpty());
186         assertFalse(contexts.containsKey(rootIdentifier.getRootIdentifier()));
187         assertFalse(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
188
189         // empty map of clustered registrations
190         assertTrue(clusterRegistrations.isEmpty());
191         assertFalse(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
192         assertFalse(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
193
194     }
195
196     @Test
197     public void testRegisterDataTreeChangeListener() {
198
199         final WriteTransaction wtx = mock(WriteTransaction.class);
200
201         doReturn(wtx).when(dataBroker).newWriteOnlyTransaction();
202         doNothing().when(wtx).merge(any(), any(), any());
203         doReturn(Futures.immediateCheckedFuture(null)).when(wtx).submit();
204         doReturn(null).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
205
206         netconfTopologyManager.init();
207
208         // verify if listener is called with right parameters = registered on right path
209
210         verify(dataBroker, times(1)).registerDataTreeChangeListener(
211                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils
212                         .createTopologyListPath(topologyId).child(Node.class)), netconfTopologyManager);
213
214     }
215
216     @Test
217     public void testClose() throws Exception {
218
219         final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts");
220         fieldContexts.setAccessible(true);
221         @SuppressWarnings("unchecked")
222         final Map<InstanceIdentifier<Node>, NetconfTopologyContext> contexts =
223                 (Map<InstanceIdentifier<Node>, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager);
224
225         final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations");
226         fieldClusterRegistrations.setAccessible(true);
227         @SuppressWarnings("unchecked")
228         final Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration> clusterRegistrations =
229                 (Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration>)
230                         fieldClusterRegistrations.get(netconfTopologyManager);
231
232         final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
233                 new NodeKey(new NodeId("node-id-1")),"topology-1");
234
235
236         final NetconfTopologyContext context = mock(NetconfTopologyContext.class);
237         final ClusterSingletonServiceRegistration clusterRegistration =
238                 mock(ClusterSingletonServiceRegistration.class);
239         contexts.put(instanceIdentifier, context);
240         clusterRegistrations.put(instanceIdentifier, clusterRegistration);
241
242         doNothing().when(context).closeFinal();
243         doNothing().when(clusterRegistration).close();
244
245         netconfTopologyManager.close();
246         verify(context, times(1)).closeFinal();
247         verify(clusterRegistration, times(1)).close();
248
249         assertTrue(contexts.isEmpty());
250         assertTrue(clusterRegistrations.isEmpty());
251
252     }
253
254     private class CustomTreeModification  implements DataTreeModification<Node> {
255
256         private final DataTreeIdentifier<Node> rootPath;
257         private final DataObjectModification<Node> rootNode;
258
259         CustomTreeModification(final DataTreeIdentifier<Node> rootPath, final DataObjectModification<Node> rootNode) {
260             this.rootPath = rootPath;
261             this.rootNode = rootNode;
262         }
263
264         @Nonnull
265         @Override
266         public DataTreeIdentifier<Node> getRootPath() {
267             return rootPath;
268         }
269
270         @Nonnull
271         @Override
272         public DataObjectModification<Node> getRootNode() {
273             return rootNode;
274         }
275     }
276 }