Remove old clustered netconf topology implementation
[netconf.git] / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / impl / NetconfTopologyImplTest.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.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.spy;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.collect.Sets;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import io.netty.util.concurrent.EventExecutor;
24 import io.netty.util.concurrent.Future;
25 import io.netty.util.concurrent.ImmediateEventExecutor;
26 import io.netty.util.concurrent.SucceededFuture;
27 import java.util.Collection;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
33 import org.opendaylight.controller.config.threadpool.ThreadPool;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
36 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
37 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.core.api.Broker;
42 import org.opendaylight.netconf.client.NetconfClientDispatcher;
43 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
45 import org.opendaylight.netconf.topology.SchemaRepositoryProvider;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.DataObject;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
66
67 public class NetconfTopologyImplTest {
68
69     private static final NodeId NODE_ID = new NodeId("testing-node");
70     private static final String TOPOLOGY_ID = "testing-topology";
71
72     @Mock
73     private Broker mockedDataBroker;
74
75     @Mock
76     private NetconfClientDispatcher mockedClientDispatcher;
77
78     @Mock
79     private BindingAwareBroker mockedBindingAwareBroker;
80
81     @Mock
82     private EventExecutor mockedEventExecutor;
83
84     @Mock
85     private ScheduledThreadPool mockedKeepaliveExecutor;
86
87     @Mock
88     private ThreadPool mockedProcessingExecutor;
89
90     @Mock
91     private SchemaRepositoryProvider mockedSchemaRepositoryProvider;
92
93
94     private TestingNetconfTopologyImpl topology;
95     private TestingNetconfTopologyImpl spyTopology;
96
97     @Before
98     public void setUp() {
99         MockitoAnnotations.initMocks(this);
100
101         when(mockedSchemaRepositoryProvider.getSharedSchemaRepository()).thenReturn(new SharedSchemaRepository("testingSharedSchemaRepo"));
102         when(mockedProcessingExecutor.getExecutor()).thenReturn(MoreExecutors.newDirectExecutorService());
103         Future future = new SucceededFuture(ImmediateEventExecutor.INSTANCE, new NetconfDeviceCapabilities());
104         when(mockedClientDispatcher.createReconnectingClient(any(NetconfReconnectingClientConfiguration.class))).thenReturn(future);
105
106         topology = new TestingNetconfTopologyImpl(TOPOLOGY_ID, mockedClientDispatcher, mockedBindingAwareBroker,
107                 mockedDataBroker, mockedEventExecutor, mockedKeepaliveExecutor, mockedProcessingExecutor, mockedSchemaRepositoryProvider);
108
109         spyTopology = spy(topology);
110     }
111
112     @Test
113     public void testOnSessionInitiated() {
114         BindingAwareBroker.ProviderContext session = mock(BindingAwareBroker.ProviderContext.class);
115         DataBroker dataBroker = mock(DataBroker.class);
116         when(session.getSALService(DataBroker.class)).thenReturn(dataBroker);
117         WriteTransaction wtx = mock(WriteTransaction.class);
118         when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
119         doNothing().when(wtx).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
120         when(wtx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
121         topology.onSessionInitiated(session);
122
123         //verify initialization of topology
124         final InstanceIdentifier<NetworkTopology> networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build();
125         final Topology topo = new TopologyBuilder().setTopologyId(new TopologyId(TOPOLOGY_ID)).build();
126         final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
127         verify(wtx).merge(LogicalDatastoreType.CONFIGURATION, networkTopologyId, networkTopology);
128         verify(wtx).merge(LogicalDatastoreType.OPERATIONAL, networkTopologyId, networkTopology);
129         verify(wtx).merge(LogicalDatastoreType.CONFIGURATION, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
130         verify(wtx).merge(LogicalDatastoreType.OPERATIONAL, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(TOPOLOGY_ID))), topo);
131     }
132
133     @Test
134     public void testOnDataTreeChange() {
135
136         DataObjectModification<Node> newNode = mock(DataObjectModification.class);
137         when(newNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
138
139         InstanceIdentifier.PathArgument pa = null;
140
141         for (InstanceIdentifier.PathArgument p : TopologyUtil.createTopologyListPath(TOPOLOGY_ID).child(Node.class, new NodeKey(NODE_ID)).getPathArguments()) {
142             pa = p;
143         }
144
145         when(newNode.getIdentifier()).thenReturn(pa);
146
147
148         NetconfNode testingNode = new NetconfNodeBuilder()
149                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
150                 .setPort(new PortNumber(9999))
151                 .setReconnectOnChangedSchema(true)
152                 .setDefaultRequestTimeoutMillis(1000L)
153                 .setBetweenAttemptsTimeoutMillis(100)
154                 .setKeepaliveDelay(1000L)
155                 .setTcpOnly(true)
156                 .setCredentials(new LoginPasswordBuilder().setUsername("testuser").setPassword("testpassword").build())
157                 .build();
158
159         NodeBuilder nn = new NodeBuilder().addAugmentation(NetconfNode.class, testingNode);
160
161         when(newNode.getDataAfter()).thenReturn(nn.build());
162
163
164
165         Collection<DataTreeModification<Node>> changes = Sets.newHashSet();
166         DataTreeModification<Node> ch = mock(DataTreeModification.class);
167         when(ch.getRootNode()).thenReturn(newNode);
168         changes.add(ch);
169         spyTopology.onDataTreeChanged(changes);
170         verify(spyTopology).connectNode(TopologyUtil.getNodeId(pa), nn.build());
171
172         when(newNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
173         spyTopology.onDataTreeChanged(changes);
174         verify(spyTopology).disconnectNode(TopologyUtil.getNodeId(pa));
175
176         when(newNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
177         spyTopology.onDataTreeChanged(changes);
178
179         //one in previous creating and deleting node and one in updating
180         verify(spyTopology, times(2)).disconnectNode(TopologyUtil.getNodeId(pa));
181         verify(spyTopology, times(2)).connectNode(TopologyUtil.getNodeId(pa), nn.build());
182
183
184     }
185
186     public static class TestingNetconfTopologyImpl extends NetconfTopologyImpl {
187
188         public TestingNetconfTopologyImpl(String topologyId, NetconfClientDispatcher clientDispatcher, BindingAwareBroker bindingAwareBroker, Broker domBroker, EventExecutor eventExecutor, ScheduledThreadPool keepaliveExecutor, ThreadPool processingExecutor, SchemaRepositoryProvider schemaRepositoryProvider) {
189             super(topologyId, clientDispatcher, bindingAwareBroker, domBroker, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider);
190         }
191
192         @Override
193         public ListenableFuture<NetconfDeviceCapabilities> connectNode(NodeId nodeId, Node configNode) {
194             return Futures.immediateFuture(new NetconfDeviceCapabilities());
195         }
196
197         @Override
198         public ListenableFuture<Void> disconnectNode(NodeId nodeId) {
199             return Futures.immediateFuture(null);
200         }
201     }
202
203 }