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.util.Collections;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentMatchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
30 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
31 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
32 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
33 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 * Test for {@link SimplifiedConfigListener}.
44 @RunWith(MockitoJUnitRunner.class)
45 public class SimplifiedConfigListenerTest {
47 private static final NodeId NODE_ID = new NodeId("testNode");
48 private InstanceIdentifier<FlowCapableNode> fcNodePath;
49 private SimplifiedConfigListener nodeListenerConfig;
50 private final LogicalDatastoreType confgDS = LogicalDatastoreType.CONFIGURATION;
51 private final LogicalDatastoreType operationalDS = LogicalDatastoreType.OPERATIONAL;
54 private SyncReactor reactor;
56 private ReadOnlyTransaction roTx;
58 private DataTreeModification<FlowCapableNode> dataTreeModification;
60 private DataObjectModification<FlowCapableNode> configModification;
62 private FlowCapableNode dataBefore;
64 private FlowCapableNode dataAfter;
67 public void setUp() throws Exception {
68 final DataBroker db = Mockito.mock(DataBroker.class);
69 final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
70 final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
71 final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
72 new FlowCapableNodeOdlDao(db, LogicalDatastoreType.OPERATIONAL));
74 nodeListenerConfig = new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
75 fcNodePath = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(NODE_ID))
76 .augmentation(FlowCapableNode.class);
78 final DataTreeIdentifier<FlowCapableNode> dataTreeIdentifier =
79 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, fcNodePath);
81 Mockito.when(db.newReadOnlyTransaction()).thenReturn(roTx);
82 Mockito.when(dataTreeModification.getRootPath()).thenReturn(dataTreeIdentifier);
83 Mockito.when(dataTreeModification.getRootNode()).thenReturn(configModification);
87 public void testDSLogicalType() throws Exception {
88 Assert.assertEquals(LogicalDatastoreType.CONFIGURATION, nodeListenerConfig.dsType());
92 public void testOnDataTreeChangedAdd() {
93 Mockito.when(configModification.getDataBefore()).thenReturn(null);
94 Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
95 final SyncupEntry syncupEntry =
96 loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, operationalDS);
98 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
100 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
101 Mockito.verifyNoMoreInteractions(reactor);
102 Mockito.verify(roTx).close();
106 public void testOnDataTreeChangedUpdate() {
107 Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
108 Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
109 final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, confgDS);
111 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
113 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
114 Mockito.verifyNoMoreInteractions(reactor);
115 Mockito.verify(roTx).close();
119 public void testOnDataTreeChangedDelete() {
120 Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
121 Mockito.when(configModification.getDataAfter()).thenReturn(null);
122 final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(null, confgDS, dataBefore, confgDS);
124 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
126 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
127 Mockito.verifyNoMoreInteractions(reactor);
128 Mockito.verify(roTx).close();
132 public void testOnDataTreeChangedSkip() {
133 Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
134 .thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
136 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
138 Mockito.verifyZeroInteractions(reactor);
139 Mockito.verify(roTx).close();
142 private SyncupEntry loadOperationalDSAndPrepareSyncupEntry(final FlowCapableNode after,
143 final LogicalDatastoreType dsTypeAfter, final FlowCapableNode before,
144 final LogicalDatastoreType dsTypeBefore) {
145 Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
146 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
147 final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
148 Mockito.when(reactor.syncup(ArgumentMatchers.<InstanceIdentifier<FlowCapableNode>>any(),
149 Mockito.eq(syncupEntry))).thenReturn(Futures.immediateFuture(Boolean.TRUE));