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.List;
12 import java.util.concurrent.ConcurrentMap;
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
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.FlowTableStatisticsUpdate;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericQueueStatistics;
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 final 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 //Add statistics to local cache
146 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
147 if(!cache.containsKey(notification.getId())){
148 cache.put(notification.getId(), new NodeStatistics());
150 cache.get(notification.getId()).setMeterConfigStats(notification.getMeterConfigStats());
152 //Publish data to configuration data store
153 NodeKey key = new NodeKey(notification.getId());
155 List<MeterConfigStats> eterConfigStatsList = notification.getMeterConfigStats();
157 for(MeterConfigStats meterConfigStats : eterConfigStatsList){
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 //Add statistics to local cache
188 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
189 if(!cache.containsKey(notification.getId())){
190 cache.put(notification.getId(), new NodeStatistics());
192 cache.get(notification.getId()).setMeterStatistics(notification.getMeterStats());
194 NodeKey key = new NodeKey(notification.getId());
196 List<MeterStats> meterStatsList = notification.getMeterStats();
198 for(MeterStats meterStats : meterStatsList){
200 //Publish data to configuration data store
201 DataModificationTransaction it = this.statisticsManager.startChange();
202 MeterBuilder meterBuilder = new MeterBuilder();
203 MeterKey meterKey = new MeterKey(meterStats.getMeterId());
204 meterBuilder.setKey(meterKey);
206 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
207 .augmentation(FlowCapableNode.class)
208 .child(Meter.class,meterKey).toInstance();
210 NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
211 MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
212 stats.fieldsFrom(meterStats);
213 meterStatsBuilder.setMeterStatistics(stats.build());
215 //Update augmented data
216 meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
217 it.putOperationalData(meterRef, meterBuilder.build());
223 public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
225 //Check if response is for the request statistics-manager sent.
226 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
229 //Add statistics to local cache
230 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
231 if(!cache.containsKey(notification.getId())){
232 cache.put(notification.getId(), new NodeStatistics());
234 cache.get(notification.getId()).setGroupDescStats(notification.getGroupDescStats());
236 //Publish data to configuration data store
237 NodeKey key = new NodeKey(notification.getId());
238 List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
240 for(GroupDescStats groupDescStats : groupDescStatsList){
241 DataModificationTransaction it = this.statisticsManager.startChange();
243 GroupBuilder groupBuilder = new GroupBuilder();
244 GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
245 groupBuilder.setKey(groupKey);
247 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
248 .augmentation(FlowCapableNode.class)
249 .child(Group.class,groupKey).toInstance();
251 NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
252 GroupDescBuilder stats = new GroupDescBuilder();
253 stats.fieldsFrom(groupDescStats);
254 groupDesc.setGroupDesc(stats.build());
256 //Update augmented data
257 groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
259 it.putOperationalData(groupRef, groupBuilder.build());
265 public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
267 //Check if response is for the request statistics-manager sent.
268 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
271 //Add statistics to local cache
272 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
273 if(!cache.containsKey(notification.getId())){
274 cache.put(notification.getId(), new NodeStatistics());
276 cache.get(notification.getId()).setGroupStatistics(notification.getGroupStats());
278 //Publish data to configuration data store
279 NodeKey key = new NodeKey(notification.getId());
280 List<GroupStats> groupStatsList = notification.getGroupStats();
282 for(GroupStats groupStats : groupStatsList){
283 DataModificationTransaction it = this.statisticsManager.startChange();
285 GroupBuilder groupBuilder = new GroupBuilder();
286 GroupKey groupKey = new GroupKey(groupStats.getGroupId());
287 groupBuilder.setKey(groupKey);
289 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
290 .augmentation(FlowCapableNode.class)
291 .child(Group.class,groupKey).toInstance();
293 NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
294 GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
295 stats.fieldsFrom(groupStats);
296 groupStatisticsBuilder.setGroupStatistics(stats.build());
298 //Update augmented data
299 groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
300 it.putOperationalData(groupRef, groupBuilder.build());
306 public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
308 //Add statistics to local cache
309 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
310 if(!cache.containsKey(notification.getId())){
311 cache.put(notification.getId(), new NodeStatistics());
313 MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
314 meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
315 meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
316 meterFeature.setMaxBands(notification.getMaxBands());
317 meterFeature.setMaxColor(notification.getMaxColor());
318 meterFeature.setMaxMeter(notification.getMaxMeter());
320 cache.get(notification.getId()).setMeterFeatures(meterFeature.build());
322 //Publish data to configuration data store
323 DataModificationTransaction it = this.statisticsManager.startChange();
324 NodeKey key = new NodeKey(notification.getId());
325 NodeRef ref = getNodeRef(key);
327 final NodeBuilder nodeData = new NodeBuilder();
328 nodeData.setKey(key);
330 NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
331 nodeMeterFeatures.setMeterFeatures(meterFeature.build());
333 //Update augmented data
334 nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
336 InstanceIdentifier<? extends Object> refValue = ref.getValue();
337 it.putOperationalData(refValue, nodeData.build());
342 public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
344 //Add statistics to local cache
345 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
346 if(!cache.containsKey(notification.getId())){
347 cache.put(notification.getId(), new NodeStatistics());
350 GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
351 groupFeatures.setActions(notification.getActions());
352 groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
353 groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
354 groupFeatures.setMaxGroups(notification.getMaxGroups());
355 cache.get(notification.getId()).setGroupFeatures(groupFeatures.build());
357 //Publish data to configuration data store
358 DataModificationTransaction it = this.statisticsManager.startChange();
359 NodeKey key = new NodeKey(notification.getId());
360 NodeRef ref = getNodeRef(key);
362 final NodeBuilder nodeData = new NodeBuilder();
363 nodeData.setKey(key);
365 NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
366 nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
368 //Update augmented data
369 nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
371 InstanceIdentifier<? extends Object> refValue = ref.getValue();
372 it.putOperationalData(refValue, nodeData.build());
377 public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
379 //Check if response is for the request statistics-manager sent.
380 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
383 NodeKey key = new NodeKey(notification.getId());
384 sucLogger.debug("Received flow stats update : {}",notification.toString());
386 for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
387 short tableId = map.getTableId();
389 DataModificationTransaction it = this.statisticsManager.startChange();
391 boolean foundOriginalFlow = false;
393 FlowBuilder flowBuilder = new FlowBuilder();
395 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
397 FlowBuilder flow = new FlowBuilder();
398 flow.setContainerName(map.getContainerName());
399 flow.setBufferId(map.getBufferId());
400 flow.setCookie(map.getCookie());
401 flow.setCookieMask(map.getCookieMask());
402 flow.setFlags(map.getFlags());
403 flow.setFlowName(map.getFlowName());
404 flow.setHardTimeout(map.getHardTimeout());
405 if(map.getFlowId() != null)
406 flow.setId(new FlowId(map.getFlowId().getValue()));
407 flow.setIdleTimeout(map.getIdleTimeout());
408 flow.setInstallHw(map.isInstallHw());
409 flow.setInstructions(map.getInstructions());
410 if(map.getFlowId()!= null)
411 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
412 flow.setMatch(map.getMatch());
413 flow.setOutGroup(map.getOutGroup());
414 flow.setOutPort(map.getOutPort());
415 flow.setPriority(map.getPriority());
416 flow.setStrict(map.isStrict());
417 flow.setTableId(tableId);
419 Flow flowRule = flow.build();
421 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
422 stats.setByteCount(map.getByteCount());
423 stats.setPacketCount(map.getPacketCount());
424 stats.setDuration(map.getDuration());
426 GenericStatistics flowStats = stats.build();
428 //Add statistics to local cache
429 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
430 if(!cache.containsKey(notification.getId())){
431 cache.put(notification.getId(), new NodeStatistics());
433 if(!cache.get(notification.getId()).getFlowAndStatsMap().containsKey(tableId)){
434 cache.get(notification.getId()).getFlowAndStatsMap().put(tableId, new HashMap<Flow,GenericStatistics>());
436 cache.get(notification.getId()).getFlowAndStatsMap().get(tableId).put(flowRule,flowStats);
438 //Augment the data to the flow node
440 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
441 flowStatistics.setByteCount(flowStats.getByteCount());
442 flowStatistics.setPacketCount(flowStats.getPacketCount());
443 flowStatistics.setDuration(flowStats.getDuration());
444 flowStatistics.setContainerName(map.getContainerName());
445 flowStatistics.setBufferId(map.getBufferId());
446 flowStatistics.setCookie(map.getCookie());
447 flowStatistics.setCookieMask(map.getCookieMask());
448 flowStatistics.setFlags(map.getFlags());
449 flowStatistics.setFlowName(map.getFlowName());
450 flowStatistics.setHardTimeout(map.getHardTimeout());
451 flowStatistics.setIdleTimeout(map.getIdleTimeout());
452 flowStatistics.setInstallHw(map.isInstallHw());
453 flowStatistics.setInstructions(map.getInstructions());
454 flowStatistics.setMatch(map.getMatch());
455 flowStatistics.setOutGroup(map.getOutGroup());
456 flowStatistics.setOutPort(map.getOutPort());
457 flowStatistics.setPriority(map.getPriority());
458 flowStatistics.setStrict(map.isStrict());
459 flowStatistics.setTableId(tableId);
461 flowStatisticsData.setFlowStatistics(flowStatistics.build());
463 sucLogger.debug("Flow : {}",flowRule.toString());
464 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
466 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
467 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
469 Table table= (Table)it.readConfigurationData(tableRef);
471 //TODO: Not a good way to do it, need to figure out better way.
472 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
473 //to the flows stored in data store.
476 for(Flow existingFlow : table.getFlow()){
477 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
478 if(flowEquals(flowRule,existingFlow)){
479 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
480 .augmentation(FlowCapableNode.class)
481 .child(Table.class, new TableKey(tableId))
482 .child(Flow.class,existingFlow.getKey()).toInstance();
483 flowBuilder.setKey(existingFlow.getKey());
484 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
485 sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
486 foundOriginalFlow = true;
487 it.putOperationalData(flowRef, flowBuilder.build());
494 if(!foundOriginalFlow){
495 sucLogger.info("Associated original flow is not found in data store. Augmenting flow in operational data st");
496 //TODO: Temporary fix: format [ 0+tableid+0+unaccounted flow counter]
497 long flowKey = Long.getLong(new String("0"+Short.toString(tableId)+"0"+Integer.toString(this.unaccountedFlowsCounter)));
498 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
499 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
500 .augmentation(FlowCapableNode.class)
501 .child(Table.class, new TableKey(tableId))
502 .child(Flow.class,newFlowKey).toInstance();
503 flowBuilder.setKey(newFlowKey);
504 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
505 sucLogger.info("Flow was no present in data store, augmenting statistics as an unaccounted flow");
506 it.putOperationalData(flowRef, flowBuilder.build());
513 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
514 //Check if response is for the request statistics-manager sent.
515 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
518 NodeKey key = new NodeKey(notification.getId());
519 sucLogger.debug("Received aggregate flow statistics update : {}",notification.toString());
521 Short tableId = this.statisticsManager.getMultipartMessageManager().getTableIdForTxId(notification.getTransactionId());
524 DataModificationTransaction it = this.statisticsManager.startChange();
526 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
527 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
529 AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
530 AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
531 aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
532 aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
533 aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
534 aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
536 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
537 if(!cache.containsKey(notification.getId())){
538 cache.put(notification.getId(), new NodeStatistics());
540 cache.get(notification.getId()).getTableAndAggregateFlowStatsMap().put(tableId,aggregateFlowStatisticsBuilder.build());
542 sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
544 TableBuilder tableBuilder = new TableBuilder();
545 tableBuilder.setKey(new TableKey(tableId));
546 tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
547 it.putOperationalData(tableRef, tableBuilder.build());
554 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
555 //Check if response is for the request statistics-manager sent.
556 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
559 NodeKey key = new NodeKey(notification.getId());
560 sucLogger.debug("Received port stats update : {}",notification.toString());
562 //Add statistics to local cache
563 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
564 if(!cache.containsKey(notification.getId())){
565 cache.put(notification.getId(), new NodeStatistics());
569 List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
570 for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
572 DataModificationTransaction it = this.statisticsManager.startChange();
574 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
575 = new FlowCapableNodeConnectorStatisticsBuilder();
576 statisticsBuilder.setBytes(portStats.getBytes());
577 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
578 statisticsBuilder.setDuration(portStats.getDuration());
579 statisticsBuilder.setPackets(portStats.getPackets());
580 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
581 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
582 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
583 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
584 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
585 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
586 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
588 //Update data in the cache
589 cache.get(notification.getId()).getNodeConnectorStats().put(portStats.getNodeConnectorId(), statisticsBuilder.build());
591 //Augment data to the node-connector
592 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
593 new FlowCapableNodeConnectorStatisticsDataBuilder();
595 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
597 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
599 NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
601 if(nodeConnector != null){
602 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
603 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
604 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
605 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
612 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
613 //Check if response is for the request statistics-manager sent.
614 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
617 NodeKey key = new NodeKey(notification.getId());
618 sucLogger.debug("Received flow table statistics update : {}",notification.toString());
620 List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
621 for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
623 DataModificationTransaction it = this.statisticsManager.startChange();
625 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
626 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
628 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
630 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
631 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
632 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
633 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
635 statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
637 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
638 if(!cache.containsKey(notification.getId())){
639 cache.put(notification.getId(), new NodeStatistics());
641 cache.get(notification.getId()).getFlowTableAndStatisticsMap().put(ftStats.getTableId().getValue(),statisticsBuilder.build());
643 sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
645 TableBuilder tableBuilder = new TableBuilder();
646 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
647 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
648 it.putOperationalData(tableRef, tableBuilder.build());
654 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
656 //Check if response is for the request statistics-manager sent.
657 if(this.statisticsManager.getMultipartMessageManager().removeTxId(notification.getTransactionId()) == null)
660 NodeKey key = new NodeKey(notification.getId());
661 sucLogger.debug("Received queue stats update : {}",notification.toString());
663 //Add statistics to local cache
664 ConcurrentMap<NodeId, NodeStatistics> cache = this.statisticsManager.getStatisticsCache();
665 if(!cache.containsKey(notification.getId())){
666 cache.put(notification.getId(), new NodeStatistics());
669 List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
670 for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
672 if(!cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap().containsKey(swQueueStats.getNodeConnectorId())){
673 cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap().put(swQueueStats.getNodeConnectorId(), new HashMap<QueueId,GenericQueueStatistics>());
676 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
678 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
680 queueStatisticsBuilder.fieldsFrom(swQueueStats);
682 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
684 cache.get(notification.getId()).getNodeConnectorAndQueuesStatsMap()
685 .get(swQueueStats.getNodeConnectorId())
686 .put(swQueueStats.getQueueId(), queueStatisticsBuilder.build());
689 DataModificationTransaction it = this.statisticsManager.startChange();
691 InstanceIdentifier<Queue> queueRef
692 = InstanceIdentifier.builder(Nodes.class)
693 .child(Node.class, key)
694 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
695 .augmentation(FlowCapableNodeConnector.class)
696 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
698 QueueBuilder queueBuilder = new QueueBuilder();
699 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
700 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
702 sucLogger.info("Augmenting queue statistics {} of queue {} to port {}"
703 ,queueStatisticsDataBuilder.build().toString(),
704 swQueueStats.getQueueId(),
705 swQueueStats.getNodeConnectorId());
707 it.putOperationalData(queueRef, queueBuilder.build());
714 private NodeRef getNodeRef(NodeKey nodeKey){
715 InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
716 return new NodeRef(builder.toInstance());
719 public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
720 if (statsFlow.getClass() != storedFlow.getClass()) {
723 if (statsFlow.getBufferId()== null) {
724 if (storedFlow.getBufferId() != null) {
727 } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
730 if (statsFlow.getContainerName()== null) {
731 if (storedFlow.getContainerName()!= null) {
734 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
737 if (statsFlow.getCookie()== null) {
738 if (storedFlow.getCookie()!= null) {
741 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
744 if (statsFlow.getMatch()== null) {
745 if (storedFlow.getMatch() != null) {
748 } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
751 if (statsFlow.getCookie()== null) {
752 if (storedFlow.getCookie()!= null) {
755 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
758 if (statsFlow.getHardTimeout() == null) {
759 if (storedFlow.getHardTimeout() != null) {
762 } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
765 if (statsFlow.getIdleTimeout()== null) {
766 if (storedFlow.getIdleTimeout() != null) {
769 } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
772 if (statsFlow.getPriority() == null) {
773 if (storedFlow.getPriority() != null) {
776 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
779 if (statsFlow.getTableId() == null) {
780 if (storedFlow.getTableId() != null) {
783 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {