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