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.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;
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;
30 import javax.annotation.Nonnull;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
35 import org.opendaylight.controller.cluster.ActorSystemProvider;
36 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
37 import org.opendaylight.controller.config.threadpool.ThreadPool;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
40 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
41 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
42 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
45 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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;
65 public class NetconfTopologyManagerTest {
67 private final String topologyId = "topologyID";
68 private NetconfTopologyManager netconfTopologyManager;
71 private DataBroker dataBroker;
74 private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
80 final RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class);
81 final ScheduledThreadPool keepaliveExecutor = mock(ScheduledThreadPool.class);
82 final ThreadPool processingExecutor = mock(ThreadPool.class);
83 final ActorSystemProvider actorSystemProvider = mock(ActorSystemProvider.class);
84 final EventExecutor eventExecutor = mock(EventExecutor.class);
85 final NetconfClientDispatcher clientDispatcher = mock(NetconfClientDispatcher.class);
86 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
87 final AAAEncryptionService encryptionService = mock(AAAEncryptionService.class);
89 final Config config = new ConfigBuilder().setWriteTransactionIdleTimeout(0).build();
90 netconfTopologyManager = new NetconfTopologyManager(dataBroker, rpcProviderRegistry,
91 clusterSingletonServiceProvider, keepaliveExecutor, processingExecutor,
92 actorSystemProvider, eventExecutor, clientDispatcher, topologyId, config,
93 mountPointService, encryptionService);
97 public void testWriteConfiguration() throws Exception {
99 final ClusterSingletonServiceRegistration clusterRegistration = mock(ClusterSingletonServiceRegistration.class);
101 final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts");
102 fieldContexts.setAccessible(true);
103 @SuppressWarnings("unchecked")
104 final Map<InstanceIdentifier<Node>, NetconfTopologyContext> contexts =
105 (Map<InstanceIdentifier<Node>, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager);
107 final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations");
108 fieldClusterRegistrations.setAccessible(true);
109 @SuppressWarnings("unchecked")
110 final Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration> clusterRegistrations =
111 (Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration>)
112 fieldClusterRegistrations.get(netconfTopologyManager);
114 final Collection<DataTreeModification<Node>> changes = new ArrayList<>();
116 final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
117 new NodeKey(new NodeId("node-id-1")),"topology-1");
119 final InstanceIdentifier<Node> instanceIdentifierDiferent = NetconfTopologyUtils.createTopologyNodeListPath(
120 new NodeKey(new NodeId("node-id-2")),"topology-2");
122 final DataTreeIdentifier<Node> rootIdentifier =
123 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
125 final DataTreeIdentifier<Node> rootIdentifierDifferent =
126 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceIdentifierDiferent);
128 @SuppressWarnings("unchecked")
129 final DataObjectModification<Node> objectModification = mock(DataObjectModification.class);
131 final NetconfNode netconfNode = new NetconfNodeBuilder()
132 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
133 .setPort(new PortNumber(9999))
134 .setReconnectOnChangedSchema(true)
135 .setDefaultRequestTimeoutMillis(1000L)
136 .setBetweenAttemptsTimeoutMillis(100)
137 .setSchemaless(false)
139 .setActorResponseWaitTime(10)
141 final Node node = new NodeBuilder().setNodeId(new NodeId("node-id"))
142 .addAugmentation(NetconfNode.class, netconfNode).build();
144 final Identifier key = new NodeKey(new NodeId("node-id"));
146 @SuppressWarnings("unchecked")
147 final InstanceIdentifier.IdentifiableItem<Node, NodeKey> pathArgument =
148 new InstanceIdentifier.IdentifiableItem(Node.class, key);
151 // testing WRITE on two identical rootIdentifiers and one different
153 changes.add(new CustomTreeModification(rootIdentifier, objectModification));
154 changes.add(new CustomTreeModification(rootIdentifier, objectModification));
155 changes.add(new CustomTreeModification(rootIdentifierDifferent, objectModification));
157 doReturn(WRITE).when(objectModification).getModificationType();
158 doReturn(node).when(objectModification).getDataAfter();
159 doReturn(pathArgument).when(objectModification).getIdentifier();
160 doReturn(clusterRegistration).when(clusterSingletonServiceProvider).registerClusterSingletonService(any());
162 netconfTopologyManager.onDataTreeChanged(changes);
164 verify(clusterSingletonServiceProvider, times(2)).registerClusterSingletonService(any());
166 // only two created contexts
167 assertEquals(2, contexts.size());
168 assertTrue(contexts.containsKey(rootIdentifier.getRootIdentifier()));
169 assertTrue(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
171 // only two created cluster registrations
172 assertEquals(2, contexts.size());
173 assertTrue(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
174 assertTrue(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
176 // after delete there should be no context and clustered registrations
177 doReturn(DELETE).when(objectModification).getModificationType();
179 doNothing().when(clusterRegistration).close();
181 netconfTopologyManager.onDataTreeChanged(changes);
183 verify(clusterRegistration, times(2)).close();
185 // empty map of contexts
186 assertTrue(contexts.isEmpty());
187 assertFalse(contexts.containsKey(rootIdentifier.getRootIdentifier()));
188 assertFalse(contexts.containsKey(rootIdentifierDifferent.getRootIdentifier()));
190 // empty map of clustered registrations
191 assertTrue(clusterRegistrations.isEmpty());
192 assertFalse(clusterRegistrations.containsKey(rootIdentifier.getRootIdentifier()));
193 assertFalse(clusterRegistrations.containsKey(rootIdentifierDifferent.getRootIdentifier()));
198 public void testRegisterDataTreeChangeListener() {
200 final WriteTransaction wtx = mock(WriteTransaction.class);
202 doReturn(wtx).when(dataBroker).newWriteOnlyTransaction();
203 doNothing().when(wtx).merge(any(), any(), any());
204 doReturn(Futures.immediateCheckedFuture(null)).when(wtx).submit();
205 doReturn(null).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
207 netconfTopologyManager.init();
209 // verify if listener is called with right parameters = registered on right path
211 verify(dataBroker, times(1)).registerDataTreeChangeListener(
212 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, NetconfTopologyUtils
213 .createTopologyListPath(topologyId).child(Node.class)), netconfTopologyManager);
218 public void testClose() throws Exception {
220 final Field fieldContexts = NetconfTopologyManager.class.getDeclaredField("contexts");
221 fieldContexts.setAccessible(true);
222 @SuppressWarnings("unchecked")
223 final Map<InstanceIdentifier<Node>, NetconfTopologyContext> contexts =
224 (Map<InstanceIdentifier<Node>, NetconfTopologyContext>) fieldContexts.get(netconfTopologyManager);
226 final Field fieldClusterRegistrations = NetconfTopologyManager.class.getDeclaredField("clusterRegistrations");
227 fieldClusterRegistrations.setAccessible(true);
228 @SuppressWarnings("unchecked")
229 final Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration> clusterRegistrations =
230 (Map<InstanceIdentifier<Node>, ClusterSingletonServiceRegistration>)
231 fieldClusterRegistrations.get(netconfTopologyManager);
233 final InstanceIdentifier<Node> instanceIdentifier = NetconfTopologyUtils.createTopologyNodeListPath(
234 new NodeKey(new NodeId("node-id-1")),"topology-1");
237 final NetconfTopologyContext context = mock(NetconfTopologyContext.class);
238 final ClusterSingletonServiceRegistration clusterRegistration =
239 mock(ClusterSingletonServiceRegistration.class);
240 contexts.put(instanceIdentifier, context);
241 clusterRegistrations.put(instanceIdentifier, clusterRegistration);
243 doNothing().when(context).closeFinal();
244 doNothing().when(clusterRegistration).close();
246 netconfTopologyManager.close();
247 verify(context, times(1)).closeFinal();
248 verify(clusterRegistration, times(1)).close();
250 assertTrue(contexts.isEmpty());
251 assertTrue(clusterRegistrations.isEmpty());
255 private class CustomTreeModification implements DataTreeModification<Node> {
257 private final DataTreeIdentifier<Node> rootPath;
258 private final DataObjectModification<Node> rootNode;
260 CustomTreeModification(final DataTreeIdentifier<Node> rootPath, final DataObjectModification<Node> rootNode) {
261 this.rootPath = rootPath;
262 this.rootNode = rootNode;
267 public DataTreeIdentifier<Node> getRootPath() {
273 public DataObjectModification<Node> getRootNode() {