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