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());
415 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flowRule);
416 cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
418 //Augment the data to the flow node
420 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
421 flowStatistics.setByteCount(flowStats.getByteCount());
422 flowStatistics.setPacketCount(flowStats.getPacketCount());
423 flowStatistics.setDuration(flowStats.getDuration());
424 flowStatistics.setContainerName(map.getContainerName());
425 flowStatistics.setBufferId(map.getBufferId());
426 flowStatistics.setCookie(map.getCookie());
427 flowStatistics.setCookieMask(map.getCookieMask());
428 flowStatistics.setFlags(map.getFlags());
429 flowStatistics.setFlowName(map.getFlowName());
430 flowStatistics.setHardTimeout(map.getHardTimeout());
431 flowStatistics.setIdleTimeout(map.getIdleTimeout());
432 flowStatistics.setInstallHw(map.isInstallHw());
433 flowStatistics.setInstructions(map.getInstructions());
434 flowStatistics.setMatch(map.getMatch());
435 flowStatistics.setOutGroup(map.getOutGroup());
436 flowStatistics.setOutPort(map.getOutPort());
437 flowStatistics.setPriority(map.getPriority());
438 flowStatistics.setStrict(map.isStrict());
439 flowStatistics.setTableId(tableId);
441 flowStatisticsData.setFlowStatistics(flowStatistics.build());
443 sucLogger.debug("Flow : {}",flowRule.toString());
444 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
446 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
447 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
449 Table table= (Table)it.readConfigurationData(tableRef);
451 //TODO: Not a good way to do it, need to figure out better way.
452 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
453 //to the flows stored in data store.
454 // Augment same statistics to all the matching masked flow
457 for(Flow existingFlow : table.getFlow()){
458 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
459 if(flowEquals(flowRule,existingFlow)){
460 it = this.statisticsManager.startChange();
461 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
462 .augmentation(FlowCapableNode.class)
463 .child(Table.class, new TableKey(tableId))
464 .child(Flow.class,existingFlow.getKey()).toInstance();
465 flowBuilder.setKey(existingFlow.getKey());
466 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
467 sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
468 foundOriginalFlow = true;
469 it.putOperationalData(flowRef, flowBuilder.build());
475 table= (Table)it.readOperationalData(tableRef);
476 if(!foundOriginalFlow && table != null){
478 for(Flow existingFlow : table.getFlow()){
479 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
480 if(augmentedflowStatisticsData != null){
481 FlowBuilder existingOperationalFlow = new FlowBuilder();
482 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
483 sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
484 if(flowEquals(flowRule,existingOperationalFlow.build())){
485 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
486 .augmentation(FlowCapableNode.class)
487 .child(Table.class, new TableKey(tableId))
488 .child(Flow.class,existingFlow.getKey()).toInstance();
489 flowBuilder.setKey(existingFlow.getKey());
490 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
491 sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
492 foundOriginalFlow = true;
493 it.putOperationalData(flowRef, flowBuilder.build());
500 if(!foundOriginalFlow){
501 long flowKey = Long.parseLong(new String("1"+Short.toString(tableId)+"0"+Integer.toString(this.unaccountedFlowsCounter)));
502 this.unaccountedFlowsCounter++;
503 FlowKey newFlowKey = new FlowKey(new FlowId(Long.toString(flowKey)));
504 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
505 .augmentation(FlowCapableNode.class)
506 .child(Table.class, new TableKey(tableId))
507 .child(Flow.class,newFlowKey).toInstance();
508 flowBuilder.setKey(newFlowKey);
509 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
510 sucLogger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
511 it.putOperationalData(flowRef, flowBuilder.build());
518 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
519 //Check if response is for the request statistics-manager sent.
520 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
523 NodeKey key = new NodeKey(notification.getId());
525 Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
528 DataModificationTransaction it = this.statisticsManager.startChange();
530 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
531 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
533 AggregateFlowStatisticsDataBuilder aggregateFlowStatisticsDataBuilder = new AggregateFlowStatisticsDataBuilder();
534 AggregateFlowStatisticsBuilder aggregateFlowStatisticsBuilder = new AggregateFlowStatisticsBuilder();
535 aggregateFlowStatisticsBuilder.setByteCount(notification.getByteCount());
536 aggregateFlowStatisticsBuilder.setFlowCount(notification.getFlowCount());
537 aggregateFlowStatisticsBuilder.setPacketCount(notification.getPacketCount());
538 aggregateFlowStatisticsDataBuilder.setAggregateFlowStatistics(aggregateFlowStatisticsBuilder.build());
540 sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
542 TableBuilder tableBuilder = new TableBuilder();
543 tableBuilder.setKey(new TableKey(tableId));
544 tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
545 it.putOperationalData(tableRef, tableBuilder.build());
552 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
553 //Check if response is for the request statistics-manager sent.
554 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
557 NodeKey key = new NodeKey(notification.getId());
559 List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
560 for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
562 DataModificationTransaction it = this.statisticsManager.startChange();
564 FlowCapableNodeConnectorStatisticsBuilder statisticsBuilder
565 = new FlowCapableNodeConnectorStatisticsBuilder();
566 statisticsBuilder.setBytes(portStats.getBytes());
567 statisticsBuilder.setCollisionCount(portStats.getCollisionCount());
568 statisticsBuilder.setDuration(portStats.getDuration());
569 statisticsBuilder.setPackets(portStats.getPackets());
570 statisticsBuilder.setReceiveCrcError(portStats.getReceiveCrcError());
571 statisticsBuilder.setReceiveDrops(portStats.getReceiveDrops());
572 statisticsBuilder.setReceiveErrors(portStats.getReceiveErrors());
573 statisticsBuilder.setReceiveFrameError(portStats.getReceiveFrameError());
574 statisticsBuilder.setReceiveOverRunError(portStats.getReceiveOverRunError());
575 statisticsBuilder.setTransmitDrops(portStats.getTransmitDrops());
576 statisticsBuilder.setTransmitErrors(portStats.getTransmitErrors());
578 //Augment data to the node-connector
579 FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder =
580 new FlowCapableNodeConnectorStatisticsDataBuilder();
582 statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
584 InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
586 NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
588 if(nodeConnector != null){
589 sucLogger.debug("Augmenting port statistics {} to port {}",statisticsDataBuilder.build().toString(),nodeConnectorRef.toString());
590 NodeConnectorBuilder nodeConnectorBuilder = new NodeConnectorBuilder();
591 nodeConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, statisticsDataBuilder.build());
592 it.putOperationalData(nodeConnectorRef, nodeConnectorBuilder.build());
599 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
600 //Check if response is for the request statistics-manager sent.
601 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
604 NodeKey key = new NodeKey(notification.getId());
606 List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
607 for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
609 DataModificationTransaction it = this.statisticsManager.startChange();
611 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
612 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(ftStats.getTableId().getValue())).toInstance();
614 FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
616 FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
617 statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
618 statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
619 statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
621 statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
623 sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
625 TableBuilder tableBuilder = new TableBuilder();
626 tableBuilder.setKey(new TableKey(ftStats.getTableId().getValue()));
627 tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
628 it.putOperationalData(tableRef, tableBuilder.build());
634 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
636 //Check if response is for the request statistics-manager sent.
637 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
640 NodeKey key = new NodeKey(notification.getId());
642 //Add statistics to local cache
643 ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
644 if(!cache.containsKey(notification.getId())){
645 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
648 NodeStatisticsAger nsa = cache.get(notification.getId());
650 List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
651 for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
653 QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
654 nsa.updateQueueStats(queueEntry);
656 FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
658 FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
660 queueStatisticsBuilder.fieldsFrom(swQueueStats);
662 queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
664 DataModificationTransaction it = this.statisticsManager.startChange();
666 InstanceIdentifier<Queue> queueRef
667 = InstanceIdentifier.builder(Nodes.class)
668 .child(Node.class, key)
669 .child(NodeConnector.class, new NodeConnectorKey(swQueueStats.getNodeConnectorId()))
670 .augmentation(FlowCapableNodeConnector.class)
671 .child(Queue.class, new QueueKey(swQueueStats.getQueueId())).toInstance();
673 QueueBuilder queueBuilder = new QueueBuilder();
674 queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
675 queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
677 sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
678 ,queueStatisticsDataBuilder.build().toString(),
679 swQueueStats.getQueueId(),
680 swQueueStats.getNodeConnectorId());
682 it.putOperationalData(queueRef, queueBuilder.build());
689 private NodeRef getNodeRef(NodeKey nodeKey){
690 InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
691 return new NodeRef(builder.toInstance());
694 public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
695 if (statsFlow.getClass() != storedFlow.getClass()) {
698 if (statsFlow.getContainerName()== null) {
699 if (storedFlow.getContainerName()!= null) {
702 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
705 if (statsFlow.getMatch()== null) {
706 if (storedFlow.getMatch() != null) {
709 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
710 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
713 if (storedFlow.getPriority() == null) {
714 if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
717 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
720 if (statsFlow.getTableId() == null) {
721 if (storedFlow.getTableId() != null) {
724 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
731 * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
732 * Flow installation process has three steps
733 * 1) Store flow in config data store
734 * 2) and send it to plugin for installation
735 * 3) Flow gets installed in switch
737 * The flow user wants to install and what finally gets installed in switch can be slightly different.
738 * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
739 * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
740 * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
741 * while comparing two ip addresses.
743 * Sometimes when user don't provide few values that is required by flow installation request, like
744 * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
745 * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
746 * But the flow stored in config data store don't have those defaults value. I included those checks
747 * in the customer flow/match equal function.
755 public boolean matchEquals(Match statsFlow, Match storedFlow) {
756 if (statsFlow == storedFlow) {
759 if (storedFlow.getClass() != statsFlow.getClass()) {
762 if (storedFlow.getEthernetMatch() == null) {
763 if (statsFlow.getEthernetMatch() != null) {
766 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
769 if (storedFlow.getIcmpv4Match()== null) {
770 if (statsFlow.getIcmpv4Match() != null) {
773 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
776 if (storedFlow.getIcmpv6Match() == null) {
777 if (statsFlow.getIcmpv6Match() != null) {
780 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
783 if (storedFlow.getInPhyPort() == null) {
784 if (statsFlow.getInPhyPort() != null) {
787 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
790 if (storedFlow.getInPort()== null) {
791 if (statsFlow.getInPort() != null) {
794 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
797 if (storedFlow.getIpMatch()== null) {
798 if (statsFlow.getIpMatch() != null) {
801 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
804 if (storedFlow.getLayer3Match()== null) {
805 if (statsFlow.getLayer3Match() != null) {
808 } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
811 if (storedFlow.getLayer4Match()== null) {
812 if (statsFlow.getLayer4Match() != null) {
815 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
818 if (storedFlow.getMetadata() == null) {
819 if (statsFlow.getMetadata() != null) {
822 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
825 if (storedFlow.getProtocolMatchFields() == null) {
826 if (statsFlow.getProtocolMatchFields() != null) {
829 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
832 if (storedFlow.getTunnel()== null) {
833 if (statsFlow.getTunnel() != null) {
836 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
839 if (storedFlow.getVlanMatch()== null) {
840 if (statsFlow.getVlanMatch() != null) {
843 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
849 private boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
851 if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
852 Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
853 Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
855 if (storedIpv4Match.getIpv4Destination()== null) {
856 if (statsIpv4Match.getIpv4Destination()!= null) {
859 } else if(!IpAddressEquals(statsIpv4Match.getIpv4Destination(),storedIpv4Match.getIpv4Destination())){
862 if (storedIpv4Match.getIpv4Source() == null) {
863 if (statsIpv4Match.getIpv4Source() != null) {
866 } else if(!IpAddressEquals(statsIpv4Match.getIpv4Source(),storedIpv4Match.getIpv4Source())) {
872 return storedLayer3Match.equals(statsLayer3Match);
876 private boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
877 IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
878 IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
880 if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
883 if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
889 private boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
890 return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) == (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
893 private boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
894 return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
898 * Method return integer version of ip address. Converted int will be mask if
901 private IntegerIpAddress StrIpToIntIp(String ipAddresss){
903 String[] parts = ipAddresss.split("/");
904 String ip = parts[0];
907 if (parts.length < 2) {
910 prefix = Integer.parseInt(parts[1]);
913 Inet4Address addr =null;
915 addr = (Inet4Address) InetAddress.getByName(ip);
916 } catch (UnknownHostException e){}
918 byte[] addrBytes = addr.getAddress();
919 int ipInt = ((addrBytes[0] & 0xFF) << 24) |
920 ((addrBytes[1] & 0xFF) << 16) |
921 ((addrBytes[2] & 0xFF) << 8) |
922 ((addrBytes[3] & 0xFF) << 0);
924 int mask = 0xffffffff << 32 - prefix;
926 return new IntegerIpAddress(ipInt, mask);
929 class IntegerIpAddress{
932 public IntegerIpAddress(int ip, int mask) {
939 public int getMask() {