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