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