3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
6 import com.google.common.base.Optional;
7 import java.util.concurrent.ExecutionException;
9 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
10 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
11 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
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.FlowFeatureCapabilityGroupStats;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeatures;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import test.mock.util.StatisticsManagerTest;
53 public class StatCollectorTest extends StatisticsManagerTest {
54 private final Object waitObject = new Object();
56 @Test(timeout = 200000)
57 public void getAllFlowStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
58 setupStatisticsManager();
60 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityFlowStats.class);
62 final Flow flow = getFlow();
64 final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
65 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(flow.getTableId()));
67 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
68 tableII.child(Flow.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
70 synchronized (waitObject) {
74 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
75 final Optional<Table> tableOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
76 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
77 .child(Table.class, new TableKey(flow.getTableId()))).checkedGet();
78 assertTrue(tableOptional.isPresent());
79 final FlowStatisticsData flowStats = tableOptional.get().getFlow().get(0).getAugmentation(FlowStatisticsData.class);
80 assertTrue(flowStats != null);
81 assertEquals(COUNTER_64_TEST_VALUE, flowStats.getFlowStatistics().getByteCount());
84 @Test(timeout = 200000)
85 public void getAllGroupStatsFeatureNotAdvertisedTest() throws ExecutionException, InterruptedException {
86 setupStatisticsManager();
88 addFlowCapableNodeWithFeatures(s1Key, true);
90 final InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
91 .augmentation(FlowCapableNode.class).child(Group.class, getGroup().getKey());
92 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
93 groupII.augmentation(NodeGroupStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
95 synchronized (waitObject) {
99 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
100 final Optional<Group> optionalGroup = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
101 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
102 .child(Group.class, getGroup().getKey())).get();
104 assertTrue(optionalGroup.isPresent());
105 assertTrue(optionalGroup.get().getAugmentation(NodeGroupDescStats.class) != null);
106 final NodeGroupStatistics groupStats = optionalGroup.get().getAugmentation(NodeGroupStatistics.class);
107 assertTrue(groupStats != null);
108 assertEquals(COUNTER_64_TEST_VALUE, groupStats.getGroupStatistics().getByteCount());
110 readTx = getDataBroker().newReadOnlyTransaction();
111 final Optional<GroupFeatures> optionalGroupFeatures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
112 .child(Node.class, s1Key).augmentation(NodeGroupFeatures.class).child(GroupFeatures.class)).get();
113 assertTrue(optionalGroupFeatures.isPresent());
114 assertEquals(1, optionalGroupFeatures.get().getMaxGroups().size());
115 assertEquals(MAX_GROUPS_TEST_VALUE, optionalGroupFeatures.get().getMaxGroups().get(0));
118 @Test(timeout = 200000)
119 public void getAllGroupStatsFeatureAdvertisedTest() throws ExecutionException, InterruptedException {
120 setupStatisticsManager();
122 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityGroupStats.class);
124 final InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
125 .augmentation(FlowCapableNode.class).child(Group.class, getGroup().getKey());
126 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
127 groupII.augmentation(NodeGroupStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
129 synchronized (waitObject) {
133 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
134 final Optional<Group> optionalGroup = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
135 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
136 .child(Group.class, getGroup().getKey())).get();
138 assertTrue(optionalGroup.isPresent());
139 assertTrue(optionalGroup.get().getAugmentation(NodeGroupDescStats.class) != null);
140 final NodeGroupStatistics groupStats = optionalGroup.get().getAugmentation(NodeGroupStatistics.class);
141 assertTrue(groupStats != null);
142 assertEquals(COUNTER_64_TEST_VALUE, groupStats.getGroupStatistics().getByteCount());
144 readTx = getDataBroker().newReadOnlyTransaction();
145 final Optional<GroupFeatures> optionalGroupFeatures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
146 .child(Node.class, s1Key).augmentation(NodeGroupFeatures.class).child(GroupFeatures.class)).get();
147 assertTrue(optionalGroupFeatures.isPresent());
148 assertEquals(1, optionalGroupFeatures.get().getMaxGroups().size());
149 assertEquals(MAX_GROUPS_TEST_VALUE, optionalGroupFeatures.get().getMaxGroups().get(0));
152 @Test(timeout = 200000)
153 public void getAllMeterStatsTest() throws ExecutionException, InterruptedException {
154 setupStatisticsManager();
156 addFlowCapableNodeWithFeatures(s1Key, true);
158 final InstanceIdentifier<Meter> meterII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
159 .augmentation(FlowCapableNode.class).child(Meter.class, getMeter().getKey());
160 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
161 meterII.augmentation(NodeMeterStatistics.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
163 synchronized (waitObject) {
167 ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
168 final Optional<Meter> optionalMeter = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
169 .child(Node.class, s1Key).augmentation(FlowCapableNode.class)
170 .child(Meter.class, getMeter().getKey())).get();
172 assertTrue(optionalMeter.isPresent());
173 assertTrue(optionalMeter.get().getAugmentation(NodeMeterConfigStats.class) != null);
174 final NodeMeterStatistics meterStats = optionalMeter.get().getAugmentation(NodeMeterStatistics.class);
175 assertTrue(meterStats != null);
176 assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getByteInCount());
177 assertEquals(COUNTER_64_TEST_VALUE, meterStats.getMeterStatistics().getPacketInCount());
179 readTx = getDataBroker().newReadOnlyTransaction();
180 final Optional<MeterFeatures> optionalMeterFeautures = readTx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class)
181 .child(Node.class, s1Key).augmentation(NodeMeterFeatures.class).child(MeterFeatures.class)).get();
182 assertTrue(optionalMeterFeautures.isPresent());
183 assertEquals(COUNTER_32_TEST_VALUE, optionalMeterFeautures.get().getMaxMeter());
186 @Test(timeout = 200000)
187 public void getAllQueueStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
188 setupStatisticsManager();
190 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityQueueStats.class);
192 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
193 final FlowCapableNodeConnectorBuilder fcncBuilder = new FlowCapableNodeConnectorBuilder();
194 ncBuilder.setKey(new NodeConnectorKey(getNodeConnectorId()));
195 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcncBuilder.build());
197 final InstanceIdentifier<NodeConnector> nodeConnectorII = InstanceIdentifier.create(Nodes.class)
198 .child(Node.class, s1Key)
199 .child(NodeConnector.class, ncBuilder.getKey());
201 final WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
202 writeTx.put(LogicalDatastoreType.OPERATIONAL, nodeConnectorII, ncBuilder.build());
203 final InstanceIdentifier<Queue> queueII = nodeConnectorII.augmentation(FlowCapableNodeConnector.class)
204 .child(Queue.class, getQueue().getKey());
205 final QueueBuilder qBuilder = new QueueBuilder(getQueue());
206 writeTx.put(LogicalDatastoreType.OPERATIONAL, queueII, qBuilder.build());
207 assertCommit(writeTx.submit());
209 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
210 queueII.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
212 synchronized (waitObject) {
216 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
217 final Optional<Queue> queueOptional = readTx.read(LogicalDatastoreType.OPERATIONAL, queueII).checkedGet();
218 assertTrue(queueOptional.isPresent());
219 final FlowCapableNodeConnectorQueueStatisticsData queueStats =
220 queueOptional.get().getAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
221 assertTrue(queueStats != null);
222 assertEquals(COUNTER_64_TEST_VALUE,
223 queueStats.getFlowCapableNodeConnectorQueueStatistics().getTransmittedBytes());
226 @Test(timeout = 200000)
227 public void getAllPortStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
228 setupStatisticsManager();
230 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityPortStats.class);
232 final InstanceIdentifier<NodeConnector> nodeConnectorII = InstanceIdentifier.create(Nodes.class)
233 .child(Node.class, s1Key).child(NodeConnector.class, new NodeConnectorKey(getNodeConnectorId()));
235 NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
236 ncBuilder.setKey(new NodeConnectorKey(getNodeConnectorId()));
237 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
238 writeTx.put(LogicalDatastoreType.OPERATIONAL, nodeConnectorII, ncBuilder.build());
239 assertCommit(writeTx.submit());
241 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
242 nodeConnectorII.augmentation(FlowCapableNodeConnectorStatisticsData.class),
243 new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
245 synchronized (waitObject) {
249 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
250 final Optional<FlowCapableNodeConnectorStatisticsData> flowCapableNodeConnectorStatisticsDataOptional =
251 readTx.read(LogicalDatastoreType.OPERATIONAL,
252 nodeConnectorII.augmentation(FlowCapableNodeConnectorStatisticsData.class)).checkedGet();
253 assertTrue(flowCapableNodeConnectorStatisticsDataOptional.isPresent());
254 assertEquals(BIG_INTEGER_TEST_VALUE,
255 flowCapableNodeConnectorStatisticsDataOptional.get().getFlowCapableNodeConnectorStatistics()
257 assertEquals(BIG_INTEGER_TEST_VALUE,
258 flowCapableNodeConnectorStatisticsDataOptional.get().getFlowCapableNodeConnectorStatistics()
259 .getCollisionCount());
262 @Test(timeout = 200000)
263 public void getAllTableStatsTest() throws ExecutionException, InterruptedException, ReadFailedException {
264 setupStatisticsManager();
266 addFlowCapableNodeWithFeatures(s1Key, false, FlowFeatureCapabilityTableStats.class);
268 final TableId tableId = getTableId();
269 final InstanceIdentifier<Table> tableII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
270 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId.getValue()));
272 getDataBroker().registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
273 tableII.augmentation(FlowTableStatisticsData.class), new ChangeListener(), AsyncDataBroker.DataChangeScope.BASE);
275 synchronized (waitObject) {
279 final ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
280 final Optional<FlowTableStatisticsData> flowTableStatisticsDataOptional = readTx.read(
281 LogicalDatastoreType.OPERATIONAL, tableII.augmentation(FlowTableStatisticsData.class)).checkedGet();
282 assertTrue(flowTableStatisticsDataOptional.isPresent());
283 assertEquals(COUNTER_32_TEST_VALUE,
284 flowTableStatisticsDataOptional.get().getFlowTableStatistics().getActiveFlows());
285 assertEquals(COUNTER_64_TEST_VALUE,
286 flowTableStatisticsDataOptional.get().getFlowTableStatistics().getPacketsLookedUp());
289 public class ChangeListener implements DataChangeListener {
292 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
293 synchronized (waitObject) {