fec3204045cd2edf87db43133dfae0e9e038181c
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedOperationalListenerTest.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.openflowplugin.applications.frsync.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import java.text.ParseException;
14 import java.text.SimpleDateFormat;
15 import java.util.Collections;
16 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentMatchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
32 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
33 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
34 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
35 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
36 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
37 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
38 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEnd;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48
49 /**
50  * Test for {@link SimplifiedOperationalListener}.
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class SimplifiedOperationalListenerTest {
54
55     private static final NodeId NODE_ID = new NodeId("testNode");
56     private InstanceIdentifier<FlowCapableNode> fcNodePath;
57     private SimplifiedOperationalListener nodeListenerOperational;
58     private final LogicalDatastoreType configDS = LogicalDatastoreType.CONFIGURATION;
59     private final LogicalDatastoreType operationalDS = LogicalDatastoreType.OPERATIONAL;
60     private final SimpleDateFormat simpleDateFormat =
61             new SimpleDateFormat(SimplifiedOperationalListener.DATE_AND_TIME_FORMAT);
62
63     @Mock
64     private SyncReactor reactor;
65     @Mock
66     private ReadOnlyTransaction roTx;
67     @Mock
68     private DataTreeModification<Node> dataTreeModification;
69     @Mock
70     private DataObjectModification<Node> operationalModification;
71     @Mock
72     private FlowCapableNode configNode;
73     @Mock
74     private Node operationalNode;
75     @Mock
76     private FlowCapableNode fcOperationalNode;
77     @Mock
78     private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
79     @Mock
80     private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
81     @Mock
82     private ReconciliationRegistry reconciliationRegistry;
83     @Mock
84     private DeviceMastershipManager deviceMastershipManager;
85     @Mock
86     private List nodeConnector;
87     @Mock
88     private Node operationalNodeEmpty;
89
90     @Before
91     public void setUp() throws Exception {
92         final DataBroker db = Mockito.mock(DataBroker.class);
93         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
94         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
95         final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
96                 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.CONFIGURATION));
97
98         nodeListenerOperational = new SimplifiedOperationalListener(reactor, operationalSnapshot, configDao,
99                 reconciliationRegistry, deviceMastershipManager);
100         InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class).child(Node.class,
101                 new NodeKey(NODE_ID));
102         fcNodePath = nodePath.augmentation(FlowCapableNode.class);
103
104         final DataTreeIdentifier<Node> dataTreeIdentifier =
105                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
106
107         Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
108         Mockito.when(operationalNode.getId()).thenReturn(NODE_ID);
109         Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
110         Mockito.when(dataTreeModification.getRootNode()).thenReturn(operationalModification);
111         Mockito.when(operationalNode.augmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
112     }
113
114     @Test
115     public void testDSLogicalType() throws Exception {
116         Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
117     }
118
119     @Test
120     public void testOnDataTreeChangedAddPhysical() {
121         operationalAdd();
122         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
123         Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
124         Mockito.verifyZeroInteractions(reactor);
125     }
126
127     @Test
128     public void testOnDataTreeChangedDeletePhysical() throws Exception {
129         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
130         Mockito.when(operationalModification.getDataAfter()).thenReturn(null);
131
132         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
133
134         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
135         Mockito.verifyZeroInteractions(reactor);
136     }
137
138     @Test
139     public void testOnDataTreeChangedDeleteLogical() {
140         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
141         Mockito.when(operationalNode.getNodeConnector()).thenReturn(nodeConnector);
142         Mockito.when(operationalNodeEmpty.getId()).thenReturn(NODE_ID);
143         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNodeEmpty);
144         Mockito.when(operationalNodeEmpty.getNodeConnector()).thenReturn(null);
145
146         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
147
148         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
149         Mockito.verifyZeroInteractions(reactor);
150     }
151
152     @Test
153     public void testOnDataTreeChangedReconcileNotRegistered() {
154         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
155         operationalUpdate();
156
157         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
158
159         Mockito.verifyZeroInteractions(reactor);
160     }
161
162     @Test
163     public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
164         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
165         operationalUpdate();
166         Mockito.when(operationalNode.augmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
167
168         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
169
170         Mockito.verifyZeroInteractions(reactor);
171     }
172
173     @Test
174     public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
175         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
176         operationalUpdate();
177         Mockito.when(operationalNode.augmentation(FlowCapableStatisticsGatheringStatus.class))
178             .thenReturn(statisticsGatheringStatus);
179         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
180
181         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
182
183         Mockito.verifyZeroInteractions(reactor);
184     }
185
186     @Test
187     public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
188         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
189         operationalUpdate();
190         Mockito.when(operationalNode.augmentation(FlowCapableStatisticsGatheringStatus.class))
191             .thenReturn(statisticsGatheringStatus);
192         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
193         Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
194
195         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
196
197         Mockito.verifyZeroInteractions(reactor);
198     }
199
200     @Test
201     public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
202         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
203         operationalUpdate();
204         prepareFreshOperational(false);
205
206         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
207
208         Mockito.verifyZeroInteractions(reactor);
209     }
210
211     @Test
212     public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
213         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
214         operationalUpdate();
215         prepareFreshOperational(true);
216         final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(
217                 configNode, configDS, fcOperationalNode, operationalDS);
218
219         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
220
221         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
222         Mockito.verify(roTx).close();
223     }
224
225
226     @Test
227     public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresentButAdd() throws ParseException {
228         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
229         operationalAdd();
230         prepareFreshOperational(false);
231         final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(
232                 configNode, configDS, fcOperationalNode, operationalDS);
233
234         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
235
236         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
237         Mockito.verify(roTx).close();
238     }
239
240     @Test
241     public void testOnDataTreeChangedReconcileAndConfigNotPresent() throws Exception {
242         // Related to bug 5920 -> https://bugs.opendaylight.org/show_bug.cgi?id=5920
243         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
244         operationalUpdate();
245         prepareFreshOperational(true);
246
247         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
248                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
249
250         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
251
252         Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
253         Mockito.verifyZeroInteractions(reactor);
254         Mockito.verify(roTx).close();
255     }
256
257     private void prepareFreshOperational(final boolean afterRegistration) throws ParseException {
258         Mockito.when(operationalNode.augmentation(FlowCapableStatisticsGatheringStatus.class))
259              .thenReturn(statisticsGatheringStatus);
260         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
261         Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(true);
262         Mockito.when(snapshotGatheringStatusEnd.getEnd()).thenReturn(Mockito.mock(DateAndTime.class));
263         final String timestampBefore = "0000-12-12T01:01:01.000-07:00";
264         final String timestampAfter = "9999-12-12T01:01:01.000-07:00";
265         if (afterRegistration) {
266             Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampAfter);
267             Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID))
268                 .thenReturn(simpleDateFormat.parse(timestampBefore));
269         } else {
270             Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
271             Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID))
272                 .thenReturn(simpleDateFormat.parse(timestampAfter));
273         }
274     }
275
276     private void operationalAdd() {
277         Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
278         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
279     }
280
281     private void operationalUpdate() {
282         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
283         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
284     }
285
286     private SyncupEntry loadConfigDSAndPrepareSyncupEntry(final FlowCapableNode after,
287             final LogicalDatastoreType dsTypeAfter, final FlowCapableNode before,
288             final LogicalDatastoreType dsTypeBefore) {
289         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
290                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(configNode)));
291         final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
292         Mockito.when(reactor.syncup(ArgumentMatchers.<InstanceIdentifier<FlowCapableNode>>any(),
293                 Mockito.eq(syncupEntry))).thenReturn(Futures.immediateFuture(Boolean.TRUE));
294         return syncupEntry;
295     }
296 }