From 4b3ef037970c89c61c4482622f20da1fdc545101 Mon Sep 17 00:00:00 2001 From: Katrina LaCurts Date: Wed, 9 Oct 2013 14:39:29 -0400 Subject: [PATCH] getByteCountIntoPrefix() function. Signed-off-by: Katrina LaCurts --- .../analytics/internal/AnalyticsManager.java | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java b/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java index 859b348..ac7c152 100644 --- a/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java +++ b/analytics/implementation/src/main/java/org/opendaylight/affinity/analytics/internal/AnalyticsManager.java @@ -8,6 +8,7 @@ package org.opendaylight.affinity.analytics.internal; +import java.lang.Short; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; @@ -249,6 +250,68 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager return b; } + private InetAddress getPrefix(InetAddress ip, Short mask) { + byte[] prefix = ip.getAddress(); + InetAddress newIP = null; + try { + int bits = (32 - mask) % 8; + int bytes = 4 - ((int) mask / 8); + if (bits > 0) { + bytes--; + } + // zero out the bytes + for (int i = 1; i <= bytes; i++) { + prefix[prefix.length - i] = 0x0; + } + // zero out the bits + if (bits > 0) { + prefix[prefix.length - bytes - 1] &= (0xFF << bits); + } + newIP = InetAddress.getByAddress(prefix); + } catch (UnknownHostException e) { + // TODO: + } + return newIP; + } + + public long getByteCountIntoHost(Host targetHost) { + long totalBytes = 0; + // We're calculating bytes *into* the target host, not out of + for (Host sourceHost : this.hostsToStats.keySet()) { + if (this.hostsToStats.get(sourceHost).get(targetHost) != null) { + totalBytes += this.hostsToStats.get(sourceHost).get(targetHost).getByteCount(); + } + } + return totalBytes; + } + + public long getByteCountIntoPrefix(String prefixAndMask, Set allHosts) { + long totalBytes = 0; + InetAddress ip; + Short mask; + + // Split 1.2.3.4/5 format into the prefix (1.2.3.4) and the mask (5) + try { + String[] splitPrefix = prefixAndMask.split("/"); + ip = InetAddress.getByName(splitPrefix[0]); + mask = (splitPrefix.length == 2) ? Short.valueOf(splitPrefix[1]) : 32; + } catch (UnknownHostException e) { + log.debug("Incorrect prefix/mask format: " + prefixAndMask); + return 0; + } + + // Match on prefixes + InetAddress targetPrefix = getPrefix(ip, mask); + for (HostNodeConnector host : allHosts) { + InetAddress hostPrefix = getPrefix(host.getNetworkAddress(), mask); + if (hostPrefix.equals(targetPrefix)) { + totalBytes += getByteCountIntoHost(host); + } + } + + return totalBytes; + } + @Override public void nodeFlowStatisticsUpdated(Node node, List flowStatsList) { Set allHosts = this.hostTracker.getAllHosts(); @@ -264,13 +327,14 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager // output, to differentiate between when the source is a // switch and when it's a host that the hosttracker // doesn't know about. The latter would be an error. - if (srcHost == null) { - log.debug("Source host is null for Flow " + f.getFlow() + ". This is NOT necessarily an error."); - continue; - } else if (dstHost == null) { + if (dstHost == null) { log.debug("Error: Destination host is null for Flow " + f.getFlow()); continue; } + else if (srcHost == null) { + log.debug("Source host is null for Flow " + f.getFlow() + ". This is NOT necessarily an error."); + continue; + } if (this.hostsToStats.get(srcHost) == null) { this.hostsToStats.put(srcHost, new HashMap()); -- 2.36.6