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 | |
| 93 | rings[1].angles[i] = (range.min + range.max)/2; |
| 94 | }); |
| 95 | |
| 96 | // arrange core switches at equal increments |
| 97 | k = 360 / rings[2].switches.length; |
| 98 | rings[2].switches.forEach(function (s, i) { |
| 99 | rings[2].angles[i] = k * i; |
| 100 | }); |
| 101 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 102 | function ringEnter(data, i) { |
| 103 | if (!data.switches.length) { |
| 104 | return; |
| 105 | } |
| 106 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 107 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 108 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 109 | .data(d3.range(data.switches.length).map(function() { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 110 | return data; |
| 111 | })) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 112 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 113 | .classed('nolabel', true) |
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 | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 119 | }); |
| 120 | |
| 121 | nodes.append("svg:circle") |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 122 | .attr('class', function (_, i) { |
| 123 | return data.className + ' ' + controllerColorMap[data.switches[i].controller]; |
| 124 | }) |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 125 | .attr("transform", function(_, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 126 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 127 | if (data.scale) { |
| 128 | m = m.scale(data.scale); |
| 129 | } |
| 130 | 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] | 131 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 132 | .attr("x", -data.width / 2) |
| 133 | .attr("y", -data.width / 2) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 134 | .attr("r", data.width) |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 135 | // .attr("fill", function (_, i) { |
| 136 | // return controllerColorMap[data.switches[i].controller] |
| 137 | // }) |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 138 | |
| 139 | nodes.append("svg:text") |
| 140 | .text(function (d, i) {return d.switches[i].dpid}) |
| 141 | .attr("x", 0) |
| 142 | .attr("y", 0) |
| 143 | .attr("transform", function(_, i) { |
| 144 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 145 | if (data.scale) { |
| 146 | m = m.scale(data.scale); |
| 147 | } |
| 148 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 149 | }) |
| 150 | |
| 151 | function showLabel(data, index) { |
| 152 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false); |
| 153 | } |
| 154 | |
| 155 | function hideLabel(data, index) { |
| 156 | d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true); |
| 157 | } |
| 158 | |
| 159 | nodes.on('mouseover', showLabel); |
| 160 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | var ring = svg.selectAll("g") |
| 164 | .data(rings) |
| 165 | .enter().append("svg:g") |
| 166 | .attr("class", "ring") |
| 167 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 168 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 169 | |
| 170 | // do mouseover zoom on edge nodes |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 171 | function zoom(data, index) { |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 172 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 173 | g.transition().duration(100).attr("r", rings[0].width*3); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | svg.selectAll('.edge').on('mouseover', zoom); |
| 177 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 178 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 179 | function unzoom(data, index) { |
| 180 | var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle'); |
| 181 | g.transition().duration(100).attr("r", rings[0].width); |
| 182 | } |
| 183 | svg.selectAll('.edge').on('mouseout', unzoom); |
| 184 | |
| 185 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 186 | // DRAW THE LINKS |
| 187 | var line = d3.svg.line() |
| 188 | .x(function(d) { |
| 189 | return d.x; |
| 190 | }) |
| 191 | .y(function(d) { |
| 192 | return d.y; |
| 193 | }) |
| 194 | .interpolate("basis"); |
| 195 | |
| 196 | d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) { |
| 197 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 198 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 199 | |
| 200 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 201 | srcPt.x = src.attr('x'); |
| 202 | srcPt.y = src.attr('y'); |
| 203 | |
| 204 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 205 | dstPt.x = dst.attr('x'); |
| 206 | dstPt.y = dst.attr('y'); |
| 207 | |
| 208 | return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]); |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | function updateControllers(model) { |
| 213 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 214 | controllers.enter().append('div') |
| 215 | .attr('class', function (d) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 216 | var color = controllerColorMap[d]; |
| 217 | if (!color) { |
| 218 | color = controllerColorMap[d] = colors.pop(); |
| 219 | } |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 220 | return 'controller ' + color; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 221 | }); |
| 222 | controllers.text(function (d) { |
| 223 | return d; |
| 224 | }); |
| 225 | controllers.exit().remove(); |
| 226 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 227 | model.controllers.forEach(function (c) { |
| 228 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 229 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 230 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 231 | controllers.on('click', function (c, index) { |
| 232 | var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 233 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 234 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 237 | var oldModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 238 | function sync(svg) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 239 | updateModel(function (newModel) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 240 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 241 | if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
| 242 | updateControllers(newModel); |
| 243 | updateTopology(svg, newModel); |
| 244 | } else { |
| 245 | console.log('no change'); |
| 246 | } |
| 247 | updateHeader(newModel); |
| 248 | |
| 249 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 250 | |
| 251 | // do it again in 1s |
| 252 | setTimeout(function () { |
| 253 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 254 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 255 | }); |
| 256 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 257 | |
| 258 | sync(createTopologyView()); |