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.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;
52 * Test for {@link SimplifiedOperationalListener}.
54 @RunWith(MockitoJUnitRunner.class)
55 public class SimplifiedOperationalListenerTest {
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);
65 private SyncReactor reactor;
67 private ReadOnlyTransaction roTx;
69 private DataTreeModification<Node> dataTreeModification;
71 private DataObjectModification<Node> operationalModification;
73 private FlowCapableNode configNode;
75 private Node operationalNode;
77 private FlowCapableNode fcOperationalNode;
79 private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
81 private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
83 private ReconciliationRegistry reconciliationRegistry;
85 private DeviceMastershipManager deviceMastershipManager;
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));
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);
99 final DataTreeIdentifier<Node> dataTreeIdentifier =
100 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
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);
110 public void testDSLogicalType() throws Exception {
111 Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
115 public void testOnDataTreeChangedAddPhysical() {
117 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
118 Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
119 Mockito.verifyZeroInteractions(reactor);
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);
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 List<NodeConnector> nodeConnectorList = Mockito.mock(List.class);
139 Mockito.when(operationalNode.getNodeConnector()).thenReturn(nodeConnectorList);
140 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
142 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
144 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
145 Mockito.verifyZeroInteractions(reactor);
149 public void testOnDataTreeChangedReconcileNotRegistered() {
150 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
153 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
155 Mockito.verifyZeroInteractions(reactor);
159 public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
160 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
162 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
164 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
166 Mockito.verifyZeroInteractions(reactor);
170 public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
171 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
173 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
174 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
176 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
178 Mockito.verifyZeroInteractions(reactor);
182 public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
183 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
185 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
186 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
187 Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
189 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
191 Mockito.verifyZeroInteractions(reactor);
195 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
196 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
198 prepareFreshOperational(false);
200 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
202 Mockito.verifyZeroInteractions(reactor);
206 public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
207 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
209 prepareFreshOperational(true);
210 final SyncupEntry syncupEntry = loadConfigDSAndPrepareSyncupEntry(configNode, configDS, fcOperationalNode, operationalDS);
212 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
214 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
215 Mockito.verify(roTx).close();
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);
223 prepareFreshOperational(true);
225 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
226 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
228 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
230 Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
231 Mockito.verifyZeroInteractions(reactor);
232 Mockito.verify(roTx).close();
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));
246 Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
247 Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampAfter));
251 private void operationalAdd() {
252 Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
253 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
256 private void operationalUpdate() {
257 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
258 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
261 private SyncupEntry loadConfigDSAndPrepareSyncupEntry(final FlowCapableNode after, final LogicalDatastoreType dsTypeAfter,
262 final FlowCapableNode before, final LogicalDatastoreType dsTypeBefore) throws InterruptedException {
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));