Use base64 encoding for netconf device passwords
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / NetconfDeviceTopologyAdapterTest.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.netconf.topology.spi;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19
20 import com.google.common.util.concurrent.Futures;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.TransactionChain;
32 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.common.api.CommitInfo;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCapabilities;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.Empty;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49
50 @RunWith(MockitoJUnitRunner.StrictStubs.class)
51 public class NetconfDeviceTopologyAdapterTest {
52     private static final KeyedInstanceIdentifier<Topology, TopologyKey> TEST_TOPOLOGY_ID =
53         // FIXME: do not use this constant
54         NetconfNodeUtils.DEFAULT_TOPOLOGY_IID;
55     private final RemoteDeviceId id = new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
56
57     @Mock
58     private DataBroker mockBroker;
59     @Mock
60     private TransactionChain mockChain;
61     @Mock
62     private WriteTransaction mockTx;
63     @Captor
64     private ArgumentCaptor<TransactionChainListener> listeners;
65
66     private NetconfDeviceTopologyAdapter adapter;
67
68     @Before
69     public void before() {
70         doReturn(mockTx).when(mockChain).newWriteOnlyTransaction();
71         // FIXME: exact match
72         doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
73             any(Node.class));
74         doReturn("test transaction").when(mockTx).getIdentifier();
75         doReturn(CommitInfo.emptyFluentFuture()).when(mockTx).commit();
76
77         doReturn(mockChain).when(mockBroker).createMergingTransactionChain(listeners.capture());
78         adapter = new NetconfDeviceTopologyAdapter(mockBroker, TEST_TOPOLOGY_ID, id);
79     }
80
81     @Test
82     public void replaceChainIfFailed() {
83         doNothing().when(mockChain).close();
84         doReturn("mockChain").when(mockChain).toString();
85         adapter.onTransactionChainFailed(mockChain, mockTx, new Exception("chain failed"));
86         verify(mockBroker, times(2)).createMergingTransactionChain(any());
87     }
88
89     @Test
90     public void testFailedDevice() {
91         // FIXME: exact match
92         doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
93             any(NetconfNode.class));
94
95         adapter.setDeviceAsFailed(null);
96
97         verify(mockChain, times(2)).newWriteOnlyTransaction();
98         // FIXME: LogicationDataStoreStype, concrete identifier, concreate (or captured/asserted) data
99         verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
100             any(Node.class));
101     }
102
103     @Test
104     public void testDeviceUpdate() throws Exception {
105         // FIXME: exact match
106         doNothing().when(mockTx).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
107             any(NetconfNode.class));
108         adapter.updateDeviceData(true, NetconfDeviceCapabilities.empty(), new SessionIdType(Uint32.ONE));
109
110         verify(mockChain, times(2)).newWriteOnlyTransaction();
111         verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
112         verify(mockTx, times(1)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class));
113     }
114
115     @Test
116     public void testRemoveDeviceConfiguration() throws Exception {
117         // FIXME: exact match
118         doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
119         doNothing().when(mockChain).close();
120
121         final var future = adapter.shutdown();
122         verify(mockChain, times(2)).newWriteOnlyTransaction();
123         verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
124             TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
125         verify(mockTx, times(2)).commit();
126         verify(mockChain).close();
127
128         assertFalse(future.isDone());
129
130         // Idempotent
131         assertSame(future, adapter.shutdown());
132
133         // future completes
134         listeners.getValue().onTransactionChainSuccessful(mockChain);
135         assertSame(Empty.value(), Futures.getDone(future));
136     }
137
138     @Test
139     public void testShutdownCompletion() throws Exception {
140         // FIXME: exact match
141         doNothing().when(mockTx).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
142         doNothing().when(mockChain).close();
143
144         final var future = adapter.shutdown();
145         verify(mockChain, times(2)).newWriteOnlyTransaction();
146         verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL,
147             TEST_TOPOLOGY_ID.child(Node.class, new NodeKey(new NodeId(id.name()))));
148         verify(mockTx, times(2)).commit();
149         verify(mockChain).close();
150
151         assertFalse(future.isDone());
152
153         // Idempotent
154         assertSame(future, adapter.shutdown());
155
156         // future completes
157         final var cause = new Throwable();
158         listeners.getValue().onTransactionChainFailed(mockChain, mockTx, cause);
159         final var ex = assertThrows(ExecutionException.class, () -> Futures.getDone(future));
160         assertSame(cause, ex.getCause());
161     }
162 }