Better serializationfor Collections in the NB API 24/2924/1
authorKatrina LaCurts <katrina.lacurts@plexxi.com>
Wed, 20 Nov 2013 21:22:52 +0000 (16:22 -0500)
committerKatrina LaCurts <katrina.lacurts@plexxi.com>
Wed, 20 Nov 2013 21:22:52 +0000 (16:22 -0500)
Signed-off-by: Katrina LaCurts <katrina.lacurts@plexxi.com>
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AllStatistics.java
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/AnalyticsNorthbound.java
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/HostStatistics.java [new file with mode: 0644]
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/IncomingHostData.java
analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/ProtocolStatistics.java [new file with mode: 0644]
scripts/analytics.py

index 19121a887368ed79d84390a5181bdce51cdb5103..964e3df3943a6000d1912e5b7ada971b6a8dddcd 100644 (file)
@@ -8,8 +8,9 @@
 
 package org.opendaylight.affinity.analytics.northbound;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
-import java.util.HashMap;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -20,14 +21,27 @@ import javax.xml.bind.annotation.XmlRootElement;
 @XmlAccessorType(XmlAccessType.NONE)
 public class AllStatistics {
     @XmlElement
-    private Map<Byte, Statistics> data;
+    private List<ProtocolStatistics> stats;
 
-    public AllStatistics() {
-        super();
-        this.data = new HashMap<Byte, Statistics>();
+    // To satisfy JAXB
+    @SuppressWarnings("unused")
+    private AllStatistics() {
     }
 
-    public void addStat(Byte proto, Statistics stat) {
-        this.data.put(proto, stat);
+    public AllStatistics(Map<Byte, Long> byteCounts, Map<Byte, Double> bitRates) {
+        this.stats = new ArrayList<ProtocolStatistics>();
+        for (Byte protocol : byteCounts.keySet()) {
+            long byteCount = byteCounts.get(protocol);
+            double bitRate = bitRates.get(protocol);
+            this.stats.add(new ProtocolStatistics(protocol, new Statistics(byteCount, bitRate)));
+        }
+    }
+
+    public List<ProtocolStatistics> getStats() {
+        return this.stats;
+    }
+
+    public void setStats(List<ProtocolStatistics> stats) {
+        this.stats = stats;
     }
 }
index 83ed834d531e24eb1ddb8087d75a35dc1d757870..d539cfc15e49c272726896c3fd1ee4006ba31a7b 100644 (file)
@@ -8,15 +8,11 @@
 
 package org.opendaylight.affinity.analytics.northbound;
 
-import java.lang.Long;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.Map;
-import java.util.HashMap;
-import java.util.ArrayList;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
@@ -196,10 +192,7 @@ public class AnalyticsNorthbound {
         Host dstHost = handleHostAvailability(containerName, dstIP);
         Map<Byte, Long> byteCounts = analyticsManager.getAllByteCounts(srcHost, dstHost);
         Map<Byte, Double> bitRates = analyticsManager.getAllBitRates(srcHost, dstHost);
-        AllStatistics allStats = new AllStatistics();
-        for (Byte protocol : byteCounts.keySet())
-            allStats.addStat(protocol, new Statistics(byteCounts.get(protocol), bitRates.get(protocol)));
-        return allStats;
+        return new AllStatistics(byteCounts, bitRates);
     }
 
     /**
@@ -293,10 +286,7 @@ public class AnalyticsNorthbound {
         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.addStat(protocol, new Statistics(byteCounts.get(protocol), bitRates.get(protocol)));
-        return allStats;
+        return new AllStatistics(byteCounts, bitRates);
     }
 
     /**
@@ -426,10 +416,7 @@ public class AnalyticsNorthbound {
 
         Map<Byte, Long> byteCounts = analyticsManager.getAllByteCounts(srcString, dstString);
         Map<Byte, Double> bitRates = analyticsManager.getAllBitRates(srcString, dstString);
-        AllStatistics allStats = new AllStatistics();
-        for (Byte protocol : byteCounts.keySet())
-            allStats.addStat(protocol, new Statistics(byteCounts.get(protocol), bitRates.get(protocol)));
-        return allStats;
+        return new AllStatistics(byteCounts, bitRates);
     }
 
     /**
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
new file mode 100644 (file)
index 0000000..03028d4
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * 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 java.net.InetAddress;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class HostStatistics {
+    @XmlElement
+    private String hostIP;
+    @XmlElement
+    private Long byteCount;
+
+    // To satisfy JAXB
+    @SuppressWarnings("unused")
+    private HostStatistics() {
+    }
+
+    public HostStatistics(InetAddress hostIP, Long byteCount) {
+        this.hostIP = hostIP.toString();
+        this.byteCount = byteCount;
+    }
+
+    public String getHostIP() {
+        return this.hostIP;
+    }
+
+    public void setHostIP(String hostIP) {
+        this.hostIP = hostIP;
+    }
+
+    public Long getByteCount() {
+        return this.byteCount;
+    }
+
+    public void setByteCount(Long byteCount) {
+        this.byteCount = byteCount;
+    }
+}
index 465306047cd020ba75b82290c0bf924443485fb7..37faf2f2629de73ad653a1cd62f6732f1ad438e2 100644 (file)
@@ -9,7 +9,8 @@
 package org.opendaylight.affinity.analytics.northbound;
 
 import java.net.InetAddress;
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 import org.opendaylight.controller.sal.core.Host;
@@ -23,19 +24,22 @@ import javax.xml.bind.annotation.XmlRootElement;
 @XmlAccessorType(XmlAccessType.NONE)
 public class IncomingHostData {
     @XmlElement
-    Map<String, Long> data;
-    // TODO: There is a better way to serialize a map
+    List<HostStatistics> stats;
 
     @SuppressWarnings("unused") // To satisfy JAXB
     private IncomingHostData() {}
 
     public IncomingHostData(Map<Host, Long> hostData) {
-        this.data = new HashMap<String, Long>();
+        this.stats = new ArrayList<HostStatistics>();
         for (Host h : hostData.keySet())
-            this.data.put(h.getNetworkAddress().toString(), hostData.get(h));
+            this.stats.add(new HostStatistics(h.getNetworkAddress(), hostData.get(h)));
     }
 
-    public Map<String, Long> getData() {
-        return this.data;
+    public List<HostStatistics> getStats() {
+        return this.stats;
+    }
+
+    public void setStats(List<HostStatistics> stats) {
+        this.stats = stats;
     }
 }
diff --git a/analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/ProtocolStatistics.java b/analytics/northbound/src/main/java/org/opendaylight/affinity/analytics/northbound/ProtocolStatistics.java
new file mode 100644 (file)
index 0000000..a79690d
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class ProtocolStatistics {
+    @XmlElement
+    private byte protocol;
+    @XmlElement
+    private Statistics stat;
+
+    // To satisfy JAXB
+    @SuppressWarnings("unused")
+    private ProtocolStatistics() {
+    }
+
+    public ProtocolStatistics(byte protocol, Statistics stat) {
+        super();
+        this.protocol = protocol;
+        this.stat = stat;
+    }
+
+    public long getProtocol() {
+        return this.protocol;
+    }
+
+    public void setProtocol(Byte protocol) {
+        this.protocol = protocol;
+    }
+
+    public Statistics getStat() {
+        return this.stat;
+    }
+
+    public void setStat(Statistics stat) {
+        this.stat = stat;
+    }
+}
index 5be77a0ab8419e85213dc5f33ab5bd71f6351804..f05fbb8b5e3be995d43feaf16fdcaf1f2fd1c9c9 100644 (file)
@@ -32,18 +32,10 @@ def stats_hosts_protocol(src, dst, protocol):
 
 def all_stats_hosts(src, dst):
     url = "http://localhost:8080/affinity/nb/v2/analytics/default/hoststats/%s/%s/all" % (src, dst)
-    data = rest_method(url, "GET")['data']['entry']
-    if (type(data) == type({})):
-        host = data['key']
-        byte_count = data['value']
-        print("%s bytes from host %s" % (byte_count, host))
-    else:
-        for entry in data:
-            protocol = entry['key']
-            byte_count = entry['value']['byteCount']
-            bit_rate = entry['value']['bitRate']
-            print("%s bytes from protocol %s" % (byte_count, protocol))
-            print("%s bit/s from protocol %s" % (bit_rate, protocol))
+    data = rest_method(url, "GET")['stats']
+    for entry in data:
+        print("%s bytes from protocol %s" % (entry['stat']['byteCount'], entry['protocol']))
+        print("%s bit/s from protocol %s" % (entry['stat']['bitRate'], entry['protocol']))
 
 ### Affinity link statistics
 
@@ -61,18 +53,10 @@ def stats_link_protocol(al, protocol):
 
 def all_stats_link(al):
     url = "http://localhost:8080/affinity/nb/v2/analytics/default/affinitylinkstats/%s/all" % al
-    data = rest_method(url, "GET")['data']['entry']
-    if (type(data) == type({})):
-        host = data['key']
-        byte_count = data['value']
-        print("%s bytes from host %s" % (byte_count, host))
-    else:
-        for entry in data:
-            protocol = entry['key']
-            byte_count = entry['value']['byteCount']
-            bit_rate = entry['value']['bitRate']
-            print("%s bytes from protocol %s" % (byte_count, protocol))
-            print("%s bit/s from protocol %s" % (bit_rate, protocol))
+    data = rest_method(url, "GET")['stats']
+    for entry in data:
+        print("%s bytes from protocol %s" % (entry['stat']['byteCount'], entry['protocol']))
+        print("%s bit/s from protocol %s" % (entry['stat']['bitRate'], entry['protocol']))
 
 ### Subnet statistics
 
@@ -104,44 +88,27 @@ def stats_subnet_protocol(src_sub, dst_sub, protocol):
 
 def all_stats_subnet(src_sub, dst_sub):
     url = "http://localhost:8080/affinity/nb/v2/analytics/default/subnetstats/%s/%s/all" % (src_sub, dst_sub)
-    data = rest_method(url, "GET")['data']['entry']
-    if (type(data) == type({})):
-        host = data['key']
-        byte_count = data['value']
-        print("%s bytes from host %s" % (byte_count, host))
-    else:
-        for entry in data:
-            protocol = entry['key']
-            byte_count = entry['value']['byteCount']
-            bit_rate = entry['value']['bitRate']
-            print("%s bytes from protocol %s" % (byte_count, protocol))
-            print("%s bit/s from protocol %s" % (bit_rate, protocol))
+    data = rest_method(url, "GET")['stats']
+    for entry in data:
+        print("%s bytes from protocol %s" % (entry['stat']['byteCount'], entry['protocol']))
+        print("%s bit/s from protocol %s" % (entry['stat']['bitRate'], entry['protocol']))
 
 def incoming_hosts(subnet):
     url = "http://localhost:8080/affinity/nb/v2/analytics/default/subnetstats/incoming/%s" % subnet
-    data = rest_method(url, "GET")['data']['entry']
+    data = rest_method(url, "GET")
+    data = rest_method(url, "GET")['stats']
     if (type(data) == type({})):
-        host = data['key']
-        byte_count = data['value']
-        print("%s bytes from host %s" % (byte_count, host))
-    else:
-        for entry in data:
-            host = entry['key']
-            byte_count = entry['value']
-            print("%s bytes from host %s" % (byte_count, host))
+        data = [data]
+    for entry in data:
+        print("%s bytes from host %s" % (entry['byteCount'], entry['hostIP']))
 
 def incoming_hosts_protocol(subnet, protocol):
     url = "http://localhost:8080/affinity/nb/v2/analytics/default/subnetstats/incoming/%s/%s" % (subnet, protocol)
     data = rest_method(url, "GET")['data']['entry']
     if (type(data) == type({})):
-        host = data['key']
-        byte_count = data['value']
-        print("%s bytes from host %s" % (byte_count, host))
-    else:
-        for entry in data:
-            host = entry['key']
-            byte_count = entry['value']
-            print("%s bytes from host %s" % (byte_count, host))
+        data = [data]
+    for entry in data:
+        print("%s bytes from host %s" % (entry['byteCount'], entry['hostIP']))
 
 def run_interactive_mode():
 
@@ -149,6 +116,7 @@ def run_interactive_mode():
 
     # Demo mode
     while True:
+
         request = raw_input("> ")
         request = request.split()
         request_type = request[0]