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 = new SimpleDateFormat(SimplifiedOperationalListener.DATE_AND_TIME_FORMAT);
63 private SyncReactor reactor;
65 private ReadOnlyTransaction roTx;
67 private DataTreeModification<Node> dataTreeModification;
69 private DataObjectModification<Node> operationalModification;
71 private FlowCapableNode configNode;
73 private Node operationalNode;
75 private FlowCapableNode fcOperationalNode;
77 private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
79 private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
81 private ReconciliationRegistry reconciliationRegistry;
83 private DeviceMastershipManager deviceMastershipManager;
85 private List nodeConnector;
87 private Node operationalNodeEmpty;
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));
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);
101 final DataTreeIdentifier<Node> dataTreeIdentifier =
102 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
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);
112 public void testDSLogicalType() throws Exception {
113 Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
117 public void testOnDataTreeChangedAddPhysical() {
119 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
120 Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
121 Mockito.verifyZeroInteractions(reactor);
125 public void testOnDataTreeChangedDeletePhysical() throws Exception {
126 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
127 Mockito.when(operationalModification.getDataAfter()).thenReturn(null);
129 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
131 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
132 Mockito.verifyZeroInteractions(reactor);
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);
143 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
145 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
146 Mockito.verifyZeroInteractions(reactor);
150 public void testOnDataTreeChangedReconcileNotRegistered() {
151 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
154 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
156 Mockito.verifyZeroInteractions(reactor);
160 public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
161 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
163 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
165 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
167 Mockito.verifyZeroInteractions(reactor);
171 public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
172 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
174 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
175 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
177 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
179 Mockito.verifyZeroInteractions(reactor);
183 public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
184 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
186 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
187 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
188 Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
190 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
192 Mockito.verifyZeroInteractions(reactor);
196 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
197 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
199 prepareFreshOperational(false);
201 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
203 Mockito.verifyZeroInteractions(reactor);
207 public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
208 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
210 prepareFreshOperational(true);
211 final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(configNode, configDS, fcOperationalNode, operationalDS);
213 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
215 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
216 Mockito.verify(roTx).close();
221 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresentButAdd() throws ParseException {
222 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
224 prepareFreshOperational(false);
225 final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(configNode, configDS, fcOperationalNode, operationalDS);
227 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
229 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
230 Mockito.verify(roTx).close();
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);
238 prepareFreshOperational(true);
240 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
241 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
243 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
245 Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
246 Mockito.verifyZeroInteractions(reactor);
247 Mockito.verify(roTx).close();
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));
261 Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
262 Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampAfter));
266 private void operationalAdd() {
267 Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
268 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
271 private void operationalUpdate() {
272 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
273 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
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));