Fix a few eclipse-reported warnings
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / StatisticsUpdateCommiter.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.statistics.manager;
9
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;
15
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;
114
115 /**
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
121  *
122  */
123 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
124         OpendaylightMeterStatisticsListener, 
125         OpendaylightFlowStatisticsListener,
126         OpendaylightPortStatisticsListener,
127         OpendaylightFlowTableStatisticsListener,
128         OpendaylightQueueStatisticsListener{
129     
130     public final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
131
132     private final StatisticsProvider statisticsManager;
133     private final MultipartMessageManager messageManager;
134     
135     private int unaccountedFlowsCounter = 1;
136
137     public StatisticsUpdateCommiter(final StatisticsProvider manager){
138
139         this.statisticsManager = manager;
140         this.messageManager = this.statisticsManager.getMultipartMessageManager();
141     }
142     
143     public StatisticsProvider getStatisticsManager(){
144         return statisticsManager;
145     }
146    
147     @Override
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()))
151             return;
152         
153         NodeKey key = new NodeKey(notification.getId());
154
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));
159         }
160         cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
161         
162         //Publish data to configuration data store
163         List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
164         
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);
170             
171             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
172                                                                                         .augmentation(FlowCapableNode.class)
173                                                                                         .child(Meter.class,meterKey).toInstance();
174             
175             NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
176             MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
177             stats.fieldsFrom(meterConfigStats);
178             meterConfig.setMeterConfigStats(stats.build());
179             
180             //Update augmented data
181             meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
182             it.putOperationalData(meterRef, meterBuilder.build());
183             it.commit();
184
185         }
186     }
187
188     @Override
189     public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
190         
191         //Check if response is for the request statistics-manager sent.
192         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
193             return;
194
195         NodeKey key = new NodeKey(notification.getId());
196         
197         List<MeterStats> meterStatsList = notification.getMeterStats();
198         
199         for(MeterStats meterStats : meterStatsList){
200
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);
206             
207             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
208                                                                                         .augmentation(FlowCapableNode.class)
209                                                                                         .child(Meter.class,meterKey).toInstance();
210             
211             NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
212             MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
213             stats.fieldsFrom(meterStats);
214             meterStatsBuilder.setMeterStatistics(stats.build());
215
216             //Update augmented data
217             meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
218             it.putOperationalData(meterRef, meterBuilder.build());
219             it.commit();
220         }
221     }
222
223     @Override
224     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
225         
226         //Check if response is for the request statistics-manager sent.
227         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
228             return;
229
230         NodeKey key = new NodeKey(notification.getId());
231
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));
236         }
237         cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
238         
239         //Publish data to configuration data store
240         List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
241
242         for(GroupDescStats groupDescStats : groupDescStatsList){
243             DataModificationTransaction it = this.statisticsManager.startChange();
244             
245             GroupBuilder groupBuilder = new GroupBuilder();
246             GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
247             groupBuilder.setKey(groupKey);
248             
249             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
250                                                                                         .augmentation(FlowCapableNode.class)
251                                                                                         .child(Group.class,groupKey).toInstance();
252
253             NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
254             GroupDescBuilder stats = new GroupDescBuilder();
255             stats.fieldsFrom(groupDescStats);
256             groupDesc.setGroupDesc(stats.build());
257             
258             //Update augmented data
259             groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
260
261             it.putOperationalData(groupRef, groupBuilder.build());
262             it.commit();
263         }
264     }
265
266     @Override
267     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
268         
269         //Check if response is for the request statistics-manager sent.
270         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
271             return;
272
273         //Publish data to configuration data store
274         NodeKey key = new NodeKey(notification.getId());
275         List<GroupStats> groupStatsList = notification.getGroupStats();
276
277         for(GroupStats groupStats : groupStatsList){
278             DataModificationTransaction it = this.statisticsManager.startChange();
279             
280             GroupBuilder groupBuilder = new GroupBuilder();
281             GroupKey groupKey = new GroupKey(groupStats.getGroupId());
282             groupBuilder.setKey(groupKey);
283             
284             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
285                                                                                         .augmentation(FlowCapableNode.class)
286                                                                                         .child(Group.class,groupKey).toInstance();
287
288             NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
289             GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
290             stats.fieldsFrom(groupStats);
291             groupStatisticsBuilder.setGroupStatistics(stats.build());
292             
293             //Update augmented data
294             groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
295             it.putOperationalData(groupRef, groupBuilder.build());
296             it.commit();
297         }
298     }
299     
300     @Override
301     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
302
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());
309         
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);
314         
315         final NodeBuilder nodeData = new NodeBuilder(); 
316         nodeData.setKey(key);
317         
318         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
319         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
320         
321         //Update augmented data
322         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
323         
324         InstanceIdentifier<? extends Object> refValue = ref.getValue();
325         it.putOperationalData(refValue, nodeData.build());
326         it.commit();
327     }
328     
329     @Override
330     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
331
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());
337         
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);
342         
343         final NodeBuilder nodeData = new NodeBuilder(); 
344         nodeData.setKey(key);
345         
346         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
347         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
348         
349         //Update augmented data
350         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
351         
352         InstanceIdentifier<? extends Object> refValue = ref.getValue();
353         it.putOperationalData(refValue, nodeData.build());
354         it.commit();
355     }
356
357     @Override
358     public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
359         
360         //Check if response is for the request statistics-manager sent.
361         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
362             return;
363
364         NodeKey key = new NodeKey(notification.getId());
365         sucLogger.debug("Received flow stats update : {}",notification.toString());
366         
367         for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
368             short tableId = map.getTableId();
369             
370             DataModificationTransaction it = this.statisticsManager.startChange();
371
372             boolean foundOriginalFlow = false;
373
374             FlowBuilder flowBuilder = new FlowBuilder();
375
376             FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
377
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);
399                 
400             Flow flowRule = flow.build();
401                 
402             FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
403             stats.setByteCount(map.getByteCount());
404             stats.setPacketCount(map.getPacketCount());
405             stats.setDuration(map.getDuration());
406                 
407             GenericStatistics flowStats = stats.build();
408                 
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));
413             }
414             NodeStatisticsAger nsa = cache.get(notification.getId());
415                 
416             //Augment the data to the flow node
417
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);
438
439             flowStatisticsData.setFlowStatistics(flowStatistics.build());
440                 
441             sucLogger.debug("Flow : {}",flowRule.toString());
442             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
443
444             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
445                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
446             
447             Table table= (Table)it.readConfigurationData(tableRef);
448
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
453             if(table != null){
454
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);
471
472                         it.putOperationalData(flowRef, flowBuilder.build());
473                         it.commit();
474                     }
475                 }
476             }
477             
478             table= (Table)it.readOperationalData(tableRef);
479             if(!foundOriginalFlow && table != null){
480
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;
496                             
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);
501
502                             it.putOperationalData(flowRef, flowBuilder.build());
503                             it.commit();
504                             break;
505                         }
506                     }
507                 }
508             }
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());
520                 
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);
525
526                 it.putOperationalData(flowRef, flowBuilder.build());
527                 it.commit();
528             }
529         }
530     }
531
532     @Override
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()))
536             return;
537
538         NodeKey key = new NodeKey(notification.getId());
539         
540         Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
541         if(tableId != null){
542             
543             DataModificationTransaction it = this.statisticsManager.startChange();
544
545             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
546                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
547
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());
554             
555             sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
556
557             TableBuilder tableBuilder = new TableBuilder();
558             tableBuilder.setKey(new TableKey(tableId));
559             tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
560             it.putOperationalData(tableRef, tableBuilder.build());
561             it.commit();
562
563         }
564     }
565
566     @Override
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()))
570             return;
571
572         NodeKey key = new NodeKey(notification.getId());
573         
574         List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
575         for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
576             
577             DataModificationTransaction it = this.statisticsManager.startChange();
578
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());
592             
593             //Augment data to the node-connector
594             FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder = 
595                     new FlowCapableNodeConnectorStatisticsDataBuilder();
596             
597             statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
598             
599             InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
600             
601             NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
602             
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());
608                 it.commit();
609             }
610         }
611     }
612
613     @Override
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()))
617             return;
618
619         NodeKey key = new NodeKey(notification.getId());
620         
621         List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
622         for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
623             
624             DataModificationTransaction it = this.statisticsManager.startChange();
625
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();
628             
629             FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
630             
631             FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
632             statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
633             statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
634             statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
635             
636             statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
637             
638             sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
639             
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());
644             it.commit();
645         }
646     }
647
648     @Override
649     public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
650         
651         //Check if response is for the request statistics-manager sent.
652         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
653             return;
654
655         NodeKey key = new NodeKey(notification.getId());
656         
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));
661         }
662         
663         NodeStatisticsAger nsa = cache.get(notification.getId());
664         
665         List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
666         for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
667             
668             QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
669             nsa.updateQueueStats(queueEntry);
670             
671             FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
672             
673             FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
674             
675             queueStatisticsBuilder.fieldsFrom(swQueueStats);
676             
677             queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
678             
679             DataModificationTransaction it = this.statisticsManager.startChange();
680
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();
687             
688             QueueBuilder queueBuilder = new QueueBuilder();
689             queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
690             queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
691
692             sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
693                                         ,queueStatisticsDataBuilder.build().toString(),
694                                         swQueueStats.getQueueId(),
695                                         swQueueStats.getNodeConnectorId());
696             
697             it.putOperationalData(queueRef, queueBuilder.build());
698             it.commit();
699             
700         }
701         
702     }
703
704     private NodeRef getNodeRef(NodeKey nodeKey){
705         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
706         return new NodeRef(builder.toInstance());
707     }
708    
709     public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
710         if (statsFlow.getClass() != storedFlow.getClass()) {
711             return false;
712         }
713         if (statsFlow.getContainerName()== null) {
714             if (storedFlow.getContainerName()!= null) {
715                 return false;
716             }
717         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
718             return false;
719         }
720         if (statsFlow.getMatch()== null) {
721             if (storedFlow.getMatch() != null) {
722                 return false;
723             }
724         } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
725         else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
726             return false;
727         }
728         if (storedFlow.getPriority() == null) {
729             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
730                 return false;
731             }
732         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
733             return false;
734         }
735         if (statsFlow.getTableId() == null) {
736             if (storedFlow.getTableId() != null) {
737                 return false;
738             }
739         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
740             return false;
741         }
742         return true;
743     }
744     
745     /**
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
751      * 
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.
757      * 
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. 
763      * 
764      * 
765      * @param statsFlow
766      * @param storedFlow
767      * @return
768      */
769     
770     public boolean matchEquals(Match statsFlow, Match storedFlow) {
771         if (statsFlow == storedFlow) {
772             return true;
773         }
774         if (storedFlow.getClass() != statsFlow.getClass()) {
775             return false;
776         }
777         if (storedFlow.getEthernetMatch() == null) {
778             if (statsFlow.getEthernetMatch() != null) {
779                 return false;
780             }
781         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
782             return false;
783         }
784         if (storedFlow.getIcmpv4Match()== null) {
785             if (statsFlow.getIcmpv4Match() != null) {
786                 return false;
787             }
788         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
789             return false;
790         }
791         if (storedFlow.getIcmpv6Match() == null) {
792             if (statsFlow.getIcmpv6Match() != null) {
793                 return false;
794             }
795         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
796             return false;
797         }
798         if (storedFlow.getInPhyPort() == null) {
799             if (statsFlow.getInPhyPort() != null) {
800                 return false;
801             }
802         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
803             return false;
804         }
805         if (storedFlow.getInPort()== null) {
806             if (statsFlow.getInPort() != null) {
807                 return false;
808             }
809         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
810             return false;
811         }
812         if (storedFlow.getIpMatch()== null) {
813             if (statsFlow.getIpMatch() != null) {
814                 return false;
815             }
816         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
817             return false;
818         }
819         if (storedFlow.getLayer3Match()== null) {
820             if (statsFlow.getLayer3Match() != null) {
821                     return false;
822             }
823         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
824             return false;
825         }
826         if (storedFlow.getLayer4Match()== null) {
827             if (statsFlow.getLayer4Match() != null) {
828                 return false;
829             }
830         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
831             return false;
832         }
833         if (storedFlow.getMetadata() == null) {
834             if (statsFlow.getMetadata() != null) {
835                 return false;
836             }
837         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
838             return false;
839         }
840         if (storedFlow.getProtocolMatchFields() == null) {
841             if (statsFlow.getProtocolMatchFields() != null) {
842                 return false;
843             }
844         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
845             return false;
846         }
847         if (storedFlow.getTunnel()== null) {
848             if (statsFlow.getTunnel() != null) {
849                 return false;
850             }
851         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
852             return false;
853         }
854         if (storedFlow.getVlanMatch()== null) {
855             if (statsFlow.getVlanMatch() != null) {
856                 return false;
857             }
858         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
859             return false;
860         }
861         return true;
862     }
863
864     private boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
865
866         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
867             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
868             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
869             
870             if (storedIpv4Match.getIpv4Destination()== null) {
871                 if (statsIpv4Match.getIpv4Destination()!= null) {
872                     return false;
873                 }
874             } else if(!IpAddressEquals(statsIpv4Match.getIpv4Destination(),storedIpv4Match.getIpv4Destination())){
875                 return false;
876             }
877             if (storedIpv4Match.getIpv4Source() == null) {
878                 if (statsIpv4Match.getIpv4Source() != null) {
879                     return false;
880                 }
881             } else if(!IpAddressEquals(statsIpv4Match.getIpv4Source(),storedIpv4Match.getIpv4Source())) {
882                 return false;
883             }
884             
885             return true;
886         }else{
887             return storedLayer3Match.equals(statsLayer3Match);
888         }
889     }
890     
891     private boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
892         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
893         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
894
895         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
896             return true;
897         }
898         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
899             return true;
900         }
901         return false;
902     }
903
904     private boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
905         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
906     }
907
908     private boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
909         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
910     }
911     
912     /*
913      * Method return integer version of ip address. Converted int will be mask if 
914      * mask specified
915      */
916     private IntegerIpAddress StrIpToIntIp(String ipAddresss){
917         
918         String[] parts = ipAddresss.split("/");
919         String ip = parts[0];
920         int prefix;
921
922         if (parts.length < 2) {
923             prefix = 32;
924         } else {
925             prefix = Integer.parseInt(parts[1]);
926         }
927
928         Inet4Address addr =null;
929         try {
930             addr = (Inet4Address) InetAddress.getByName(ip);
931         } catch (UnknownHostException e){}
932
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);
938
939         int mask = 0xffffffff << 32 - prefix;
940
941         return new IntegerIpAddress(ipInt, mask);
942     }
943     
944     class IntegerIpAddress{
945         int ip;
946         int mask;
947         public IntegerIpAddress(int ip, int mask) {
948             this.ip = ip;
949             this.mask = mask;
950         }
951         public int getIp() {
952             return ip;
953         }
954         public int getMask() {
955             return mask;
956         }
957     }
958 }
959