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