Bug 1484 - StatisticManager performance improvement refactoring
[controller.git] / opendaylight / md-sal / statistics-manager / src / test / java / test / mock / FlowStatisticsTest.java
1 package test.mock;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.Collections;
8 import java.util.concurrent.ExecutionException;
9
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;
29
30 import test.mock.util.StatisticsManagerTest;
31
32 import com.google.common.base.Optional;
33
34 public class FlowStatisticsTest extends StatisticsManagerTest {
35     private final Object waitObject = new Object();
36
37 //    @Test(timeout = 5000)
38     public void addedFlowOnDemandStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
39         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
40         activator.onSessionInitiated(providerContext);
41
42         addFlowCapableNode(s1Key);
43
44         final Flow flow = getFlow();
45
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();
52
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());
59
60         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
61                 flowII.augmentation(FlowStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
62
63         synchronized (waitObject) {
64             waitObject.wait();
65         }
66
67         final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
68         final Optional<FlowStatisticsData> flowStatDataOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, flowII.augmentation(FlowStatisticsData.class))
69                 .checkedGet();
70         assertTrue(flowStatDataOptional.isPresent());
71         assertEquals(COUNTER_64_TEST_VALUE, flowStatDataOptional.get().getFlowStatistics().getByteCount());
72
73     }
74
75 //    @Test(timeout = 5000)
76     public void deletedFlowStatsRemovalTest() throws ExecutionException, InterruptedException, ReadFailedException {
77         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
78         activator.onSessionInitiated(providerContext);
79
80         addFlowCapableNode(s1Key);
81
82         final Flow flow = getFlow();
83
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();
90
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);
96
97         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
98                 flowII.augmentation(FlowStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
99
100         assertCommit(writeTx.submit());
101
102         synchronized (waitObject) {
103             waitObject.wait();
104         }
105
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());
110
111         writeTx = getDataBroker().newWriteOnlyTransaction();
112         writeTx.delete(LogicalDatastoreType.CONFIGURATION, flowII);
113         assertCommit(writeTx.submit());
114
115         readTx = getDataBroker().newReadOnlyTransaction();
116         flowStatDataOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, flowII).checkedGet();
117         assertFalse(flowStatDataOptional.isPresent());
118     }
119
120 //    @Test(timeout = 23000)
121     public void getAllStatsWhenNodeIsConnectedTest() throws ExecutionException, InterruptedException, ReadFailedException {
122         final StatisticsManagerActivator activator = new StatisticsManagerActivator();
123         activator.onSessionInitiated(providerContext);
124
125         addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityFlowStats.class);
126
127         final Flow flow = getFlow();
128
129         final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
130                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
131
132         getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
133                 tableII.child(Flow.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
134
135         synchronized (waitObject) {
136             waitObject.wait();
137         }
138
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());
147     }
148
149     public class ChangeListener implements DataChangeListener {
150
151         @Override
152         public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
153             synchronized (waitObject) {
154                 waitObject.notify();
155             }
156         }
157     }
158 }
159