2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.singleton.impl;
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;
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;
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;
66 public class NetconfTopologyManagerTest {
68 private final String topologyId = "topologyID";
69 private NetconfTopologyManager netconfTopologyManager;
72 private DataBroker dataBroker;
75 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
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);
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);
98 public void testWriteConfiguration() throws Exception {
99 writeConfiguration(false);
103 public void testWriteConfigurationFail() throws Exception {
104 writeConfiguration(true);
108 public void testRegisterDataTreeChangeListener() {
110 final WriteTransaction wtx = mock(WriteTransaction.class);
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());
117 netconfTopologyManager.init();
119 // verify if listener is called with right parameters = registered on right path
121 verify(dataBroker, times(1)).registerDataTreeChangeListener(
122 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils
123 .createTopologyListPath(topologyId).child(Node.class)), netconfTopologyManager);
128 public void testClose() throws Exception {
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);
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);
143 final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
144 new NodeKey(new NodeId("node-id-1")), "topology-1");
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);
153 doNothing().when(context).closeFinal();
154 doNothing().when(clusterRegistration).close();
156 netconfTopologyManager.close();
157 verify(context, times(1)).closeFinal();
158 verify(clusterRegistration, times(1)).close();
160 assertTrue(contexts.isEmpty());
161 assertTrue(clusterRegistrations.isEmpty());
165 private void writeConfiguration(final boolean fail) throws Exception {
167 final ClusterSingletonServiceRegistration clusterRegistration = mock(ClusterSingletonServiceRegistration.class);
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);
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);
182 final Collection<DataTreeModification<Node>> changes = new ArrayList<>();
184 final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
185 new NodeKey(new NodeId("node-id-1")), "topology-1");
187 final InstanceIdentifier<Node> instanceIdentifierDiferent = NetconfTopologyUtils.createTopologyNodeListPath(
188 new NodeKey(new NodeId("node-id-2")), "topology-2");
190 final DataTreeIdentifier<Node> rootIdentifier =
191 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
193 final DataTreeIdentifier<Node> rootIdentifierDifferent =
194 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierDiferent);
196 @SuppressWarnings("unchecked") final DataObjectModification<Node> objectModification =
197 mock(DataObjectModification.class);
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)
207 .setActorResponseWaitTime(10)
209 final Node node = new NodeBuilder().setNodeId(new NodeId("node-id"))
210 .addAugmentation(NetconfNode.class, netconfNode).build();
212 final Identifier key = new NodeKey(new NodeId("node-id"));
214 @SuppressWarnings("unchecked") final InstanceIdentifier.IdentifiableItem<Node, NodeKey> pathArgument =
215 new InstanceIdentifier.IdentifiableItem(Node.class, key);
218 // testing WRITE on two identical rootIdentifiers and one different
220 changes.add(new CustomTreeModification(rootIdentifier, objectModification));
222 changes.add(new CustomTreeModification(rootIdentifier, objectModification));
223 changes.add(new CustomTreeModification(rootIdentifier, objectModification));
224 changes.add(new CustomTreeModification(rootIdentifierDifferent, objectModification));
226 doReturn(WRITE).when(objectModification).getModificationType();
227 doReturn(node).when(objectModification).getDataAfter();
228 doReturn(pathArgument).when(objectModification).getIdentifier();
231 doThrow(new RuntimeException("error")).when(clusterSingletonServiceProvider)
232 .registerClusterSingletonService(any());
234 doReturn(clusterRegistration).when(clusterSingletonServiceProvider).registerClusterSingletonService(any());
236 netconfTopologyManager.onDataTreeChanged(changes);
239 verify(clusterSingletonServiceProvider, times(3))
240 .registerClusterSingletonService(any());
241 assertTrue(contexts.isEmpty());
242 assertTrue(clusterRegistrations.isEmpty());
244 verify(clusterSingletonServiceProvider, times(2))
245 .registerClusterSingletonService(any());
247 // only two created contexts
248 assertEquals(2, contexts.size());
249 assertTrue(contexts.containsKey(rootIdentifier.getRootIdentifier()));
250 assertTrue(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
252 // only two created cluster registrations
253 assertEquals(2, clusterRegistrations.size());
254 assertTrue(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
255 assertTrue(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
257 // after delete there should be no context and clustered registrations
258 doReturn(DELETE).when(objectModification).getModificationType();
260 doNothing().when(clusterRegistration).close();
262 netconfTopologyManager.onDataTreeChanged(changes);
264 verify(clusterRegistration, times(2)).close();
266 // empty map of contexts
267 assertTrue(contexts.isEmpty());
268 assertFalse(contexts.containsKey(rootIdentifier.getRootIdentifier()));
269 assertFalse(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
271 // empty map of clustered registrations
272 assertTrue(clusterRegistrations.isEmpty());
273 assertFalse(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
274 assertFalse(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
278 private class CustomTreeModification implements DataTreeModification<Node> {
280 private final DataTreeIdentifier<Node> rootPath;
281 private final DataObjectModification<Node> rootNode;
283 CustomTreeModification(final DataTreeIdentifier<Node> rootPath, final DataObjectModification<Node> rootNode) {
284 this.rootPath = rootPath;
285 this.rootNode = rootNode;
290 public DataTreeIdentifier<Node> getRootPath() {
296 public DataObjectModification<Node> getRootNode() {