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.List;
11 import java.util.concurrent.ConcurrentMap;
13 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.FlowEntry;
14 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.QueueEntry;
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
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.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
104 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
105 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
106 import org.slf4j.Logger;
107 import org.slf4j.LoggerFactory;
110 * Class implement statistics manager related listener interface and augment all the
111 * received statistics data to data stores.
112 * TODO: Need to add error message listener and clean-up the associated tx id
113 * if it exists in the tx-id cache.
114 * @author vishnoianil
117 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
118 OpendaylightMeterStatisticsListener,
119 OpendaylightFlowStatisticsListener,
120 OpendaylightPortStatisticsListener,
121 OpendaylightFlowTableStatisticsListener,
122 OpendaylightQueueStatisticsListener{
124 public final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
126 private final StatisticsProvider statisticsManager;
128 private int unaccountedFlowsCounter = 1;
130 public StatisticsUpdateCommiter(final StatisticsProvider manager){
132 this.statisticsManager = manager;
135 public StatisticsProvider getStatisticsManager(){
136 return statisticsManager;
140 public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
141 //Check if response is for the request statistics-manager sent.
142 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
145 NodeKey key = new NodeKey(notification.getId());
147 //Add statistics to local cache
148 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
149 if(!cache.containsKey(notification.getId())){
150 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
152 cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
154 //Publish data to configuration data store
155 List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
157 for(MeterConfigStats meterConfigStats : meterConfigStatsList){
158 DataModificationTransaction it = this.statisticsManager.startChange();
159 MeterBuilder meterBuilder = new MeterBuilder();
160 MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
161 meterBuilder.setKey(meterKey);
163 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
164 .augmentation(FlowCapableNode.class)
165 .child(Meter.class,meterKey).toInstance();
167 NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
168 MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
169 stats.fieldsFrom(meterConfigStats);
170 meterConfig.setMeterConfigStats(stats.build());
172 //Update augmented data
173 meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
174 it.putOperationalData(meterRef, meterBuilder.build());
181 public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
183 //Check if response is for the request statistics-manager sent.
184 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
187 NodeKey key = new NodeKey(notification.getId());
189 List<MeterStats> meterStatsList = notification.getMeterStats();
191 for(MeterStats meterStats : meterStatsList){
193 //Publish data to configuration data store
194 DataModificationTransaction it = this.statisticsManager.startChange();
195 MeterBuilder meterBuilder = new MeterBuilder();
196 MeterKey meterKey = new MeterKey(meterStats.getMeterId());
197 meterBuilder.setKey(meterKey);
199 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
200 .augmentation(FlowCapableNode.class)
201 .child(Meter.class,meterKey).toInstance();
203 NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
204 MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
205 stats.fieldsFrom(meterStats);
206 meterStatsBuilder.setMeterStatistics(stats.build());
208 //Update augmented data
209 meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
210 it.putOperationalData(meterRef, meterBuilder.build());
216 public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
218 //Check if response is for the request statistics-manager sent.
219 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
222 NodeKey key = new NodeKey(notification.getId());
224 //Add statistics to local cache
225 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
226 if(!cache.containsKey(notification.getId())){
227 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
229 cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
231 //Publish data to configuration data store
232 List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
234 for(GroupDescStats groupDescStats : groupDescStatsList){
235 DataModificationTransaction it = this.statisticsManager.startChange();
237 GroupBuilder groupBuilder = new GroupBuilder();
238 GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
239 groupBuilder.setKey(groupKey);
241 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
242 .augmentation(FlowCapableNode.class)
243 .child(Group.class,groupKey).toInstance();
245 NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
246 GroupDescBuilder stats = new GroupDescBuilder();
247 stats.fieldsFrom(groupDescStats);
248 groupDesc.setGroupDesc(stats.build());
250 //Update augmented data
251 groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
253 it.putOperationalData(groupRef, groupBuilder.build());
259 public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
261 //Check if response is for the request statistics-manager sent.
262 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
265 //Publish data to configuration data store
266 NodeKey key = new NodeKey(notification.getId());
267 List<GroupStats> groupStatsList = notification.getGroupStats();
269 for(GroupStats groupStats : groupStatsList){
270 DataModificationTransaction it = this.statisticsManager.startChange();
272 GroupBuilder groupBuilder = new GroupBuilder();
273 GroupKey groupKey = new GroupKey(groupStats.getGroupId());
274 groupBuilder.setKey(groupKey);
276 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
277 .augmentation(FlowCapableNode.class)
278 .child(Group.class,groupKey).toInstance();
280 NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
281 GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
282 stats.fieldsFrom(groupStats);
283 groupStatisticsBuilder.setGroupStatistics(stats.build());
285 //Update augmented data
286 groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
287 it.putOperationalData(groupRef, groupBuilder.build());
293 public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
295 MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
296 meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
297 meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
298 meterFeature.setMaxBands(notification.getMaxBands());
299 meterFeature.setMaxColor(notification.getMaxColor());
300 meterFeature.setMaxMeter(notification.getMaxMeter());
302 //Publish data to configuration data store
303 DataModificationTransaction it = this.statisticsManager.startChange();
304 NodeKey key = new NodeKey(notification.getId());
305 NodeRef ref = getNodeRef(key);
307 final NodeBuilder nodeData = new NodeBuilder();
308 nodeData.setKey(key);
310 NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
311 nodeMeterFeatures.setMeterFeatures(meterFeature.build());
313 //Update augmented data
314 nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
316 InstanceIdentifier<? extends Object> refValue = ref.getValue();
317 it.putOperationalData(refValue, nodeData.build());
322 public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
324 GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
325 groupFeatures.setActions(notification.getActions());
326 groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
327 groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
328 groupFeatures.setMaxGroups(notification.getMaxGroups());
330 //Publish data to configuration data store
331 DataModificationTransaction it = this.statisticsManager.startChange();
332 NodeKey key = new NodeKey(notification.getId());
333 NodeRef ref = getNodeRef(key);
335 final NodeBuilder nodeData = new NodeBuilder();
336 nodeData.setKey(key);
338 NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
339 nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
341 //Update augmented data
342 nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
344 InstanceIdentifier<? extends Object> refValue = ref.getValue();
345 it.putOperationalData(refValue, nodeData.build());
350 public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
352 //Check if response is for the request statistics-manager sent.
353 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
356 NodeKey key = new NodeKey(notification.getId());
357 sucLogger.debug("Received flow stats update : {}",notification.toString());
359 for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
360 short tableId = map.getTableId();
362 DataModificationTransaction it = this.statisticsManager.startChange();
364 boolean foundOriginalFlow = false;
366 FlowBuilder flowBuilder = new FlowBuilder();
368 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
370 FlowBuilder flow = new FlowBuilder();
371 flow.setContainerName(map.getContainerName());
372 flow.setBufferId(map.getBufferId());
373 flow.setCookie(map.getCookie());
374 flow.setCookieMask(map.getCookieMask());
375 flow.setFlags(map.getFlags());
376 flow.setFlowName(map.getFlowName());
377 flow.setHardTimeout(map.getHardTimeout());
378 if(map.getFlowId() != null)
379 flow.setId(new FlowId(map.getFlowId().getValue()));
380 flow.setIdleTimeout(map.getIdleTimeout());
381 flow.setInstallHw(map.isInstallHw());
382 flow.setInstructions(map.getInstructions());
383 if(map.getFlowId()!= null)
384 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
385 flow.setMatch(map.getMatch());
386 flow.setOutGroup(map.getOutGroup());
387 flow.setOutPort(map.getOutPort());
388 flow.setPriority(map.getPriority());
389 flow.setStrict(map.isStrict());
390 flow.setTableId(tableId);
392 Flow flowRule = flow.build();
394 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
395 stats.setByteCount(map.getByteCount());
396 stats.setPacketCount(map.getPacketCount());
397 stats.setDuration(map.getDuration());
399 GenericStatistics flowStats = stats.build();
401 //Add statistics to local cache
402 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
403 if(!cache.containsKey(notification.getId())){
404 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
406 NodeStatisticsAger nsa = cache.get(notification.getId());
407 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flowRule);
408 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
410 //Augment the data to the flow node
412 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
413 flowStatistics.setByteCount(flowStats.getByteCount());
414 flowStatistics.setPacketCount(flowStats.getPacketCount());
415 flowStatistics.setDuration(flowStats.getDuration());
416 flowStatistics.setContainerName(map.getContainerName());
417 flowStatistics.setBufferId(map.getBufferId());
418 flowStatistics.setCookie(map.getCookie());
419 flowStatistics.setCookieMask(map.getCookieMask());
420 flowStatistics.setFlags(map.getFlags());
421 flowStatistics.setFlowName(map.getFlowName());
422 flowStatistics.setHardTimeout(map.getHardTimeout());
423 flowStatistics.setIdleTimeout(map.getIdleTimeout());
424 flowStatistics.setInstallHw(map.isInstallHw());
425 flowStatistics.setInstructions(map.getInstructions());
426 flowStatistics.setMatch(map.getMatch());
427 flowStatistics.setOutGroup(map.getOutGroup());
428 flowStatistics.setOutPort(map.getOutPort());
429 flowStatistics.setPriority(map.getPriority());
430 flowStatistics.setStrict(map.isStrict());
431 flowStatistics.setTableId(tableId);
433 flowStatisticsData.setFlowStatistics(flowStatistics.build());
435 sucLogger.debug("Flow : {}",flowRule.toString());
436 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
438 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
439 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
441 Table table= (Table)it.readConfigurationData(tableRef);
443 //TODO: Not a good way to do it, need to figure out better way.
444 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
445 //to the flows stored in data store.
448 for(Flow existingFlow : table.getFlow()){
449 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
450 if(flowEquals(flowRule,existingFlow)){
451 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
452 .augmentation(FlowCapableNode.class)
453 .child(Table.class, new TableKey(tableId))
454 .child(Flow.class,existingFlow.getKey()).toInstance();
455 flowBuilder.setKey(existingFlow.getKey());
456 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
457 sucLogger.info("Found matching flow in the datastore, augmenting statistics");
458 foundOriginalFlow = true;
459 it.putOperationalData(flowRef, flowBuilder.build());
466 table= (Table)it.readOperationalData(tableRef);
467 if(!foundOriginalFlow && table != null){
469 for(Flow existingFlow : table.getFlow()){
470 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
471 if(augmentedflowStatisticsData != null){
472 FlowBuilder existingOperationalFlow = new FlowBuilder();
473 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
474 sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
475 if(flowEquals(flowRule,existingOperationalFlow.build())){
476 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
477 .augmentation(FlowCapableNode.class)
478 .child(Table.class, new TableKey(tableId))
479 .child(Flow.class,existingFlow.getKey()).toInstance();
480 flowBuilder.setKey(existingFlow.getKey());
481 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
482 sucLogger.debug("Found matching flow in the operational datastore, augmenting statistics");
483 foundOriginalFlow = true;
484 it.putOperationalData(flowRef, flowBuilder.build());
491 if(!foundOriginalFlow){
492 sucLogger.debug("Associated original flow is not found in data store. Augmenting flow in operational data store");
493 long flowKey = Long.parseLong(new String("1"+Short.toString(tableId)+"0"+Integer.toString(this.unaccountedFlowsCounter)));
494 this.unaccountedFlowsCounter++;
495 FlowKey newFlowKey = new FlowKey(new FlowId(Long.toString(flowKey)));
496 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
497 .augmentation(FlowCapableNode.class)
498 .child(Table.class, new TableKey(tableId))
499 .child(Flow.class,newFlowKey).toInstance();
500 flowBuilder.setKey(newFlowKey);
501 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
502 sucLogger.info("Flow was no present in data store, augmenting statistics as an unaccounted flow");
503 it.putOperationalData(flowRef, flowBuilder.build());
510 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
511 //Check if response is for the request statistics-manager sent.
512 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
515 NodeKey key = new NodeKey(notification.getId());
516 sucLogger.debug("Received aggregate flow statistics update : {}",notification.toString());
518 Short tableId = this.statisticsManager.getMultipartMessageManager().getTableIdForTxId(notification.getTransactionId());
521 DataModificationTransaction it = this.statisticsManager.startChange();
523 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
524 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
526 AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
527 AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
528 aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
529 aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
530 aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
531 aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
533 sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
535 TableBuilder tableBuilder = new TableBuilder();
536 tableBuilder.setKey(new TableKey(tableId));
537 tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
538 it.putOperationalData(tableRef, tableBuilder.build());
545 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
546 //Check if response is for the request statistics-manager sent.
547 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
550 NodeKey key = new NodeKey(notification.getId());
551 sucLogger.debug("Received port stats update : {}",notification.toString());
553 List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
554 for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
556 DataModificationTransaction it = this.statisticsManager.startChange();
558 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
559 = new FlowCapableNodeConnectorStatisticsBuilder();
560 statisticsBuilder.setBytes(portStats.getBytes());
561 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
562 statisticsBuilder.setDuration(portStats.getDuration());
563 statisticsBuilder.setPackets(portStats.getPackets());
564 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
565 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
566 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
567 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
568 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
569 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
570 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
572 //Augment data to the node-connector
573 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
574 new FlowCapableNodeConnectorStatisticsDataBuilder();
576 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
578 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
580 NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
582 if(nodeConnector != null){
583 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
584 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
585 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
586 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
593 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
594 //Check if response is for the request statistics-manager sent.
595 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
598 NodeKey key = new NodeKey(notification.getId());
599 sucLogger.debug("Received flow table statistics update : {}",notification.toString());
601 List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
602 for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
604 DataModificationTransaction it = this.statisticsManager.startChange();
606 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
607 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
609 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
611 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
612 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
613 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
614 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
616 statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
618 sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
620 TableBuilder tableBuilder = new TableBuilder();
621 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
622 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
623 it.putOperationalData(tableRef, tableBuilder.build());
629 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
631 //Check if response is for the request statistics-manager sent.
632 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
635 NodeKey key = new NodeKey(notification.getId());
636 sucLogger.debug("Received queue stats update : {}",notification.toString());
638 //Add statistics to local cache
639 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
640 if(!cache.containsKey(notification.getId())){
641 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
644 NodeStatisticsAger nsa = cache.get(notification.getId());
646 List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
647 for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
649 QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
650 nsa.updateQueueStats(queueEntry);
652 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
654 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
656 queueStatisticsBuilder.fieldsFrom(swQueueStats);
658 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
660 DataModificationTransaction it = this.statisticsManager.startChange();
662 InstanceIdentifier<Queue> queueRef
663 = InstanceIdentifier.builder(Nodes.class)
664 .child(Node.class, key)
665 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
666 .augmentation(FlowCapableNodeConnector.class)
667 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
669 QueueBuilder queueBuilder = new QueueBuilder();
670 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
671 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
673 sucLogger.info("Augmenting queue statistics {} of queue {} to port {}"
674 ,queueStatisticsDataBuilder.build().toString(),
675 swQueueStats.getQueueId(),
676 swQueueStats.getNodeConnectorId());
678 it.putOperationalData(queueRef, queueBuilder.build());
685 private NodeRef getNodeRef(NodeKey nodeKey){
686 InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
687 return new NodeRef(builder.toInstance());
690 public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
691 if (statsFlow.getClass() != storedFlow.getClass()) {
694 if (statsFlow.getBufferId()== null) {
695 if (storedFlow.getBufferId() != null) {
698 } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
701 if (statsFlow.getContainerName()== null) {
702 if (storedFlow.getContainerName()!= null) {
705 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
708 if (statsFlow.getCookie()== null) {
709 if (storedFlow.getCookie()!= null) {
712 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
715 if (statsFlow.getMatch()== null) {
716 if (storedFlow.getMatch() != null) {
719 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
720 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
723 if (statsFlow.getHardTimeout() == null) {
724 if (storedFlow.getHardTimeout() != null) {
727 } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
730 if (statsFlow.getIdleTimeout()== null) {
731 if (storedFlow.getIdleTimeout() != null) {
734 } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
737 if (statsFlow.getPriority() == null) {
738 if (storedFlow.getPriority() != null) {
741 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
744 if (statsFlow.getTableId() == null) {
745 if (storedFlow.getTableId() != null) {
748 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
755 * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
756 * Usecase: e.g If user don't set any ethernet source and destination address for match,data store will store null for
758 * e.g [_ethernetMatch=EthernetMatch [_ethernetDestination=null, _ethernetSource=null, _ethernetType=
759 * EthernetType [_type=EtherType [_value=2048], _mask=null, augmentation=[]]
761 * But when you fetch the flows from switch, openflow driver library converts all zero bytes of mac address in the
762 * message stream to 00:00:00:00:00:00. Following string shows how library interpret the zero mac address bytes and
763 * eventually when translator convert it to MD-SAL match, this is how it looks
764 * [_ethernetDestination=EthernetDestination [_address=MacAddress [_value=00:00:00:00:00:00], _mask=null, augmentation=[]],
765 * _ethernetSource=EthernetSource [_address=MacAddress [_value=00:00:00:00:00:00], _mask=null, augmentation=[]],
766 * _ethernetType=EthernetType [_type=EtherType [_value=2048], _mask=null, augmentation=[]]
768 * Similarly for inPort, if user/application don't set any value for it, FRM will store null value for it in data store.
769 * When we fetch the same flow (with its statistics) from switch, plugin converts its value to openflow:X:0.
770 * e.g _inPort=Uri [_value=openflow:1:0]
772 * So this custom equals method add additional check to take care of these scenario, in case any match element is null in data-store-flow, but not
773 * in the flow fetched from switch.
780 public boolean matchEquals(Match statsFlow, Match storedFlow) {
781 if (statsFlow == storedFlow) {
784 if (storedFlow.getClass() != statsFlow.getClass()) {
787 if (storedFlow.getEthernetMatch() == null) {
788 if (statsFlow.getEthernetMatch() != null) {
791 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
794 if (storedFlow.getIcmpv4Match()== null) {
795 if (statsFlow.getIcmpv4Match() != null) {
798 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
801 if (storedFlow.getIcmpv6Match() == null) {
802 if (statsFlow.getIcmpv6Match() != null) {
805 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
808 if (storedFlow.getInPhyPort() == null) {
809 if (statsFlow.getInPhyPort() != null) {
812 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
815 if (storedFlow.getInPort()== null) {
816 if (statsFlow.getInPort() != null) {
819 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
822 if (storedFlow.getIpMatch()== null) {
823 if (statsFlow.getIpMatch() != null) {
826 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
829 if (storedFlow.getLayer3Match()== null) {
830 if (statsFlow.getLayer3Match() != null) {
833 } else if(!storedFlow.getLayer3Match().equals(statsFlow.getLayer3Match())) {
836 if (storedFlow.getLayer4Match()== null) {
837 if (statsFlow.getLayer4Match() != null) {
840 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
843 if (storedFlow.getMetadata() == null) {
844 if (statsFlow.getMetadata() != null) {
847 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
850 if (storedFlow.getProtocolMatchFields() == null) {
851 if (statsFlow.getProtocolMatchFields() != null) {
854 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
857 if (storedFlow.getTunnel()== null) {
858 if (statsFlow.getTunnel() != null) {
861 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
864 if (storedFlow.getVlanMatch()== null) {
865 if (statsFlow.getVlanMatch() != null) {
868 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {