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