Bug 6170 + Bug 5919 - made FRS cluster-aware
[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.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;
49
50 /**
51  * Test for {@link SimplifiedOperationalListener}.
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class SimplifiedOperationalListenerTest {
55
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);
61
62     @Mock
63     private SyncReactor reactor;
64     @Mock
65     private ReadOnlyTransaction roTx;
66     @Mock
67     private DataTreeModification<Node> dataTreeModification;
68     @Mock
69     private DataObjectModification<Node> operationalModification;
70     @Mock
71     private FlowCapableNode configNode;
72     @Mock
73     private Node operationalNode;
74     @Mock
75     private FlowCapableNode fcOperationalNode;
76     @Mock
77     private FlowCapableStatisticsGatheringStatus statisticsGatheringStatus;
78     @Mock
79     private SnapshotGatheringStatusEnd snapshotGatheringStatusEnd;
80     @Mock
81     private ReconciliationRegistry reconciliationRegistry;
82     @Mock
83     private DeviceMastershipManager deviceMastershipManager;
84
85     @Before
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));
92
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);
96
97         final DataTreeIdentifier<Node> dataTreeIdentifier =
98                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, nodePath);
99
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);
105     }
106
107     @Test
108     public void testDSLogicalType() throws Exception {
109         Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, nodeListenerOperational.dsType());
110     }
111
112     @Test
113     public void testOnDataTreeChangeAddSyncup() throws Exception {
114         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
115         operationalAdd();
116         prepareFreshOperational(true);
117
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));
122
123         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
124
125         Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
126         Mockito.verify(reactor).syncup(fcNodePath, configNode, fcOperationalNode, dsType);
127         Mockito.verify(roTx).close();
128     }
129
130     @Test
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);
134         operationalAdd();
135         prepareFreshOperational(true);
136
137         Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, fcNodePath))
138                 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
139
140         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
141
142         Mockito.verify(deviceMastershipManager).onDeviceConnected(NODE_ID);
143         Mockito.verifyZeroInteractions(reactor);
144         Mockito.verify(roTx).close();
145     }
146
147     @Test
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);
153
154         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
155
156         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
157         Mockito.verifyZeroInteractions(reactor);
158     }
159
160     @Test
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);
166
167         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
168
169         Mockito.verify(deviceMastershipManager).onDeviceDisconnected(NODE_ID);
170         Mockito.verifyZeroInteractions(reactor);
171     }
172
173     @Test
174     public void testOnDataTreeChangedReconcileNotRegistered() {
175         operationalUpdate();
176         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(false);
177
178         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
179
180         Mockito.verifyZeroInteractions(reactor);
181     }
182
183     @Test
184     public void testOnDataTreeChangedReconcileButStaticsGatheringNotStarted() {
185         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
186         operationalUpdate();
187         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(null);
188
189         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
190
191         Mockito.verifyZeroInteractions(reactor);
192     }
193
194     @Test
195     public void testOnDataTreeChangedReconcileButStaticsGatheringNotFinished() {
196         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
197         operationalUpdate();
198         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
199         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(null);
200
201         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
202
203         Mockito.verifyZeroInteractions(reactor);
204     }
205
206     @Test
207     public void testOnDataTreeChangedReconcileButStaticsGatheringNotSuccessful() {
208         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
209         operationalUpdate();
210         Mockito.when(operationalNode.getAugmentation(FlowCapableStatisticsGatheringStatus.class)).thenReturn(statisticsGatheringStatus);
211         Mockito.when(statisticsGatheringStatus.getSnapshotGatheringStatusEnd()).thenReturn(snapshotGatheringStatusEnd);
212         Mockito.when(snapshotGatheringStatusEnd.isSucceeded()).thenReturn(false);
213
214         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
215
216         Mockito.verifyZeroInteractions(reactor);
217     }
218
219     @Test
220     public void testOnDataTreeChangedReconcileAndFreshOperationalNotPresent() throws ParseException {
221         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
222         operationalUpdate();
223         prepareFreshOperational(false);
224
225         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
226
227         Mockito.verifyZeroInteractions(reactor);
228     }
229
230     @Test
231     public void testOnDataTreeChangedReconcileAndFreshOperationalPresent() throws Exception {
232         Mockito.when(reconciliationRegistry.isRegistered(NODE_ID)).thenReturn(true);
233         operationalUpdate();
234         prepareFreshOperational(true);
235
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));
240
241         nodeListenerOperational.onDataTreeChanged(Collections.singleton(dataTreeModification));
242
243         Mockito.verify(reactor).syncup(fcNodePath, configNode, fcOperationalNode, dsType);
244         Mockito.verify(roTx).close();
245     }
246
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));
257         } else {
258             Mockito.when(snapshotGatheringStatusEnd.getEnd().getValue()).thenReturn(timestampBefore);
259             Mockito.when(reconciliationRegistry.getRegistrationTimestamp(NODE_ID)).thenReturn(simpleDateFormat.parse(timestampAfter));
260         }
261     }
262
263     private void operationalAdd() {
264         Mockito.when(operationalModification.getDataBefore()).thenReturn(null);
265         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
266     }
267
268     private void operationalUpdate() {
269         Mockito.when(operationalModification.getDataBefore()).thenReturn(operationalNode);
270         Mockito.when(operationalModification.getDataAfter()).thenReturn(operationalNode);
271     }
272 }