Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1 | /*global d3*/ |
| 2 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 3 | |
| 4 | var colors = [ |
| 5 | '#EC0033', |
| 6 | '#FFBA00', |
| 7 | '#3714B0', |
| 8 | '#B12C49', |
| 9 | '#BF9830', |
| 10 | '#402C84', |
| 11 | '#990021', |
| 12 | '#A67900', |
| 13 | '#F53D65', |
| 14 | '#1F0772', |
| 15 | '#F56E8B', |
| 16 | '#FFCB40', |
| 17 | '#6949D7', |
| 18 | '#FFD973' |
| 19 | ] |
| 20 | |
| 21 | var controllerColorMap = {}; |
| 22 | |
| 23 | |
| 24 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 25 | function createTopologyView() { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 26 | return d3.select('#svg-container').append('svg:svg').append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none'). |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 27 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 30 | function updateHeader(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 31 | d3.select('#lastUpdate').text(model.timestamp); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 32 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 33 | d3.select('#activeFlows').text(model.flows.length); |
| 34 | } |
| 35 | |
| 36 | function toRadians (angle) { |
| 37 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 40 | function updateTopology(svg, model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 41 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 42 | // DRAW THE NODES |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 43 | var rings = [{ |
| 44 | radius: 3, |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 45 | width: 4, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 46 | switches: model.edgeSwitches, |
| 47 | className: 'edge' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 48 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 49 | radius: 2.25, |
| 50 | width: 8, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 51 | switches: model.aggregationSwitches, |
| 52 | className: 'aggregation' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 53 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 54 | radius: .75, |
| 55 | width: 16, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 56 | switches: model.coreSwitches, |
| 57 | className: 'core' |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 58 | }]; |
| 59 | |
| 60 | function ringEnter(data, i) { |
| 61 | if (!data.switches.length) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | var k = 360 / data.switches.length; |
| 66 | |
| 67 | d3.select(this).selectAll("g") |
| 68 | .data(d3.range(data.switches.length).map(function() { |
| 69 | return data; |
| 70 | })) |
| 71 | .enter().append("svg:g") |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 72 | .attr("id", function (_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 73 | return data.switches[i].dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 74 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 75 | .attr("transform", function(_, i) { |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 76 | return "rotate(" + i * k + ")translate(" + data.radius * 150 + ")rotate(" + (-i * k) + ")"; |
| 77 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 78 | .append("svg:circle") |
| 79 | .attr('class', data.className) |
| 80 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 81 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 82 | if (data.scale) { |
| 83 | m = m.scale(data.scale); |
| 84 | } |
| 85 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 86 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 87 | .attr("x", -data.width / 2) |
| 88 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 89 | .attr("r", data.width) |
| 90 | .attr("fill", function (_, i) { |
| 91 | return controllerColorMap[data.switches[i].controller] |
| 92 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | var ring = svg.selectAll("g") |
| 96 | .data(rings) |
| 97 | .enter().append("svg:g") |
| 98 | .attr("class", "ring") |
| 99 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 100 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 101 | function zoom(data, index) { |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 102 | svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 103 | .attr("r", function (data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 104 | return rings[0].width * (index == i ? 2 : 1); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 105 | }) |
| 106 | } |
| 107 | |
| 108 | svg.selectAll('.edge').on('mouseover', zoom); |
| 109 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 110 | |
| 111 | // DRAW THE LINKS |
| 112 | var line = d3.svg.line() |
| 113 | .x(function(d) { |
| 114 | return d.x; |
| 115 | }) |
| 116 | .y(function(d) { |
| 117 | return d.y; |
| 118 | }) |
| 119 | .interpolate("basis"); |
| 120 | |
| 121 | d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) { |
| 122 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 123 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 124 | |
| 125 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 126 | srcPt.x = src.attr('x'); |
| 127 | srcPt.y = src.attr('y'); |
| 128 | |
| 129 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 130 | dstPt.x = dst.attr('x'); |
| 131 | dstPt.y = dst.attr('y'); |
| 132 | |
| 133 | return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]); |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | function updateControllers(model) { |
| 138 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
| 139 | controllers.enter().append('div').attr('class', 'controller') |
| 140 | .attr('style', function (d) { |
| 141 | var color = controllerColorMap[d]; |
| 142 | if (!color) { |
| 143 | color = controllerColorMap[d] = colors.pop(); |
| 144 | } |
| 145 | return 'background-color:' + color; |
| 146 | }); |
| 147 | controllers.text(function (d) { |
| 148 | return d; |
| 149 | }); |
| 150 | controllers.exit().remove(); |
| 151 | |
| 152 | controllers.on('click', function (data, index) { |
| 153 | |
| 154 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | function sync(svg) { |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 158 | updateModel(function (model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 159 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 160 | updateHeader(model); |
| 161 | updateControllers(model); |
| 162 | updateTopology(svg, model); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 163 | |
| 164 | // do it again in 1s |
| 165 | setTimeout(function () { |
| 166 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame^] | 167 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 168 | }); |
| 169 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 170 | |
| 171 | sync(createTopologyView()); |