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.Matchers;
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 = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, operationalDS);
97 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
99 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
100 Mockito.verifyNoMoreInteractions(reactor);
101 Mockito.verify(roTx).close();
105 public void testOnDataTreeChangedUpdate() {
106 Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
107 Mockito.when(configModification.getDataAfter()).thenReturn(dataAfter);
108 final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(dataAfter, confgDS, dataBefore, confgDS);
110 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
112 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
113 Mockito.verifyNoMoreInteractions(reactor);
114 Mockito.verify(roTx).close();
118 public void testOnDataTreeChangedDelete() {
119 Mockito.when(configModification.getDataBefore()).thenReturn(dataBefore);
120 Mockito.when(configModification.getDataAfter()).thenReturn(null);
121 final SyncupEntry syncupEntry = loadOperationalDSAndPrepareSyncupEntry(null, confgDS, dataBefore, confgDS);
123 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
125 Mockito.verify(reactor).syncup(fcNodePath, syncupEntry);
126 Mockito.verifyNoMoreInteractions(reactor);
127 Mockito.verify(roTx).close();
131 public void testOnDataTreeChangedSkip() {
132 Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath)).
133 thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
135 nodeListenerConfig.onDataTreeChanged(Collections.singleton(dataTreeModification));
137 Mockito.verifyZeroInteractions(reactor);
138 Mockito.verify(roTx).close();
141 private SyncupEntry loadOperationalDSAndPrepareSyncupEntry(final FlowCapableNode after, final LogicalDatastoreType dsTypeAfter,
142 final FlowCapableNode before, final LogicalDatastoreType dsTypeBefore) {
143 Mockito.when(roTx.read(LogicalDatastoreType.OPERATIONAL, fcNodePath))
144 .thenReturn(Futures.immediateCheckedFuture(Optional.of(dataBefore)));
145 final SyncupEntry syncupEntry = new SyncupEntry(after, dsTypeAfter, before, dsTypeBefore);
146 Mockito.when(reactor.syncup(Matchers.<InstanceIdentifier<FlowCapableNode>>any(), Mockito.eq(syncupEntry)))
147 .thenReturn(Futures.immediateFuture(Boolean.TRUE));