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