Bug 6745 SimplifiedOperationalListener optimation
[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.Matchers;
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 = new SimpleDateFormat(SimplifiedOperationalListener.DATE_AND_TIME_FORMAT);
61
62     @Mock
63     private SyncReactor reactor;
64     @Mock
65     private ReadOnlyTransaction roTx;
66     @Mock
67     private DataTreeModification<Node> dataTreeModification;
68     @Mock
69     private DataObjectModification<Node> operationalModification;
70     @Mock
71     private FlowCapableNode configNode;
72     @Mock
73     private Node operationalNode;
74     @Mock
75     private FlowCapableNode fcOperationalNode;
76     @Mock
77     private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
78     @Mock
79     private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
80     @Mock
81     private ReconciliationRegistry reconciliationRegistry;
82     @Mock
83     private DeviceMastershipManager deviceMastershipManager;
84     @Mock
85     private List nodeConnector;
86     @Mock
87     private Node operationalNodeEmpty;
88
89     @Before
90     public void setUp() throws Exception {
91         final DataBroker db = Mockito.mock(DataBroker.class);
92         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
93         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
94         final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
95                 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.CONFIGURATION));
96
97         nodeListenerOperational = new SimplifiedOperationalListener(reactor, operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
98         InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID));
99         fcNodePath = nodePath.augmentation(FlowCapableNode.class);
100
101         final DataTreeIdentifier<Node> dataTreeIdentifier =
102                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
103
104         Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
105         Mockito.when(operationalNode.getId()).thenReturn(NODE_ID);
106         Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
107         Mockito.when(dataTreeModification.getRootNode()).thenReturn(operationalModification);
108         Mockito.when(operationalNode.getAugmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
109     }
110
111     @Test
112     public void testDSLogicalType() throws Exception {
113         Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
114     }
115
116     @Test
117     public void testOnDataTreeChangedAddPhysical() {
118         operationalAdd();
119         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
120         Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
121         Mockito.verifyZeroInteractions(reactor);
122     }
123
124     @Test
125     public void testOnDataTreeChangedDeletePhysical() throws Exception {
126         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
127         Mockito.when(operationalModification.getDataAfter()).thenReturn(null);
128
129         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
130
131         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
132         Mockito.verifyZeroInteractions(reactor);
133     }
134
135     @Test
136     public void testOnDataTreeChangedDeleteLogical() {
137         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
138         Mockito.when(operationalNode.getNodeConnector()).thenReturn(nodeConnector);
139         Mockito.when(operationalNodeEmpty.getId()).thenReturn(NODE_ID);
140         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNodeEmpty);
141         Mockito.when(operationalNodeEmpty.getNodeConnector()).thenReturn(null);
142
143         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
144
145         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
146         Mockito.verifyZeroInteractions(reactor);
147     }
148
149     @Test
150     public void testOnDataTreeChangedReconcileNotRegistered() {
151         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
152         operationalUpdate();
153
154         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
155
156         Mockito.verifyZeroInteractions(reactor);
157     }
158
159     @Test
160     public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
161         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
162         operationalUpdate();
163         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
164
165         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
166
167         Mockito.verifyZeroInteractions(reactor);
168     }
169
170     @Test
171     public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
172         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
173         operationalUpdate();
174         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
175         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
176
177         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
178
179         Mockito.verifyZeroInteractions(reactor);
180     }
181
182     @Test
183     public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
184         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
185         operationalUpdate();
186         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
187         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
188         Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
189
190         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
191
192         Mockito.verifyZeroInteractions(reactor);
193     }
194
195     @Test
196     public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
197         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
198         operationalUpdate();
199         prepareFreshOperational(false);
200
201         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
202
203         Mockito.verifyZeroInteractions(reactor);
204     }
205
206     @Test
207     public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
208         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
209         operationalUpdate();
210         prepareFreshOperational(true);
211         final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(configNode, configDS, fcOperationalNode, operationalDS);
212
213         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
214
215         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
216         Mockito.verify(roTx).close();
217     }
218
219
220     @Test
221     public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresentButAdd() throws ParseException {
222         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
223         operationalAdd();
224         prepareFreshOperational(false);
225         final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(configNode, configDS, fcOperationalNode, operationalDS);
226
227         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
228
229         Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
230         Mockito.verify(roTx).close();
231     }
232
233     @Test
234     public void testOnDataTreeChangedReconcileAndConfigNotPresent() throws Exception {
235         // Related to bug 5920 -> https://bugs.opendaylight.org/show_bug.cgi?id=5920
236         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
237         operationalUpdate();
238         prepareFreshOperational(true);
239
240         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
241                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
242
243         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
244
245         Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
246         Mockito.verifyZeroInteractions(reactor);
247         Mockito.verify(roTx).close();
248     }
249
250     private void prepareFreshOperational(final boolean afterRegistration) throws ParseException {
251         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
252         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
253         Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(true);
254         Mockito.when(snapshotGatheringStatusEnd.getEnd()).thenReturn(Mockito.mock(DateAndTime.class));
255         final String timestampBefore = "0000-12-12T01:01:01.000-07:00";
256         final String timestampAfter = "9999-12-12T01:01:01.000-07:00";
257         if (afterRegistration) {
258             Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampAfter);
259             Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampBefore));
260         } else {
261             Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
262             Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampAfter));
263         }
264     }
265
266     private void operationalAdd() {
267         Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
268         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
269     }
270
271     private void operationalUpdate() {
272         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
273         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
274     }
275
276     private SyncupEntry loadConfigDSAndPrepareSyncupEntry(final FlowCapableNode after, final LogicalDatastoreType dsTypeAfter,
277                                                           final FlowCapableNode before, final LogicalDatastoreType dsTypeBefore) {
278         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
279                 .thenReturn(Futures.immediateCheckedFuture(Optional.of(configNode)));
280         final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
281         Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Mockito.eq(syncupEntry)))
282                 .thenReturn(Futures.immediateFuture(Boolean.TRUE));
283         return syncupEntry;
284     }
285 }