19f25944b45f0ce34a94c9224ce4b107da4f467b
[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             FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flowRule);
416             cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
417                 
418             //Augment the data to the flow node
419
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);
440
441             flowStatisticsData.setFlowStatistics(flowStatistics.build());
442                 
443             sucLogger.debug("Flow : {}",flowRule.toString());
444             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
445
446             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
447                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
448             
449             Table table= (Table)it.readConfigurationData(tableRef);
450
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
455             if(table != null){
456
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());
470                         it.commit();
471                     }
472                 }
473             }
474             
475             table= (Table)it.readOperationalData(tableRef);
476             if(!foundOriginalFlow && table != null){
477
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());
494                             it.commit();
495                             break;
496                         }
497                     }
498                 }
499             }
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());
512                 it.commit();
513             }
514         }
515     }
516
517     @Override
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()))
521             return;
522
523         NodeKey key = new NodeKey(notification.getId());
524         
525         Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
526         if(tableId != null){
527             
528             DataModificationTransaction it = this.statisticsManager.startChange();
529
530             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
531                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
532
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());
539             
540             sucLogger.debug("Augment aggregate statistics: {} for table {} on Node {}",aggregateFlowStatisticsBuilder.build().toString(),tableId,key);
541
542             TableBuilder tableBuilder = new TableBuilder();
543             tableBuilder.setKey(new TableKey(tableId));
544             tableBuilder.addAugmentation(AggregateFlowStatisticsData.class, aggregateFlowStatisticsDataBuilder.build());
545             it.putOperationalData(tableRef, tableBuilder.build());
546             it.commit();
547
548         }
549     }
550
551     @Override
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()))
555             return;
556
557         NodeKey key = new NodeKey(notification.getId());
558         
559         List<NodeConnectorStatisticsAndPortNumberMap> portsStats = notification.getNodeConnectorStatisticsAndPortNumberMap();
560         for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
561             
562             DataModificationTransaction it = this.statisticsManager.startChange();
563
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());
577             
578             //Augment data to the node-connector
579             FlowCapableNodeConnectorStatisticsDataBuilder statisticsDataBuilder = 
580                     new FlowCapableNodeConnectorStatisticsDataBuilder();
581             
582             statisticsDataBuilder.setFlowCapableNodeConnectorStatistics(statisticsBuilder.build());
583             
584             InstanceIdentifier<NodeConnector> nodeConnectorRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).child(NodeConnector.class, new NodeConnectorKey(portStats.getNodeConnectorId())).toInstance();
585             
586             NodeConnector nodeConnector = (NodeConnector)it.readOperationalData(nodeConnectorRef);
587             
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());
593                 it.commit();
594             }
595         }
596     }
597
598     @Override
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()))
602             return;
603
604         NodeKey key = new NodeKey(notification.getId());
605         
606         List<FlowTableAndStatisticsMap> flowTablesStatsList = notification.getFlowTableAndStatisticsMap();
607         for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
608             
609             DataModificationTransaction it = this.statisticsManager.startChange();
610
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();
613             
614             FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
615             
616             FlowTableStatisticsBuilder statisticsBuilder = new FlowTableStatisticsBuilder();
617             statisticsBuilder.setActiveFlows(ftStats.getActiveFlows());
618             statisticsBuilder.setPacketsLookedUp(ftStats.getPacketsLookedUp());
619             statisticsBuilder.setPacketsMatched(ftStats.getPacketsMatched());
620             
621             statisticsDataBuilder.setFlowTableStatistics(statisticsBuilder.build());
622             
623             sucLogger.debug("Augment flow table statistics: {} for table {} on Node {}",statisticsBuilder.build().toString(),ftStats.getTableId(),key);
624             
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());
629             it.commit();
630         }
631     }
632
633     @Override
634     public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
635         
636         //Check if response is for the request statistics-manager sent.
637         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
638             return;
639
640         NodeKey key = new NodeKey(notification.getId());
641         
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));
646         }
647         
648         NodeStatisticsAger nsa = cache.get(notification.getId());
649         
650         List<QueueIdAndStatisticsMap> queuesStats = notification.getQueueIdAndStatisticsMap();
651         for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
652             
653             QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
654             nsa.updateQueueStats(queueEntry);
655             
656             FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
657             
658             FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
659             
660             queueStatisticsBuilder.fieldsFrom(swQueueStats);
661             
662             queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
663             
664             DataModificationTransaction it = this.statisticsManager.startChange();
665
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();
672             
673             QueueBuilder queueBuilder = new QueueBuilder();
674             queueBuilder.addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, queueStatisticsDataBuilder.build());
675             queueBuilder.setKey(new QueueKey(swQueueStats.getQueueId()));
676
677             sucLogger.debug("Augmenting queue statistics {} of queue {} to port {}"
678                                         ,queueStatisticsDataBuilder.build().toString(),
679                                         swQueueStats.getQueueId(),
680                                         swQueueStats.getNodeConnectorId());
681             
682             it.putOperationalData(queueRef, queueBuilder.build());
683             it.commit();
684             
685         }
686         
687     }
688
689     private NodeRef getNodeRef(NodeKey nodeKey){
690         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
691         return new NodeRef(builder.toInstance());
692     }
693    
694     public boolean flowEquals(Flow statsFlow, Flow storedFlow) {
695         if (statsFlow.getClass() != storedFlow.getClass()) {
696             return false;
697         }
698         if (statsFlow.getContainerName()== null) {
699             if (storedFlow.getContainerName()!= null) {
700                 return false;
701             }
702         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
703             return false;
704         }
705         if (statsFlow.getMatch()== null) {
706             if (storedFlow.getMatch() != null) {
707                 return false;
708             }
709         } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
710         else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
711             return false;
712         }
713         if (storedFlow.getPriority() == null) {
714             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
715                 return false;
716             }
717         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
718             return false;
719         }
720         if (statsFlow.getTableId() == null) {
721             if (storedFlow.getTableId() != null) {
722                 return false;
723             }
724         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
725             return false;
726         }
727         return true;
728     }
729     
730     /**
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
736      * 
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.
742      * 
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. 
748      * 
749      * 
750      * @param statsFlow
751      * @param storedFlow
752      * @return
753      */
754     
755     public boolean matchEquals(Match statsFlow, Match storedFlow) {
756         if (statsFlow == storedFlow) {
757             return true;
758         }
759         if (storedFlow.getClass() != statsFlow.getClass()) {
760             return false;
761         }
762         if (storedFlow.getEthernetMatch() == null) {
763             if (statsFlow.getEthernetMatch() != null) {
764                 return false;
765             }
766         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
767             return false;
768         }
769         if (storedFlow.getIcmpv4Match()== null) {
770             if (statsFlow.getIcmpv4Match() != null) {
771                 return false;
772             }
773         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
774             return false;
775         }
776         if (storedFlow.getIcmpv6Match() == null) {
777             if (statsFlow.getIcmpv6Match() != null) {
778                 return false;
779             }
780         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
781             return false;
782         }
783         if (storedFlow.getInPhyPort() == null) {
784             if (statsFlow.getInPhyPort() != null) {
785                 return false;
786             }
787         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
788             return false;
789         }
790         if (storedFlow.getInPort()== null) {
791             if (statsFlow.getInPort() != null) {
792                 return false;
793             }
794         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
795             return false;
796         }
797         if (storedFlow.getIpMatch()== null) {
798             if (statsFlow.getIpMatch() != null) {
799                 return false;
800             }
801         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
802             return false;
803         }
804         if (storedFlow.getLayer3Match()== null) {
805             if (statsFlow.getLayer3Match() != null) {
806                     return false;
807             }
808         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
809             return false;
810         }
811         if (storedFlow.getLayer4Match()== null) {
812             if (statsFlow.getLayer4Match() != null) {
813                 return false;
814             }
815         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
816             return false;
817         }
818         if (storedFlow.getMetadata() == null) {
819             if (statsFlow.getMetadata() != null) {
820                 return false;
821             }
822         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
823             return false;
824         }
825         if (storedFlow.getProtocolMatchFields() == null) {
826             if (statsFlow.getProtocolMatchFields() != null) {
827                 return false;
828             }
829         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
830             return false;
831         }
832         if (storedFlow.getTunnel()== null) {
833             if (statsFlow.getTunnel() != null) {
834                 return false;
835             }
836         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
837             return false;
838         }
839         if (storedFlow.getVlanMatch()== null) {
840             if (statsFlow.getVlanMatch() != null) {
841                 return false;
842             }
843         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
844             return false;
845         }
846         return true;
847     }
848
849     private boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
850
851         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
852             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
853             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
854             
855             if (storedIpv4Match.getIpv4Destination()== null) {
856                 if (statsIpv4Match.getIpv4Destination()!= null) {
857                     return false;
858                 }
859             } else if(!IpAddressEquals(statsIpv4Match.getIpv4Destination(),storedIpv4Match.getIpv4Destination())){
860                 return false;
861             }
862             if (storedIpv4Match.getIpv4Source() == null) {
863                 if (statsIpv4Match.getIpv4Source() != null) {
864                     return false;
865                 }
866             } else if(!IpAddressEquals(statsIpv4Match.getIpv4Source(),storedIpv4Match.getIpv4Source())) {
867                 return false;
868             }
869             
870             return true;
871         }else{
872             return storedLayer3Match.equals(statsLayer3Match);
873         }
874     }
875     
876     private boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
877         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
878         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
879
880         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
881             return true;
882         }
883         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
884             return true;
885         }
886         return false;
887     }
888
889     private boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
890         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
891     }
892
893     private boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
894         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
895     }
896     
897     /*
898      * Method return integer version of ip address. Converted int will be mask if 
899      * mask specified
900      */
901     private IntegerIpAddress StrIpToIntIp(String ipAddresss){
902         
903         String[] parts = ipAddresss.split("/");
904         String ip = parts[0];
905         int prefix;
906
907         if (parts.length < 2) {
908             prefix = 32;
909         } else {
910             prefix = Integer.parseInt(parts[1]);
911         }
912
913         Inet4Address addr =null;
914         try {
915             addr = (Inet4Address) InetAddress.getByName(ip);
916         } catch (UnknownHostException e){}
917
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);
923
924         int mask = 0xffffffff << 32 - prefix;
925
926         return new IntegerIpAddress(ipInt, mask);
927     }
928     
929     class IntegerIpAddress{
930         int ip;
931         int mask;
932         public IntegerIpAddress(int ip, int mask) {
933             this.ip = ip;
934             this.mask = mask;
935         }
936         public int getIp() {
937             return ip;
938         }
939         public int getMask() {
940             return mask;
941         }
942     }
943 }
944