2 * Copyright IBM Corporation, 2013. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.statistics.manager;
10 import java.net.Inet4Address;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
14 import org.opendaylight.controller.md.statistics.manager.NodeStatisticsAger.FlowEntry;
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupFeaturesUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.GenericStatistics;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * Class implement statistics manager related listener interface and augment all the
59 * received statistics data to data stores.
60 * TODO: Need to add error message listener and clean-up the associated tx id
61 * if it exists in the tx-id cache.
65 public class StatisticsUpdateCommiter implements OpendaylightGroupStatisticsListener,
66 OpendaylightMeterStatisticsListener,
67 OpendaylightFlowStatisticsListener,
68 OpendaylightPortStatisticsListener,
69 OpendaylightFlowTableStatisticsListener,
70 OpendaylightQueueStatisticsListener{
72 private final static Logger sucLogger = LoggerFactory.getLogger(StatisticsUpdateCommiter.class);
74 private final StatisticsProvider statisticsManager;
75 private final MultipartMessageManager messageManager;
77 private int unaccountedFlowsCounter = 1;
83 public StatisticsUpdateCommiter(final StatisticsProvider manager){
84 this.statisticsManager = manager;
85 this.messageManager = this.statisticsManager.getMultipartMessageManager();
89 public void onMeterConfigStatsUpdated(final MeterConfigStatsUpdated notification) {
90 //Check if response is for the request statistics-manager sent.
91 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
94 //Add statistics to local cache
95 final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
97 sna.updateMeterConfigStats(notification.getMeterConfigStats());
102 public void onMeterStatisticsUpdated(MeterStatisticsUpdated notification) {
103 //Check if response is for the request statistics-manager sent.
104 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
107 //Add statistics to local cache
108 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
110 nsa.updateMeterStats(notification.getMeterStats());
115 public void onGroupDescStatsUpdated(GroupDescStatsUpdated notification) {
116 //Check if response is for the request statistics-manager sent.
117 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
120 final NodeStatisticsAger nsa = statisticsManager.getStatisticsHandler(notification.getId());
122 nsa.updateGroupDescStats(notification.getGroupDescStats());
127 public void onGroupStatisticsUpdated(GroupStatisticsUpdated notification) {
128 //Check if response is for the request statistics-manager sent.
129 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
132 final NodeStatisticsAger nsa = statisticsManager.getStatisticsHandler(notification.getId());
134 nsa.updateGroupStats(notification.getGroupStats());
139 public void onMeterFeaturesUpdated(MeterFeaturesUpdated notification) {
140 final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
142 sna.updateMeterFeatures(notification);
147 public void onGroupFeaturesUpdated(GroupFeaturesUpdated notification) {
148 final NodeStatisticsAger sna = this.statisticsManager.getStatisticsHandler(notification.getId());
150 sna.updateGroupFeatures(notification);
155 public void onFlowsStatisticsUpdate(final FlowsStatisticsUpdate notification) {
157 //Check if response is for the request statistics-manager sent.
158 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
161 sucLogger.debug("Received flow stats update : {}",notification.toString());
163 final NodeKey key = new NodeKey(notification.getId());
164 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(key.getId());
165 DataModificationTransaction it = this.statisticsManager.startChange();
167 for(FlowAndStatisticsMapList map: notification.getFlowAndStatisticsMapList()){
168 short tableId = map.getTableId();
171 boolean foundOriginalFlow = false;
173 FlowBuilder flowBuilder = new FlowBuilder();
175 FlowStatisticsDataBuilder flowStatisticsData = new FlowStatisticsDataBuilder();
177 FlowBuilder flow = new FlowBuilder();
178 flow.setContainerName(map.getContainerName());
179 flow.setBufferId(map.getBufferId());
180 flow.setCookie(map.getCookie());
181 flow.setCookieMask(map.getCookieMask());
182 flow.setFlags(map.getFlags());
183 flow.setFlowName(map.getFlowName());
184 flow.setHardTimeout(map.getHardTimeout());
185 if(map.getFlowId() != null)
186 flow.setId(new FlowId(map.getFlowId().getValue()));
187 flow.setIdleTimeout(map.getIdleTimeout());
188 flow.setInstallHw(map.isInstallHw());
189 flow.setInstructions(map.getInstructions());
190 if(map.getFlowId()!= null)
191 flow.setKey(new FlowKey(new FlowId(map.getKey().getFlowId().getValue())));
192 flow.setMatch(map.getMatch());
193 flow.setOutGroup(map.getOutGroup());
194 flow.setOutPort(map.getOutPort());
195 flow.setPriority(map.getPriority());
196 flow.setStrict(map.isStrict());
197 flow.setTableId(tableId);
199 Flow flowRule = flow.build();
201 FlowAndStatisticsMapListBuilder stats = new FlowAndStatisticsMapListBuilder();
202 stats.setByteCount(map.getByteCount());
203 stats.setPacketCount(map.getPacketCount());
204 stats.setDuration(map.getDuration());
206 GenericStatistics flowStats = stats.build();
208 //Augment the data to the flow node
210 FlowStatisticsBuilder flowStatistics = new FlowStatisticsBuilder();
211 flowStatistics.setByteCount(flowStats.getByteCount());
212 flowStatistics.setPacketCount(flowStats.getPacketCount());
213 flowStatistics.setDuration(flowStats.getDuration());
214 flowStatistics.setContainerName(map.getContainerName());
215 flowStatistics.setBufferId(map.getBufferId());
216 flowStatistics.setCookie(map.getCookie());
217 flowStatistics.setCookieMask(map.getCookieMask());
218 flowStatistics.setFlags(map.getFlags());
219 flowStatistics.setFlowName(map.getFlowName());
220 flowStatistics.setHardTimeout(map.getHardTimeout());
221 flowStatistics.setIdleTimeout(map.getIdleTimeout());
222 flowStatistics.setInstallHw(map.isInstallHw());
223 flowStatistics.setInstructions(map.getInstructions());
224 flowStatistics.setMatch(map.getMatch());
225 flowStatistics.setOutGroup(map.getOutGroup());
226 flowStatistics.setOutPort(map.getOutPort());
227 flowStatistics.setPriority(map.getPriority());
228 flowStatistics.setStrict(map.isStrict());
229 flowStatistics.setTableId(tableId);
231 flowStatisticsData.setFlowStatistics(flowStatistics.build());
233 sucLogger.debug("Flow : {}",flowRule.toString());
234 sucLogger.debug("Statistics to augment : {}",flowStatistics.build().toString());
236 InstanceIdentifier<Table> tableRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
237 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId)).toInstance();
239 Table table= (Table)it.readConfigurationData(tableRef);
241 //TODO: Not a good way to do it, need to figure out better way.
242 //TODO: major issue in any alternate approach is that flow key is incrementally assigned
243 //to the flows stored in data store.
244 // Augment same statistics to all the matching masked flow
247 for(Flow existingFlow : table.getFlow()){
248 sucLogger.debug("Existing flow in data store : {}",existingFlow.toString());
249 if(flowEquals(flowRule,existingFlow)){
250 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
251 .augmentation(FlowCapableNode.class)
252 .child(Table.class, new TableKey(tableId))
253 .child(Flow.class,existingFlow.getKey()).toInstance();
254 flowBuilder.setKey(existingFlow.getKey());
255 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
256 sucLogger.debug("Found matching flow in the datastore, augmenting statistics");
257 foundOriginalFlow = true;
258 // Update entry with timestamp of latest response
259 flow.setKey(existingFlow.getKey());
260 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
261 nsa.updateFlowStats(flowStatsEntry);
263 it.putOperationalData(flowRef, flowBuilder.build());
268 table= (Table)it.readOperationalData(tableRef);
269 if(!foundOriginalFlow && table != null){
271 for(Flow existingFlow : table.getFlow()){
272 FlowStatisticsData augmentedflowStatisticsData = existingFlow.getAugmentation(FlowStatisticsData.class);
273 if(augmentedflowStatisticsData != null){
274 FlowBuilder existingOperationalFlow = new FlowBuilder();
275 existingOperationalFlow.fieldsFrom(augmentedflowStatisticsData.getFlowStatistics());
276 sucLogger.debug("Existing unaccounted flow in operational data store : {}",existingFlow.toString());
277 if(flowEquals(flowRule,existingOperationalFlow.build())){
278 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
279 .augmentation(FlowCapableNode.class)
280 .child(Table.class, new TableKey(tableId))
281 .child(Flow.class,existingFlow.getKey()).toInstance();
282 flowBuilder.setKey(existingFlow.getKey());
283 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
284 sucLogger.debug("Found matching unaccounted flow in the operational datastore, augmenting statistics");
285 foundOriginalFlow = true;
287 // Update entry with timestamp of latest response
288 flow.setKey(existingFlow.getKey());
289 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
290 nsa.updateFlowStats(flowStatsEntry);
292 it.putOperationalData(flowRef, flowBuilder.build());
298 if(!foundOriginalFlow){
299 String flowKey = "#UF$TABLE*"+Short.toString(tableId)+"*"+Integer.toString(this.unaccountedFlowsCounter);
300 this.unaccountedFlowsCounter++;
301 FlowKey newFlowKey = new FlowKey(new FlowId(flowKey));
302 InstanceIdentifier<Flow> flowRef = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
303 .augmentation(FlowCapableNode.class)
304 .child(Table.class, new TableKey(tableId))
305 .child(Flow.class,newFlowKey).toInstance();
306 flowBuilder.setKey(newFlowKey);
307 flowBuilder.addAugmentation(FlowStatisticsData.class, flowStatisticsData.build());
308 sucLogger.debug("Flow {} is not present in config data store, augmenting statistics as an unaccounted flow",flowBuilder.build());
310 // Update entry with timestamp of latest response
311 flow.setKey(newFlowKey);
312 FlowEntry flowStatsEntry = nsa.new FlowEntry(tableId,flow.build());
313 nsa.updateFlowStats(flowStatsEntry);
315 it.putOperationalData(flowRef, flowBuilder.build());
322 public void onAggregateFlowStatisticsUpdate(AggregateFlowStatisticsUpdate notification) {
323 //Check if response is for the request statistics-manager sent.
324 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
327 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
329 final Short tableId = messageManager.getTableIdForTxId(notification.getId(),notification.getTransactionId());
330 nsa.updateAggregateFlowStats(tableId, notification);
335 public void onNodeConnectorStatisticsUpdate(NodeConnectorStatisticsUpdate notification) {
336 //Check if response is for the request statistics-manager sent.
337 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
340 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
342 nsa.updateNodeConnectorStats(notification.getNodeConnectorStatisticsAndPortNumberMap());
347 public void onFlowTableStatisticsUpdate(FlowTableStatisticsUpdate notification) {
348 //Check if response is for the request statistics-manager sent.
349 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
352 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
354 nsa.updateFlowTableStats(notification.getFlowTableAndStatisticsMap());
359 public void onQueueStatisticsUpdate(QueueStatisticsUpdate notification) {
361 //Check if response is for the request statistics-manager sent.
362 if(!messageManager.isRequestTxIdExist(notification.getId(),notification.getTransactionId(),notification.isMoreReplies()))
365 //Add statistics to local cache
366 final NodeStatisticsAger nsa = this.statisticsManager.getStatisticsHandler(notification.getId());
368 nsa.updateQueueStats(notification.getQueueIdAndStatisticsMap());
372 public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
373 if (statsFlow.getClass() != storedFlow.getClass()) {
376 if (statsFlow.getContainerName()== null) {
377 if (storedFlow.getContainerName()!= null) {
380 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
383 if (statsFlow.getMatch()== null) {
384 if (storedFlow.getMatch() != null) {
387 } //else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
388 else if(!matchEquals(statsFlow.getMatch(), storedFlow.getMatch())) {
391 if (storedFlow.getPriority() == null) {
392 if (statsFlow.getPriority() != null && statsFlow.getPriority()!= 0x8000) {
395 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
398 if (statsFlow.getTableId() == null) {
399 if (storedFlow.getTableId() != null) {
402 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
409 * Explicit equals method to compare the 'match' for flows stored in the data-stores and flow fetched from the switch.
410 * Flow installation process has three steps
411 * 1) Store flow in config data store
412 * 2) and send it to plugin for installation
413 * 3) Flow gets installed in switch
415 * The flow user wants to install and what finally gets installed in switch can be slightly different.
416 * E.g, If user installs flow with src/dst ip=10.0.0.1/24, when it get installed in the switch
417 * src/dst ip will be changes to 10.0.0.0/24 because of netmask of 24. When statistics manager fetch
418 * stats it gets 10.0.0.0/24 rather then 10.0.0.1/24. Custom match takes care of by using masked ip
419 * while comparing two ip addresses.
421 * Sometimes when user don't provide few values that is required by flow installation request, like
422 * priority,hard timeout, idle timeout, cookies etc, plugin usages default values before sending
423 * request to the switch. So when statistics manager gets flow statistics, it gets the default value.
424 * But the flow stored in config data store don't have those defaults value. I included those checks
425 * in the customer flow/match equal function.
433 public static boolean matchEquals(Match statsFlow, Match storedFlow) {
434 if (statsFlow == storedFlow) {
437 if (storedFlow.getClass() != statsFlow.getClass()) {
440 if (storedFlow.getEthernetMatch() == null) {
441 if (statsFlow.getEthernetMatch() != null) {
444 } else if(!storedFlow.getEthernetMatch().equals(statsFlow.getEthernetMatch())) {
447 if (storedFlow.getIcmpv4Match()== null) {
448 if (statsFlow.getIcmpv4Match() != null) {
451 } else if(!storedFlow.getIcmpv4Match().equals(statsFlow.getIcmpv4Match())) {
454 if (storedFlow.getIcmpv6Match() == null) {
455 if (statsFlow.getIcmpv6Match() != null) {
458 } else if(!storedFlow.getIcmpv6Match().equals(statsFlow.getIcmpv6Match())) {
461 if (storedFlow.getInPhyPort() == null) {
462 if (statsFlow.getInPhyPort() != null) {
465 } else if(!storedFlow.getInPhyPort().equals(statsFlow.getInPhyPort())) {
468 if (storedFlow.getInPort()== null) {
469 if (statsFlow.getInPort() != null) {
472 } else if(!storedFlow.getInPort().equals(statsFlow.getInPort())) {
475 if (storedFlow.getIpMatch()== null) {
476 if (statsFlow.getIpMatch() != null) {
479 } else if(!storedFlow.getIpMatch().equals(statsFlow.getIpMatch())) {
482 if (storedFlow.getLayer3Match()== null) {
483 if (statsFlow.getLayer3Match() != null) {
486 } else if(!layer3MatchEquals(statsFlow.getLayer3Match(),storedFlow.getLayer3Match())) {
489 if (storedFlow.getLayer4Match()== null) {
490 if (statsFlow.getLayer4Match() != null) {
493 } else if(!storedFlow.getLayer4Match().equals(statsFlow.getLayer4Match())) {
496 if (storedFlow.getMetadata() == null) {
497 if (statsFlow.getMetadata() != null) {
500 } else if(!storedFlow.getMetadata().equals(statsFlow.getMetadata())) {
503 if (storedFlow.getProtocolMatchFields() == null) {
504 if (statsFlow.getProtocolMatchFields() != null) {
507 } else if(!storedFlow.getProtocolMatchFields().equals(statsFlow.getProtocolMatchFields())) {
510 if (storedFlow.getTunnel()== null) {
511 if (statsFlow.getTunnel() != null) {
514 } else if(!storedFlow.getTunnel().equals(statsFlow.getTunnel())) {
517 if (storedFlow.getVlanMatch()== null) {
518 if (statsFlow.getVlanMatch() != null) {
521 } else if(!storedFlow.getVlanMatch().equals(statsFlow.getVlanMatch())) {
527 protected static boolean layer3MatchEquals(Layer3Match statsLayer3Match, Layer3Match storedLayer3Match){
528 boolean verdict = true;
529 if(statsLayer3Match instanceof Ipv4Match && storedLayer3Match instanceof Ipv4Match){
530 Ipv4Match statsIpv4Match = (Ipv4Match)statsLayer3Match;
531 Ipv4Match storedIpv4Match = (Ipv4Match)storedLayer3Match;
534 verdict = compareNullSafe(
535 storedIpv4Match.getIpv4Destination(), statsIpv4Match.getIpv4Destination());
538 verdict = compareNullSafe(
539 statsIpv4Match.getIpv4Source(), storedIpv4Match.getIpv4Source());
542 Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
543 if (nullCheckOut != null) {
544 verdict = nullCheckOut;
546 verdict = storedLayer3Match.equals(statsLayer3Match);
553 private static boolean compareNullSafe(Ipv4Prefix statsIpv4, Ipv4Prefix storedIpv4) {
554 boolean verdict = true;
555 Boolean checkDestNullValuesOut = checkNullValues(storedIpv4, statsIpv4);
556 if (checkDestNullValuesOut != null) {
557 verdict = checkDestNullValuesOut;
558 } else if(!IpAddressEquals(statsIpv4, storedIpv4)){
565 private static Boolean checkNullValues(Object v1, Object v2) {
566 Boolean verdict = null;
567 if (v1 == null && v2 != null) {
568 verdict = Boolean.FALSE;
569 } else if (v1 != null && v2 == null) {
570 verdict = Boolean.FALSE;
571 } else if (v1 == null && v2 == null) {
572 verdict = Boolean.TRUE;
579 * TODO: why don't we use the default Ipv4Prefix.equals()?
581 * @param statsIpAddress
582 * @param storedIpAddress
583 * @return true if IPv4prefixes equals
585 private static boolean IpAddressEquals(Ipv4Prefix statsIpAddress, Ipv4Prefix storedIpAddress) {
586 IntegerIpAddress statsIpAddressInt = StrIpToIntIp(statsIpAddress.getValue());
587 IntegerIpAddress storedIpAddressInt = StrIpToIntIp(storedIpAddress.getValue());
589 if(IpAndMaskBasedMatch(statsIpAddressInt,storedIpAddressInt)){
592 if(IpBasedMatch(statsIpAddressInt,storedIpAddressInt)){
598 private static boolean IpAndMaskBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
599 return ((statsIpAddressInt.getIp() & statsIpAddressInt.getMask()) == (storedIpAddressInt.getIp() & storedIpAddressInt.getMask()));
602 private static boolean IpBasedMatch(IntegerIpAddress statsIpAddressInt,IntegerIpAddress storedIpAddressInt){
603 return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
607 * Method return integer version of ip address. Converted int will be mask if
610 private static IntegerIpAddress StrIpToIntIp(String ipAddresss){
612 String[] parts = ipAddresss.split("/");
613 String ip = parts[0];
616 if (parts.length < 2) {
619 prefix = Integer.parseInt(parts[1]);
622 IntegerIpAddress integerIpAddress = null;
624 Inet4Address addr = (Inet4Address) InetAddress.getByName(ip);
625 byte[] addrBytes = addr.getAddress();
626 int ipInt = ((addrBytes[0] & 0xFF) << 24) |
627 ((addrBytes[1] & 0xFF) << 16) |
628 ((addrBytes[2] & 0xFF) << 8) |
629 ((addrBytes[3] & 0xFF) << 0);
631 int mask = 0xffffffff << 32 - prefix;
633 integerIpAddress = new IntegerIpAddress(ipInt, mask);
634 } catch (UnknownHostException e){
635 sucLogger.error("Failed to determine host IP address by name: {}", e.getMessage(), e);
638 return integerIpAddress;
641 static class IntegerIpAddress{
644 public IntegerIpAddress(int ip, int mask) {
651 public int getMask() {