Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 IBM |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | window.TopologyView = Backbone.View.extend({ |
| 18 | initialize:function () { |
| 19 | this.template = _.template(tpl.get('topology')); |
| 20 | this.model.bind("change", this.render, this); |
| 21 | this.hosts = this.options.hosts.models; |
| 22 | this.host_links = []; |
| 23 | }, |
| 24 | |
| 25 | render:function (eventName) { |
| 26 | $(this.el).html(this.template()); |
| 27 | var width = 900, |
| 28 | height = 600; |
| 29 | var color = d3.scale.category20(); |
| 30 | var force = d3.layout.force() |
| 31 | .charge(-500) |
| 32 | .linkDistance(200) |
| 33 | .size([width, height]); |
| 34 | var svg = d3.select("#topology-graph").append("svg") |
| 35 | .attr("width", width) |
| 36 | .attr("height", height); |
| 37 | if(this.model.nodes) { |
| 38 | for (var i = 0; i < this.model.nodes.length; i++) { |
| 39 | this.model.nodes[i].group = 1; |
| 40 | this.model.nodes[i].id = this.model.nodes[i].name; |
| 41 | } |
| 42 | |
| 43 | for (var i = 0; i < this.hosts.length; i++) { |
| 44 | host = this.hosts[i]; |
| 45 | if (host.attributes['ipv4'].length > 0) { |
| 46 | host.name = host.attributes['ipv4'][0] + "\n" + host.id; |
| 47 | } else { |
| 48 | host.name = host.id; |
| 49 | } |
| 50 | host.group = 2; |
| 51 | //console.log(host); |
| 52 | } |
| 53 | |
| 54 | var all_nodes = this.model.nodes.concat(this.hosts); |
| 55 | |
| 56 | var all_nodes_map = []; |
| 57 | |
| 58 | _.each(all_nodes, function(n) { |
| 59 | all_nodes_map[n.id] = n; |
| 60 | }); |
| 61 | |
| 62 | for (var i = 0; i < this.hosts.length; i++) { |
| 63 | host = this.hosts[i]; |
| 64 | //for (var j = 0; j < host.attributes['attachmentPoint'].length; j++) { |
| 65 | for (var j = 0; j < 1; j++) { // FIXME hack to ignore multiple APs |
| 66 | var link = {source:all_nodes_map[host.id], |
| 67 | target:all_nodes_map[host.attributes['attachmentPoint'][j]['switchDPID']], |
| 68 | value:10}; |
| 69 | //console.log(link); |
| 70 | if ( link.source && link.target) { |
| 71 | this.host_links.push(link); |
| 72 | } else { |
| 73 | console.log("Error: skipping link with undefined stuff!") |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | var all_links = this.model.links.concat(this.host_links); |
| 79 | |
| 80 | force.nodes(all_nodes).links(all_links).start(); |
| 81 | var link = svg.selectAll("line.link").data(all_links).enter() |
| 82 | .append("line").attr("class", "link") |
| 83 | .style("stroke", function (d) { return "black"; }); |
| 84 | var node = svg.selectAll(".node").data(all_nodes) |
| 85 | .enter().append("g") |
| 86 | .attr("class", "node") |
| 87 | .call(force.drag); |
| 88 | |
| 89 | node.append("image") |
| 90 | .attr("xlink:href", function (d) {return d.group==1 ? "/ui/img/switch.png" : "/ui/img/server.png"}) |
| 91 | .attr("x", -16).attr("y", -16) |
| 92 | .attr("width", 32).attr("height", 32); |
| 93 | node.append("text").attr("dx", 20).attr("dy", ".35em") |
| 94 | .text(function(d) { return d.name }); |
| 95 | node.on("click", function (d) { |
| 96 | // TODO we could add some functionality here |
| 97 | console.log('clicked '+d.name); |
| 98 | }); |
| 99 | node.append("title").text(function(d) { return d.name; }); |
| 100 | force.on("tick", function() { |
| 101 | link.attr("x1", function(d) { return d.source.x; }) |
| 102 | .attr("y1", function(d) { return d.source.y; }) |
| 103 | .attr("x2", function(d) { return d.target.x; }) |
| 104 | .attr("y2", function(d) { return d.target.y; }); |
| 105 | node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); |
| 106 | |
| 107 | }); |
| 108 | } |
| 109 | return this; |
| 110 | } |
| 111 | }); |