c36deb88f5e36e6145a566b00f1aae3fe6533cca
[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     private 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     /**
138      * default ctor
139      * @param manager
140      */
141     public StatisticsUpdateCommiter(final StatisticsProvider manager){
142
143         this.statisticsManager = manager;
144         this.messageManager = this.statisticsManager.getMultipartMessageManager();
145     }
146
147     public StatisticsProvider getStatisticsManager(){
148         return statisticsManager;
149     }
150
151     @Override
152     public void onMeterConfigStatsUpdated(MeterConfigStatsUpdated notification) {
153         //Check if response is for the request statistics-manager sent.
154         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
155             return;
156
157         NodeKey key = new NodeKey(notification.getId());
158
159         //Add statistics to local cache
160         ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
161         if(!cache.containsKey(notification.getId())){
162             cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
163         }
164         cache.get(notification.getId()).updateMeterConfigStats(notification.getMeterConfigStats());
165
166         //Publish data to configuration data store
167         List<MeterConfigStats> meterConfigStatsList = notification.getMeterConfigStats();
168         DataModificationTransaction it = this.statisticsManager.startChange();
169
170         for(MeterConfigStats meterConfigStats : meterConfigStatsList){
171             MeterBuilder meterBuilder = new MeterBuilder();
172             MeterKey meterKey = new MeterKey(meterConfigStats.getMeterId());
173             meterBuilder.setKey(meterKey);
174
175             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
176                                                                                         .augmentation(FlowCapableNode.class)
177                                                                                         .child(Meter.class,meterKey).toInstance();
178
179             NodeMeterConfigStatsBuilder meterConfig= new NodeMeterConfigStatsBuilder();
180             MeterConfigStatsBuilder stats = new MeterConfigStatsBuilder();
181             stats.fieldsFrom(meterConfigStats);
182             meterConfig.setMeterConfigStats(stats.build());
183
184             //Update augmented data
185             meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
186             it.putOperationalData(meterRef, meterBuilder.build());
187         }
188         it.commit();
189     }
190
191     @Override
192     public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
193
194         //Check if response is for the request statistics-manager sent.
195         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
196             return;
197
198         NodeKey key = new NodeKey(notification.getId());
199
200         //Publish data to configuration data store
201         List<MeterStats> meterStatsList = notification.getMeterStats();
202         DataModificationTransaction it = this.statisticsManager.startChange();
203
204         for(MeterStats meterStats : meterStatsList){
205
206             MeterBuilder meterBuilder = new MeterBuilder();
207             MeterKey meterKey = new MeterKey(meterStats.getMeterId());
208             meterBuilder.setKey(meterKey);
209
210             InstanceIdentifier<Meter> meterRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
211                                                                                         .augmentation(FlowCapableNode.class)
212                                                                                         .child(Meter.class,meterKey).toInstance();
213
214             NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
215             MeterStatisticsBuilder stats = new MeterStatisticsBuilder();
216             stats.fieldsFrom(meterStats);
217             meterStatsBuilder.setMeterStatistics(stats.build());
218
219             //Update augmented data
220             meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
221             it.putOperationalData(meterRef, meterBuilder.build());
222         }
223         it.commit();
224     }
225
226     @Override
227     public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
228
229         //Check if response is for the request statistics-manager sent.
230         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
231             return;
232
233         NodeKey key = new NodeKey(notification.getId());
234
235         //Add statistics to local cache
236         ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
237         if(!cache.containsKey(notification.getId())){
238             cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
239         }
240         cache.get(notification.getId()).updateGroupDescStats(notification.getGroupDescStats());
241
242         //Publish data to configuration data store
243         List<GroupDescStats> groupDescStatsList = notification.getGroupDescStats();
244         DataModificationTransaction it = this.statisticsManager.startChange();
245
246         for(GroupDescStats groupDescStats : groupDescStatsList){
247
248             GroupBuilder groupBuilder = new GroupBuilder();
249             GroupKey groupKey = new GroupKey(groupDescStats.getGroupId());
250             groupBuilder.setKey(groupKey);
251
252             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
253                                                                                         .augmentation(FlowCapableNode.class)
254                                                                                         .child(Group.class,groupKey).toInstance();
255
256             NodeGroupDescStatsBuilder groupDesc= new NodeGroupDescStatsBuilder();
257             GroupDescBuilder stats = new GroupDescBuilder();
258             stats.fieldsFrom(groupDescStats);
259             groupDesc.setGroupDesc(stats.build());
260
261             //Update augmented data
262             groupBuilder.addAugmentation(NodeGroupDescStats.class, groupDesc.build());
263
264             it.putOperationalData(groupRef, groupBuilder.build());
265         }
266         it.commit();
267     }
268
269     @Override
270     public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
271
272         //Check if response is for the request statistics-manager sent.
273         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
274             return;
275
276         //Publish data to configuration data store
277         NodeKey key = new NodeKey(notification.getId());
278         List<GroupStats> groupStatsList = notification.getGroupStats();
279         DataModificationTransaction it = this.statisticsManager.startChange();
280
281         for(GroupStats groupStats : groupStatsList){
282
283             GroupBuilder groupBuilder = new GroupBuilder();
284             GroupKey groupKey = new GroupKey(groupStats.getGroupId());
285             groupBuilder.setKey(groupKey);
286
287             InstanceIdentifier<Group> groupRef = InstanceIdentifier.builder(Nodes.class).child(Node.class,key)
288                                                                                         .augmentation(FlowCapableNode.class)
289                                                                                         .child(Group.class,groupKey).toInstance();
290
291             NodeGroupStatisticsBuilder groupStatisticsBuilder= new NodeGroupStatisticsBuilder();
292             GroupStatisticsBuilder stats = new GroupStatisticsBuilder();
293             stats.fieldsFrom(groupStats);
294             groupStatisticsBuilder.setGroupStatistics(stats.build());
295
296             //Update augmented data
297             groupBuilder.addAugmentation(NodeGroupStatistics.class, groupStatisticsBuilder.build());
298             it.putOperationalData(groupRef, groupBuilder.build());
299         }
300         it.commit();
301     }
302
303     @Override
304     public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
305
306         MeterFeaturesBuilder meterFeature = new MeterFeaturesBuilder();
307         meterFeature.setMeterBandSupported(notification.getMeterBandSupported());
308         meterFeature.setMeterCapabilitiesSupported(notification.getMeterCapabilitiesSupported());
309         meterFeature.setMaxBands(notification.getMaxBands());
310         meterFeature.setMaxColor(notification.getMaxColor());
311         meterFeature.setMaxMeter(notification.getMaxMeter());
312
313         //Publish data to configuration data store
314         DataModificationTransaction it = this.statisticsManager.startChange();
315         NodeKey key = new NodeKey(notification.getId());
316         NodeRef ref = getNodeRef(key);
317
318         final NodeBuilder nodeData = new NodeBuilder();
319         nodeData.setKey(key);
320
321         NodeMeterFeaturesBuilder nodeMeterFeatures= new NodeMeterFeaturesBuilder();
322         nodeMeterFeatures.setMeterFeatures(meterFeature.build());
323
324         //Update augmented data
325         nodeData.addAugmentation(NodeMeterFeatures.class, nodeMeterFeatures.build());
326
327         InstanceIdentifier<? extends Object> refValue = ref.getValue();
328         it.putOperationalData(refValue, nodeData.build());
329         it.commit();
330     }
331
332     @Override
333     public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
334
335         GroupFeaturesBuilder groupFeatures = new GroupFeaturesBuilder();
336         groupFeatures.setActions(notification.getActions());
337         groupFeatures.setGroupCapabilitiesSupported(notification.getGroupCapabilitiesSupported());
338         groupFeatures.setGroupTypesSupported(notification.getGroupTypesSupported());
339         groupFeatures.setMaxGroups(notification.getMaxGroups());
340
341         //Publish data to configuration data store
342         DataModificationTransaction it = this.statisticsManager.startChange();
343         NodeKey key = new NodeKey(notification.getId());
344         NodeRef ref = getNodeRef(key);
345
346         final NodeBuilder nodeData = new NodeBuilder();
347         nodeData.setKey(key);
348
349         NodeGroupFeaturesBuilder nodeGroupFeatures= new NodeGroupFeaturesBuilder();
350         nodeGroupFeatures.setGroupFeatures(groupFeatures.build());
351
352         //Update augmented data
353         nodeData.addAugmentation(NodeGroupFeatures.class, nodeGroupFeatures.build());
354
355         InstanceIdentifier<? extends Object> refValue = ref.getValue();
356         it.putOperationalData(refValue, nodeData.build());
357         it.commit();
358     }
359
360     @Override
361     public void onFlowsStatisticsUpdate(FlowsStatisticsUpdate notification) {
362
363         //Check if response is for the request statistics-manager sent.
364         if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
365             return;
366
367         NodeKey key = new NodeKey(notification.getId());
368         sucLogger.debug("Received flow stats update : {}",notification.toString());
369         DataModificationTransaction it = this.statisticsManager.startChange();
370
371         for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
372             short tableId = map.getTableId();
373
374
375             boolean foundOriginalFlow = false;
376
377             FlowBuilder flowBuilder = new FlowBuilder();
378
379             FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
380
381             FlowBuilder flow = new FlowBuilder();
382             flow.setContainerName(map.getContainerName());
383             flow.setBufferId(map.getBufferId());
384             flow.setCookie(map.getCookie());
385             flow.setCookieMask(map.getCookieMask());
386             flow.setFlags(map.getFlags());
387             flow.setFlowName(map.getFlowName());
388             flow.setHardTimeout(map.getHardTimeout());
389             if(map.getFlowId() != null)
390                 flow.setId(new FlowId(map.getFlowId().getValue()));
391             flow.setIdleTimeout(map.getIdleTimeout());
392             flow.setInstallHw(map.isInstallHw());
393             flow.setInstructions(map.getInstructions());
394             if(map.getFlowId()!= null)
395                 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
396             flow.setMatch(map.getMatch());
397             flow.setOutGroup(map.getOutGroup());
398             flow.setOutPort(map.getOutPort());
399             flow.setPriority(map.getPriority());
400             flow.setStrict(map.isStrict());
401             flow.setTableId(tableId);
402
403             Flow flowRule = flow.build();
404
405             FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
406             stats.setByteCount(map.getByteCount());
407             stats.setPacketCount(map.getPacketCount());
408             stats.setDuration(map.getDuration());
409
410             GenericStatistics flowStats = stats.build();
411
412             //Add statistics to local cache
413             ConcurrentMap<NodeId, NodeStatisticsAger> cache = this.statisticsManager.getStatisticsCache();
414             if(!cache.containsKey(notification.getId())){
415                 cache.put(notification.getId(), new NodeStatisticsAger(statisticsManager,key));
416             }
417             NodeStatisticsAger nsa = cache.get(notification.getId());
418
419             //Augment the data to the flow node
420
421             FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
422             flowStatistics.setByteCount(flowStats.getByteCount());
423             flowStatistics.setPacketCount(flowStats.getPacketCount());
424             flowStatistics.setDuration(flowStats.getDuration());
425             flowStatistics.setContainerName(map.getContainerName());
426             flowStatistics.setBufferId(map.getBufferId());
427             flowStatistics.setCookie(map.getCookie());
428             flowStatistics.setCookieMask(map.getCookieMask());
429             flowStatistics.setFlags(map.getFlags());
430             flowStatistics.setFlowName(map.getFlowName());
431             flowStatistics.setHardTimeout(map.getHardTimeout());
432             flowStatistics.setIdleTimeout(map.getIdleTimeout());
433             flowStatistics.setInstallHw(map.isInstallHw());
434             flowStatistics.setInstructions(map.getInstructions());
435             flowStatistics.setMatch(map.getMatch());
436             flowStatistics.setOutGroup(map.getOutGroup());
437             flowStatistics.setOutPort(map.getOutPort());
438             flowStatistics.setPriority(map.getPriority());
439             flowStatistics.setStrict(map.isStrict());
440             flowStatistics.setTableId(tableId);
441
442             flowStatisticsData.setFlowStatistics(flowStatistics.build());
443
444             sucLogger.debug("Flow : {}",flowRule.toString());
445             sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
446
447             InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
448                     .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
449
450             Table table= (Table)it.readConfigurationData(tableRef);
451
452             //TODO: Not a good way to do it, need to figure out better way.
453             //TODO: major issue in any alternate approach is that flow key is incrementally assigned
454             //to the flows stored in data store.
455             // Augment same statistics to all the matching masked flow
456             if(table != null){
457
458                 for(Flow existingFlow : table.getFlow()){
459                     sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
460                     if(flowEquals(flowRule,existingFlow)){
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                         // Update entry with timestamp of latest response
470                         flow.setKey(existingFlow.getKey());
471                         FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
472                         cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
473
474                         it.putOperationalData(flowRef, flowBuilder.build());
475                     }
476                 }
477             }
478
479             table= (Table)it.readOperationalData(tableRef);
480             if(!foundOriginalFlow && table != null){
481
482                 for(Flow existingFlow : table.getFlow()){
483                     FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
484                     if(augmentedflowStatisticsData != null){
485                         FlowBuilder existingOperationalFlow = new FlowBuilder();
486                         existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
487                         sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
488                         if(flowEquals(flowRule,existingOperationalFlow.build())){
489                             InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
490                                     .augmentation(FlowCapableNode.class)
491                                     .child(Table.class, new TableKey(tableId))
492                                     .child(Flow.class,existingFlow.getKey()).toInstance();
493                             flowBuilder.setKey(existingFlow.getKey());
494                             flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
495                             sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
496                             foundOriginalFlow = true;
497
498                             // Update entry with timestamp of latest response
499                             flow.setKey(existingFlow.getKey());
500                             FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
501                             cache.get(notification.getId()).updateFlowStats(flowStatsEntry);
502
503                             it.putOperationalData(flowRef, flowBuilder.build());
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             }
528         }
529         it.commit();
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         DataModificationTransaction it = this.statisticsManager.startChange();
576
577         for(NodeConnectorStatisticsAndPortNumberMap portStats : portsStats){
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             }
609         }
610         it.commit();
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         DataModificationTransaction it = this.statisticsManager.startChange();
623
624         for (FlowTableAndStatisticsMap ftStats : flowTablesStatsList){
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         }
645         it.commit();
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         DataModificationTransaction it = this.statisticsManager.startChange();
667
668         for(QueueIdAndStatisticsMap swQueueStats : queuesStats){
669
670             QueueEntry queueEntry = nsa.new QueueEntry(swQueueStats.getNodeConnectorId(),swQueueStats.getQueueId());
671             nsa.updateQueueStats(queueEntry);
672
673             FlowCapableNodeConnectorQueueStatisticsDataBuilder queueStatisticsDataBuilder = new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
674
675             FlowCapableNodeConnectorQueueStatisticsBuilder queueStatisticsBuilder = new FlowCapableNodeConnectorQueueStatisticsBuilder();
676
677             queueStatisticsBuilder.fieldsFrom(swQueueStats);
678
679             queueStatisticsDataBuilder.setFlowCapableNodeConnectorQueueStatistics(queueStatisticsBuilder.build());
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         }
699         it.commit();
700     }
701
702     private static NodeRef getNodeRef(NodeKey nodeKey){
703         InstanceIdentifierBuilder<?> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
704         return new NodeRef(builder.toInstance());
705     }
706
707     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
708         if (statsFlow.getClass() != storedFlow.getClass()) {
709             return false;
710         }
711         if (statsFlow.getContainerName()== null) {
712             if (storedFlow.getContainerName()!= null) {
713                 return false;
714             }
715         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
716             return false;
717         }
718         if (statsFlow.getMatch()== null) {
719             if (storedFlow.getMatch() != null) {
720                 return false;
721             }
722         } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
723         else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
724             return false;
725         }
726         if (storedFlow.getPriority() == null) {
727             if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
728                 return false;
729             }
730         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
731             return false;
732         }
733         if (statsFlow.getTableId() == null) {
734             if (storedFlow.getTableId() != null) {
735                 return false;
736             }
737         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
738             return false;
739         }
740         return true;
741     }
742
743     /**
744      * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
745      * Flow installation process has three steps
746      * 1) Store flow in config data store
747      * 2) and send it to plugin for installation
748      * 3) Flow gets installed in switch
749      *
750      * The flow user wants to install and what finally gets installed in switch can be slightly different.
751      * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
752      * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
753      * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
754      * while comparing two ip addresses.
755      *
756      * Sometimes when user don't provide few values that is required by flow installation request, like
757      * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
758      * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
759      * But the flow stored in config data store don't have those defaults value. I included those checks
760      * in the customer flow/match equal function.
761      *
762      *
763      * @param statsFlow
764      * @param storedFlow
765      * @return
766      */
767
768     public static boolean matchEquals(Match statsFlow, Match storedFlow) {
769         if (statsFlow == storedFlow) {
770             return true;
771         }
772         if (storedFlow.getClass() != statsFlow.getClass()) {
773             return false;
774         }
775         if (storedFlow.getEthernetMatch() == null) {
776             if (statsFlow.getEthernetMatch() != null) {
777                 return false;
778             }
779         } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
780             return false;
781         }
782         if (storedFlow.getIcmpv4Match()== null) {
783             if (statsFlow.getIcmpv4Match() != null) {
784                 return false;
785             }
786         } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
787             return false;
788         }
789         if (storedFlow.getIcmpv6Match() == null) {
790             if (statsFlow.getIcmpv6Match() != null) {
791                 return false;
792             }
793         } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
794             return false;
795         }
796         if (storedFlow.getInPhyPort() == null) {
797             if (statsFlow.getInPhyPort() != null) {
798                 return false;
799             }
800         } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
801             return false;
802         }
803         if (storedFlow.getInPort()== null) {
804             if (statsFlow.getInPort() != null) {
805                 return false;
806             }
807         } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
808             return false;
809         }
810         if (storedFlow.getIpMatch()== null) {
811             if (statsFlow.getIpMatch() != null) {
812                 return false;
813             }
814         } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
815             return false;
816         }
817         if (storedFlow.getLayer3Match()== null) {
818             if (statsFlow.getLayer3Match() != null) {
819                     return false;
820             }
821         } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
822             return false;
823         }
824         if (storedFlow.getLayer4Match()== null) {
825             if (statsFlow.getLayer4Match() != null) {
826                 return false;
827             }
828         } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
829             return false;
830         }
831         if (storedFlow.getMetadata() == null) {
832             if (statsFlow.getMetadata() != null) {
833                 return false;
834             }
835         } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
836             return false;
837         }
838         if (storedFlow.getProtocolMatchFields() == null) {
839             if (statsFlow.getProtocolMatchFields() != null) {
840                 return false;
841             }
842         } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
843             return false;
844         }
845         if (storedFlow.getTunnel()== null) {
846             if (statsFlow.getTunnel() != null) {
847                 return false;
848             }
849         } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
850             return false;
851         }
852         if (storedFlow.getVlanMatch()== null) {
853             if (statsFlow.getVlanMatch() != null) {
854                 return false;
855             }
856         } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
857             return false;
858         }
859         return true;
860     }
861
862     protected static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
863         boolean verdict = true;
864         if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
865             Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
866             Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
867
868             if (verdict) {
869                 verdict = compareNullSafe(
870                         storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
871             }
872             if (verdict) {
873                 verdict = compareNullSafe(
874                         statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
875             }
876         } else {
877             Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
878             if (nullCheckOut != null) {
879                 verdict = nullCheckOut;
880             } else {
881                 verdict = storedLayer3Match.equals(statsLayer3Match);
882             }
883         }
884
885         return verdict;
886     }
887
888     private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
889         boolean verdict = true;
890         Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
891         if (checkDestNullValuesOut != null) {
892             verdict = checkDestNullValuesOut;
893         } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
894             verdict = false;
895         }
896
897         return verdict;
898     }
899
900     private static Boolean checkNullValues(Object v1, Object v2) {
901         Boolean verdict = null;
902         if (v1 == null && v2 != null) {
903             verdict = Boolean.FALSE;
904         } else if (v1 != null && v2 == null) {
905             verdict = Boolean.FALSE;
906         } else if (v1 == null && v2 == null) {
907             verdict = Boolean.TRUE;
908         }
909
910         return verdict;
911     }
912
913     /**
914      * TODO: why don't we use the default Ipv4Prefix.equals()?
915      *
916      * @param statsIpAddress
917      * @param storedIpAddress
918      * @return true if IPv4prefixes equals
919      */
920     private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
921         IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
922         IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
923
924         if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
925             return true;
926         }
927         if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
928             return true;
929         }
930         return false;
931     }
932
933     private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
934         return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) ==  (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
935     }
936
937     private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
938         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
939     }
940
941     /**
942      * Method return integer version of ip address. Converted int will be mask if
943      * mask specified
944      */
945     private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
946
947         String[] parts = ipAddresss.split("/");
948         String ip = parts[0];
949         int prefix;
950
951         if (parts.length < 2) {
952             prefix = 32;
953         } else {
954             prefix = Integer.parseInt(parts[1]);
955         }
956
957         IntegerIpAddress integerIpAddress = null;
958         try {
959             Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
960             byte[] addrBytes = addr.getAddress();
961             int ipInt = ((addrBytes[0] & 0xFF) << 24) |
962                     ((addrBytes[1] & 0xFF) << 16) |
963                     ((addrBytes[2] & 0xFF) << 8)  |
964                     ((addrBytes[3] & 0xFF) << 0);
965
966             int mask = 0xffffffff << 32 - prefix;
967
968             integerIpAddress = new IntegerIpAddress(ipInt, mask);
969         } catch (UnknownHostException e){
970             sucLogger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
971         }
972
973         return integerIpAddress;
974     }
975
976     static class IntegerIpAddress{
977         int ip;
978         int mask;
979         public IntegerIpAddress(int ip, int mask) {
980             this.ip = ip;
981             this.mask = mask;
982         }
983         public int getIp() {
984             return ip;
985         }
986         public int getMask() {
987             return mask;
988         }
989     }
990 }
991