Complete NB API for affinity link statistics; analytics.py demonstrates its usage... 19/2919/1
authorKatrina LaCurts <katrina.lacurts@plexxi.com>
Wed, 20 Nov 2013 15:58:50 +0000 (10:58 -0500)
committerKatrina LaCurts <katrina.lacurts@plexxi.com>
Wed, 20 Nov 2013 15:58:50 +0000 (10:58 -0500)
Signed-off-by: Katrina LaCurts <katrina.lacurts@plexxi.com>
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AllStatistics.java [moved from analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AllHostStatistics.java with 76% similarity]
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AnalyticsNorthbound.java
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/HostStatistics.java [deleted file]
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/Statistics.java [moved from analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AffinityLinkStatistics.java with 73% similarity]
scripts/analytics.py

similarity index 76%
rename from analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AllHostStatistics.java
rename to analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AllStatistics.java
index 78ef5e85f4d70cf97156c65477cfd45101811304..57ee7773f6dda8f0d5b691c59d226f807f2ceea2 100644 (file)
@@ -18,16 +18,16 @@ import javax.xml.bind.annotation.XmlRootElement;
 
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.NONE)
-public class AllHostStatistics {
+public class AllStatistics {
     @XmlElement
-    private Map<Byte, HostStatistics> data;
+    private Map<Byte, Statistics> data;
 
-    public AllHostStatistics() {
+    public AllStatistics() {
         super();
-        this.data = new HashMap<Byte, HostStatistics>();
+        this.data = new HashMap<Byte, Statistics>();
     }
 
-    public void addHostStat(Byte proto, HostStatistics stat) {
+    public void addHostStat(Byte proto, Statistics stat) {
         this.data.put(proto, stat);
     }
 }
index b329e3eae7b554ffb4514a633d8c327e1dfdec19..af582b5f4cdb70b3f18c900fbd819ff03c959444 100644 (file)
@@ -107,15 +107,15 @@ public class AnalyticsNorthbound {
     @Path("/{containerName}/hoststats/{srcNetworkAddr}/{dstNetworkAddr}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @TypeHint(HostStatistics.class)
+    @TypeHint(Statistics.class)
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Operation successful"),
-        @ResponseCode(code = 404, condition = "The containerName is not found"),
-        @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public HostStatistics getHostStatistics(
-        @PathParam("containerName") String containerName,
-        @PathParam("srcNetworkAddr") String srcNetworkAddr,
-        @PathParam("dstNetworkAddr") String dstNetworkAddr) {
+            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 404, condition = "The containerName is not found"),
+            @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+    public Statistics getHostStatistics(
+           @PathParam("containerName") String containerName,
+           @PathParam("srcNetworkAddr") String srcNetworkAddr,
+           @PathParam("dstNetworkAddr") String dstNetworkAddr) {
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
             throw new UnauthorizedException("User is not authorized to perform this operation on container " + containerName);
         handleDefaultDisabled(containerName);
@@ -129,7 +129,7 @@ public class AnalyticsNorthbound {
         long byteCount = analyticsManager.getByteCount(srcHost, dstHost);
         double bitRate = analyticsManager.getBitRate(srcHost, dstHost);
 
-        return new HostStatistics(srcHost, dstHost, byteCount, bitRate);
+        return new Statistics(byteCount, bitRate);
     }
 
     /**
@@ -144,12 +144,12 @@ public class AnalyticsNorthbound {
     @Path("/{containerName}/hoststats/{srcIP}/{dstIP}/{protocol}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @TypeHint(HostStatistics.class)
+    @TypeHint(Statistics.class)
     @StatusCodes({
         @ResponseCode(code = 200, condition = "Operation successful"),
         @ResponseCode(code = 404, condition = "The containerName is not found"),
         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public HostStatistics getHostStatistics(
+    public Statistics getHostStatistics(
         @PathParam("containerName") String containerName,
         @PathParam("srcIP") String srcIP,
         @PathParam("dstIP") String dstIP,
@@ -167,7 +167,7 @@ public class AnalyticsNorthbound {
         long byteCount = analyticsManager.getByteCount(srcHost, dstHost, IPProtocols.getProtocolNumberByte(protocol));
         double bitRate = analyticsManager.getBitRate(srcHost, dstHost, IPProtocols.getProtocolNumberByte(protocol));
 
-        return new HostStatistics(srcHost, dstHost, byteCount, bitRate);
+        return new Statistics(byteCount, bitRate);
     }
 
     /**
@@ -181,12 +181,12 @@ public class AnalyticsNorthbound {
     @Path("/{containerName}/hoststats/{srcIP}/{dstIP}/all")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @TypeHint(AllHostStatistics.class)
+    @TypeHint(AllStatistics.class)
     @StatusCodes({
         @ResponseCode(code = 200, condition = "Operation successful"),
         @ResponseCode(code = 404, condition = "The containerName is not found"),
         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public AllHostStatistics getAllHostStatistics(
+    public AllStatistics getAllHostStatistics(
         @PathParam("containerName") String containerName,
         @PathParam("srcIP") String srcIP,
         @PathParam("dstIP") String dstIP) {
@@ -202,10 +202,9 @@ public class AnalyticsNorthbound {
         Host dstHost = handleHostAvailability(containerName, dstIP);
         Map<Byte, Long> byteCounts = analyticsManager.getAllByteCounts(srcHost, dstHost);
         Map<Byte, Double> bitRates = analyticsManager.getAllBitRates(srcHost, dstHost);
-        AllHostStatistics allStats = new AllHostStatistics();
+        AllStatistics allStats = new AllStatistics();
         for (Byte protocol : byteCounts.keySet())
-            allStats.addHostStat(protocol, new HostStatistics(srcHost, dstHost, byteCounts.get(protocol), bitRates.get(protocol)));
-        System.out.println(">>> " + allStats);
+            allStats.addHostStat(protocol, new Statistics(byteCounts.get(protocol), bitRates.get(protocol)));
         return allStats;
     }
 
@@ -219,12 +218,12 @@ public class AnalyticsNorthbound {
     @Path("/{containerName}/affinitylinkstats/{linkName}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @TypeHint(HostStatistics.class)
+    @TypeHint(Statistics.class)
     @StatusCodes({
         @ResponseCode(code = 200, condition = "Operation successful"),
         @ResponseCode(code = 404, condition = "The containerName is not found"),
         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public AffinityLinkStatistics getAffinityLinkStatistics(
+   public Statistics getAffinityLinkStatistics(
         @PathParam("containerName") String containerName,
         @PathParam("linkName") String affinityLinkName) {
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
@@ -239,7 +238,77 @@ public class AnalyticsNorthbound {
         long byteCount = analyticsManager.getByteCount(al);
         double bitRate = analyticsManager.getBitRate(al);
 
-        return new AffinityLinkStatistics(al, byteCount, bitRate);
+        return new Statistics(byteCount, bitRate);
+    }
+
+    /**
+     * Returns the affinity link statistics for a given link.
+     *
+     * @param containerName: Name of the Container
+     * @param linkName: AffinityLink name
+     * @param protocol: IP Protocol
+     * @return List of Affinity Link Statistics for a given link.
+     */
+    @Path("/{containerName}/affinitylinkstats/{linkName}/{protocol}")
+    @GET
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @TypeHint(Statistics.class)
+    @StatusCodes({
+        @ResponseCode(code = 200, condition = "Operation successful"),
+        @ResponseCode(code = 404, condition = "The containerName is not found"),
+        @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+    public Statistics getAffinityLinkStatistics(
+        @PathParam("containerName") String containerName,
+        @PathParam("linkName") String affinityLinkName,
+        @PathParam("protocol") String protocol) {
+        if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
+            throw new UnauthorizedException("User is not authorized to perform this operation on container " + containerName);
+        handleDefaultDisabled(containerName);
+
+        IAnalyticsManager analyticsManager = getAnalyticsService(containerName);
+        if (analyticsManager == null)
+            throw new ServiceUnavailableException("Analytics " + RestMessages.SERVICEUNAVAILABLE.toString());
+
+        AffinityLink al = handleAffinityLinkAvailability(containerName, affinityLinkName);
+        long byteCount = analyticsManager.getByteCount(al, IPProtocols.getProtocolNumberByte(protocol));
+        double bitRate = analyticsManager.getBitRate(al, IPProtocols.getProtocolNumberByte(protocol));
+
+        return new Statistics(byteCount, bitRate);
+    }
+
+    /**
+     * Returns all affinity link statistics for a given link.
+     *
+     * @param containerName: Name of the Container
+     * @param linkName: AffinityLink name
+     * @return List of Affinity Link Statistics for a given link.
+     */
+    @Path("/{containerName}/affinitylinkstats/{linkName}/all")
+    @GET
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @TypeHint(AllStatistics.class)
+    @StatusCodes({
+        @ResponseCode(code = 200, condition = "Operation successful"),
+        @ResponseCode(code = 404, condition = "The containerName is not found"),
+        @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+    public AllStatistics getAllAffinityLinkStatistics(
+        @PathParam("containerName") String containerName,
+        @PathParam("linkName") String affinityLinkName) {
+        if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
+            throw new UnauthorizedException("User is not authorized to perform this operation on container " + containerName);
+        handleDefaultDisabled(containerName);
+
+        IAnalyticsManager analyticsManager = getAnalyticsService(containerName);
+        if (analyticsManager == null)
+            throw new ServiceUnavailableException("Analytics " + RestMessages.SERVICEUNAVAILABLE.toString());
+
+        AffinityLink al = handleAffinityLinkAvailability(containerName, affinityLinkName);
+        Map<Byte, Long> byteCounts = analyticsManager.getAllByteCounts(al);
+        Map<Byte, Double> bitRates = analyticsManager.getAllBitRates(al);
+        AllStatistics allStats = new AllStatistics();
+        for (Byte protocol : byteCounts.keySet())
+            allStats.addHostStat(protocol, new Statistics(byteCounts.get(protocol), bitRates.get(protocol)));
+        return allStats;
     }
 
     /**
@@ -257,15 +326,15 @@ public class AnalyticsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(SubnetStatistics.class)
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Operation successful"),
-        @ResponseCode(code = 404, condition = "The containerName is not found"),
-        @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public SubnetStatistics getSubnetStatistics(
-        @PathParam("containerName") String containerName,
-        @PathParam("srcIP") String srcIP,
-        @PathParam("srcMask") String srcMask,
-        @PathParam("dstIP") String dstIP,
-        @PathParam("dstMask") String dstMask) {
+            @ResponseCode(code = 200, condition = "Operation successful"),
+                @ResponseCode(code = 404, condition = "The containerName is not found"),
+                @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+                public SubnetStatistics getSubnetStatistics(
+                                                            @PathParam("containerName") String containerName,
+                                                            @PathParam("srcIP") String srcIP,
+                                                            @PathParam("srcMask") String srcMask,
+                                                            @PathParam("dstIP") String dstIP,
+                                                            @PathParam("dstMask") String dstMask) {
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
             throw new UnauthorizedException("User is not authorized to perform this operation on container " + containerName);
         handleDefaultDisabled(containerName);
@@ -298,13 +367,13 @@ public class AnalyticsNorthbound {
     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(AllHosts.class)
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Operation successful"),
-        @ResponseCode(code = 404, condition = "The containerName is not found"),
-        @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
-    public AllHosts getIncomingHostByteCounts(
-        @PathParam("containerName") String containerName,
-        @PathParam("ip") String ip,
-        @PathParam("mask") String mask) {
+            @ResponseCode(code = 200, condition = "Operation successful"),
+                @ResponseCode(code = 404, condition = "The containerName is not found"),
+                @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
+                public AllHosts getIncomingHostByteCounts(
+                                                          @PathParam("containerName") String containerName,
+                                                          @PathParam("ip") String ip,
+                                                          @PathParam("mask") String mask) {
         // TODO: Change AllHosts class name to something better
         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this))
             throw new UnauthorizedException("User is not authorized to perform this operation on container " + containerName);
diff --git a/analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/HostStatistics.java b/analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/HostStatistics.java
deleted file mode 100644 (file)
index ae34fef..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2013 Plexxi, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.affinity.analytics.northbound;
-
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import org.opendaylight.controller.sal.core.Host;
-
-@XmlRootElement
-@XmlAccessorType(XmlAccessType.NONE)
-public class HostStatistics {
-    @XmlElement
-    private Host srcHost;
-    @XmlElement
-    private Host dstHost;
-    @XmlElement
-    private long byteCount;
-    @XmlElement
-    private double bitRate;
-
-    // To satisfy JAXB
-    @SuppressWarnings("unused")
-    private HostStatistics() {
-    }
-
-    public HostStatistics(Host srcHost, Host dstHost, long byteCount, double bitRate) {
-        super();
-        this.srcHost = srcHost;
-        this.dstHost = dstHost;
-        this.byteCount = byteCount;
-        this.bitRate = bitRate;
-    }
-
-    public Host getSrcHost() {
-        return this.srcHost;
-    }
-
-    public void setSrcHost(Host host) {
-        this.srcHost = host;
-    }
-
-    public Host getDstHost() {
-        return this.dstHost;
-    }
-
-    public void setDstHost(Host host) {
-        this.dstHost = host;
-    }
-
-    public long getByteCount() {
-        return this.byteCount;
-    }
-
-    public void setByteCount(long byteCount) {
-        this.byteCount = byteCount;
-    }
-
-    public double getBitRate() {
-        return this.bitRate;
-    }
-
-    public void setBitRate(double bitRate) {
-        this.bitRate = bitRate;
-    }
-}
similarity index 73%
rename from analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AffinityLinkStatistics.java
rename to analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/Statistics.java
index 19f9850a4f750beaa4538c07ead7331092ae940c..5e911124218a2641d7db5f7caac42097473cc07b 100644 (file)
@@ -13,13 +13,9 @@ import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
-import org.opendaylight.affinity.affinity.AffinityLink;
-
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.NONE)
-public class AffinityLinkStatistics {
-
-    private AffinityLink link;
+public class Statistics {
     @XmlElement
     private long byteCount;
     @XmlElement
@@ -27,24 +23,15 @@ public class AffinityLinkStatistics {
 
     // To satisfy JAXB
     @SuppressWarnings("unused")
-    private AffinityLinkStatistics() {
+    private Statistics() {
     }
 
-    public AffinityLinkStatistics(AffinityLink link, long byteCount, double bitRate) {
+    public Statistics(long byteCount, double bitRate) {
         super();
-        this.link = link;
         this.byteCount = byteCount;
         this.bitRate = bitRate;
     }
 
-    public AffinityLink getLink() {
-        return this.link;
-    }
-
-    public void setLink(AffinityLink link) {
-        this.link = link;
-    }
-
     public long getByteCount() {
         return this.byteCount;
     }
index 0d77c2204cea98e3ec45a06f46c85f0eed6a808d..0f48640024ebb5a1ff1378c8914bfca094c1a7c6 100644 (file)
@@ -56,6 +56,42 @@ def all_bit_rates_hosts(src, dst):
 
 ### Affinity link statistics
 
+def bytes_on_link(al):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s" % al
+    data = rest_method(url, "GET")
+    print("%s bytes on link %s" % (data['byteCount'], al))
+
+def bytes_on_link_protocol(al, protocol):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s/%s" % (al, protocol)
+    data = rest_method(url, "GET")
+    print("%s bytes on link %s for protocol %s" % (data['byteCount'], al, protocol))
+
+def rate_on_link(al):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s" % al
+    data = rest_method(url, "GET")
+    print("%s bit/s on link %s" % (data['bitRate'], al))
+
+def rate_on_link_protocol(al, protocol):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s/%s" % (al, protocol)
+    data = rest_method(url, "GET")
+    print("%s bit/s on link %s for protocol %s" % (data['bitRate'], al, protocol))
+
+def all_byte_counts_link(al):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s/all" % al
+    data = rest_method(url, "GET")['data']['entry']
+    for entry in data:
+        protocol = entry['key']
+        byte_count = entry['value']['byteCount']
+        print("%s bytes from protocol %s" % (byte_count, protocol))
+
+def all_bit_rates_link(al):
+    url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s/all" % al
+    data = rest_method(url, "GET")['data']['entry']
+    for entry in data:
+        protocol = entry['key']
+        bit_rate = entry['value']['bitRate']
+        print("%s bit/s from protocol %s" % (bit_rate, protocol))
+
 def run_interactive_mode():
 
     print "Usage: [host | link] [src dst | link-name] {protocol}"
@@ -63,30 +99,38 @@ def run_interactive_mode():
     # Demo mode
     while True:
         request = raw_input("> ")
-        try:
-            request = request.split()
-            request_type = request[0]
-
-            if (request_type == "quit" or request_type == "exit"):
-                sys.exit()
-
-            if (request_type == "host"):
-                if (len(request) == 3):
-                    src, dst = request[1:3]
-                    bytes_between_hosts(src, dst)
-                    rate_between_hosts(src, dst)
-                    all_byte_counts_hosts(src, dst)
-                    all_bit_rates_hosts(src, dst)
-                elif (len(request) == 4):
-                    src, dst, protocol = request[1:4]
-                    bytes_between_hosts_protocol(src, dst, int(protocol))
-                    rate_between_hosts_protocol(src, dst, int(protocol))
-
-            elif (request_type == "link"):
+        request = request.split()
+        request_type = request[0]
+
+        if (request_type == "quit" or request_type == "exit"):
+            sys.exit()
+
+        if (request_type == "host"):
+            if (len(request) == 3):
+                src, dst = request[1:3]
+                bytes_between_hosts(src, dst)
+                rate_between_hosts(src, dst)
+                all_byte_counts_hosts(src, dst)
+                all_bit_rates_hosts(src, dst)
+            elif (len(request) == 4):
+                src, dst, protocol = request[1:4]
+                bytes_between_hosts_protocol(src, dst, int(protocol))
+                rate_between_hosts_protocol(src, dst, int(protocol))
+
+        elif (request_type == "link"):
+            if (len(request) == 2):
                 link = request[1]
-
-            elif (request_type == "subnet"):
-                subnet = request[1]
+                bytes_on_link(link)
+                rate_on_link(link)
+                all_byte_counts_link(link)
+                all_bit_rates_link(link)
+            elif (len(request) == 3):
+                link, protocol = request[1:3]
+                bytes_on_link_protocol(link, protocol)
+                rate_on_link_protocol(link, protocol)
+                
+        elif (request_type == "subnet"):
+            subnet = request[1]
 
 def main():
 
@@ -95,10 +139,10 @@ def main():
     subnet_control.add_subnet("defaultSubnet", "10.0.0.254/8")
 
     # Set up an affinity link
-#    affinity_control = AffinityControl()
-#    affinity_control.add_affinity_group("testAG1", ips=["10.0.0.1", "10.0.0.2"])
-#    affinity_control.add_affinity_group("testAG2", ips=["10.0.0.3", "10.0.0.4"])
-#    affinity_control.add_affinity_link("testAL", "testAG1", "testAG2")
+    affinity_control = AffinityControl()
+    affinity_control.add_affinity_group("testAG1", ips=["10.0.0.1", "10.0.0.2"])
+    affinity_control.add_affinity_group("testAG2", ips=["10.0.0.3", "10.0.0.4"])
+    affinity_control.add_affinity_link("testAL", "testAG1", "testAG2")
 #    raw_input("[Press enter to continue]" )
 
     run_interactive_mode()