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 | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 45 | width: 6, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 46 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 47 | className: 'edge', |
| 48 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 49 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 50 | radius: 2.25, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 51 | width: 12, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 52 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 53 | className: 'aggregation', |
| 54 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 55 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 56 | radius: .75, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 57 | width: 18, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 58 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 59 | className: 'core', |
| 60 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 61 | }]; |
| 62 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 63 | |
| 64 | var aggRanges = {}; |
| 65 | |
| 66 | // arrange edge switches at equal increments |
| 67 | var k = 360 / rings[0].switches.length; |
| 68 | rings[0].switches.forEach(function (s, i) { |
| 69 | var angle = k * i; |
| 70 | |
| 71 | rings[0].angles[i] = angle; |
| 72 | |
| 73 | // record the angle for the agg switch layout |
| 74 | var dpid = s.dpid.split(':'); |
| 75 | dpid[7] = '00'; |
| 76 | var aggdpid = dpid.join(':'); |
| 77 | var aggRange = aggRanges[aggdpid]; |
| 78 | if (!aggRange) { |
| 79 | aggRange = aggRanges[aggdpid] = {}; |
| 80 | aggRange.min = aggRange.max = angle; |
| 81 | } else { |
| 82 | aggRange.max = angle; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | }); |
| 87 | |
| 88 | // arrange aggregation switches to "fan out" to edge switches |
| 89 | k = 360 / rings[1].switches.length; |
| 90 | rings[1].switches.forEach(function (s, i) { |
| 91 | // rings[1].angles[i] = k * i; |
| 92 | var range = aggRanges[s.dpid]; |
| 93 | |
| 94 | rings[1].angles[i] = (range.min + range.max)/2; |
| 95 | }); |
| 96 | |
| 97 | // arrange core switches at equal increments |
| 98 | k = 360 / rings[2].switches.length; |
| 99 | rings[2].switches.forEach(function (s, i) { |
| 100 | rings[2].angles[i] = k * i; |
| 101 | }); |
| 102 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 103 | function ringEnter(data, i) { |
| 104 | if (!data.switches.length) { |
| 105 | return; |
| 106 | } |
| 107 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 108 | |
| 109 | d3.select(this).selectAll("g") |
| 110 | .data(d3.range(data.switches.length).map(function() { |
| 111 | return data; |
| 112 | })) |
| 113 | .enter().append("svg:g") |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 114 | .attr("id", function (_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 115 | return data.switches[i].dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 116 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 117 | .attr("transform", function(_, i) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 118 | return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")"; |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 119 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 120 | .append("svg:circle") |
| 121 | .attr('class', data.className) |
| 122 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 123 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 124 | if (data.scale) { |
| 125 | m = m.scale(data.scale); |
| 126 | } |
| 127 | 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] | 128 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 129 | .attr("x", -data.width / 2) |
| 130 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 131 | .attr("r", data.width) |
| 132 | .attr("fill", function (_, i) { |
| 133 | return controllerColorMap[data.switches[i].controller] |
| 134 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | var ring = svg.selectAll("g") |
| 138 | .data(rings) |
| 139 | .enter().append("svg:g") |
| 140 | .attr("class", "ring") |
| 141 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 142 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 143 | function zoom(data, index) { |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 144 | svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 145 | .attr("r", function (data, i) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame^] | 146 | return rings[0].width * (index == i ? 3 : 1); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 147 | }) |
| 148 | } |
| 149 | |
| 150 | svg.selectAll('.edge').on('mouseover', zoom); |
| 151 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 152 | |
| 153 | // DRAW THE LINKS |
| 154 | var line = d3.svg.line() |
| 155 | .x(function(d) { |
| 156 | return d.x; |
| 157 | }) |
| 158 | .y(function(d) { |
| 159 | return d.y; |
| 160 | }) |
| 161 | .interpolate("basis"); |
| 162 | |
| 163 | d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) { |
| 164 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 165 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 166 | |
| 167 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 168 | srcPt.x = src.attr('x'); |
| 169 | srcPt.y = src.attr('y'); |
| 170 | |
| 171 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 172 | dstPt.x = dst.attr('x'); |
| 173 | dstPt.y = dst.attr('y'); |
| 174 | |
| 175 | return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]); |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | function updateControllers(model) { |
| 180 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
| 181 | controllers.enter().append('div').attr('class', 'controller') |
| 182 | .attr('style', function (d) { |
| 183 | var color = controllerColorMap[d]; |
| 184 | if (!color) { |
| 185 | color = controllerColorMap[d] = colors.pop(); |
| 186 | } |
| 187 | return 'background-color:' + color; |
| 188 | }); |
| 189 | controllers.text(function (d) { |
| 190 | return d; |
| 191 | }); |
| 192 | controllers.exit().remove(); |
| 193 | |
| 194 | controllers.on('click', function (data, index) { |
| 195 | |
| 196 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | function sync(svg) { |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 200 | updateModel(function (model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 201 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 202 | updateHeader(model); |
| 203 | updateControllers(model); |
| 204 | updateTopology(svg, model); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 205 | |
| 206 | // do it again in 1s |
| 207 | setTimeout(function () { |
| 208 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 209 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 210 | }); |
| 211 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 212 | |
| 213 | sync(createTopologyView()); |