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.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.node.NodeConnector;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 * Test for {@link SimplifiedOperationalListener}.
53 @RunWith(MockitoJUnitRunner.class)
54 public class SimplifiedOperationalListenerTest {
56 private static final NodeId NODE_ID = new NodeId("testNode");
57 private InstanceIdentifier<FlowCapableNode> fcNodePath;
58 private SimplifiedOperationalListener nodeListenerOperational;
59 private final LogicalDatastoreType dsType = 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;
86 public void setUp() throws Exception {
87 final DataBroker db = Mockito.mock(DataBroker.class);
88 final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
89 final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
90 final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
91 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.CONFIGURATION));
93 nodeListenerOperational = new SimplifiedOperationalListener(reactor, operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
94 InstanceIdentifier<Node> nodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID));
95 fcNodePath = nodePath.augmentation(FlowCapableNode.class);
97 final DataTreeIdentifier<Node> dataTreeIdentifier =
98 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
100 Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
101 Mockito.when(operationalNode.getId()).thenReturn(NODE_ID);
102 Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
103 Mockito.when(dataTreeModification.getRootNode()).thenReturn(operationalModification);
104 Mockito.when(operationalNode.getAugmentation(FlowCapableNode.class)).thenReturn(fcOperationalNode);
108 public void testDSLogicalType() throws Exception {
109 Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
113 public void testOnDataTreeChangeAddSyncup() throws Exception {
114 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
116 prepareFreshOperational(true);
118 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
119 .thenReturn(Futures.immediateCheckedFuture(Optional.of(configNode)));
120 Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Matchers.<FlowCapableNode>any(),
121 Matchers.<FlowCapableNode>any(), Matchers.<LogicalDatastoreType>any())).thenReturn(Futures.immediateFuture(Boolean.TRUE));
123 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
125 Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
126 Mockito.verify(reactor).syncup(fcNodePath, configNode, fcOperationalNode, dsType);
127 Mockito.verify(roTx).close();
131 public void testOnDataTreeChangedAddSkip() throws Exception {
132 // Related to bug 5920 -> https://bugs.opendaylight.org/show_bug.cgi?id=5920
133 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
135 prepareFreshOperational(true);
137 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
138 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
140 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
142 Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
143 Mockito.verifyZeroInteractions(reactor);
144 Mockito.verify(roTx).close();
148 public void testOnDataTreeChangedSyncupDeletePhysical() throws Exception {
149 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
150 Mockito.when(operationalModification.getDataAfter()).thenReturn(null);
151 Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.DELETE);
152 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
154 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
156 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
157 Mockito.verifyZeroInteractions(reactor);
161 public void testOnDataTreeChangedSyncupDeleteLogical() {
162 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
163 List<NodeConnector> nodeConnectorList = Mockito.mock(List.class);
164 Mockito.when(operationalNode.getNodeConnector()).thenReturn(nodeConnectorList);
165 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
167 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
169 Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
170 Mockito.verifyZeroInteractions(reactor);
174 public void testOnDataTreeChangedReconcileNotRegistered() {
176 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
178 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
180 Mockito.verifyZeroInteractions(reactor);
184 public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
185 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
187 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
189 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
191 Mockito.verifyZeroInteractions(reactor);
195 public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
196 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
198 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
199 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
201 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
203 Mockito.verifyZeroInteractions(reactor);
207 public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
208 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
210 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
211 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
212 Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
214 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
216 Mockito.verifyZeroInteractions(reactor);
220 public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
221 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
223 prepareFreshOperational(false);
225 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
227 Mockito.verifyZeroInteractions(reactor);
231 public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
232 Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
234 prepareFreshOperational(true);
236 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
237 .thenReturn(Futures.immediateCheckedFuture(Optional.of(configNode)));
238 Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Matchers.<FlowCapableNode>any(),
239 Matchers.<FlowCapableNode>any(), Matchers.<LogicalDatastoreType>any())).thenReturn(Futures.immediateFuture(Boolean.TRUE));
241 nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
243 Mockito.verify(reactor).syncup(fcNodePath, configNode, fcOperationalNode, dsType);
244 Mockito.verify(roTx).close();
247 private void prepareFreshOperational(final boolean afterRegistration) throws ParseException {
248 Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
249 Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
250 Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(true);
251 Mockito.when(snapshotGatheringStatusEnd.getEnd()).thenReturn(Mockito.mock(DateAndTime.class));
252 final String timestampBefore = "0000-12-12T01:01:01.000-07:00";
253 final String timestampAfter = "9999-12-12T01:01:01.000-07:00";
254 if (afterRegistration) {
255 Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampAfter);
256 Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampBefore));
258 Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
259 Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampAfter));
263 private void operationalAdd() {
264 Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
265 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
268 private void operationalUpdate() {
269 Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
270 Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);