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