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