Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 1 | /*global d3, document∆*/ |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 2 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 3 | d3.selection.prototype.moveToFront = function() { |
| 4 | return this.each(function(){ |
| 5 | this.parentNode.appendChild(this); |
| 6 | }); |
| 7 | }; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 8 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 9 | var line = d3.svg.line() |
| 10 | .x(function(d) { |
| 11 | return d.x; |
| 12 | }) |
| 13 | .y(function(d) { |
| 14 | return d.y; |
| 15 | }); |
| 16 | |
| 17 | var svg, selectedFlowsView; |
| 18 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 19 | var colors = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 20 | 'color1', |
| 21 | 'color2', |
| 22 | 'color3', |
| 23 | 'color4', |
| 24 | 'color5', |
| 25 | 'color6', |
| 26 | 'color7', |
| 27 | 'color8', |
| 28 | 'color9', |
| 29 | 'color10', |
| 30 | 'color11', |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 31 | 'color12' |
| 32 | ]; |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 33 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 34 | |
| 35 | var controllerColorMap = {}; |
| 36 | |
| 37 | |
| 38 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 39 | function createTopologyView() { |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 40 | var svg = d3.select('#svg-container').append('svg:svg'); |
| 41 | |
| 42 | svg.append("svg:defs").append("svg:marker") |
| 43 | .attr("id", "arrow") |
| 44 | .attr("viewBox", "0 -5 10 10") |
| 45 | .attr("refX", -1) |
| 46 | .attr("markerWidth", 5) |
| 47 | .attr("markerHeight", 5) |
| 48 | .attr("orient", "auto") |
| 49 | .append("svg:path") |
Paul Greyson | 45303ac | 2013-03-23 16:44:01 -0700 | [diff] [blame] | 50 | .attr("d", "M0,-3L10,0L0,3"); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 51 | |
| 52 | return 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] | 53 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 56 | var selectedFlowsData = [ |
| 57 | {selected: false, flow: null}, |
| 58 | {selected: false, flow: null}, |
| 59 | {selected: false, flow: null}, |
| 60 | {selected: false, flow: null}, |
| 61 | {selected: false, flow: null} |
| 62 | ]; |
| 63 | |
| 64 | function drawFlows() { |
| 65 | // DRAW THE FLOWS |
| 66 | var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) { |
| 67 | return d.flow ? d.flow.flowId.value : null; |
| 68 | }); |
| 69 | |
| 70 | flows.enter().append("svg:path") |
| 71 | .attr('class', 'flow') |
| 72 | .attr('d', function (d) { |
| 73 | if (!d.flow) { |
| 74 | return; |
| 75 | } |
| 76 | var pts = []; |
| 77 | d.flow.dataPath.flowEntries.forEach(function (flowEntry) { |
| 78 | var s = d3.select(document.getElementById(flowEntry.dpid.value)); |
| 79 | var pt = document.querySelector('svg').createSVGPoint(); |
| 80 | pt.x = s.attr('x'); |
| 81 | pt.y = s.attr('y'); |
| 82 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 83 | pts.push(pt); |
| 84 | }); |
| 85 | return line(pts); |
| 86 | }) |
Paul Greyson | acb5941 | 2013-03-25 23:48:06 -0700 | [diff] [blame^] | 87 | .attr('stroke-dasharray', '3, 10') |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 88 | .append('svg:animate') |
| 89 | .attr('attributeName', 'stroke-dashoffset') |
| 90 | .attr('attributeType', 'xml') |
| 91 | .attr('from', '500') |
| 92 | .attr('to', '-500') |
| 93 | .attr('dur', '20s') |
| 94 | .attr('repeatCount', 'indefinite'); |
| 95 | |
| 96 | flows.style('visibility', function (d) { |
| 97 | if (d) { |
| 98 | return d.selected ? '' : 'hidden'; |
| 99 | } |
| 100 | }) |
| 101 | |
| 102 | flows.select('animate').attr('from', function (d) { |
| 103 | if (d.flow) { |
| 104 | if (d.selected) { |
| 105 | return '500'; |
| 106 | } else { |
| 107 | return '-500'; |
| 108 | } |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | function updateFlowView() { |
| 114 | selectedFlowsView.data(selectedFlowsData); |
| 115 | |
| 116 | selectedFlowsView.classed('selected', function (d) { |
| 117 | if (d.flow) { |
| 118 | return d.selected; |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | selectedFlowsView.select('.flowId') |
| 123 | .text(function (d) { |
| 124 | if (d.flow) { |
| 125 | return d.flow.flowId.value; |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | selectedFlowsView.select('.srcDPID') |
| 130 | .text(function (d) { |
| 131 | if (d.flow) { |
| 132 | return d.flow.dataPath.srcPort.dpid.value; |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | selectedFlowsView.select('.dstDPID') |
| 137 | .text(function (d) { |
| 138 | if (d.flow) { |
| 139 | return d.flow.dataPath.dstPort.dpid.value; |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | function createFlowView() { |
| 145 | function rowEnter(d, i) { |
| 146 | var row = d3.select(this); |
| 147 | |
| 148 | row.on('click', function () { |
| 149 | selectedFlowsData[i].selected = !selectedFlowsData[i].selected; |
| 150 | updateFlowView(); |
| 151 | drawFlows(); |
| 152 | }); |
| 153 | |
| 154 | row.append('div') |
| 155 | .classed('flowIndex', true) |
| 156 | .text(function () { |
| 157 | return i+1; |
| 158 | }); |
| 159 | |
| 160 | row.append('div') |
| 161 | .classed('flowId', true); |
| 162 | |
| 163 | row.append('div') |
| 164 | .classed('srcDPID', true); |
| 165 | |
| 166 | row.append('div') |
| 167 | .classed('dstDPID', true); |
| 168 | |
| 169 | row.append('div') |
| 170 | .classed('iperf', true); |
| 171 | } |
| 172 | |
| 173 | var flows = d3.select('#selectedFlows') |
| 174 | .selectAll('.selectedFlow') |
| 175 | .data(selectedFlowsData) |
| 176 | .enter() |
| 177 | .append('div') |
| 178 | .classed('selectedFlow', true) |
| 179 | .each(rowEnter); |
| 180 | |
| 181 | |
| 182 | return flows; |
| 183 | } |
| 184 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 185 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 186 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 187 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 188 | d3.select('#activeFlows').text(model.flows.length); |
| 189 | } |
| 190 | |
| 191 | function toRadians (angle) { |
| 192 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 195 | function createRingsFromModel(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 196 | var rings = [{ |
| 197 | radius: 3, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 198 | width: 6, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 199 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 200 | className: 'edge', |
| 201 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 202 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 203 | radius: 2.25, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 204 | width: 12, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 205 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 206 | className: 'aggregation', |
| 207 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 208 | }, { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 209 | radius: 0.75, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 210 | width: 18, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 211 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 212 | className: 'core', |
| 213 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 214 | }]; |
| 215 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 216 | |
| 217 | var aggRanges = {}; |
| 218 | |
| 219 | // arrange edge switches at equal increments |
| 220 | var k = 360 / rings[0].switches.length; |
| 221 | rings[0].switches.forEach(function (s, i) { |
| 222 | var angle = k * i; |
| 223 | |
| 224 | rings[0].angles[i] = angle; |
| 225 | |
| 226 | // record the angle for the agg switch layout |
| 227 | var dpid = s.dpid.split(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 228 | 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] | 229 | var aggdpid = dpid.join(':'); |
| 230 | var aggRange = aggRanges[aggdpid]; |
| 231 | if (!aggRange) { |
| 232 | aggRange = aggRanges[aggdpid] = {}; |
| 233 | aggRange.min = aggRange.max = angle; |
| 234 | } else { |
| 235 | aggRange.max = angle; |
| 236 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 237 | }); |
| 238 | |
| 239 | // arrange aggregation switches to "fan out" to edge switches |
| 240 | k = 360 / rings[1].switches.length; |
| 241 | rings[1].switches.forEach(function (s, i) { |
| 242 | // rings[1].angles[i] = k * i; |
| 243 | var range = aggRanges[s.dpid]; |
| 244 | |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 245 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 246 | }); |
| 247 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 248 | // find the association between core switches and aggregation switches |
| 249 | var aggregationSwitchMap = {}; |
| 250 | model.aggregationSwitches.forEach(function (s, i) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 251 | aggregationSwitchMap[s.dpid] = i; |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 252 | }); |
| 253 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 254 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 255 | k = 360 / rings[2].switches.length; |
| 256 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 257 | // rings[2].angles[i] = k * i; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 258 | var associatedAggregationSwitches = model.configuration.association[s.dpid]; |
| 259 | // TODO: go between if there are multiple |
| 260 | var index = aggregationSwitchMap[associatedAggregationSwitches[0]]; |
| 261 | |
| 262 | rings[2].angles[i] = rings[1].angles[index]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 263 | }); |
| 264 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 265 | // TODO: construct this form initially rather than converting. it works better because |
| 266 | // it allows binding by dpid |
| 267 | var testRings = []; |
| 268 | rings.forEach(function (ring) { |
| 269 | var testRing = []; |
| 270 | ring.switches.forEach(function (s, i) { |
| 271 | var testSwitch = { |
| 272 | dpid: s.dpid, |
| 273 | state: s.state, |
| 274 | radius: ring.radius, |
| 275 | width: ring.width, |
| 276 | className: ring.className, |
| 277 | angle: ring.angles[i], |
| 278 | controller: s.controller |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 279 | }; |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 280 | testRing.push(testSwitch); |
| 281 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 282 | |
| 283 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 284 | testRings.push(testRing); |
| 285 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 286 | |
| 287 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 288 | // return rings; |
| 289 | return testRings; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | function updateTopology(svg, model) { |
| 293 | |
| 294 | // DRAW THE SWITCHES |
| 295 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 296 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 297 | function ringEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 298 | if (!data.length) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 302 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 303 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 304 | .data(data, function (data) { |
| 305 | return data.dpid; |
| 306 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 307 | .enter().append("svg:g") |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 308 | .classed('nolabel', true) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 309 | .attr("id", function (data, i) { |
| 310 | return data.dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 311 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 312 | .attr("transform", function(data, i) { |
| 313 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 314 | }); |
| 315 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 316 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 317 | nodes.append("svg:circle") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 318 | .attr("transform", function(data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 319 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 320 | if (data.scale) { |
| 321 | m = m.scale(data.scale); |
| 322 | } |
| 323 | 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] | 324 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 325 | .attr("x", function (data) { |
| 326 | return -data.width / 2; |
| 327 | }) |
| 328 | .attr("y", function (data) { |
| 329 | return -data.width / 2; |
| 330 | }) |
| 331 | .attr("r", function (data) { |
| 332 | return data.width; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 333 | }); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 334 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 335 | // setup the mouseover behaviors |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 336 | function showLabel(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 337 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | function hideLabel(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 341 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | nodes.on('mouseover', showLabel); |
| 345 | nodes.on('mouseout', hideLabel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 348 | // append switches |
| 349 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 350 | .attr("class", "ring") |
| 351 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 352 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 353 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 354 | function ringUpdate(data, i) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 355 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 356 | .data(data, function (data) { |
| 357 | return data.dpid; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 358 | }); |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 359 | nodes.select('circle').attr('class', function (data, i) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 360 | if (data.state === 'ACTIVE') { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 361 | return data.className + ' ' + controllerColorMap[data.controller]; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 362 | } else { |
| 363 | return data.className + ' ' + 'colorInactive'; |
| 364 | } |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 365 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // update switches |
| 369 | rings.each(ringUpdate); |
| 370 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 371 | |
| 372 | // Now setup the labels |
| 373 | // This is done separately because SVG draws in node order and we want the labels |
| 374 | // always on top |
| 375 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 376 | |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 377 | function labelRingEnter(data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 378 | if (!data.length) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | |
| 382 | // create the nodes |
| 383 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 384 | .data(data, function (data) { |
| 385 | return data.dpid; |
| 386 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 387 | .enter().append("svg:g") |
| 388 | .classed('nolabel', true) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 389 | .attr("id", function (data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 390 | return data.dpid + '-label'; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 391 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 392 | .attr("transform", function(data, i) { |
| 393 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 394 | }); |
| 395 | |
| 396 | // add the text nodes which show on mouse over |
| 397 | nodes.append("svg:text") |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 398 | .text(function (data) {return data.dpid;}) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 399 | .attr("x", function (data) { |
| 400 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 401 | if (data.className == 'edge') { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 402 | return - data.width*3 - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 403 | } else { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 404 | return - data.width - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 405 | } |
| 406 | } else { |
| 407 | if (data.className == 'edge') { |
| 408 | return data.width*3 + 4; |
| 409 | } else { |
| 410 | return data.width + 4; |
| 411 | } |
| 412 | } |
| 413 | }) |
| 414 | .attr("y", function (data) { |
| 415 | var y; |
| 416 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 417 | if (data.className == 'edge') { |
| 418 | y = data.width*3/2 + 4; |
| 419 | } else { |
| 420 | y = data.width/2 + 4; |
| 421 | } |
| 422 | } else { |
| 423 | if (data.className == 'edge') { |
| 424 | y = data.width*3/2 + 4; |
| 425 | } else { |
| 426 | y = data.width/2 + 4; |
| 427 | } |
| 428 | } |
| 429 | return y - 6; |
| 430 | }) |
| 431 | .attr("text-anchor", function (data) { |
| 432 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 433 | return "end"; |
| 434 | } else { |
| 435 | return "start"; |
| 436 | } |
| 437 | }) |
| 438 | .attr("transform", function(data) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 439 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 440 | if (data.scale) { |
| 441 | m = m.scale(data.scale); |
| 442 | } |
| 443 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 444 | }) |
| 445 | } |
| 446 | |
| 447 | labelRings.enter().append("svg:g") |
| 448 | .attr("class", "textRing") |
| 449 | .each(labelRingEnter); |
| 450 | |
| 451 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 452 | // switches should not change during operation of the ui so no |
| 453 | // rings.exit() |
| 454 | |
| 455 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 456 | // do mouseover zoom on edge nodes |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 457 | function zoom(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 458 | var g = d3.select(document.getElementById(data.dpid)).select('circle'); |
| 459 | g.transition().duration(100).attr("r", data.width*3); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 460 | // TODO: this doesn't work because the data binding is by index |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 461 | d3.select(this.parentNode).moveToFront(); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 462 | } |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 463 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 464 | svg.selectAll('.edge').on('mouseover', zoom); |
| 465 | svg.selectAll('.edge').on('mousedown', zoom); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 466 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 467 | function unzoom(data, index) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 468 | var g = d3.select(document.getElementById(data.dpid)).select('circle'); |
| 469 | g.transition().duration(100).attr("r", data.width); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 470 | } |
| 471 | svg.selectAll('.edge').on('mouseout', unzoom); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 472 | |
| 473 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 474 | // DRAW THE LINKS |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 475 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 476 | // key on link dpids since these will come/go during demo |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 477 | var links = d3.select('svg').selectAll('.link').data(model.links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 478 | return d['src-switch']+'->'+d['dst-switch']; |
| 479 | }); |
| 480 | |
| 481 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 482 | links.enter().append("svg:path") |
| 483 | .attr("class", "link") |
| 484 | .attr("d", function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 485 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 486 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 487 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 488 | |
| 489 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 490 | srcPt.x = src.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 491 | srcPt.y = src.attr('y'); |
| 492 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 493 | |
| 494 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 495 | dstPt.x = dst.attr('x'); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 496 | dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable |
| 497 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 498 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 499 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 500 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 501 | midPt.y = (srcPt.y + dstPt.y)/2; |
| 502 | |
| 503 | return line([srcPt, midPt, dstPt]); |
| 504 | }) |
| 505 | .attr("marker-mid", function(d) { return "url(#arrow)"; }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 506 | |
| 507 | // remove old links |
| 508 | links.exit().remove(); |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 509 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 510 | |
| 511 | drawFlows(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | function updateControllers(model) { |
| 515 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 516 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 517 | .each(function (c) { |
| 518 | controllerColorMap[c] = colors.pop(); |
| 519 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 520 | }) |
| 521 | .text(function (d) { |
| 522 | return d; |
| 523 | }); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 524 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 525 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 526 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 527 | if (model.activeControllers.indexOf(d) != -1) { |
| 528 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 529 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 530 | var className = 'controller ' + color; |
| 531 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 532 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 533 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 534 | // this should never be needed |
| 535 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 536 | |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 537 | controllers.on('click', function (c, index) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 538 | var allSelected = true; |
| 539 | for (var key in controllerColorMap) { |
| 540 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 541 | allSelected = false; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | if (allSelected) { |
| 546 | for (var key in controllerColorMap) { |
| 547 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 548 | } |
| 549 | } else { |
| 550 | for (var key in controllerColorMap) { |
| 551 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 556 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 557 | }); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 560 | var oldModel; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 561 | function sync(svg, selectedFlowsView) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 562 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 563 | updateModel(function (newModel) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 564 | console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 565 | |
Paul Greyson | a4e0f54 | 2013-03-23 18:24:44 -0700 | [diff] [blame] | 566 | if (!oldModel || JSON.stringify(oldModel) != JSON.stringify(newModel)) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 567 | updateControllers(newModel); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 568 | |
| 569 | // fake flows right now |
| 570 | var i; |
| 571 | for (i = 0; i < newModel.flows.length; i+=1) { |
| 572 | var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false; |
| 573 | selectedFlowsData[i].flow = newModel.flows[i]; |
| 574 | selectedFlowsData[i].selected = selected; |
| 575 | } |
| 576 | |
| 577 | updateFlowView(newModel); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 578 | updateTopology(svg, newModel); |
| 579 | } else { |
| 580 | console.log('no change'); |
| 581 | } |
| 582 | updateHeader(newModel); |
| 583 | |
| 584 | oldModel = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 585 | |
| 586 | // do it again in 1s |
| 587 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 588 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 589 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 590 | }); |
| 591 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 592 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 593 | svg = createTopologyView(); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 594 | selectedFlowsView = createFlowView(); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 595 | // workaround for Chrome v25 bug |
| 596 | // if executed immediately, the view box transform logic doesn't work properly |
| 597 | // fixed in Chrome v27 |
| 598 | setTimeout(function () { |
| 599 | // workaround for another Chrome v25 bug |
| 600 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 601 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 602 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 603 | sync(svg, selectedFlowsView); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 604 | }, 100); |