3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.util.Collections;
8 import java.util.concurrent.ExecutionException;
10 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.md.statistics.manager.StatisticsManagerActivator;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import test.mock.util.StatisticsManagerTest;
32 import com.google.common.base.Optional;
34 public class FlowStatisticsTest extends StatisticsManagerTest {
35 private final Object waitObject = new Object();
37 // @Test(timeout = 5000)
38 public void addedFlowOnDemandStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
39 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
40 activator.onSessionInitiated(providerContext);
42 addFlowCapableNode(s1Key);
44 final Flow flow = getFlow();
46 final InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
47 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()))
48 .child(Flow.class, flow.getKey());
49 final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
50 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
51 final Table table = new TableBuilder().setKey(new TableKey(flow.getTableId())).setFlow(Collections.<Flow>emptyList()).build();
53 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
54 writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
55 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
56 writeTx.put(LogicalDatastoreType.OPERATIONAL, tableII, table);
57 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowII, flow);
58 assertCommit(writeTx.submit());
60 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
61 flowII.augmentation(FlowStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
63 synchronized (waitObject) {
67 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
68 final Optional<FlowStatisticsData> flowStatDataOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, flowII.augmentation(FlowStatisticsData.class))
70 assertTrue(flowStatDataOptional.isPresent());
71 assertEquals(COUNTER_64_TEST_VALUE, flowStatDataOptional.get().getFlowStatistics().getByteCount());
75 // @Test(timeout = 5000)
76 public void deletedFlowStatsRemovalTest() throws ExecutionException, InterruptedException, ReadFailedException {
77 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
78 activator.onSessionInitiated(providerContext);
80 addFlowCapableNode(s1Key);
82 final Flow flow = getFlow();
84 final InstanceIdentifier<Flow> flowII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
85 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()))
86 .child(Flow.class, flow.getKey());
87 final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
88 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
89 final Table table = new TableBuilder().setKey(new TableKey(flow.getTableId())).setFlow(Collections.<Flow>emptyList()).build();
91 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
92 writeTx.put(LogicalDatastoreType.CONFIGURATION, tableII, table);
93 writeTx.put(LogicalDatastoreType.CONFIGURATION, flowII, flow);
94 writeTx.put(LogicalDatastoreType.OPERATIONAL, tableII, table);
95 writeTx.put(LogicalDatastoreType.OPERATIONAL, flowII, flow);
97 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
98 flowII.augmentation(FlowStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
100 assertCommit(writeTx.submit());
102 synchronized (waitObject) {
106 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
107 Optional<Flow> flowStatDataOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, flowII).checkedGet();
108 assertTrue(flowStatDataOptional.isPresent());
109 // assertEquals(COUNTER_64_TEST_VALUE, flowStatDataOptional.get().getFlowStatistics().getByteCount());
111 writeTx = getDataBroker().newWriteOnlyTransaction();
112 writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
113 assertCommit(writeTx.submit());
115 readTx = getDataBroker().newReadOnlyTransaction();
116 flowStatDataOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, flowII).checkedGet();
117 assertFalse(flowStatDataOptional.isPresent());
120 // @Test(timeout = 23000)
121 public void getAllStatsWhenNodeIsConnectedTest() throws ExecutionException, InterruptedException, ReadFailedException {
122 final StatisticsManagerActivator activator = new StatisticsManagerActivator();
123 activator.onSessionInitiated(providerContext);
125 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityFlowStats.class);
127 final Flow flow = getFlow();
129 final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
130 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
132 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
133 tableII.child(Flow.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
135 synchronized (waitObject) {
139 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
140 final Optional<Table> tableOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
141 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
142 .child(Table.class, new TableKey(flow.getTableId()))).checkedGet();
143 assertTrue(tableOptional.isPresent());
144 final FlowStatisticsData flowStats = tableOptional.get().getFlow().get(0).getAugmentation(FlowStatisticsData.class);
145 assertTrue(flowStats != null);
146 assertEquals(COUNTER_64_TEST_VALUE, flowStats.getFlowStatistics().getByteCount());
149 public class ChangeListener implements DataChangeListener {
152 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
153 synchronized (waitObject) {