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 = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 5 | 'color1', |
| 6 | 'color2', |
| 7 | 'color3', |
| 8 | 'color4', |
| 9 | 'color5', |
| 10 | 'color6', |
| 11 | 'color7', |
| 12 | 'color8', |
| 13 | 'color9', |
| 14 | 'color10', |
| 15 | 'color11', |
| 16 | 'color12', |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 17 | ] |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 18 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 19 | |
| 20 | var controllerColorMap = {}; |
| 21 | |
| 22 | |
| 23 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 24 | function createTopologyView() { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 25 | 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] | 26 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 29 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 30 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 31 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 32 | d3.select('#activeFlows').text(model.flows.length); |
| 33 | } |
| 34 | |
| 35 | function toRadians (angle) { |
| 36 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 39 | function updateTopology(svg, model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 40 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 41 | // DRAW THE NODES |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 42 | var rings = [{ |
| 43 | radius: 3, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 44 | width: 6, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 45 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 46 | className: 'edge', |
| 47 | angles: [] |
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, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 50 | width: 12, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 51 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 52 | className: 'aggregation', |
| 53 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 54 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 55 | radius: .75, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 56 | width: 18, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 57 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 58 | className: 'core', |
| 59 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 60 | }]; |
| 61 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 62 | |
| 63 | var aggRanges = {}; |
| 64 | |
| 65 | // arrange edge switches at equal increments |
| 66 | var k = 360 / rings[0].switches.length; |
| 67 | rings[0].switches.forEach(function (s, i) { |
| 68 | var angle = k * i; |
| 69 | |
| 70 | rings[0].angles[i] = angle; |
| 71 | |
| 72 | // record the angle for the agg switch layout |
| 73 | var dpid = s.dpid.split(':'); |
| 74 | dpid[7] = '00'; |
| 75 | var aggdpid = dpid.join(':'); |
| 76 | var aggRange = aggRanges[aggdpid]; |
| 77 | if (!aggRange) { |
| 78 | aggRange = aggRanges[aggdpid] = {}; |
| 79 | aggRange.min = aggRange.max = angle; |
| 80 | } else { |
| 81 | aggRange.max = angle; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | }); |
| 86 | |
| 87 | // arrange aggregation switches to "fan out" to edge switches |
| 88 | k = 360 / rings[1].switches.length; |
| 89 | rings[1].switches.forEach(function (s, i) { |
| 90 | // rings[1].angles[i] = k * i; |
| 91 | var range = aggRanges[s.dpid]; |
| 92 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame^] | 93 | if (range) { |
| 94 | rings[1].angles[i] = (range.min + range.max)/2; |
| 95 | } else { |
| 96 | rings[1].angles[i] = 0; |
| 97 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 98 | }); |
| 99 | |
| 100 | // arrange core switches at equal increments |
| 101 | k = 360 / rings[2].switches.length; |
| 102 | rings[2].switches.forEach(function (s, i) { |
| 103 | rings[2].angles[i] = k * i; |
| 104 | }); |
| 105 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 106 | function ringEnter(data, i) { |
| 107 | if (!data.switches.length) { |
| 108 | return; |
| 109 | } |
| 110 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 111 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 112 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 113 | .data(d3.range(data.switches.length).map(function() { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 114 | return data; |
| 115 | })) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 116 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 117 | .classed('nolabel', true) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 118 | .attr("id", function (_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 119 | return data.switches[i].dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 120 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 121 | .attr("transform", function(_, i) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 122 | return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 123 | }); |
| 124 | |
| 125 | nodes.append("svg:circle") |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 126 | .attr('class', function (_, i) { |
| 127 | return data.className + ' ' + controllerColorMap[data.switches[i].controller]; |
| 128 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 129 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 130 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 131 | if (data.scale) { |
| 132 | m = m.scale(data.scale); |
| 133 | } |
| 134 | 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] | 135 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 136 | .attr("x", -data.width / 2) |
| 137 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 138 | .attr("r", data.width) |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 139 | // .attr("fill", function (_, i) { |
| 140 | // return controllerColorMap[data.switches[i].controller] |
| 141 | // }) |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 142 | |
| 143 | nodes.append("svg:text") |
| 144 | .text(function (d, i) {return d.switches[i].dpid}) |
| 145 | .attr("x", 0) |
| 146 | .attr("y", 0) |
| 147 | .attr("transform", function(_, i) { |
| 148 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 149 | if (data.scale) { |
| 150 | m = m.scale(data.scale); |
| 151 | } |
| 152 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 153 | }) |
| 154 | |
| 155 | function showLabel(data, index) { |
| 156 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false); |
| 157 | } |
| 158 | |
| 159 | function hideLabel(data, index) { |
| 160 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true); |
| 161 | } |
| 162 | |
| 163 | nodes.on('mouseover', showLabel); |
| 164 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | var ring = svg.selectAll("g") |
| 168 | .data(rings) |
| 169 | .enter().append("svg:g") |
| 170 | .attr("class", "ring") |
| 171 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 172 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 173 | |
| 174 | // do mouseover zoom on edge nodes |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 175 | function zoom(data, index) { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 176 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 177 | g.transition().duration(100).attr("r", rings[0].width*3); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | svg.selectAll('.edge').on('mouseover', zoom); |
| 181 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 182 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 183 | function unzoom(data, index) { |
| 184 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 185 | g.transition().duration(100).attr("r", rings[0].width); |
| 186 | } |
| 187 | svg.selectAll('.edge').on('mouseout', unzoom); |
| 188 | |
| 189 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 190 | // DRAW THE LINKS |
| 191 | var line = d3.svg.line() |
| 192 | .x(function(d) { |
| 193 | return d.x; |
| 194 | }) |
| 195 | .y(function(d) { |
| 196 | return d.y; |
| 197 | }) |
| 198 | .interpolate("basis"); |
| 199 | |
| 200 | d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) { |
| 201 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 202 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 203 | |
| 204 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 205 | srcPt.x = src.attr('x'); |
| 206 | srcPt.y = src.attr('y'); |
| 207 | |
| 208 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 209 | dstPt.x = dst.attr('x'); |
| 210 | dstPt.y = dst.attr('y'); |
| 211 | |
| 212 | return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]); |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | function updateControllers(model) { |
| 217 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 218 | controllers.enter().append('div') |
| 219 | .attr('class', function (d) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame^] | 220 | |
| 221 | var color = 'color0'; |
| 222 | if (model.activeControllers.indexOf(d) != -1) { |
| 223 | color = controllerColorMap[d]; |
| 224 | if (!color) { |
| 225 | color = controllerColorMap[d] = colors.pop(); |
| 226 | } |
| 227 | } else { |
| 228 | controllerColorMap[d] = color; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 229 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame^] | 230 | var className = 'controller ' + color; |
| 231 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 232 | }); |
| 233 | controllers.text(function (d) { |
| 234 | return d; |
| 235 | }); |
| 236 | controllers.exit().remove(); |
| 237 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 238 | model.controllers.forEach(function (c) { |
| 239 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 240 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 241 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 242 | controllers.on('click', function (c, index) { |
| 243 | var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 244 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 245 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 248 | var oldModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 249 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame^] | 250 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 251 | updateModel(function (newModel) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame^] | 252 | console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 253 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 254 | if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
| 255 | updateControllers(newModel); |
| 256 | updateTopology(svg, newModel); |
| 257 | } else { |
| 258 | console.log('no change'); |
| 259 | } |
| 260 | updateHeader(newModel); |
| 261 | |
| 262 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 263 | |
| 264 | // do it again in 1s |
| 265 | setTimeout(function () { |
| 266 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 267 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 268 | }); |
| 269 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 270 | |
| 271 | sync(createTopologyView()); |