Hello All.

I midified ryu.topology.js to dinamicaly add or remove switch
using WebSocket on Ryu Topology Viewer.
Sorry, I had sent a pull request but it was closed.

Signed-off-by: Yoshiharu Yamashita <[email protected]>
---
ryu/app/gui_topology/html/ryu.topology.js | 61 
+++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/ryu/app/gui_topology/html/ryu.topology.js 
b/ryu/app/gui_topology/html/ryu.topology.js
index 9d1102e..c333dce 100644
--- a/ryu/app/gui_topology/html/ryu.topology.js
+++ b/ryu/app/gui_topology/html/ryu.topology.js
@@ -21,8 +21,8 @@ ws.onmessage = function(event) {
     this.send(JSON.stringify(ret));
 }
 
-function trim_zero(s) {
-    return s.replace(/^0+/, "");
+function trim_zero(obj) {
+    return String(obj).replace(/^0+/, "");
 }
 
 function dpid_to_int(dpid) {
@@ -85,7 +85,7 @@ elem.update = function () {
         .attr("class", "link");
 
     this.node = this.node.data(topo.nodes);
-    // NOTE: Removing node is not supported. 
+    this.node.exit().remove();
     var nodeEnter = this.node.enter().append("g")
         .attr("class", "node")
         .on("dblclick", function(d) { d3.select(this).classed("fixed", d.fixed 
= false); })
@@ -126,15 +126,11 @@ var topo = {
         this.add_nodes(data.switches);
         this.add_links(data.links);
     },
-    add_nodes: function (switches) {
-        for (var i = 0; i < switches.length; i++) {
-            this.nodes[i] = switches[i];
-        }
-
-        this.node_index = {};
-        for (var i = 0; i < this.nodes.length; i++) {
-            this.node_index[this.nodes[i].dpid] = i;
+    add_nodes: function (nodes) {
+        for (var i = 0; i < nodes.length; i++) {
+            this.nodes.push(nodes[i]);
         }
+        this.refresh_node_index();
     },
     add_links: function (links) {
         for (var i = 0; i < links.length; i++) {
@@ -156,6 +152,15 @@ var topo = {
             this.links.push(link);
         }
     },
+    delete_nodes: function (nodes) {
+        for (var i = 0; i < nodes.length; i++) {
+            console.log("delete switch: " + JSON.stringify(nodes[i]));
+
+            node_index = this.get_node_index(nodes[i]);
+            this.nodes.splice(node_index, 1);
+        }
+        this.refresh_node_index();
+    },
     delete_links: function (links) {
         for (var i = 0; i < links.length; i++) {
             if (!is_valid_link(links[i])) continue;
@@ -165,6 +170,14 @@ var topo = {
             this.links.splice(link_index, 1);
         }
     },
+    get_node_index: function (node) {
+        for (var i = 0; i < this.nodes.length; i++) {
+            if (node.dpid == this.nodes[i].dpid) {
+                return i;
+            }
+        }
+        return null;
+    },
     get_link_index: function (link) {
         for (var i = 0; i < this.links.length; i++) {
             if (link.src.dpid == this.links[i].port.src.dpid &&
@@ -181,7 +194,7 @@ var topo = {
         var pushed = {};
         for (var i = 0; i < this.links.length; i++) {
             function _push(p, dir) {
-                key = p.dpid + ":" + p.port_no
+                key = p.dpid + ":" + p.port_no;
                 if (key in pushed) {
                     return 0;
                 }
@@ -213,14 +226,32 @@ var topo = {
 
         return {x: x, y: y};
     },
+    refresh_node_index: function(){
+        this.node_index = {};
+        for (var i = 0; i < this.nodes.length; i++) {
+            this.node_index[this.nodes[i].dpid] = i;
+        }
+    },
 }
 
 var rpc = {
     event_switch_enter: function (params) {
-        console.log("Not Implemented: event_switch_enter, " + 
JSON.stringify(params));
+        var switches = [];
+        for(var i=0; i < params.length; i++){
+            switches.push({"dpid":params[i].dpid,"ports":params[i].ports});
+        }
+        topo.add_nodes(switches);
+        elem.update();
+        return "";
     },
     event_switch_leave: function (params) {
-        console.log("Not Implemented: event_switch_leave, " + 
JSON.stringify(params));
+        var switches = [];
+        for(var i=0; i < params.length; i++){
+            switches.push({"dpid":params[i].dpid,"ports":params[i].ports});
+        }
+        topo.delete_nodes(switches);
+        elem.update();
+        return "";
     },
     event_link_add: function (links) {
         topo.add_links(links);
@@ -247,4 +278,4 @@ function main() {
     initialize_topology();
 }
 
-main()
+main();
--
1.9.1
------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to