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