From d04c5126fe9cd04a4f849f09604795ea28eb15dd Mon Sep 17 00:00:00 2001 From: Katrina LaCurts Date: Mon, 25 Nov 2013 16:54:56 -0500 Subject: [PATCH] Better abstraction for analytics API. Signed-off-by: Katrina LaCurts --- .../analytics/internal/AnalyticsManager.java | 357 +++++++----------- 1 file changed, 132 insertions(+), 225 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 0032e88..6a025b6 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 @@ -12,6 +12,7 @@ import java.lang.Short; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -170,103 +171,179 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager return srcHost; } + /* Return the number of bytes transferred between two sets, + * per-protocol (across all protocols if protocol is null).*/ + private long getByteCount(Set srcSet, Set dstSet, Byte protocol) { + long byteCount = 0; + for (Host src : srcSet) { + for (Host dst : dstSet) { + if (this.hostsToStats.get(src) != null && + this.hostsToStats.get(src).get(dst) != null) { + if (protocol == null) + byteCount += this.hostsToStats.get(src).get(dst).getByteCount(); + else + byteCount += this.hostsToStats.get(src).get(dst).getByteCount(protocol); + } + } + } + return byteCount; + } + + /* Return all protocols used between two sets */ + public Set getProtocols(Set srcSet, Set dstSet) { + Set protocols = new HashSet(); + for (Host src : srcSet) { + for (Host dst : dstSet) { + if (this.hostsToStats.get(src) != null && + this.hostsToStats.get(src).get(dst) != null) { + protocols.addAll(this.hostsToStats.get(src).get(dst).getProtocols()); + } + } + } + return protocols; + } + + /* Returns a map of all byte counts between two sets */ + public Map getAllByteCounts(Set srcSet, Set dstSet) { + Map byteCounts = new HashMap(); + Set protocols = getProtocols(srcSet, dstSet); + for (Byte protocol : protocols) + byteCounts.put(protocol, getByteCount(srcSet, dstSet, protocol)); + return byteCounts; + } + + /* Returns the duration of communication between two sets (max + * duration of all flows) for a particular protocol (across all + * protocols if protocol is null) */ + public double getDuration(Set srcSet, Set dstSet, Byte protocol) { + double maxDuration = 0.0; + for (Host src : srcSet) { + for (Host dst : dstSet) { + double duration; + if (protocol == null) + duration = this.hostsToStats.get(src).get(dst).getDuration(); + else + duration = this.hostsToStats.get(src).get(dst).getDuration(protocol); + if (duration > maxDuration) + maxDuration = duration; + } + } + return maxDuration; + } + + /* Returns the bit rate between two sets */ + public double getBitRate(Set srcSet, Set dstSet, Byte protocol) { + double duration = getDuration(srcSet, dstSet, protocol); + long totalBytes = getByteCount(srcSet, dstSet, protocol); + if (duration == 0.0) + return 0.0; + return (totalBytes * 8.0) / duration; + } + + public Map getAllBitRates(Set srcSet, Set dstSet) { + Map bitRates = new HashMap(); + Set protocols = getProtocols(srcSet, dstSet); + for (Byte protocol : protocols) + bitRates.put(protocol, getBitRate(srcSet, dstSet, protocol)); + return bitRates; + } + /* These are all basic getters/setters, most of which are required * by IAnalyticsManager */ public long getByteCount(Host src, Host dst) { - return getByteCountBetweenHostsInternal(src, dst, null); + return getByteCount(src, dst, null); } public long getByteCount(Host src, Host dst, Byte protocol) { - return getByteCountBetweenHostsInternal(src, dst, protocol); + Set srcSet = new HashSet(Arrays.asList(src)); + Set dstSet = new HashSet(Arrays.asList(dst)); + return getByteCount(srcSet, dstSet, protocol); } public Map getAllByteCounts(Host src, Host dst) { - if (this.hostsToStats.get(src) == null || - this.hostsToStats.get(src).get(dst) == null) - return new HashMap(); - return this.hostsToStats.get(src).get(dst).getAllByteCounts(); + Set srcSet = new HashSet(Arrays.asList(src)); + Set dstSet = new HashSet(Arrays.asList(dst)); + return getAllByteCounts(srcSet, dstSet); } public double getBitRate(Host src, Host dst) { - return getBitRateBetweenHostsInternal(src, dst, null); + return getBitRate(src, dst, null); } public double getBitRate(Host src, Host dst, Byte protocol) { - return getBitRateBetweenHostsInternal(src, dst, protocol); + Set srcSet = new HashSet(Arrays.asList(src)); + Set dstSet = new HashSet(Arrays.asList(dst)); + return getBitRate(srcSet, dstSet, protocol); } public Map getAllBitRates(Host src, Host dst) { - if (this.hostsToStats.get(src) == null || - this.hostsToStats.get(src).get(dst) == null) - return new HashMap(); - return this.hostsToStats.get(src).get(dst).getAllBitRates(); + Set srcSet = new HashSet(Arrays.asList(src)); + Set dstSet = new HashSet(Arrays.asList(dst)); + return getAllBitRates(srcSet, dstSet); } public long getByteCount(AffinityLink al) { - return getByteCountOnAffinityLinkInternal(al, null); + return getByteCount(al, null); } public long getByteCount(AffinityLink al, Byte protocol) { - return getByteCountOnAffinityLinkInternal(al, protocol); + Set srcSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getToGroup())); + Set dstSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getFromGroup())); + return getByteCount(srcSet, dstSet, protocol); } public Map getAllByteCounts(AffinityLink al) { - Map byteCounts = new HashMap(); - Set protocols = getProtocols(al); - for (Byte protocol : protocols) { - Long thisByteCounts = getByteCount(al, protocol); - byteCounts.put(protocol, thisByteCounts); - } - return byteCounts; + Set srcSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getToGroup())); + Set dstSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getFromGroup())); + return getAllByteCounts(srcSet, dstSet); } public double getBitRate(AffinityLink al) { - return getBitRateOnAffinityLinkInternal(al, null); + return getBitRate(al, null); } public double getBitRate(AffinityLink al, Byte protocol) { - return getBitRateOnAffinityLinkInternal(al, protocol); + Set srcSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getToGroup())); + Set dstSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getFromGroup())); + return getBitRate(srcSet, dstSet, protocol); } public Map getAllBitRates(AffinityLink al) { - Map bitRates = new HashMap(); - Set protocols = getProtocols(al); - for (Byte protocol : protocols) - bitRates.put(protocol, getBitRate(al, protocol)); - return bitRates; + Set srcSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getToGroup())); + Set dstSet = new HashSet(this.affinityManager.getAllElementsByHost(al.getFromGroup())); + return getAllBitRates(srcSet, dstSet); } public long getByteCount(String srcSubnet, String dstSubnet) { - return getByteCountBySubnetInternal(srcSubnet, dstSubnet, null); + return getByteCount(srcSubnet, dstSubnet, null); } public long getByteCount(String srcSubnet, String dstSubnet, Byte protocol) { - return getByteCountBySubnetInternal(srcSubnet, dstSubnet, protocol); + Set srcSet = getSrcHosts(srcSubnet, dstSubnet); + Set dstSet = getSrcHosts(dstSubnet, srcSubnet); // reverse arguments + return getByteCount(srcSet, dstSet, protocol); } public Map getAllByteCounts(String srcSubnet, String dstSubnet) { - Map byteCounts = new HashMap(); - Set protocols = getProtocols(srcSubnet, dstSubnet); - for (Byte protocol : protocols) { - byteCounts.put(protocol, getByteCount(srcSubnet, dstSubnet, protocol)); - } - return byteCounts; + Set srcSet = getSrcHosts(srcSubnet, dstSubnet); + Set dstSet = getSrcHosts(dstSubnet, srcSubnet); // reverse arguments + return getAllByteCounts(srcSet, dstSet); } public double getBitRate(String srcSubnet, String dstSubnet) { - return getBitRateBySubnetInternal(srcSubnet, dstSubnet, null); + return getBitRate(srcSubnet, dstSubnet, null); } public double getBitRate(String srcSubnet, String dstSubnet, Byte protocol) { - return getBitRateBySubnetInternal(srcSubnet, dstSubnet, protocol); + Set srcSet = getSrcHosts(srcSubnet, dstSubnet); + Set dstSet = getSrcHosts(dstSubnet, srcSubnet); // reverse arguments + return getBitRate(srcSet, dstSet, protocol); } public Map getAllBitRates(String srcSubnet, String dstSubnet) { - Map bitRates = new HashMap(); - Set protocols = getProtocols(srcSubnet, dstSubnet); - for (Byte protocol : protocols) - bitRates.put(protocol, getBitRate(srcSubnet, dstSubnet, protocol)); - return bitRates; + Set srcSet = getSrcHosts(srcSubnet, dstSubnet); + Set dstSet = getSrcHosts(dstSubnet, srcSubnet); // reverse arguments + return getAllBitRates(srcSet, dstSet); } public Map getIncomingHostByteCounts(String subnet) { @@ -277,145 +354,6 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager return getIncomingHostByteCountsInternal(subnet, protocol); } - /* Return byte count between two hosts, either per-protocol or not */ - private long getByteCountBetweenHostsInternal(Host src, Host dst, Byte protocol) { - long byteCount = 0; - if (this.hostsToStats.get(src) != null && - this.hostsToStats.get(src).get(dst) != null) { - if (protocol == null) - byteCount = this.hostsToStats.get(src).get(dst).getByteCount(); - else - byteCount = this.hostsToStats.get(src).get(dst).getByteCount(protocol); - } - return byteCount; - } - - /* Return the total bit rate between two hosts, either per-protocol or not */ - private double getBitRateBetweenHostsInternal(Host src, Host dst, Byte protocol) { - double bitRate = 0; - if (this.hostsToStats.get(src) != null && - this.hostsToStats.get(src).get(dst) != null) { - if (protocol == null) - bitRate = this.hostsToStats.get(src).get(dst).getBitRate(); - else - bitRate = this.hostsToStats.get(src).get(dst).getBitRate(protocol); - } - return bitRate; - } - - /* Return the duration between two hosts, either per-protocol or not */ - private double getDurationBetweenHostsInternal(Host src, Host dst, Byte protocol) { - double duration = 0.0; - if (this.hostsToStats.get(src) != null && - this.hostsToStats.get(src).get(dst) !=null) { - if (protocol == null) - duration = this.hostsToStats.get(src).get(dst).getDuration(); - else - duration = this.hostsToStats.get(src).get(dst).getDuration(protocol); - } - return duration; - } - - /* Return the byte count on an affinity link, per-protocol or not */ - private long getByteCountOnAffinityLinkInternal(AffinityLink al, Byte protocol) { - long b = 0; - List> flows = this.affinityManager.getAllFlowsByHost(al); - for (Entry flow : flows) { - Host h1 = flow.getKey(); - Host h2 = flow.getValue(); - // This will handle protocol being null - b += getByteCountBetweenHostsInternal(h1, h2, protocol); - } - return b; - } - - /* Returns bit rate in bits-per-second on an affinity link, per-protocol or not */ - private double getBitRateOnAffinityLinkInternal(AffinityLink al, Byte protocol) { - double duration = getDurationOnAffinityLinkInternal(al, protocol); - long totalBytes = getByteCountOnAffinityLinkInternal(al, protocol); - if (duration == 0.0) - return 0.0; - return (totalBytes * 8.0) / duration; - } - - /* Returns the duration of communication on an affinity link, per-protocol or not */ - private double getDurationOnAffinityLinkInternal(AffinityLink al, Byte protocol) { - double maxDuration = 0.0; - for (Entry flow : this.affinityManager.getAllFlowsByHost(al)) { - Host h1 = flow.getKey(); - Host h2 = flow.getValue(); - // This will handle protocol being null - double duration = getDurationBetweenHostsInternal(h1, h2, protocol); - if (duration > maxDuration) - maxDuration = duration; - } - return maxDuration; - } - - /* Return the total bytes for a particular protocol between these subnets. */ - private long getByteCountBySubnetInternal(String srcSubnet, String dstSubnet, Byte protocol) { - long totalBytes = 0; - if (srcSubnet == null && dstSubnet == null) { - log.debug("Source and destination subnets cannot both be null."); - return totalBytes; - } - Set srcHosts; - Set dstHosts; - if (srcSubnet == null) { - dstHosts = getHostsInSubnet(dstSubnet); - srcHosts = getHostsNotInSubnet(dstSubnet); - } else if (dstSubnet == null) { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsNotInSubnet(srcSubnet); - } else { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsInSubnet(dstSubnet); - } - - for (Host srcHost : srcHosts) - for (Host dstHost : dstHosts) - totalBytes += getByteCount(srcHost, dstHost, protocol); - return totalBytes; - } - - /* Returns the duration of communication between two subnetes, per-protocol or not */ - private double getDurationBySubnetInternal(String srcSubnet, String dstSubnet, Byte protocol) { - double maxDuration = 0.0; - if (srcSubnet == null && dstSubnet == null) { - log.debug("Source and destination subnet cannot both be null."); - return maxDuration; - } - Set srcHosts; - Set dstHosts; - if (srcSubnet == null) { - dstHosts = getHostsInSubnet(dstSubnet); - srcHosts = getHostsNotInSubnet(dstSubnet); - } else if (dstSubnet == null) { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsNotInSubnet(srcSubnet); - } else { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsInSubnet(dstSubnet); - } - for (Host srcHost : srcHosts) { - for (Host dstHost : dstHosts) { - double duration = getDurationBetweenHostsInternal(srcHost, dstHost, protocol); - if (duration > maxDuration) - maxDuration = duration; - } - } - return maxDuration; - } - - /* Returns the bit rate between these subnects, per-protocol or not. */ - private double getBitRateBySubnetInternal(String srcSubnet, String dstSubnet, Byte protocol) { - double duration = getDurationBySubnetInternal(srcSubnet, dstSubnet, protocol); - long totalBytes = getByteCountBySubnetInternal(srcSubnet, dstSubnet, protocol); - if (duration == 0.0) - return 0.0; - return (totalBytes * 8.0) / duration; - } - /* Returns all hosts that transferred data into this subnet. */ private Map getIncomingHostByteCountsInternal(String subnet, Byte protocol) { Map hosts = new HashMap(); @@ -431,49 +369,6 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager return hosts; } - private Set getProtocols(Host src, Host dst) { - if (this.hostsToStats.get(src) == null || - this.hostsToStats.get(src).get(dst) == null) - return new HashSet(); - return this.hostsToStats.get(src).get(dst).getProtocols(); - } - - private Set getProtocols(AffinityLink al) { - Set protocols = new HashSet(); - for (Entry flow : this.affinityManager.getAllFlowsByHost(al)) { - Host h1 = flow.getKey(); - Host h2 = flow.getValue(); - Set thisProtocols = getProtocols(h1, h2); - protocols.addAll(thisProtocols); - } - return protocols; - } - - private Set getProtocols(String srcSubnet, String dstSubnet) { - if (srcSubnet == null && dstSubnet == null) { - log.debug("Source and destination subnets cannot both be null."); - return null; - } - Set protocols = new HashSet(); - Set srcHosts; - Set dstHosts; - if (srcSubnet == null) { - dstHosts = getHostsInSubnet(dstSubnet); - srcHosts = getHostsNotInSubnet(dstSubnet); - } else if (dstSubnet == null) { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsNotInSubnet(srcSubnet); - } else { - srcHosts = getHostsInSubnet(srcSubnet); - dstHosts = getHostsInSubnet(dstSubnet); - } - - for (Host srcHost : srcHosts) - for (Host dstHost : dstHosts) - protocols.addAll(getProtocols(srcHost, dstHost)); - return protocols; - } - private Set getHostsNotInSubnet(String subnet) { return getHostsNotInSubnet(subnet, this.hostTracker.getAllHosts()); } @@ -488,6 +383,18 @@ public class AnalyticsManager implements IReadServiceListener, IAnalyticsManager return hostsNotInSubnet; } + // Handles null subnets + private Set getSrcHosts(String srcSubnet, String dstSubnet) { + if (srcSubnet == null && dstSubnet == null) { + log.debug("Source and destination subnets cannot both be null."); + return new HashSet(); + } + if (srcSubnet == null) + return getHostsNotInSubnet(dstSubnet); + else + return getHostsInSubnet(srcSubnet); + } + private Set getHostsInSubnet(String subnet) { return getHostsInSubnet(subnet, this.hostTracker.getAllHosts()); } -- 2.36.6