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;
127 private final MultipartMessageManager messageManager;
129 private int unaccountedFlowsCounter = 1;
131 public StatisticsUpdateCommiter(final StatisticsProvider manager){
133 this.statisticsManager = manager;
134 this.messageManager = this.statisticsManager.getMultipartMessageManager();
137 public StatisticsProvider getStatisticsManager(){
138 return statisticsManager;
142 public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
143 //Check if response is for the request statistics-manager sent.
144 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
147 NodeKey key = new NodeKey(notification.getId());
149 //Add statistics to local cache
150 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
151 if(!cache.containsKey(notification.getId())){
152 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
154 cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
156 //Publish data to configuration data store
157 List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
159 for(MeterConfigStats meterConfigStats : meterConfigStatsList){
160 DataModificationTransaction it = this.statisticsManager.startChange();
161 MeterBuilder meterBuilder = new MeterBuilder();
162 MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
163 meterBuilder.setKey(meterKey);
165 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
166 .augmentation(FlowCapableNode.class)
167 .child(Meter.class,meterKey).toInstance();
169 NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
170 MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
171 stats.fieldsFrom(meterConfigStats);
172 meterConfig.setMeterConfigStats(stats.build());
174 //Update augmented data
175 meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
176 it.putOperationalData(meterRef, meterBuilder.build());
183 public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
185 //Check if response is for the request statistics-manager sent.
186 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
189 NodeKey key = new NodeKey(notification.getId());
191 List<MeterStats> meterStatsList = notification.getMeterStats();
193 for(MeterStats meterStats : meterStatsList){
195 //Publish data to configuration data store
196 DataModificationTransaction it = this.statisticsManager.startChange();
197 MeterBuilder meterBuilder = new MeterBuilder();
198 MeterKey meterKey = new MeterKey(meterStats.getMeterId());
199 meterBuilder.setKey(meterKey);
201 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
202 .augmentation(FlowCapableNode.class)
203 .child(Meter.class,meterKey).toInstance();
205 NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
206 MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
207 stats.fieldsFrom(meterStats);
208 meterStatsBuilder.setMeterStatistics(stats.build());
210 //Update augmented data
211 meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
212 it.putOperationalData(meterRef, meterBuilder.build());
218 public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
220 //Check if response is for the request statistics-manager sent.
221 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
224 NodeKey key = new NodeKey(notification.getId());
226 //Add statistics to local cache
227 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
228 if(!cache.containsKey(notification.getId())){
229 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
231 cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
233 //Publish data to configuration data store
234 List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
236 for(GroupDescStats groupDescStats : groupDescStatsList){
237 DataModificationTransaction it = this.statisticsManager.startChange();
239 GroupBuilder groupBuilder = new GroupBuilder();
240 GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
241 groupBuilder.setKey(groupKey);
243 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
244 .augmentation(FlowCapableNode.class)
245 .child(Group.class,groupKey).toInstance();
247 NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
248 GroupDescBuilder stats = new GroupDescBuilder();
249 stats.fieldsFrom(groupDescStats);
250 groupDesc.setGroupDesc(stats.build());
252 //Update augmented data
253 groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
255 it.putOperationalData(groupRef, groupBuilder.build());
261 public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
263 //Check if response is for the request statistics-manager sent.
264 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
267 //Publish data to configuration data store
268 NodeKey key = new NodeKey(notification.getId());
269 List<GroupStats> groupStatsList = notification.getGroupStats();
271 for(GroupStats groupStats : groupStatsList){
272 DataModificationTransaction it = this.statisticsManager.startChange();
274 GroupBuilder groupBuilder = new GroupBuilder();
275 GroupKey groupKey = new GroupKey(groupStats.getGroupId());
276 groupBuilder.setKey(groupKey);
278 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
279 .augmentation(FlowCapableNode.class)
280 .child(Group.class,groupKey).toInstance();
282 NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
283 GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
284 stats.fieldsFrom(groupStats);
285 groupStatisticsBuilder.setGroupStatistics(stats.build());
287 //Update augmented data
288 groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
289 it.putOperationalData(groupRef, groupBuilder.build());
295 public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
297 MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
298 meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
299 meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
300 meterFeature.setMaxBands(notification.getMaxBands());
301 meterFeature.setMaxColor(notification.getMaxColor());
302 meterFeature.setMaxMeter(notification.getMaxMeter());
304 //Publish data to configuration data store
305 DataModificationTransaction it = this.statisticsManager.startChange();
306 NodeKey key = new NodeKey(notification.getId());
307 NodeRef ref = getNodeRef(key);
309 final NodeBuilder nodeData = new NodeBuilder();
310 nodeData.setKey(key);
312 NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
313 nodeMeterFeatures.setMeterFeatures(meterFeature.build());
315 //Update augmented data
316 nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
318 InstanceIdentifier<? extends Object> refValue = ref.getValue();
319 it.putOperationalData(refValue, nodeData.build());
324 public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
326 GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
327 groupFeatures.setActions(notification.getActions());
328 groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
329 groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
330 groupFeatures.setMaxGroups(notification.getMaxGroups());
332 //Publish data to configuration data store
333 DataModificationTransaction it = this.statisticsManager.startChange();
334 NodeKey key = new NodeKey(notification.getId());
335 NodeRef ref = getNodeRef(key);
337 final NodeBuilder nodeData = new NodeBuilder();
338 nodeData.setKey(key);
340 NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
341 nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
343 //Update augmented data
344 nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
346 InstanceIdentifier<? extends Object> refValue = ref.getValue();
347 it.putOperationalData(refValue, nodeData.build());
352 public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
354 //Check if response is for the request statistics-manager sent.
355 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
358 NodeKey key = new NodeKey(notification.getId());
359 sucLogger.debug("Received flow stats update : {}",notification.toString());
361 for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
362 short tableId = map.getTableId();
364 DataModificationTransaction it = this.statisticsManager.startChange();
366 boolean foundOriginalFlow = false;
368 FlowBuilder flowBuilder = new FlowBuilder();
370 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
372 FlowBuilder flow = new FlowBuilder();
373 flow.setContainerName(map.getContainerName());
374 flow.setBufferId(map.getBufferId());
375 flow.setCookie(map.getCookie());
376 flow.setCookieMask(map.getCookieMask());
377 flow.setFlags(map.getFlags());
378 flow.setFlowName(map.getFlowName());
379 flow.setHardTimeout(map.getHardTimeout());
380 if(map.getFlowId() != null)
381 flow.setId(new FlowId(map.getFlowId().getValue()));
382 flow.setIdleTimeout(map.getIdleTimeout());
383 flow.setInstallHw(map.isInstallHw());
384 flow.setInstructions(map.getInstructions());
385 if(map.getFlowId()!= null)
386 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
387 flow.setMatch(map.getMatch());
388 flow.setOutGroup(map.getOutGroup());
389 flow.setOutPort(map.getOutPort());
390 flow.setPriority(map.getPriority());
391 flow.setStrict(map.isStrict());
392 flow.setTableId(tableId);
394 Flow flowRule = flow.build();
396 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
397 stats.setByteCount(map.getByteCount());
398 stats.setPacketCount(map.getPacketCount());
399 stats.setDuration(map.getDuration());
401 GenericStatistics flowStats = stats.build();
403 //Add statistics to local cache
404 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
405 if(!cache.containsKey(notification.getId())){
406 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
408 NodeStatisticsAger nsa = cache.get(notification.getId());
409 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flowRule);
410 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
412 //Augment the data to the flow node
414 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
415 flowStatistics.setByteCount(flowStats.getByteCount());
416 flowStatistics.setPacketCount(flowStats.getPacketCount());
417 flowStatistics.setDuration(flowStats.getDuration());
418 flowStatistics.setContainerName(map.getContainerName());
419 flowStatistics.setBufferId(map.getBufferId());
420 flowStatistics.setCookie(map.getCookie());
421 flowStatistics.setCookieMask(map.getCookieMask());
422 flowStatistics.setFlags(map.getFlags());
423 flowStatistics.setFlowName(map.getFlowName());
424 flowStatistics.setHardTimeout(map.getHardTimeout());
425 flowStatistics.setIdleTimeout(map.getIdleTimeout());
426 flowStatistics.setInstallHw(map.isInstallHw());
427 flowStatistics.setInstructions(map.getInstructions());
428 flowStatistics.setMatch(map.getMatch());
429 flowStatistics.setOutGroup(map.getOutGroup());
430 flowStatistics.setOutPort(map.getOutPort());
431 flowStatistics.setPriority(map.getPriority());
432 flowStatistics.setStrict(map.isStrict());
433 flowStatistics.setTableId(tableId);
435 flowStatisticsData.setFlowStatistics(flowStatistics.build());
437 sucLogger.debug("Flow : {}",flowRule.toString());
438 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
440 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
441 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
443 Table table= (Table)it.readConfigurationData(tableRef);
445 //TODO: Not a good way to do it, need to figure out better way.
446 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
447 //to the flows stored in data store.
450 for(Flow existingFlow : table.getFlow()){
451 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
452 if(flowEquals(flowRule,existingFlow)){
453 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
454 .augmentation(FlowCapableNode.class)
455 .child(Table.class, new TableKey(tableId))
456 .child(Flow.class,existingFlow.getKey()).toInstance();
457 flowBuilder.setKey(existingFlow.getKey());
458 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
459 sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
460 foundOriginalFlow = true;
461 it.putOperationalData(flowRef, flowBuilder.build());
468 table= (Table)it.readOperationalData(tableRef);
469 if(!foundOriginalFlow && table != null){
471 for(Flow existingFlow : table.getFlow()){
472 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
473 if(augmentedflowStatisticsData != null){
474 FlowBuilder existingOperationalFlow = new FlowBuilder();
475 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
476 sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
477 if(flowEquals(flowRule,existingOperationalFlow.build())){
478 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
479 .augmentation(FlowCapableNode.class)
480 .child(Table.class, new TableKey(tableId))
481 .child(Flow.class,existingFlow.getKey()).toInstance();
482 flowBuilder.setKey(existingFlow.getKey());
483 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
484 sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
485 foundOriginalFlow = true;
486 it.putOperationalData(flowRef, flowBuilder.build());
493 if(!foundOriginalFlow){
494 long flowKey = Long.parseLong(new String("1"+Short.toString(tableId)+"0"+Integer.toString(this.unaccountedFlowsCounter)));
495 this.unaccountedFlowsCounter++;
496 FlowKey newFlowKey = new FlowKey(new FlowId(Long.toString(flowKey)));
497 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
498 .augmentation(FlowCapableNode.class)
499 .child(Table.class, new TableKey(tableId))
500 .child(Flow.class,newFlowKey).toInstance();
501 flowBuilder.setKey(newFlowKey);
502 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
503 sucLogger.info("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
504 it.putOperationalData(flowRef, flowBuilder.build());
511 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
512 //Check if response is for the request statistics-manager sent.
513 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
516 NodeKey key = new NodeKey(notification.getId());
518 Short tableId = messageManager.getTableIdForTxId(notification.getId(),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(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
550 NodeKey key = new NodeKey(notification.getId());
552 List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
553 for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
555 DataModificationTransaction it = this.statisticsManager.startChange();
557 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
558 = new FlowCapableNodeConnectorStatisticsBuilder();
559 statisticsBuilder.setBytes(portStats.getBytes());
560 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
561 statisticsBuilder.setDuration(portStats.getDuration());
562 statisticsBuilder.setPackets(portStats.getPackets());
563 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
564 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
565 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
566 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
567 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
568 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
569 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
571 //Augment data to the node-connector
572 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
573 new FlowCapableNodeConnectorStatisticsDataBuilder();
575 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
577 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
579 NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
581 if(nodeConnector != null){
582 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
583 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
584 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
585 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
592 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
593 //Check if response is for the request statistics-manager sent.
594 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
597 NodeKey key = new NodeKey(notification.getId());
599 List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
600 for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
602 DataModificationTransaction it = this.statisticsManager.startChange();
604 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
605 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
607 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
609 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
610 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
611 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
612 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
614 statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
616 sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
618 TableBuilder tableBuilder = new TableBuilder();
619 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
620 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
621 it.putOperationalData(tableRef, tableBuilder.build());
627 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
629 //Check if response is for the request statistics-manager sent.
630 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
633 NodeKey key = new NodeKey(notification.getId());
635 //Add statistics to local cache
636 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
637 if(!cache.containsKey(notification.getId())){
638 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
641 NodeStatisticsAger nsa = cache.get(notification.getId());
643 List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
644 for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
646 QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
647 nsa.updateQueueStats(queueEntry);
649 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
651 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
653 queueStatisticsBuilder.fieldsFrom(swQueueStats);
655 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
657 DataModificationTransaction it = this.statisticsManager.startChange();
659 InstanceIdentifier<Queue> queueRef
660 = InstanceIdentifier.builder(Nodes.class)
661 .child(Node.class, key)
662 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
663 .augmentation(FlowCapableNodeConnector.class)
664 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
666 QueueBuilder queueBuilder = new QueueBuilder();
667 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
668 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
670 sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
671 ,queueStatisticsDataBuilder.build().toString(),
672 swQueueStats.getQueueId(),
673 swQueueStats.getNodeConnectorId());
675 it.putOperationalData(queueRef, queueBuilder.build());
682 private NodeRef getNodeRef(NodeKey nodeKey){
683 InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
684 return new NodeRef(builder.toInstance());
687 public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
688 if (statsFlow.getClass() != storedFlow.getClass()) {
691 if (statsFlow.getBufferId()== null) {
692 if (storedFlow.getBufferId() != null) {
695 } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
698 if (statsFlow.getContainerName()== null) {
699 if (storedFlow.getContainerName()!= null) {
702 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
705 if (statsFlow.getCookie()== null) {
706 if (storedFlow.getCookie()!= null) {
709 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
712 if (statsFlow.getMatch()== null) {
713 if (storedFlow.getMatch() != null) {
716 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
717 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
720 if (statsFlow.getHardTimeout() == null) {
721 if (storedFlow.getHardTimeout() != null) {
724 } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
727 if (statsFlow.getIdleTimeout()== null) {
728 if (storedFlow.getIdleTimeout() != null) {
731 } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
734 if (statsFlow.getPriority() == null) {
735 if (storedFlow.getPriority() != null) {
738 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
741 if (statsFlow.getTableId() == null) {
742 if (storedFlow.getTableId() != null) {
745 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
752 * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
753 * Usecase: e.g If user don't set any ethernet source and destination address for match,data store will store null for
755 * e.g [_ethernetMatch=EthernetMatch [_ethernetDestination=null, _ethernetSource=null, _ethernetType=
756 * EthernetType [_type=EtherType [_value=2048], _mask=null, augmentation=[]]
758 * But when you fetch the flows from switch, openflow driver library converts all zero bytes of mac address in the
759 * message stream to 00:00:00:00:00:00. Following string shows how library interpret the zero mac address bytes and
760 * eventually when translator convert it to MD-SAL match, this is how it looks
761 * [_ethernetDestination=EthernetDestination [_address=MacAddress [_value=00:00:00:00:00:00], _mask=null, augmentation=[]],
762 * _ethernetSource=EthernetSource [_address=MacAddress [_value=00:00:00:00:00:00], _mask=null, augmentation=[]],
763 * _ethernetType=EthernetType [_type=EtherType [_value=2048], _mask=null, augmentation=[]]
765 * Similarly for inPort, if user/application don't set any value for it, FRM will store null value for it in data store.
766 * When we fetch the same flow (with its statistics) from switch, plugin converts its value to openflow:X:0.
767 * e.g _inPort=Uri [_value=openflow:1:0]
769 * 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
770 * in the flow fetched from switch.
777 public boolean matchEquals(Match statsFlow, Match storedFlow) {
778 if (statsFlow == storedFlow) {
781 if (storedFlow.getClass() != statsFlow.getClass()) {
784 if (storedFlow.getEthernetMatch() == null) {
785 if (statsFlow.getEthernetMatch() != null) {
788 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
791 if (storedFlow.getIcmpv4Match()== null) {
792 if (statsFlow.getIcmpv4Match() != null) {
795 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
798 if (storedFlow.getIcmpv6Match() == null) {
799 if (statsFlow.getIcmpv6Match() != null) {
802 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
805 if (storedFlow.getInPhyPort() == null) {
806 if (statsFlow.getInPhyPort() != null) {
809 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
812 if (storedFlow.getInPort()== null) {
813 if (statsFlow.getInPort() != null) {
816 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
819 if (storedFlow.getIpMatch()== null) {
820 if (statsFlow.getIpMatch() != null) {
823 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
826 if (storedFlow.getLayer3Match()== null) {
827 if (statsFlow.getLayer3Match() != null) {
830 } else if(!storedFlow.getLayer3Match().equals(statsFlow.getLayer3Match())) {
833 if (storedFlow.getLayer4Match()== null) {
834 if (statsFlow.getLayer4Match() != null) {
837 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
840 if (storedFlow.getMetadata() == null) {
841 if (statsFlow.getMetadata() != null) {
844 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
847 if (storedFlow.getProtocolMatchFields() == null) {
848 if (statsFlow.getProtocolMatchFields() != null) {
851 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
854 if (storedFlow.getTunnel()== null) {
855 if (statsFlow.getTunnel() != null) {
858 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
861 if (storedFlow.getVlanMatch()== null) {
862 if (statsFlow.getVlanMatch() != null) {
865 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {