2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
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;
50 * Test for {@link SimplifiedOperationalListener}.
52 @RunWith(MockitoJUnitRunner.class)
53 public class SimplifiedOperationalListenerTest {
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);
64 private SyncReactor reactor;
66 private ReadOnlyTransaction roTx;
68 private DataTreeModification<Node> dataTreeModification;
70 private DataObjectModification<Node> operationalModification;
72 private FlowCapableNode configNode;
74 private Node operationalNode;
76 private FlowCapableNode fcOperationalNode;
78 private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
80 private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
82 private ReconciliationRegistry reconciliationRegistry;
84 private DeviceMastershipManager deviceMastershipManager;
86 private List nodeConnector;
88 private Node operationalNodeEmpty;
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));
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);
104 final DataTreeIdentifier<Node> dataTreeIdentifier =
105 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
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.getAugmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
115 public void testDSLogicalType() throws Exception {
116 Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
120 public void testOnDataTreeChangedAddPhysical() {
122 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
123 Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
124 Mockito.verifyZeroInteractions(reactor);
128 public void testOnDataTreeChangedDeletePhysical() throws Exception {
129 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
130 Mockito.when(operationalModification.getDataAfter()).thenReturn(null);
132 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
134 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
135 Mockito.verifyZeroInteractions(reactor);
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);
146 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
148 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
149 Mockito.verifyZeroInteractions(reactor);
153 public void testOnDataTreeChangedReconcileNotRegistered() {
154 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
157 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
159 Mockito.verifyZeroInteractions(reactor);
163 public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
164 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
166 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
168 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
170 Mockito.verifyZeroInteractions(reactor);
174 public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
175 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
177 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class))
178 .thenReturn(statisticsGatheringStatus);
179 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
181 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
183 Mockito.verifyZeroInteractions(reactor);
187 public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
188 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
190 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class))
191 .thenReturn(statisticsGatheringStatus);
192 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
193 Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
195 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
197 Mockito.verifyZeroInteractions(reactor);
201 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
202 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
204 prepareFreshOperational(false);
206 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
208 Mockito.verifyZeroInteractions(reactor);
212 public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
213 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
215 prepareFreshOperational(true);
216 final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(
217 configNode, configDS, fcOperationalNode, operationalDS);
219 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
221 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
222 Mockito.verify(roTx).close();
227 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresentButAdd() throws ParseException {
228 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
230 prepareFreshOperational(false);
231 final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(
232 configNode, configDS, fcOperationalNode, operationalDS);
234 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
236 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
237 Mockito.verify(roTx).close();
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);
245 prepareFreshOperational(true);
247 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
248 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
250 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
252 Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
253 Mockito.verifyZeroInteractions(reactor);
254 Mockito.verify(roTx).close();
257 private void prepareFreshOperational(final boolean afterRegistration) throws ParseException {
258 Mockito.when(operationalNode.getAugmentation(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));
270 Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
271 Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID))
272 .thenReturn(simpleDateFormat.parse(timestampAfter));
276 private void operationalAdd() {
277 Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
278 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
281 private void operationalUpdate() {
282 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
283 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
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(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Mockito.eq(syncupEntry)))
293 .thenReturn(Futures.immediateFuture(Boolean.TRUE));