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