2 * Copyright IBM Corporation, 2013. 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
8 package org.opendaylight.controller.md.statistics.manager;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
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.FlowCapableNodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
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.inventory.rev130819.tables.table.FlowBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupFeatures;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterFeatures;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.AggregateFlowStatistics;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
92 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
93 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
97 import com.google.common.base.Preconditions;
100 * This class handles the lifecycle of per-node statistics. It receives data
101 * from StatisticsListener, stores it in the data store and keeps track of
102 * when the data should be removed.
104 * @author avishnoi@in.ibm.com
106 public class NodeStatisticsHandler implements AutoCloseable {
107 private static final Logger logger = LoggerFactory.getLogger(NodeStatisticsHandler.class);
108 private static final int NUMBER_OF_WAIT_CYCLES = 2;
110 private final Map<GroupDescStats,Long> groupDescStatsUpdate = new HashMap<>();
111 private final Map<MeterConfigStats,Long> meterConfigStatsUpdate = new HashMap<>();
112 private final Map<FlowEntry,Long> flowStatsUpdate = new HashMap<>();
113 private final Map<QueueEntry,Long> queuesStatsUpdate = new HashMap<>();
114 private final InstanceIdentifier<Node> targetNodeIdentifier;
115 private final StatisticsProvider statisticsProvider;
116 private final NodeKey targetNodeKey;
117 private int unaccountedFlowsCounter = 1;
119 public NodeStatisticsHandler(StatisticsProvider statisticsProvider, NodeKey nodeKey){
120 this.statisticsProvider = Preconditions.checkNotNull(statisticsProvider);
121 this.targetNodeKey = Preconditions.checkNotNull(nodeKey);
122 this.targetNodeIdentifier = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey).build();
125 private static class FlowEntry {
126 private final Short tableId;
127 private final Flow flow;
129 public FlowEntry(Short tableId, Flow flow){
130 this.tableId = tableId;
134 public Short getTableId() {
138 public Flow getFlow() {
143 public int hashCode() {
144 final int prime = 31;
146 result = prime * result + ((flow == null) ? 0 : flow.hashCode());
147 result = prime * result + ((tableId == null) ? 0 : tableId.hashCode());
152 public boolean equals(Object obj) {
157 if (getClass() != obj.getClass())
159 FlowEntry other = (FlowEntry) obj;
161 if (other.flow != null)
163 } else if (!flow.equals(other.flow))
165 if (tableId == null) {
166 if (other.tableId != null)
168 } else if (!tableId.equals(other.tableId))
174 private static final class QueueEntry{
175 private final NodeConnectorId nodeConnectorId;
176 private final QueueId queueId;
177 public QueueEntry(NodeConnectorId ncId, QueueId queueId){
178 this.nodeConnectorId = ncId;
179 this.queueId = queueId;
181 public NodeConnectorId getNodeConnectorId() {
182 return nodeConnectorId;
184 public QueueId getQueueId() {
188 public int hashCode() {
189 final int prime = 31;
191 result = prime * result + ((nodeConnectorId == null) ? 0 : nodeConnectorId.hashCode());
192 result = prime * result + ((queueId == null) ? 0 : queueId.hashCode());
196 public boolean equals(Object obj) {
203 if (!(obj instanceof QueueEntry)) {
206 QueueEntry other = (QueueEntry) obj;
207 if (nodeConnectorId == null) {
208 if (other.nodeConnectorId != null) {
211 } else if (!nodeConnectorId.equals(other.nodeConnectorId)) {
214 if (queueId == null) {
215 if (other.queueId != null) {
218 } else if (!queueId.equals(other.queueId)) {
225 public NodeKey getTargetNodeKey() {
226 return targetNodeKey;
229 public synchronized void updateGroupDescStats(List<GroupDescStats> list){
230 final Long expiryTime = getExpiryTime();
231 final DataModificationTransaction trans = statisticsProvider.startChange();
233 for (GroupDescStats groupDescStats : list) {
234 GroupBuilder groupBuilder = new GroupBuilder();
235 GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
236 groupBuilder.setKey(groupKey);
238 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
239 .augmentation(FlowCapableNode.class)
240 .child(Group.class,groupKey).toInstance();
242 NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
243 GroupDescBuilder stats = new GroupDescBuilder();
244 stats.fieldsFrom(groupDescStats);
245 groupDesc.setGroupDesc(stats.build());
247 //Update augmented data
248 groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
250 trans.putOperationalData(groupRef, groupBuilder.build());
251 this.groupDescStatsUpdate.put(groupDescStats, expiryTime);
258 public synchronized void updateGroupStats(List<GroupStats> list) {
259 final DataModificationTransaction trans = statisticsProvider.startChange();
261 for(GroupStats groupStats : list) {
262 GroupBuilder groupBuilder = new GroupBuilder();
263 GroupKey groupKey = new GroupKey(groupStats.getGroupId());
264 groupBuilder.setKey(groupKey);
266 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
267 .augmentation(FlowCapableNode.class)
268 .child(Group.class,groupKey).toInstance();
270 NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
271 GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
272 stats.fieldsFrom(groupStats);
273 groupStatisticsBuilder.setGroupStatistics(stats.build());
275 //Update augmented data
276 groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
277 trans.putOperationalData(groupRef, groupBuilder.build());
279 // FIXME: should we be tracking this data?
285 public synchronized void updateMeterConfigStats(List<MeterConfigStats> list) {
286 final Long expiryTime = getExpiryTime();
287 final DataModificationTransaction trans = statisticsProvider.startChange();
289 for(MeterConfigStats meterConfigStats : list) {
290 MeterBuilder meterBuilder = new MeterBuilder();
291 MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
292 meterBuilder.setKey(meterKey);
294 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
295 .augmentation(FlowCapableNode.class)
296 .child(Meter.class,meterKey).toInstance();
298 NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
299 MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
300 stats.fieldsFrom(meterConfigStats);
301 meterConfig.setMeterConfigStats(stats.build());
303 //Update augmented data
304 meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
306 trans.putOperationalData(meterRef, meterBuilder.build());
307 this.meterConfigStatsUpdate.put(meterConfigStats, expiryTime);
314 public synchronized void updateMeterStats(List<MeterStats> list) {
315 final DataModificationTransaction trans = statisticsProvider.startChange();
317 for(MeterStats meterStats : list) {
318 MeterBuilder meterBuilder = new MeterBuilder();
319 MeterKey meterKey = new MeterKey(meterStats.getMeterId());
320 meterBuilder.setKey(meterKey);
322 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
323 .augmentation(FlowCapableNode.class)
324 .child(Meter.class,meterKey).toInstance();
326 NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
327 MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
328 stats.fieldsFrom(meterStats);
329 meterStatsBuilder.setMeterStatistics(stats.build());
331 //Update augmented data
332 meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
333 trans.putOperationalData(meterRef, meterBuilder.build());
335 // FIXME: should we be tracking this data?
341 public synchronized void updateQueueStats(List<QueueIdAndStatisticsMap> list) {
342 final Long expiryTime = getExpiryTime();
343 final DataModificationTransaction trans = statisticsProvider.startChange();
345 for (QueueIdAndStatisticsMap swQueueStats : list) {
347 QueueEntry queueEntry = new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
349 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
351 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
353 queueStatisticsBuilder.fieldsFrom(swQueueStats);
355 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
357 InstanceIdentifier<Queue> queueRef
358 = InstanceIdentifier.builder(Nodes.class)
359 .child(Node.class, targetNodeKey)
360 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
361 .augmentation(FlowCapableNodeConnector.class)
362 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
364 QueueBuilder queueBuilder = new QueueBuilder();
365 FlowCapableNodeConnectorQueueStatisticsData qsd = queueStatisticsDataBuilder.build();
366 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, qsd);
367 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
369 logger.debug("Augmenting queue statistics {} of queue {} to port {}",
371 swQueueStats.getQueueId(),
372 swQueueStats.getNodeConnectorId());
374 trans.putOperationalData(queueRef, queueBuilder.build());
375 this.queuesStatsUpdate.put(queueEntry, expiryTime);
381 public synchronized void updateFlowTableStats(List<FlowTableAndStatisticsMap> list) {
382 final DataModificationTransaction trans = statisticsProvider.startChange();
384 for (FlowTableAndStatisticsMap ftStats : list) {
386 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
387 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
389 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
391 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
392 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
393 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
394 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
396 final FlowTableStatistics stats = statisticsBuilder.build();
397 statisticsDataBuilder.setFlowTableStatistics(stats);
399 logger.debug("Augment flow table statistics: {} for table {} on Node {}",
400 stats,ftStats.getTableId(), targetNodeKey);
402 TableBuilder tableBuilder = new TableBuilder();
403 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
404 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
405 trans.putOperationalData(tableRef, tableBuilder.build());
407 // FIXME: should we be tracking this data?
413 public synchronized void updateNodeConnectorStats(List<NodeConnectorStatisticsAndPortNumberMap> list) {
414 final DataModificationTransaction trans = statisticsProvider.startChange();
416 for(NodeConnectorStatisticsAndPortNumberMap portStats : list) {
418 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
419 = new FlowCapableNodeConnectorStatisticsBuilder();
420 statisticsBuilder.setBytes(portStats.getBytes());
421 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
422 statisticsBuilder.setDuration(portStats.getDuration());
423 statisticsBuilder.setPackets(portStats.getPackets());
424 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
425 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
426 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
427 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
428 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
429 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
430 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
432 //Augment data to the node-connector
433 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
434 new FlowCapableNodeConnectorStatisticsDataBuilder();
436 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
438 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class)
439 .child(Node.class, targetNodeKey)
440 .child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
442 // FIXME: can we bypass this read?
443 NodeConnector nodeConnector = (NodeConnector)trans.readOperationalData(nodeConnectorRef);
444 if(nodeConnector != null){
445 final FlowCapableNodeConnectorStatisticsData stats = statisticsDataBuilder.build();
446 logger.debug("Augmenting port statistics {} to port {}",stats,nodeConnectorRef.toString());
447 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
448 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, stats);
449 trans.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
452 // FIXME: should we be tracking this data?
458 public synchronized void updateAggregateFlowStats(Short tableId, AggregateFlowStatistics flowStats) {
459 if (tableId != null) {
460 final DataModificationTransaction trans = statisticsProvider.startChange();
463 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
464 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
466 AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
467 AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder(flowStats);
469 aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
471 logger.debug("Augment aggregate statistics: {} for table {} on Node {}",
472 aggregateFlowStatisticsBuilder.build().toString(),tableId,targetNodeKey);
474 TableBuilder tableBuilder = new TableBuilder();
475 tableBuilder.setKey(new TableKey(tableId));
476 tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
477 trans.putOperationalData(tableRef, tableBuilder.build());
479 // FIXME: should we be tracking this data?
484 public synchronized void updateGroupFeatures(GroupFeatures notification) {
485 final DataModificationTransaction trans = statisticsProvider.startChange();
487 final NodeBuilder nodeData = new NodeBuilder();
488 nodeData.setKey(targetNodeKey);
490 NodeGroupFeaturesBuilder nodeGroupFeatures = new NodeGroupFeaturesBuilder();
491 GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder(notification);
492 nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
494 //Update augmented data
495 nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
496 trans.putOperationalData(targetNodeIdentifier, nodeData.build());
498 // FIXME: should we be tracking this data?
502 public synchronized void updateMeterFeatures(MeterFeatures features) {
503 final DataModificationTransaction trans = statisticsProvider.startChange();
505 final NodeBuilder nodeData = new NodeBuilder();
506 nodeData.setKey(targetNodeKey);
508 NodeMeterFeaturesBuilder nodeMeterFeatures = new NodeMeterFeaturesBuilder();
509 MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder(features);
510 nodeMeterFeatures.setMeterFeatures(meterFeature.build());
512 //Update augmented data
513 nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
514 trans.putOperationalData(targetNodeIdentifier, nodeData.build());
516 // FIXME: should we be tracking this data?
520 public synchronized void updateFlowStats(List<FlowAndStatisticsMapList> list) {
521 final Long expiryTime = getExpiryTime();
522 final DataModificationTransaction trans = statisticsProvider.startChange();
524 for(FlowAndStatisticsMapList map : list) {
525 short tableId = map.getTableId();
526 boolean foundOriginalFlow = false;
528 FlowBuilder flowBuilder = new FlowBuilder();
530 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
532 FlowBuilder flow = new FlowBuilder();
533 flow.setContainerName(map.getContainerName());
534 flow.setBufferId(map.getBufferId());
535 flow.setCookie(map.getCookie());
536 flow.setCookieMask(map.getCookieMask());
537 flow.setFlags(map.getFlags());
538 flow.setFlowName(map.getFlowName());
539 flow.setHardTimeout(map.getHardTimeout());
540 if(map.getFlowId() != null)
541 flow.setId(new FlowId(map.getFlowId().getValue()));
542 flow.setIdleTimeout(map.getIdleTimeout());
543 flow.setInstallHw(map.isInstallHw());
544 flow.setInstructions(map.getInstructions());
545 if(map.getFlowId()!= null)
546 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
547 flow.setMatch(map.getMatch());
548 flow.setOutGroup(map.getOutGroup());
549 flow.setOutPort(map.getOutPort());
550 flow.setPriority(map.getPriority());
551 flow.setStrict(map.isStrict());
552 flow.setTableId(tableId);
554 Flow flowRule = flow.build();
556 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
557 stats.setByteCount(map.getByteCount());
558 stats.setPacketCount(map.getPacketCount());
559 stats.setDuration(map.getDuration());
561 GenericStatistics flowStats = stats.build();
563 //Augment the data to the flow node
565 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
566 flowStatistics.setByteCount(flowStats.getByteCount());
567 flowStatistics.setPacketCount(flowStats.getPacketCount());
568 flowStatistics.setDuration(flowStats.getDuration());
569 flowStatistics.setContainerName(map.getContainerName());
570 flowStatistics.setBufferId(map.getBufferId());
571 flowStatistics.setCookie(map.getCookie());
572 flowStatistics.setCookieMask(map.getCookieMask());
573 flowStatistics.setFlags(map.getFlags());
574 flowStatistics.setFlowName(map.getFlowName());
575 flowStatistics.setHardTimeout(map.getHardTimeout());
576 flowStatistics.setIdleTimeout(map.getIdleTimeout());
577 flowStatistics.setInstallHw(map.isInstallHw());
578 flowStatistics.setInstructions(map.getInstructions());
579 flowStatistics.setMatch(map.getMatch());
580 flowStatistics.setOutGroup(map.getOutGroup());
581 flowStatistics.setOutPort(map.getOutPort());
582 flowStatistics.setPriority(map.getPriority());
583 flowStatistics.setStrict(map.isStrict());
584 flowStatistics.setTableId(tableId);
586 flowStatisticsData.setFlowStatistics(flowStatistics.build());
588 logger.debug("Flow : {}",flowRule.toString());
589 logger.debug("Statistics to augment : {}",flowStatistics.build().toString());
591 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
592 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
594 Table table= (Table)trans.readConfigurationData(tableRef);
596 //TODO: Not a good way to do it, need to figure out better way.
597 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
598 //to the flows stored in data store.
599 // Augment same statistics to all the matching masked flow
602 for(Flow existingFlow : table.getFlow()){
603 logger.debug("Existing flow in data store : {}",existingFlow.toString());
604 if(FlowComparator.flowEquals(flowRule,existingFlow)){
605 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
606 .augmentation(FlowCapableNode.class)
607 .child(Table.class, new TableKey(tableId))
608 .child(Flow.class,existingFlow.getKey()).toInstance();
609 flowBuilder.setKey(existingFlow.getKey());
610 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
611 logger.debug("Found matching flow in the datastore, augmenting statistics");
612 foundOriginalFlow = true;
613 // Update entry with timestamp of latest response
614 flow.setKey(existingFlow.getKey());
615 FlowEntry flowStatsEntry = new FlowEntry(tableId,flow.build());
616 flowStatsUpdate.put(flowStatsEntry, expiryTime);
618 trans.putOperationalData(flowRef, flowBuilder.build());
623 table = (Table)trans.readOperationalData(tableRef);
624 if(!foundOriginalFlow && table != null){
626 for(Flow existingFlow : table.getFlow()){
627 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
628 if(augmentedflowStatisticsData != null){
629 FlowBuilder existingOperationalFlow = new FlowBuilder();
630 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
631 logger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
632 if(FlowComparator.flowEquals(flowRule,existingOperationalFlow.build())){
633 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
634 .augmentation(FlowCapableNode.class)
635 .child(Table.class, new TableKey(tableId))
636 .child(Flow.class,existingFlow.getKey()).toInstance();
637 flowBuilder.setKey(existingFlow.getKey());
638 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
639 logger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
640 foundOriginalFlow = true;
642 // Update entry with timestamp of latest response
643 flow.setKey(existingFlow.getKey());
644 FlowEntry flowStatsEntry = new FlowEntry(tableId,flow.build());
645 flowStatsUpdate.put(flowStatsEntry, expiryTime);
646 trans.putOperationalData(flowRef, flowBuilder.build());
652 if(!foundOriginalFlow){
653 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
654 this.unaccountedFlowsCounter++;
655 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
656 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, targetNodeKey)
657 .augmentation(FlowCapableNode.class)
658 .child(Table.class, new TableKey(tableId))
659 .child(Flow.class,newFlowKey).toInstance();
660 flowBuilder.setKey(newFlowKey);
661 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
662 logger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",
663 flowBuilder.build());
665 // Update entry with timestamp of latest response
666 flow.setKey(newFlowKey);
667 FlowEntry flowStatsEntry = new FlowEntry(tableId,flow.build());
668 flowStatsUpdate.put(flowStatsEntry, expiryTime);
669 trans.putOperationalData(flowRef, flowBuilder.build());
676 private static Long getExpiryTime(){
677 final long now = System.nanoTime();
678 return now + TimeUnit.MILLISECONDS.toNanos(StatisticsProvider.STATS_THREAD_EXECUTION_TIME * NUMBER_OF_WAIT_CYCLES);
681 public synchronized void cleanStaleStatistics(){
682 final DataModificationTransaction trans = this.statisticsProvider.startChange();
683 final long now = System.nanoTime();
685 //Clean stale statistics related to group
686 for (Iterator<Entry<GroupDescStats, Long>> it = this.groupDescStatsUpdate.entrySet().iterator();it.hasNext();){
687 Entry<GroupDescStats, Long> e = it.next();
688 if (now > e.getValue()) {
689 cleanGroupStatsFromDataStore(trans, e.getKey());
694 //Clean stale statistics related to meter
695 for (Iterator<Entry<MeterConfigStats, Long>> it = this.meterConfigStatsUpdate.entrySet().iterator();it.hasNext();){
696 Entry<MeterConfigStats, Long> e = it.next();
697 if (now > e.getValue()) {
698 cleanMeterStatsFromDataStore(trans, e.getKey());
703 //Clean stale statistics related to flow
704 for (Iterator<Entry<FlowEntry, Long>> it = this.flowStatsUpdate.entrySet().iterator();it.hasNext();){
705 Entry<FlowEntry, Long> e = it.next();
706 if (now > e.getValue()) {
707 cleanFlowStatsFromDataStore(trans, e.getKey());
712 //Clean stale statistics related to queue
713 for (Iterator<Entry<QueueEntry, Long>> it = this.queuesStatsUpdate.entrySet().iterator();it.hasNext();){
714 Entry<QueueEntry, Long> e = it.next();
715 if (now > e.getValue()) {
716 cleanQueueStatsFromDataStore(trans, e.getKey());
724 private void cleanQueueStatsFromDataStore(DataModificationTransaction trans, QueueEntry queueEntry) {
725 InstanceIdentifier<?> queueRef
726 = InstanceIdentifier.builder(Nodes.class)
727 .child(Node.class, this.targetNodeKey)
728 .child(NodeConnector.class, new NodeConnectorKey(queueEntry.getNodeConnectorId()))
729 .augmentation(FlowCapableNodeConnector.class)
730 .child(Queue.class, new QueueKey(queueEntry.getQueueId()))
731 .augmentation(FlowCapableNodeConnectorQueueStatisticsData.class).toInstance();
732 trans.removeOperationalData(queueRef);
735 private void cleanFlowStatsFromDataStore(DataModificationTransaction trans, FlowEntry flowEntry) {
736 InstanceIdentifier<?> flowRef
737 = InstanceIdentifier.builder(Nodes.class).child(Node.class, this.targetNodeKey)
738 .augmentation(FlowCapableNode.class)
739 .child(Table.class, new TableKey(flowEntry.getTableId()))
740 .child(Flow.class,flowEntry.getFlow().getKey())
741 .augmentation(FlowStatisticsData.class).toInstance();
742 trans.removeOperationalData(flowRef);
745 private void cleanMeterStatsFromDataStore(DataModificationTransaction trans, MeterConfigStats meterConfigStats) {
746 InstanceIdentifierBuilder<Meter> meterRef
747 = InstanceIdentifier.builder(Nodes.class).child(Node.class,this.targetNodeKey)
748 .augmentation(FlowCapableNode.class)
749 .child(Meter.class,new MeterKey(meterConfigStats.getMeterId()));
751 InstanceIdentifier<?> nodeMeterConfigStatsAugmentation = meterRef.augmentation(NodeMeterConfigStats.class).toInstance();
752 trans.removeOperationalData(nodeMeterConfigStatsAugmentation);
754 InstanceIdentifier<?> nodeMeterStatisticsAugmentation = meterRef.augmentation(NodeMeterStatistics.class).toInstance();
755 trans.removeOperationalData(nodeMeterStatisticsAugmentation);
758 private void cleanGroupStatsFromDataStore(DataModificationTransaction trans, GroupDescStats groupDescStats) {
759 InstanceIdentifierBuilder<Group> groupRef
760 = InstanceIdentifier.builder(Nodes.class).child(Node.class,this.targetNodeKey)
761 .augmentation(FlowCapableNode.class)
762 .child(Group.class,new GroupKey(groupDescStats.getGroupId()));
764 InstanceIdentifier<?> nodeGroupDescStatsAugmentation = groupRef.augmentation(NodeGroupDescStats.class).toInstance();
765 trans.removeOperationalData(nodeGroupDescStatsAugmentation);
767 InstanceIdentifier<?> nodeGroupStatisticsAugmentation = groupRef.augmentation(NodeGroupStatistics.class).toInstance();
768 trans.removeOperationalData(nodeGroupStatisticsAugmentation);
772 public void close() {
773 // FIXME: cleanup any resources we hold (registrations, etc.)
774 logger.debug("Statistics handler for {} shut down", targetNodeKey.getId());