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.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.List;
14 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.FlowEntry;
17 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.QueueEntry;
18 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsData;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsDataBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.aggregate.flow.statistics.AggregateFlowStatisticsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStats;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupDescStatsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeaturesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.desc.GroupDescBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.features.GroupFeaturesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeaturesBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
110 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
111 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
112 import org.slf4j.Logger;
113 import org.slf4j.LoggerFactory;
116 * Class implement statistics manager related listener interface and augment all the
117 * received statistics data to data stores.
118 * TODO: Need to add error message listener and clean-up the associated tx id
119 * if it exists in the tx-id cache.
120 * @author vishnoianil
123 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
124 OpendaylightMeterStatisticsListener,
125 OpendaylightFlowStatisticsListener,
126 OpendaylightPortStatisticsListener,
127 OpendaylightFlowTableStatisticsListener,
128 OpendaylightQueueStatisticsListener{
130 public final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
132 private final StatisticsProvider statisticsManager;
133 private final MultipartMessageManager messageManager;
135 private int unaccountedFlowsCounter = 1;
137 public StatisticsUpdateCommiter(final StatisticsProvider manager){
139 this.statisticsManager = manager;
140 this.messageManager = this.statisticsManager.getMultipartMessageManager();
143 public StatisticsProvider getStatisticsManager(){
144 return statisticsManager;
148 public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
149 //Check if response is for the request statistics-manager sent.
150 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
153 NodeKey key = new NodeKey(notification.getId());
155 //Add statistics to local cache
156 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
157 if(!cache.containsKey(notification.getId())){
158 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
160 cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
162 //Publish data to configuration data store
163 List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
165 for(MeterConfigStats meterConfigStats : meterConfigStatsList){
166 DataModificationTransaction it = this.statisticsManager.startChange();
167 MeterBuilder meterBuilder = new MeterBuilder();
168 MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
169 meterBuilder.setKey(meterKey);
171 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
172 .augmentation(FlowCapableNode.class)
173 .child(Meter.class,meterKey).toInstance();
175 NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
176 MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
177 stats.fieldsFrom(meterConfigStats);
178 meterConfig.setMeterConfigStats(stats.build());
180 //Update augmented data
181 meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
182 it.putOperationalData(meterRef, meterBuilder.build());
189 public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
191 //Check if response is for the request statistics-manager sent.
192 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
195 NodeKey key = new NodeKey(notification.getId());
197 List<MeterStats> meterStatsList = notification.getMeterStats();
199 for(MeterStats meterStats : meterStatsList){
201 //Publish data to configuration data store
202 DataModificationTransaction it = this.statisticsManager.startChange();
203 MeterBuilder meterBuilder = new MeterBuilder();
204 MeterKey meterKey = new MeterKey(meterStats.getMeterId());
205 meterBuilder.setKey(meterKey);
207 InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
208 .augmentation(FlowCapableNode.class)
209 .child(Meter.class,meterKey).toInstance();
211 NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
212 MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
213 stats.fieldsFrom(meterStats);
214 meterStatsBuilder.setMeterStatistics(stats.build());
216 //Update augmented data
217 meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
218 it.putOperationalData(meterRef, meterBuilder.build());
224 public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
226 //Check if response is for the request statistics-manager sent.
227 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
230 NodeKey key = new NodeKey(notification.getId());
232 //Add statistics to local cache
233 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
234 if(!cache.containsKey(notification.getId())){
235 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
237 cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
239 //Publish data to configuration data store
240 List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
242 for(GroupDescStats groupDescStats : groupDescStatsList){
243 DataModificationTransaction it = this.statisticsManager.startChange();
245 GroupBuilder groupBuilder = new GroupBuilder();
246 GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
247 groupBuilder.setKey(groupKey);
249 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
250 .augmentation(FlowCapableNode.class)
251 .child(Group.class,groupKey).toInstance();
253 NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
254 GroupDescBuilder stats = new GroupDescBuilder();
255 stats.fieldsFrom(groupDescStats);
256 groupDesc.setGroupDesc(stats.build());
258 //Update augmented data
259 groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
261 it.putOperationalData(groupRef, groupBuilder.build());
267 public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
269 //Check if response is for the request statistics-manager sent.
270 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
273 //Publish data to configuration data store
274 NodeKey key = new NodeKey(notification.getId());
275 List<GroupStats> groupStatsList = notification.getGroupStats();
277 for(GroupStats groupStats : groupStatsList){
278 DataModificationTransaction it = this.statisticsManager.startChange();
280 GroupBuilder groupBuilder = new GroupBuilder();
281 GroupKey groupKey = new GroupKey(groupStats.getGroupId());
282 groupBuilder.setKey(groupKey);
284 InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
285 .augmentation(FlowCapableNode.class)
286 .child(Group.class,groupKey).toInstance();
288 NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
289 GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
290 stats.fieldsFrom(groupStats);
291 groupStatisticsBuilder.setGroupStatistics(stats.build());
293 //Update augmented data
294 groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
295 it.putOperationalData(groupRef, groupBuilder.build());
301 public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
303 MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
304 meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
305 meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
306 meterFeature.setMaxBands(notification.getMaxBands());
307 meterFeature.setMaxColor(notification.getMaxColor());
308 meterFeature.setMaxMeter(notification.getMaxMeter());
310 //Publish data to configuration data store
311 DataModificationTransaction it = this.statisticsManager.startChange();
312 NodeKey key = new NodeKey(notification.getId());
313 NodeRef ref = getNodeRef(key);
315 final NodeBuilder nodeData = new NodeBuilder();
316 nodeData.setKey(key);
318 NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
319 nodeMeterFeatures.setMeterFeatures(meterFeature.build());
321 //Update augmented data
322 nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
324 InstanceIdentifier<? extends Object> refValue = ref.getValue();
325 it.putOperationalData(refValue, nodeData.build());
330 public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
332 GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
333 groupFeatures.setActions(notification.getActions());
334 groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
335 groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
336 groupFeatures.setMaxGroups(notification.getMaxGroups());
338 //Publish data to configuration data store
339 DataModificationTransaction it = this.statisticsManager.startChange();
340 NodeKey key = new NodeKey(notification.getId());
341 NodeRef ref = getNodeRef(key);
343 final NodeBuilder nodeData = new NodeBuilder();
344 nodeData.setKey(key);
346 NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
347 nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
349 //Update augmented data
350 nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
352 InstanceIdentifier<? extends Object> refValue = ref.getValue();
353 it.putOperationalData(refValue, nodeData.build());
358 public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
360 //Check if response is for the request statistics-manager sent.
361 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
364 NodeKey key = new NodeKey(notification.getId());
365 sucLogger.debug("Received flow stats update : {}",notification.toString());
367 for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
368 short tableId = map.getTableId();
370 DataModificationTransaction it = this.statisticsManager.startChange();
372 boolean foundOriginalFlow = false;
374 FlowBuilder flowBuilder = new FlowBuilder();
376 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
378 FlowBuilder flow = new FlowBuilder();
379 flow.setContainerName(map.getContainerName());
380 flow.setBufferId(map.getBufferId());
381 flow.setCookie(map.getCookie());
382 flow.setCookieMask(map.getCookieMask());
383 flow.setFlags(map.getFlags());
384 flow.setFlowName(map.getFlowName());
385 flow.setHardTimeout(map.getHardTimeout());
386 if(map.getFlowId() != null)
387 flow.setId(new FlowId(map.getFlowId().getValue()));
388 flow.setIdleTimeout(map.getIdleTimeout());
389 flow.setInstallHw(map.isInstallHw());
390 flow.setInstructions(map.getInstructions());
391 if(map.getFlowId()!= null)
392 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
393 flow.setMatch(map.getMatch());
394 flow.setOutGroup(map.getOutGroup());
395 flow.setOutPort(map.getOutPort());
396 flow.setPriority(map.getPriority());
397 flow.setStrict(map.isStrict());
398 flow.setTableId(tableId);
400 Flow flowRule = flow.build();
402 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
403 stats.setByteCount(map.getByteCount());
404 stats.setPacketCount(map.getPacketCount());
405 stats.setDuration(map.getDuration());
407 GenericStatistics flowStats = stats.build();
409 //Add statistics to local cache
410 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
411 if(!cache.containsKey(notification.getId())){
412 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
414 NodeStatisticsAger nsa = cache.get(notification.getId());
416 //Augment the data to the flow node
418 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
419 flowStatistics.setByteCount(flowStats.getByteCount());
420 flowStatistics.setPacketCount(flowStats.getPacketCount());
421 flowStatistics.setDuration(flowStats.getDuration());
422 flowStatistics.setContainerName(map.getContainerName());
423 flowStatistics.setBufferId(map.getBufferId());
424 flowStatistics.setCookie(map.getCookie());
425 flowStatistics.setCookieMask(map.getCookieMask());
426 flowStatistics.setFlags(map.getFlags());
427 flowStatistics.setFlowName(map.getFlowName());
428 flowStatistics.setHardTimeout(map.getHardTimeout());
429 flowStatistics.setIdleTimeout(map.getIdleTimeout());
430 flowStatistics.setInstallHw(map.isInstallHw());
431 flowStatistics.setInstructions(map.getInstructions());
432 flowStatistics.setMatch(map.getMatch());
433 flowStatistics.setOutGroup(map.getOutGroup());
434 flowStatistics.setOutPort(map.getOutPort());
435 flowStatistics.setPriority(map.getPriority());
436 flowStatistics.setStrict(map.isStrict());
437 flowStatistics.setTableId(tableId);
439 flowStatisticsData.setFlowStatistics(flowStatistics.build());
441 sucLogger.debug("Flow : {}",flowRule.toString());
442 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
444 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
445 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
447 Table table= (Table)it.readConfigurationData(tableRef);
449 //TODO: Not a good way to do it, need to figure out better way.
450 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
451 //to the flows stored in data store.
452 // Augment same statistics to all the matching masked flow
455 for(Flow existingFlow : table.getFlow()){
456 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
457 if(flowEquals(flowRule,existingFlow)){
458 it = this.statisticsManager.startChange();
459 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
460 .augmentation(FlowCapableNode.class)
461 .child(Table.class, new TableKey(tableId))
462 .child(Flow.class,existingFlow.getKey()).toInstance();
463 flowBuilder.setKey(existingFlow.getKey());
464 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
465 sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
466 foundOriginalFlow = true;
467 // Update entry with timestamp of latest response
468 flow.setKey(existingFlow.getKey());
469 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
470 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
472 it.putOperationalData(flowRef, flowBuilder.build());
478 table= (Table)it.readOperationalData(tableRef);
479 if(!foundOriginalFlow && table != null){
481 for(Flow existingFlow : table.getFlow()){
482 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
483 if(augmentedflowStatisticsData != null){
484 FlowBuilder existingOperationalFlow = new FlowBuilder();
485 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
486 sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
487 if(flowEquals(flowRule,existingOperationalFlow.build())){
488 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
489 .augmentation(FlowCapableNode.class)
490 .child(Table.class, new TableKey(tableId))
491 .child(Flow.class,existingFlow.getKey()).toInstance();
492 flowBuilder.setKey(existingFlow.getKey());
493 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
494 sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
495 foundOriginalFlow = true;
497 // Update entry with timestamp of latest response
498 flow.setKey(existingFlow.getKey());
499 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
500 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
502 it.putOperationalData(flowRef, flowBuilder.build());
509 if(!foundOriginalFlow){
510 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
511 this.unaccountedFlowsCounter++;
512 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
513 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
514 .augmentation(FlowCapableNode.class)
515 .child(Table.class, new TableKey(tableId))
516 .child(Flow.class,newFlowKey).toInstance();
517 flowBuilder.setKey(newFlowKey);
518 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
519 sucLogger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
521 // Update entry with timestamp of latest response
522 flow.setKey(newFlowKey);
523 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
524 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
526 it.putOperationalData(flowRef, flowBuilder.build());
533 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
534 //Check if response is for the request statistics-manager sent.
535 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
538 NodeKey key = new NodeKey(notification.getId());
540 Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
543 DataModificationTransaction it = this.statisticsManager.startChange();
545 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
546 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
548 AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
549 AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
550 aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
551 aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
552 aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
553 aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
555 sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
557 TableBuilder tableBuilder = new TableBuilder();
558 tableBuilder.setKey(new TableKey(tableId));
559 tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
560 it.putOperationalData(tableRef, tableBuilder.build());
567 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
568 //Check if response is for the request statistics-manager sent.
569 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
572 NodeKey key = new NodeKey(notification.getId());
574 List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
575 for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
577 DataModificationTransaction it = this.statisticsManager.startChange();
579 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
580 = new FlowCapableNodeConnectorStatisticsBuilder();
581 statisticsBuilder.setBytes(portStats.getBytes());
582 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
583 statisticsBuilder.setDuration(portStats.getDuration());
584 statisticsBuilder.setPackets(portStats.getPackets());
585 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
586 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
587 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
588 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
589 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
590 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
591 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
593 //Augment data to the node-connector
594 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
595 new FlowCapableNodeConnectorStatisticsDataBuilder();
597 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
599 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
601 NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
603 if(nodeConnector != null){
604 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
605 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
606 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
607 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
614 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
615 //Check if response is for the request statistics-manager sent.
616 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
619 NodeKey key = new NodeKey(notification.getId());
621 List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
622 for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
624 DataModificationTransaction it = this.statisticsManager.startChange();
626 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
627 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
629 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
631 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
632 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
633 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
634 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
636 statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
638 sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
640 TableBuilder tableBuilder = new TableBuilder();
641 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
642 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
643 it.putOperationalData(tableRef, tableBuilder.build());
649 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
651 //Check if response is for the request statistics-manager sent.
652 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
655 NodeKey key = new NodeKey(notification.getId());
657 //Add statistics to local cache
658 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
659 if(!cache.containsKey(notification.getId())){
660 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
663 NodeStatisticsAger nsa = cache.get(notification.getId());
665 List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
666 for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
668 QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
669 nsa.updateQueueStats(queueEntry);
671 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
673 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
675 queueStatisticsBuilder.fieldsFrom(swQueueStats);
677 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
679 DataModificationTransaction it = this.statisticsManager.startChange();
681 InstanceIdentifier<Queue> queueRef
682 = InstanceIdentifier.builder(Nodes.class)
683 .child(Node.class, key)
684 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
685 .augmentation(FlowCapableNodeConnector.class)
686 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
688 QueueBuilder queueBuilder = new QueueBuilder();
689 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
690 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
692 sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
693 ,queueStatisticsDataBuilder.build().toString(),
694 swQueueStats.getQueueId(),
695 swQueueStats.getNodeConnectorId());
697 it.putOperationalData(queueRef, queueBuilder.build());
704 private NodeRef getNodeRef(NodeKey nodeKey){
705 InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
706 return new NodeRef(builder.toInstance());
709 public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
710 if (statsFlow.getClass() != storedFlow.getClass()) {
713 if (statsFlow.getContainerName()== null) {
714 if (storedFlow.getContainerName()!= null) {
717 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
720 if (statsFlow.getMatch()== null) {
721 if (storedFlow.getMatch() != null) {
724 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
725 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
728 if (storedFlow.getPriority() == null) {
729 if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
732 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
735 if (statsFlow.getTableId() == null) {
736 if (storedFlow.getTableId() != null) {
739 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
746 * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
747 * Flow installation process has three steps
748 * 1) Store flow in config data store
749 * 2) and send it to plugin for installation
750 * 3) Flow gets installed in switch
752 * The flow user wants to install and what finally gets installed in switch can be slightly different.
753 * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
754 * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
755 * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
756 * while comparing two ip addresses.
758 * Sometimes when user don't provide few values that is required by flow installation request, like
759 * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
760 * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
761 * But the flow stored in config data store don't have those defaults value. I included those checks
762 * in the customer flow/match equal function.
770 public boolean matchEquals(Match statsFlow, Match storedFlow) {
771 if (statsFlow == storedFlow) {
774 if (storedFlow.getClass() != statsFlow.getClass()) {
777 if (storedFlow.getEthernetMatch() == null) {
778 if (statsFlow.getEthernetMatch() != null) {
781 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
784 if (storedFlow.getIcmpv4Match()== null) {
785 if (statsFlow.getIcmpv4Match() != null) {
788 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
791 if (storedFlow.getIcmpv6Match() == null) {
792 if (statsFlow.getIcmpv6Match() != null) {
795 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
798 if (storedFlow.getInPhyPort() == null) {
799 if (statsFlow.getInPhyPort() != null) {
802 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
805 if (storedFlow.getInPort()== null) {
806 if (statsFlow.getInPort() != null) {
809 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
812 if (storedFlow.getIpMatch()== null) {
813 if (statsFlow.getIpMatch() != null) {
816 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
819 if (storedFlow.getLayer3Match()== null) {
820 if (statsFlow.getLayer3Match() != null) {
823 } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
826 if (storedFlow.getLayer4Match()== null) {
827 if (statsFlow.getLayer4Match() != null) {
830 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
833 if (storedFlow.getMetadata() == null) {
834 if (statsFlow.getMetadata() != null) {
837 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
840 if (storedFlow.getProtocolMatchFields() == null) {
841 if (statsFlow.getProtocolMatchFields() != null) {
844 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
847 if (storedFlow.getTunnel()== null) {
848 if (statsFlow.getTunnel() != null) {
851 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
854 if (storedFlow.getVlanMatch()== null) {
855 if (statsFlow.getVlanMatch() != null) {
858 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
864 private boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
866 if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
867 Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
868 Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
870 if (storedIpv4Match.getIpv4Destination()== null) {
871 if (statsIpv4Match.getIpv4Destination()!= null) {
874 } else if(!IpAddressEquals(statsIpv4Match.getIpv4Destination(),storedIpv4Match.getIpv4Destination())){
877 if (storedIpv4Match.getIpv4Source() == null) {
878 if (statsIpv4Match.getIpv4Source() != null) {
881 } else if(!IpAddressEquals(statsIpv4Match.getIpv4Source(),storedIpv4Match.getIpv4Source())) {
887 return storedLayer3Match.equals(statsLayer3Match);
891 private boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
892 IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
893 IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
895 if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
898 if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
904 private boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
905 return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) == (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
908 private boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
909 return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
913 * Method return integer version of ip address. Converted int will be mask if
916 private IntegerIpAddress StrIpToIntIp(String ipAddresss){
918 String[] parts = ipAddresss.split("/");
919 String ip = parts[0];
922 if (parts.length < 2) {
925 prefix = Integer.parseInt(parts[1]);
928 Inet4Address addr =null;
930 addr = (Inet4Address) InetAddress.getByName(ip);
931 } catch (UnknownHostException e){}
933 byte[] addrBytes = addr.getAddress();
934 int ipInt = ((addrBytes[0] & 0xFF) << 24) |
935 ((addrBytes[1] & 0xFF) << 16) |
936 ((addrBytes[2] & 0xFF) << 8) |
937 ((addrBytes[3] & 0xFF) << 0);
939 int mask = 0xffffffff << 32 - prefix;
941 return new IntegerIpAddress(ipInt, mask);
944 class IntegerIpAddress{
947 public IntegerIpAddress(int ip, int mask) {
954 public int getMask() {