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 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 17 | var model; |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 18 | var svg; |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 19 | var updateTopology; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 20 | var pendingLinks = {}; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 21 | var selectedFlows = []; |
| 22 | |
| 23 | var pendingTimeout = 10000; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 24 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 25 | var colors = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 26 | 'color1', |
| 27 | 'color2', |
| 28 | 'color3', |
| 29 | 'color4', |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 30 | 'color7', |
| 31 | 'color8', |
| 32 | 'color9', |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 33 | // 'color11', |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 34 | 'color12' |
| 35 | ]; |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 36 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 37 | |
| 38 | var controllerColorMap = {}; |
| 39 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 40 | function setPending(selection) { |
| 41 | selection.classed('pending', false); |
| 42 | setTimeout(function () { |
| 43 | selection.classed('pending', true); |
| 44 | }) |
| 45 | } |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 46 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 47 | function createTopologyView() { |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 48 | |
| 49 | window.addEventListener('resize', function () { |
| 50 | // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied |
| 51 | // either that or is it possible to position the paths so they get the automatic transform as well? |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 52 | // updateTopology(); |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 53 | }); |
| 54 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 55 | var svg = d3.select('#svg-container').append('svg:svg'); |
| 56 | |
| 57 | svg.append("svg:defs").append("svg:marker") |
| 58 | .attr("id", "arrow") |
| 59 | .attr("viewBox", "0 -5 10 10") |
| 60 | .attr("refX", -1) |
| 61 | .attr("markerWidth", 5) |
| 62 | .attr("markerHeight", 5) |
| 63 | .attr("orient", "auto") |
| 64 | .append("svg:path") |
Paul Greyson | 45303ac | 2013-03-23 16:44:01 -0700 | [diff] [blame] | 65 | .attr("d", "M0,-3L10,0L0,3"); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 66 | |
| 67 | 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] | 68 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 71 | function updateSelectedFlowsTopology() { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 72 | // DRAW THE FLOWS |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 73 | var flows = d3.select('svg').selectAll('.flow').data(selectedFlows); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 74 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 75 | flows.enter().append("svg:path").attr('class', 'flow') |
| 76 | .attr('stroke-dasharray', '4, 10') |
| 77 | .append('svg:animate') |
| 78 | .attr('attributeName', 'stroke-dashoffset') |
| 79 | .attr('attributeType', 'xml') |
| 80 | .attr('from', '500') |
| 81 | .attr('to', '-500') |
| 82 | .attr('dur', '20s') |
| 83 | .attr('repeatCount', 'indefinite'); |
| 84 | |
| 85 | |
| 86 | flows.attr('d', function (d) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 87 | if (!d) { |
| 88 | return; |
| 89 | } |
| 90 | var pts = []; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 91 | if (!d.dataPath.flowEntries) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 92 | // create a temporary vector to indicate the pending flow |
| 93 | var s1 = d3.select(document.getElementById(d.dataPath.srcPort.dpid.value)); |
| 94 | var s2 = d3.select(document.getElementById(d.dataPath.dstPort.dpid.value)); |
| 95 | |
| 96 | var pt1 = document.querySelector('svg').createSVGPoint(); |
| 97 | pt1.x = s1.attr('x'); |
| 98 | pt1.y = s1.attr('y'); |
| 99 | pt1 = pt1.matrixTransform(s1[0][0].getCTM()); |
| 100 | pts.push(pt1); |
| 101 | |
| 102 | var pt2 = document.querySelector('svg').createSVGPoint(); |
| 103 | pt2.x = s2.attr('x'); |
| 104 | pt2.y = s2.attr('y'); |
| 105 | pt2 = pt2.matrixTransform(s2[0][0].getCTM()); |
| 106 | pts.push(pt2); |
| 107 | |
| 108 | } else { |
| 109 | d.dataPath.flowEntries.forEach(function (flowEntry) { |
| 110 | var s = d3.select(document.getElementById(flowEntry.dpid.value)); |
| 111 | // s[0] is null if the flow entry refers to a non-existent switch |
| 112 | if (s[0][0]) { |
| 113 | var pt = document.querySelector('svg').createSVGPoint(); |
| 114 | pt.x = s.attr('x'); |
| 115 | pt.y = s.attr('y'); |
| 116 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 117 | pts.push(pt); |
| 118 | } else { |
| 119 | console.log('flow refers to non-existent switch: ' + flowEntry.dpid.value); |
| 120 | } |
| 121 | }); |
| 122 | } |
| 123 | return line(pts); |
| 124 | }) |
Paul Greyson | 71b550d | 2013-03-28 11:56:19 -0700 | [diff] [blame] | 125 | .attr('id', function (d) { |
| 126 | if (d) { |
| 127 | return makeFlowKey(d); |
| 128 | } |
| 129 | }) |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 130 | .classed('pending', function (d) { |
| 131 | return d && d.pending |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 132 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 133 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 134 | // "marching ants" |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 135 | flows.select('animate').attr('from', 500); |
| 136 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | function updateSelectedFlowsTable() { |
| 140 | function rowEnter(d) { |
| 141 | var row = d3.select(this); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 142 | row.append('div').classed('deleteFlow', true); |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 143 | row.append('div').classed('flowId', true); |
| 144 | row.append('div').classed('srcDPID', true); |
| 145 | row.append('div').classed('dstDPID', true); |
| 146 | row.append('div').classed('iperf', true); |
Paul Greyson | 71b550d | 2013-03-28 11:56:19 -0700 | [diff] [blame] | 147 | |
| 148 | row.on('mouseover', function (d) { |
| 149 | if (d) { |
| 150 | var path = document.getElementById(makeFlowKey(d)); |
| 151 | d3.select(path).classed('highlight', true); |
| 152 | } |
| 153 | }); |
| 154 | row.on('mouseout', function (d) { |
| 155 | if (d) { |
| 156 | var path = document.getElementById(makeFlowKey(d)); |
| 157 | d3.select(path).classed('highlight', false); |
| 158 | } |
| 159 | }) |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | function rowUpdate(d) { |
| 163 | var row = d3.select(this); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 164 | row.select('.deleteFlow').on('click', function () { |
| 165 | if (d) { |
| 166 | var prompt = 'Delete flow ' + d.flowId.value + '?'; |
| 167 | if (confirm(prompt)) { |
| 168 | deleteFlow(d); |
| 169 | d.pending = true; |
| 170 | updateSelectedFlows(); |
| 171 | |
| 172 | setTimeout(function () { |
| 173 | d.pending = false; |
| 174 | updateSelectedFlows(); |
| 175 | }, pendingTimeout) |
| 176 | }; |
| 177 | } |
| 178 | }); |
| 179 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 180 | row.select('.flowId') |
| 181 | .text(function (d) { |
| 182 | if (d) { |
| 183 | if (d.flowId) { |
| 184 | return d.flowId.value; |
| 185 | } else { |
| 186 | return '0x--'; |
| 187 | } |
| 188 | } |
| 189 | }) |
| 190 | .classed('pending', d && d.pending); |
| 191 | |
| 192 | row.select('.srcDPID') |
| 193 | .text(function (d) { |
| 194 | if (d) { |
| 195 | return d.dataPath.srcPort.dpid.value; |
| 196 | } |
| 197 | }); |
| 198 | |
| 199 | row.select('.dstDPID') |
| 200 | .text(function (d) { |
| 201 | if (d) { |
| 202 | return d.dataPath.dstPort.dpid.value; |
| 203 | } |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | var flows = d3.select('#selectedFlows') |
| 208 | .selectAll('.selectedFlow') |
| 209 | .data(selectedFlows); |
| 210 | |
| 211 | flows.enter() |
| 212 | .append('div') |
| 213 | .classed('selectedFlow', true) |
| 214 | .each(rowEnter); |
| 215 | |
| 216 | flows.each(rowUpdate); |
| 217 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 218 | flows.exit().remove(); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 221 | function updateSelectedFlows() { |
| 222 | // make sure that all of the selected flows are either |
| 223 | // 1) valid (meaning they are in the latest list of flows) |
| 224 | // 2) pending |
| 225 | if (model) { |
| 226 | var flowMap = {}; |
| 227 | model.flows.forEach(function (flow) { |
| 228 | flowMap[makeFlowKey(flow)] = flow; |
| 229 | }); |
| 230 | |
| 231 | var newSelectedFlows = []; |
| 232 | selectedFlows.forEach(function (flow) { |
| 233 | if (flow) { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame^] | 234 | var liveFlow = flowMap[makeFlowKey(flow)]; |
| 235 | if (liveFlow) { |
| 236 | newSelectedFlows.push(liveFlow); |
| 237 | } else if (flow.pending) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 238 | newSelectedFlows.push(flow); |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 239 | } |
| 240 | } else { |
| 241 | newSelectedFlows.push(null); |
| 242 | } |
| 243 | }); |
| 244 | selectedFlows = newSelectedFlows; |
| 245 | } |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 246 | while (selectedFlows.length < 3) { |
| 247 | selectedFlows.push(null); |
| 248 | } |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 249 | |
| 250 | updateSelectedFlowsTable(); |
| 251 | updateSelectedFlowsTopology(); |
| 252 | } |
| 253 | |
| 254 | function selectFlow(flow) { |
Paul Greyson | c30f75e | 2013-03-28 11:45:15 -0700 | [diff] [blame] | 255 | var flowKey = makeFlowKey(flow); |
| 256 | var alreadySelected = false; |
| 257 | selectedFlows.forEach(function (f) { |
| 258 | if (f && makeFlowKey(f) === flowKey) { |
| 259 | alreadySelected = true; |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | if (!alreadySelected) { |
| 264 | selectedFlows.unshift(flow); |
| 265 | selectedFlows = selectedFlows.slice(0, 3); |
| 266 | updateSelectedFlows(); |
| 267 | } |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame^] | 270 | function deselectFlow(flow, ifPending) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 271 | var flowKey = makeFlowKey(flow); |
| 272 | var newSelectedFlows = []; |
| 273 | selectedFlows.forEach(function (flow) { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame^] | 274 | if (!flow || |
| 275 | flowKey !== makeFlowKey(flow) || |
| 276 | flowKey === makeFlowKey(flow) && ifPending && !flow.pending ) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 277 | newSelectedFlows.push(flow); |
| 278 | } |
| 279 | }); |
| 280 | selectedFlows = newSelectedFlows; |
| 281 | while (selectedFlows.length < 3) { |
| 282 | selectedFlows.push(null); |
| 283 | } |
| 284 | |
| 285 | updateSelectedFlows(); |
| 286 | } |
| 287 | |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame^] | 288 | function deselectFlowIfPending(flow) { |
| 289 | deselectFlow(flow, true); |
| 290 | } |
| 291 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 292 | function showFlowChooser() { |
| 293 | function rowEnter(d) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 294 | var row = d3.select(this); |
| 295 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 296 | row.append('div') |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 297 | .classed('black-eye', true). |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 298 | on('click', function () { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 299 | selectFlow(d); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 300 | }); |
| 301 | |
| 302 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 303 | .classed('flowId', true) |
| 304 | .text(function (d) { |
| 305 | return d.flowId.value; |
| 306 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 307 | |
| 308 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 309 | .classed('srcDPID', true) |
| 310 | .text(function (d) { |
| 311 | return d.dataPath.srcPort.dpid.value; |
| 312 | }); |
| 313 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 314 | |
| 315 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 316 | .classed('dstDPID', true) |
| 317 | .text(function (d) { |
| 318 | return d.dataPath.dstPort.dpid.value; |
| 319 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 320 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 323 | var flows = d3.select('#flowChooser') |
| 324 | .append('div') |
| 325 | .style('pointer-events', 'auto') |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 326 | .selectAll('.selectedFlow') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 327 | .data(model.flows) |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 328 | .enter() |
| 329 | .append('div') |
| 330 | .classed('selectedFlow', true) |
| 331 | .each(rowEnter); |
| 332 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 333 | setTimeout(function () { |
| 334 | d3.select(document.body).on('click', function () { |
| 335 | d3.select('#flowChooser').html(''); |
| 336 | d3.select(document.body).on('click', null); |
| 337 | }); |
| 338 | }, 0); |
| 339 | } |
| 340 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 341 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 342 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 343 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 344 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 345 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 346 | d3.select('#activeFlows').text(model.flows.length); |
| 347 | } |
| 348 | |
| 349 | function toRadians (angle) { |
| 350 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 353 | var widths = { |
| 354 | edge: 6, |
| 355 | aggregation: 12, |
| 356 | core: 18 |
| 357 | } |
| 358 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 359 | function createRingsFromModel(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 360 | var rings = [{ |
| 361 | radius: 3, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 362 | width: widths.edge, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 363 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 364 | className: 'edge', |
| 365 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 366 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 367 | radius: 2.25, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 368 | width: widths.aggregation, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 369 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 370 | className: 'aggregation', |
| 371 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 372 | }, { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 373 | radius: 0.75, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 374 | width: widths.core, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 375 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 376 | className: 'core', |
| 377 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 378 | }]; |
| 379 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 380 | |
| 381 | var aggRanges = {}; |
| 382 | |
| 383 | // arrange edge switches at equal increments |
| 384 | var k = 360 / rings[0].switches.length; |
| 385 | rings[0].switches.forEach(function (s, i) { |
| 386 | var angle = k * i; |
| 387 | |
| 388 | rings[0].angles[i] = angle; |
| 389 | |
| 390 | // record the angle for the agg switch layout |
| 391 | var dpid = s.dpid.split(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 392 | 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] | 393 | var aggdpid = dpid.join(':'); |
| 394 | var aggRange = aggRanges[aggdpid]; |
| 395 | if (!aggRange) { |
| 396 | aggRange = aggRanges[aggdpid] = {}; |
| 397 | aggRange.min = aggRange.max = angle; |
| 398 | } else { |
| 399 | aggRange.max = angle; |
| 400 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 401 | }); |
| 402 | |
| 403 | // arrange aggregation switches to "fan out" to edge switches |
| 404 | k = 360 / rings[1].switches.length; |
| 405 | rings[1].switches.forEach(function (s, i) { |
| 406 | // rings[1].angles[i] = k * i; |
| 407 | var range = aggRanges[s.dpid]; |
| 408 | |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 409 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 410 | }); |
| 411 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 412 | // find the association between core switches and aggregation switches |
| 413 | var aggregationSwitchMap = {}; |
| 414 | model.aggregationSwitches.forEach(function (s, i) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 415 | aggregationSwitchMap[s.dpid] = i; |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 416 | }); |
| 417 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 418 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 419 | k = 360 / rings[2].switches.length; |
| 420 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 421 | // rings[2].angles[i] = k * i; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 422 | var associatedAggregationSwitches = model.configuration.association[s.dpid]; |
| 423 | // TODO: go between if there are multiple |
| 424 | var index = aggregationSwitchMap[associatedAggregationSwitches[0]]; |
| 425 | |
| 426 | rings[2].angles[i] = rings[1].angles[index]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 427 | }); |
| 428 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 429 | // TODO: construct this form initially rather than converting. it works better because |
| 430 | // it allows binding by dpid |
| 431 | var testRings = []; |
| 432 | rings.forEach(function (ring) { |
| 433 | var testRing = []; |
| 434 | ring.switches.forEach(function (s, i) { |
| 435 | var testSwitch = { |
| 436 | dpid: s.dpid, |
| 437 | state: s.state, |
| 438 | radius: ring.radius, |
| 439 | width: ring.width, |
| 440 | className: ring.className, |
| 441 | angle: ring.angles[i], |
| 442 | controller: s.controller |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 443 | }; |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 444 | testRing.push(testSwitch); |
| 445 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 446 | |
| 447 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 448 | testRings.push(testRing); |
| 449 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 450 | |
| 451 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 452 | // return rings; |
| 453 | return testRings; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 456 | function makeLinkKey(link) { |
| 457 | return link['src-switch'] + '=>' + link['dst-switch']; |
| 458 | } |
| 459 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 460 | function makeFlowKey(flow) { |
| 461 | return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value; |
| 462 | } |
| 463 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 464 | function createLinkMap(links) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 465 | var linkMap = {}; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 466 | links.forEach(function (link) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 467 | var srcDPID = link['src-switch']; |
| 468 | var dstDPID = link['dst-switch']; |
| 469 | |
| 470 | var srcMap = linkMap[srcDPID] || {}; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 471 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 472 | srcMap[dstDPID] = link; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 473 | |
| 474 | linkMap[srcDPID] = srcMap; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 475 | }); |
| 476 | return linkMap; |
| 477 | } |
| 478 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 479 | // removes links from the pending list that are now in the model |
| 480 | function reconcilePendingLinks(model) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 481 | var links = []; |
| 482 | model.links.forEach(function (link) { |
| 483 | links.push(link); |
| 484 | delete pendingLinks[makeLinkKey(link)] |
| 485 | }) |
| 486 | var linkId; |
| 487 | for (linkId in pendingLinks) { |
| 488 | links.push(pendingLinks[linkId]); |
| 489 | } |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 490 | return links |
| 491 | } |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 492 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 493 | updateTopology = function() { |
| 494 | |
| 495 | // DRAW THE SWITCHES |
| 496 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 497 | |
| 498 | var links = reconcilePendingLinks(model); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 499 | var linkMap = createLinkMap(links); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 500 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 501 | function mouseOverSwitch(data) { |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 502 | |
| 503 | d3.event.preventDefault(); |
| 504 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 505 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false); |
| 506 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 507 | if (data.highlighted) { |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // only highlight valid link or flow destination by checking for class of existing highlighted circle |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 512 | var highlighted = svg.selectAll('.highlight')[0]; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 513 | if (highlighted.length == 1) { |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 514 | var s = d3.select(highlighted[0]).select('circle'); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 515 | // only allow links |
| 516 | // edge->edge (flow) |
| 517 | // aggregation->core |
| 518 | // core->core |
| 519 | if (data.className == 'edge' && !s.classed('edge') || |
| 520 | data.className == 'core' && !s.classed('core') && !s.classed('aggregation') || |
| 521 | data.className == 'aggregation' && !s.classed('core')) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | // don't highlight if there's already a link or flow |
| 526 | // var map = linkMap[data.dpid]; |
| 527 | // console.log(map); |
| 528 | // console.log(s.data()[0].dpid); |
| 529 | // console.log(map[s.data()[0].dpid]); |
| 530 | // if (map && map[s.data()[0].dpid]) { |
| 531 | // return; |
| 532 | // } |
| 533 | |
| 534 | // the second highlighted switch is the target for a link or flow |
| 535 | data.target = true; |
| 536 | } |
| 537 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 538 | var node = d3.select(document.getElementById(data.dpid)); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 539 | node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 540 | data.highlighted = true; |
| 541 | node.moveToFront(); |
| 542 | } |
| 543 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 544 | function mouseOutSwitch(data) { |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 545 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true); |
| 546 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 547 | if (data.mouseDown) |
| 548 | return; |
| 549 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 550 | var node = d3.select(document.getElementById(data.dpid)); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 551 | node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 552 | data.highlighted = false; |
| 553 | data.target = false; |
| 554 | } |
| 555 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 556 | function mouseDownSwitch(data) { |
| 557 | mouseOverSwitch(data); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 558 | data.mouseDown = true; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 559 | d3.select('#topology').classed('linking', true); |
| 560 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 561 | d3.select('svg') |
| 562 | .append('svg:path') |
| 563 | .attr('id', 'linkVector') |
| 564 | .attr('d', function () { |
| 565 | var s = d3.select(document.getElementById(data.dpid)); |
| 566 | |
| 567 | var pt = document.querySelector('svg').createSVGPoint(); |
| 568 | pt.x = s.attr('x'); |
| 569 | pt.y = s.attr('y'); |
| 570 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 571 | |
| 572 | return line([pt, pt]); |
| 573 | }); |
| 574 | |
| 575 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 576 | if (data.className === 'core') { |
| 577 | d3.selectAll('.edge').classed('nodrop', true); |
| 578 | } |
| 579 | if (data.className === 'edge') { |
| 580 | d3.selectAll('.core').classed('nodrop', true); |
| 581 | d3.selectAll('.aggregation').classed('nodrop', true); |
| 582 | } |
| 583 | if (data.className === 'aggregation') { |
| 584 | d3.selectAll('.edge').classed('nodrop', true); |
| 585 | d3.selectAll('.aggregation').classed('nodrop', true); |
| 586 | } |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 589 | function mouseUpSwitch(data) { |
| 590 | if (data.mouseDown) { |
| 591 | data.mouseDown = false; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 592 | d3.select('#topology').classed('linking', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 593 | d3.event.stopPropagation(); |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 594 | d3.selectAll('.nodrop').classed('nodrop', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | function doubleClickSwitch(data) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 599 | var circle = d3.select(document.getElementById(data.dpid)).select('circle'); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 600 | if (data.state == 'ACTIVE') { |
| 601 | var prompt = 'Deactivate ' + data.dpid + '?'; |
| 602 | if (confirm(prompt)) { |
| 603 | switchDown(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 604 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 605 | } |
| 606 | } else { |
| 607 | var prompt = 'Activate ' + data.dpid + '?'; |
| 608 | if (confirm(prompt)) { |
| 609 | switchUp(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 610 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 615 | function ringEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 616 | if (!data.length) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 617 | return; |
| 618 | } |
| 619 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 620 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 621 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 622 | .data(data, function (data) { |
| 623 | return data.dpid; |
| 624 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 625 | .enter().append("svg:g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 626 | .attr("id", function (data, i) { |
| 627 | return data.dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 628 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 629 | .attr("transform", function(data, i) { |
| 630 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 631 | }); |
| 632 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 633 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 634 | nodes.append("svg:circle") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 635 | .attr("transform", function(data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 636 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 637 | if (data.scale) { |
| 638 | m = m.scale(data.scale); |
| 639 | } |
| 640 | 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] | 641 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 642 | .attr("x", function (data) { |
| 643 | return -data.width / 2; |
| 644 | }) |
| 645 | .attr("y", function (data) { |
| 646 | return -data.width / 2; |
| 647 | }) |
| 648 | .attr("r", function (data) { |
| 649 | return data.width; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 650 | }); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 651 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 652 | // setup the mouseover behaviors |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 653 | nodes.on('mouseover', mouseOverSwitch); |
| 654 | nodes.on('mouseout', mouseOutSwitch); |
| 655 | nodes.on('mouseup', mouseUpSwitch); |
| 656 | nodes.on('mousedown', mouseDownSwitch); |
| 657 | |
| 658 | // only do switch up/down for core switches |
| 659 | if (i == 2) { |
| 660 | nodes.on('dblclick', doubleClickSwitch); |
| 661 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 664 | // append switches |
| 665 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 666 | .attr("class", "ring") |
| 667 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 668 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 669 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 670 | function ringUpdate(data, i) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 671 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 672 | .data(data, function (data) { |
| 673 | return data.dpid; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 674 | }); |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 675 | nodes.select('circle') |
| 676 | .each(function (data) { |
| 677 | // if there's a pending state changed and then the state changes, clear the pending class |
| 678 | var circle = d3.select(this); |
| 679 | if (data.state === 'ACTIVE' && circle.classed('inactive') || |
| 680 | data.state === 'INACTIVE' && circle.classed('active')) { |
| 681 | circle.classed('pending', false); |
| 682 | } |
| 683 | }) |
| 684 | .attr('class', function (data) { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 685 | if (data.state === 'ACTIVE' && data.controller) { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 686 | return data.className + ' active ' + controllerColorMap[data.controller]; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 687 | } else { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 688 | return data.className + ' inactive ' + 'colorInactive'; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 689 | } |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 690 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | // update switches |
| 694 | rings.each(ringUpdate); |
| 695 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 696 | |
| 697 | // Now setup the labels |
| 698 | // This is done separately because SVG draws in node order and we want the labels |
| 699 | // always on top |
| 700 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 701 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 702 | d3.select(document.body).on('mousemove', function () { |
| 703 | if (!d3.select('#topology').classed('linking')) { |
| 704 | return; |
| 705 | } |
| 706 | var linkVector = document.getElementById('linkVector'); |
| 707 | if (!linkVector) { |
| 708 | return; |
| 709 | } |
| 710 | linkVector = d3.select(linkVector); |
| 711 | |
| 712 | var highlighted = svg.selectAll('.highlight')[0]; |
| 713 | var s1 = null, s2 = null; |
| 714 | if (highlighted.length > 1) { |
| 715 | var s1 = d3.select(highlighted[0]); |
| 716 | var s2 = d3.select(highlighted[1]); |
| 717 | |
| 718 | } else if (highlighted.length > 0) { |
| 719 | var s1 = d3.select(highlighted[0]); |
| 720 | } |
| 721 | var src = s1; |
| 722 | if (s2 && !s2.data()[0].target) { |
| 723 | src = s2; |
| 724 | } |
| 725 | if (src) { |
| 726 | linkVector.attr('d', function () { |
| 727 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 728 | srcPt.x = src.attr('x'); |
| 729 | srcPt.y = src.attr('y'); |
| 730 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
| 731 | |
| 732 | var svg = document.getElementById('topology'); |
| 733 | var mouse = d3.mouse(viewbox); |
| 734 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 735 | dstPt.x = mouse[0]; |
| 736 | dstPt.y = mouse[1]; |
| 737 | dstPt = dstPt.matrixTransform(viewbox.getCTM()); |
| 738 | |
| 739 | return line([srcPt, dstPt]); |
| 740 | }); |
| 741 | } |
| 742 | }); |
| 743 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 744 | d3.select(document.body).on('mouseup', function () { |
| 745 | function clearHighlight() { |
| 746 | svg.selectAll('circle').each(function (data) { |
| 747 | data.mouseDown = false; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 748 | d3.select('#topology').classed('linking', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 749 | mouseOutSwitch(data); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 750 | }); |
| 751 | d3.select('#linkVector').remove(); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 752 | }; |
| 753 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 754 | d3.selectAll('.nodrop').classed('nodrop', false); |
| 755 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 756 | function removeLink(link) { |
| 757 | var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']); |
| 758 | var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']); |
| 759 | |
| 760 | if (path1) { |
| 761 | setPending(d3.select(path1)); |
| 762 | } |
| 763 | if (path2) { |
| 764 | setPending(d3.select(path2)); |
| 765 | } |
| 766 | |
| 767 | linkDown(link); |
| 768 | } |
| 769 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 770 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 771 | var highlighted = svg.selectAll('.highlight')[0]; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 772 | if (highlighted.length == 2) { |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 773 | var s1Data = highlighted[0].__data__; |
| 774 | var s2Data = highlighted[1].__data__; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 775 | |
| 776 | var srcData, dstData; |
| 777 | if (s1Data.target) { |
| 778 | dstData = s1Data; |
| 779 | srcData = s2Data; |
| 780 | } else { |
| 781 | dstData = s2Data; |
| 782 | srcData = s1Data; |
| 783 | } |
| 784 | |
| 785 | if (s1Data.className == 'edge' && s2Data.className == 'edge') { |
| 786 | var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?'; |
| 787 | if (confirm(prompt)) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 788 | addFlow(srcData, dstData); |
| 789 | |
| 790 | var flow = { |
| 791 | dataPath: { |
| 792 | srcPort: { |
| 793 | dpid: { |
| 794 | value: srcData.dpid |
| 795 | } |
| 796 | }, |
| 797 | dstPort: { |
| 798 | dpid: { |
| 799 | value: dstData.dpid |
| 800 | } |
| 801 | } |
| 802 | }, |
| 803 | pending: true |
| 804 | }; |
| 805 | |
| 806 | selectFlow(flow); |
| 807 | |
| 808 | setTimeout(function () { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame^] | 809 | deselectFlowIfPending(flow); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 810 | }, pendingTimeout); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 811 | } |
| 812 | } else { |
| 813 | var map = linkMap[srcData.dpid]; |
| 814 | if (map && map[dstData.dpid]) { |
| 815 | var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 816 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 817 | removeLink(map[dstData.dpid]); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 818 | } |
| 819 | } else { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 820 | map = linkMap[dstData.dpid]; |
| 821 | if (map && map[srcData.dpid]) { |
| 822 | var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?'; |
| 823 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 824 | removeLink(map[srcData.dpid]); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 825 | } |
| 826 | } else { |
| 827 | var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 828 | if (confirm(prompt)) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 829 | var link1 = { |
| 830 | 'src-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 831 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 832 | 'dst-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 833 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 834 | pending: true |
| 835 | }; |
| 836 | pendingLinks[makeLinkKey(link1)] = link1; |
| 837 | var link2 = { |
| 838 | 'src-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 839 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 840 | 'dst-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 841 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 842 | pending: true |
| 843 | }; |
| 844 | pendingLinks[makeLinkKey(link2)] = link2; |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 845 | updateTopology(); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 846 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 847 | linkUp(link1); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 848 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 849 | // remove the pending links after 10s |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 850 | setTimeout(function () { |
| 851 | delete pendingLinks[makeLinkKey(link1)]; |
| 852 | delete pendingLinks[makeLinkKey(link2)]; |
| 853 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 854 | updateTopology(); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 855 | }, pendingTimeout); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 856 | } |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | clearHighlight(); |
| 862 | } else { |
| 863 | clearHighlight(); |
| 864 | } |
| 865 | |
| 866 | }); |
| 867 | |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 868 | function labelRingEnter(data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 869 | if (!data.length) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | // create the nodes |
| 874 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 875 | .data(data, function (data) { |
| 876 | return data.dpid; |
| 877 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 878 | .enter().append("svg:g") |
| 879 | .classed('nolabel', true) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 880 | .attr("id", function (data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 881 | return data.dpid + '-label'; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 882 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 883 | .attr("transform", function(data, i) { |
| 884 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 885 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 886 | |
| 887 | // add the text nodes which show on mouse over |
| 888 | nodes.append("svg:text") |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 889 | .text(function (data) {return data.dpid;}) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 890 | .attr("x", function (data) { |
| 891 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 892 | if (data.className == 'edge') { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 893 | return - data.width*3 - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 894 | } else { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 895 | return - data.width - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 896 | } |
| 897 | } else { |
| 898 | if (data.className == 'edge') { |
| 899 | return data.width*3 + 4; |
| 900 | } else { |
| 901 | return data.width + 4; |
| 902 | } |
| 903 | } |
| 904 | }) |
| 905 | .attr("y", function (data) { |
| 906 | var y; |
| 907 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 908 | if (data.className == 'edge') { |
| 909 | y = data.width*3/2 + 4; |
| 910 | } else { |
| 911 | y = data.width/2 + 4; |
| 912 | } |
| 913 | } else { |
| 914 | if (data.className == 'edge') { |
| 915 | y = data.width*3/2 + 4; |
| 916 | } else { |
| 917 | y = data.width/2 + 4; |
| 918 | } |
| 919 | } |
| 920 | return y - 6; |
| 921 | }) |
| 922 | .attr("text-anchor", function (data) { |
| 923 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 924 | return "end"; |
| 925 | } else { |
| 926 | return "start"; |
| 927 | } |
| 928 | }) |
| 929 | .attr("transform", function(data) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 930 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 931 | if (data.scale) { |
| 932 | m = m.scale(data.scale); |
| 933 | } |
| 934 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 935 | }) |
| 936 | } |
| 937 | |
| 938 | labelRings.enter().append("svg:g") |
| 939 | .attr("class", "textRing") |
| 940 | .each(labelRingEnter); |
| 941 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 942 | // switches should not change during operation of the ui so no |
| 943 | // rings.exit() |
| 944 | |
| 945 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 946 | // DRAW THE LINKS |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 947 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 948 | // key on link dpids since these will come/go during demo |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 949 | var links = d3.select('svg').selectAll('.link').data(links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 950 | return d['src-switch']+'->'+d['dst-switch']; |
| 951 | }); |
| 952 | |
| 953 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 954 | links.enter().append("svg:path") |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 955 | .attr("class", "link"); |
| 956 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 957 | links.attr('id', function (d) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 958 | return makeLinkKey(d); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 959 | }) |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 960 | .attr("d", function (d) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 961 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 962 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 963 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 964 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 965 | srcPt.x = src.attr('x'); |
| 966 | srcPt.y = src.attr('y'); |
| 967 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 968 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 969 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 970 | dstPt.x = dst.attr('x'); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 971 | dstPt.y = dst.attr('y'); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 972 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 973 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 974 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 975 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 976 | midPt.y = (srcPt.y + dstPt.y)/2; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 977 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 978 | return line([srcPt, midPt, dstPt]); |
| 979 | }) |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 980 | .attr("marker-mid", function(d) { return "url(#arrow)"; }) |
| 981 | .classed('pending', function (d) { |
| 982 | return d.pending; |
| 983 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 984 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 985 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 986 | // remove old links |
| 987 | links.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 990 | function updateControllers() { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 991 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 992 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 993 | .each(function (c) { |
| 994 | controllerColorMap[c] = colors.pop(); |
| 995 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 996 | }) |
| 997 | .text(function (d) { |
| 998 | return d; |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 999 | }) |
| 1000 | .append('div') |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 1001 | .attr('class', 'black-eye'); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1002 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 1003 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 1004 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1005 | if (model.activeControllers.indexOf(d) != -1) { |
| 1006 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1007 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1008 | var className = 'controller ' + color; |
| 1009 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1010 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1011 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 1012 | // this should never be needed |
| 1013 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1014 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 1015 | controllers.on('dblclick', function (c) { |
| 1016 | if (model.activeControllers.indexOf(c) != -1) { |
| 1017 | var prompt = 'Dectivate ' + c + '?'; |
| 1018 | if (confirm(prompt)) { |
| 1019 | controllerDown(c); |
| 1020 | setPending(d3.select(this)); |
| 1021 | }; |
| 1022 | } else { |
| 1023 | var prompt = 'Activate ' + c + '?'; |
| 1024 | if (confirm(prompt)) { |
| 1025 | controllerUp(c); |
| 1026 | setPending(d3.select(this)); |
| 1027 | }; |
| 1028 | } |
| 1029 | }); |
| 1030 | |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 1031 | controllers.select('.black-eye').on('click', function (c) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 1032 | var allSelected = true; |
| 1033 | for (var key in controllerColorMap) { |
| 1034 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 1035 | allSelected = false; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | if (allSelected) { |
| 1040 | for (var key in controllerColorMap) { |
| 1041 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 1042 | } |
| 1043 | } else { |
| 1044 | for (var key in controllerColorMap) { |
| 1045 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 1050 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1051 | }); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 1052 | |
| 1053 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1056 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1057 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1058 | updateModel(function (newModel) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 1059 | // console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1060 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 1061 | var modelChanged = false; |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 1062 | if (!model || JSON.stringify(model) != JSON.stringify(newModel)) { |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 1063 | modelChanged = true; |
| 1064 | model = newModel; |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1065 | } else { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 1066 | // console.log('no change'); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1067 | } |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1068 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 1069 | if (modelChanged) { |
| 1070 | updateControllers(); |
| 1071 | updateSelectedFlows(); |
| 1072 | updateTopology(); |
| 1073 | } |
| 1074 | |
| 1075 | updateHeader(newModel); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1076 | |
| 1077 | // do it again in 1s |
| 1078 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 1079 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1080 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 1081 | }); |
| 1082 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1083 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1084 | svg = createTopologyView(); |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1085 | updateSelectedFlows(); |
| 1086 | |
| 1087 | d3.select('#showFlowChooser').on('click', function () { |
| 1088 | showFlowChooser(); |
| 1089 | }); |
| 1090 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 1091 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1092 | // workaround for Chrome v25 bug |
| 1093 | // if executed immediately, the view box transform logic doesn't work properly |
| 1094 | // fixed in Chrome v27 |
| 1095 | setTimeout(function () { |
| 1096 | // workaround for another Chrome v25 bug |
| 1097 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 1098 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1099 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1100 | sync(svg); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1101 | }, 100); |