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(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 74 | dpid[7] = '01'; // the last component of the agg switch is always '01' |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 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 | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 93 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 94 | }); |
| 95 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 96 | // find the association between core switches and aggregation switches |
| 97 | var aggregationSwitchMap = {}; |
| 98 | model.aggregationSwitches.forEach(function (s, i) { |
| 99 | aggregationSwitchMap[s.dpid] = i + 1; |
| 100 | }); |
| 101 | |
| 102 | var coreSwitchMap = {}; |
| 103 | model.coreSwitches.forEach(function (s, i) { |
| 104 | coreSwitchMap[s.dpid] = i + 1; |
| 105 | }); |
| 106 | |
| 107 | var coreLinks = {}; |
| 108 | model.links.forEach(function (l) { |
| 109 | if (aggregationSwitchMap[l['src-switch']] && coreSwitchMap[l['dst-switch']]) { |
| 110 | coreLinks[l['dst-switch']] = aggregationSwitchMap[l['src-switch']] - 1; |
| 111 | } |
| 112 | }); |
| 113 | |
| 114 | |
| 115 | |
| 116 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 117 | k = 360 / rings[2].switches.length; |
| 118 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 119 | // rings[2].angles[i] = k * i; |
| 120 | rings[2].angles[i] = rings[1].angles[coreLinks[s.dpid]]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 121 | }); |
| 122 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 123 | function ringEnter(data, i) { |
| 124 | if (!data.switches.length) { |
| 125 | return; |
| 126 | } |
| 127 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 128 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 129 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 130 | .data(d3.range(data.switches.length).map(function() { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 131 | return data; |
| 132 | })) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 133 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 134 | .classed('nolabel', true) |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 135 | .attr("id", function (_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 136 | return data.switches[i].dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 137 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 138 | .attr("transform", function(_, i) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 139 | 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] | 140 | }); |
| 141 | |
| 142 | nodes.append("svg:circle") |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 143 | .attr('class', function (_, i) { |
| 144 | return data.className + ' ' + controllerColorMap[data.switches[i].controller]; |
| 145 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 146 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 147 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 148 | if (data.scale) { |
| 149 | m = m.scale(data.scale); |
| 150 | } |
| 151 | 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] | 152 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 153 | .attr("x", -data.width / 2) |
| 154 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 155 | .attr("r", data.width) |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 156 | // .attr("fill", function (_, i) { |
| 157 | // return controllerColorMap[data.switches[i].controller] |
| 158 | // }) |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 159 | |
| 160 | nodes.append("svg:text") |
| 161 | .text(function (d, i) {return d.switches[i].dpid}) |
| 162 | .attr("x", 0) |
| 163 | .attr("y", 0) |
| 164 | .attr("transform", function(_, i) { |
| 165 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 166 | if (data.scale) { |
| 167 | m = m.scale(data.scale); |
| 168 | } |
| 169 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 170 | }) |
| 171 | |
| 172 | function showLabel(data, index) { |
| 173 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false); |
| 174 | } |
| 175 | |
| 176 | function hideLabel(data, index) { |
| 177 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true); |
| 178 | } |
| 179 | |
| 180 | nodes.on('mouseover', showLabel); |
| 181 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | var ring = svg.selectAll("g") |
| 185 | .data(rings) |
| 186 | .enter().append("svg:g") |
| 187 | .attr("class", "ring") |
| 188 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 189 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 190 | |
| 191 | // do mouseover zoom on edge nodes |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 192 | function zoom(data, index) { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 193 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 194 | g.transition().duration(100).attr("r", rings[0].width*3); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | svg.selectAll('.edge').on('mouseover', zoom); |
| 198 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 199 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 200 | function unzoom(data, index) { |
| 201 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 202 | g.transition().duration(100).attr("r", rings[0].width); |
| 203 | } |
| 204 | svg.selectAll('.edge').on('mouseout', unzoom); |
| 205 | |
| 206 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 207 | // DRAW THE LINKS |
| 208 | var line = d3.svg.line() |
| 209 | .x(function(d) { |
| 210 | return d.x; |
| 211 | }) |
| 212 | .y(function(d) { |
| 213 | return d.y; |
| 214 | }) |
| 215 | .interpolate("basis"); |
| 216 | |
| 217 | d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) { |
| 218 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 219 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 220 | |
| 221 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 222 | srcPt.x = src.attr('x'); |
| 223 | srcPt.y = src.attr('y'); |
| 224 | |
| 225 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 226 | dstPt.x = dst.attr('x'); |
| 227 | dstPt.y = dst.attr('y'); |
| 228 | |
| 229 | return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]); |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | function updateControllers(model) { |
| 234 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 235 | controllers.enter().append('div') |
| 236 | .attr('class', function (d) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 237 | |
| 238 | var color = 'color0'; |
| 239 | if (model.activeControllers.indexOf(d) != -1) { |
| 240 | color = controllerColorMap[d]; |
| 241 | if (!color) { |
| 242 | color = controllerColorMap[d] = colors.pop(); |
| 243 | } |
| 244 | } else { |
| 245 | controllerColorMap[d] = color; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 246 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 247 | var className = 'controller ' + color; |
| 248 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 249 | }); |
| 250 | controllers.text(function (d) { |
| 251 | return d; |
| 252 | }); |
| 253 | controllers.exit().remove(); |
| 254 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 255 | model.controllers.forEach(function (c) { |
| 256 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 257 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 258 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 259 | controllers.on('click', function (c, index) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 260 | var allSelected = true; |
| 261 | for (var key in controllerColorMap) { |
| 262 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 263 | allSelected = false; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | if (allSelected) { |
| 268 | for (var key in controllerColorMap) { |
| 269 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 270 | } |
| 271 | } else { |
| 272 | for (var key in controllerColorMap) { |
| 273 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 278 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 279 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 282 | var oldModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 283 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 284 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 285 | updateModel(function (newModel) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 286 | console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 287 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 288 | if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
| 289 | updateControllers(newModel); |
| 290 | updateTopology(svg, newModel); |
| 291 | } else { |
| 292 | console.log('no change'); |
| 293 | } |
| 294 | updateHeader(newModel); |
| 295 | |
| 296 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 297 | |
| 298 | // do it again in 1s |
| 299 | setTimeout(function () { |
| 300 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 301 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 302 | }); |
| 303 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 304 | |
| 305 | sync(createTopologyView()); |