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