blob: 8cb50efa1f549c5b579b88fe856b7d4845cfecbe [file] [log] [blame]
Paul Greyson127d7fb2013-03-25 23:39:20 -07001/*global d3, document∆*/
Paul Greyson740bdaf2013-03-18 16:10:48 -07002
Paul Greyson6d9ed862013-03-23 17:37:15 -07003d3.selection.prototype.moveToFront = function() {
4 return this.each(function(){
5 this.parentNode.appendChild(this);
6 });
7};
Paul Greysond1a22d92013-03-19 12:15:19 -07008
Paul Greyson127d7fb2013-03-25 23:39:20 -07009var line = d3.svg.line()
10 .x(function(d) {
11 return d.x;
12 })
13 .y(function(d) {
14 return d.y;
15 });
16
Paul Greyson56378ed2013-03-26 23:17:36 -070017var model;
Paul Greyson127d7fb2013-03-25 23:39:20 -070018var svg, selectedFlowsView;
Paul Greyson56378ed2013-03-26 23:17:36 -070019var updateTopology;
Paul Greyson127d7fb2013-03-25 23:39:20 -070020
Paul Greysond1a22d92013-03-19 12:15:19 -070021var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -070022 'color1',
23 'color2',
24 'color3',
25 'color4',
26 'color5',
27 'color6',
28 'color7',
29 'color8',
30 'color9',
31 'color10',
32 'color11',
Paul Greyson127d7fb2013-03-25 23:39:20 -070033 'color12'
34];
Paul Greyson01a5dff2013-03-19 15:50:14 -070035colors.reverse();
Paul Greysond1a22d92013-03-19 12:15:19 -070036
37var controllerColorMap = {};
38
39
40
Paul Greyson740bdaf2013-03-18 16:10:48 -070041function createTopologyView() {
Paul Greyson56378ed2013-03-26 23:17:36 -070042
43 window.addEventListener('resize', function () {
44 // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied
45 // either that or is it possible to position the paths so they get the automatic transform as well?
46// updateTopology(svg, model);
47 });
48
Paul Greysonb367de22013-03-23 11:09:11 -070049 var svg = d3.select('#svg-container').append('svg:svg');
50
51 svg.append("svg:defs").append("svg:marker")
52 .attr("id", "arrow")
53 .attr("viewBox", "0 -5 10 10")
54 .attr("refX", -1)
55 .attr("markerWidth", 5)
56 .attr("markerHeight", 5)
57 .attr("orient", "auto")
58 .append("svg:path")
Paul Greyson45303ac2013-03-23 16:44:01 -070059 .attr("d", "M0,-3L10,0L0,3");
Paul Greysonb367de22013-03-23 11:09:11 -070060
61 return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070062 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070063}
64
Paul Greyson127d7fb2013-03-25 23:39:20 -070065var selectedFlowsData = [
66 {selected: false, flow: null},
67 {selected: false, flow: null},
Paul Greyson127d7fb2013-03-25 23:39:20 -070068 {selected: false, flow: null}
69];
70
71function drawFlows() {
72 // DRAW THE FLOWS
73 var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) {
74 return d.flow ? d.flow.flowId.value : null;
75 });
76
77 flows.enter().append("svg:path")
78 .attr('class', 'flow')
79 .attr('d', function (d) {
80 if (!d.flow) {
81 return;
82 }
83 var pts = [];
84 d.flow.dataPath.flowEntries.forEach(function (flowEntry) {
85 var s = d3.select(document.getElementById(flowEntry.dpid.value));
86 var pt = document.querySelector('svg').createSVGPoint();
87 pt.x = s.attr('x');
88 pt.y = s.attr('y');
89 pt = pt.matrixTransform(s[0][0].getCTM());
90 pts.push(pt);
91 });
92 return line(pts);
93 })
Paul Greysonacb59412013-03-25 23:48:06 -070094 .attr('stroke-dasharray', '3, 10')
Paul Greyson127d7fb2013-03-25 23:39:20 -070095 .append('svg:animate')
96 .attr('attributeName', 'stroke-dashoffset')
97 .attr('attributeType', 'xml')
98 .attr('from', '500')
99 .attr('to', '-500')
100 .attr('dur', '20s')
101 .attr('repeatCount', 'indefinite');
102
103 flows.style('visibility', function (d) {
104 if (d) {
105 return d.selected ? '' : 'hidden';
106 }
107 })
108
Paul Greyson56378ed2013-03-26 23:17:36 -0700109 // "marching ants"
110 // TODO: this will only be true if there's an iperf session running
Paul Greyson127d7fb2013-03-25 23:39:20 -0700111 flows.select('animate').attr('from', function (d) {
112 if (d.flow) {
113 if (d.selected) {
114 return '500';
115 } else {
116 return '-500';
117 }
118 }
119 });
120}
121
122function updateFlowView() {
123 selectedFlowsView.data(selectedFlowsData);
124
125 selectedFlowsView.classed('selected', function (d) {
126 if (d.flow) {
127 return d.selected;
128 }
129 });
130
131 selectedFlowsView.select('.flowId')
132 .text(function (d) {
133 if (d.flow) {
134 return d.flow.flowId.value;
135 }
136 });
137
138 selectedFlowsView.select('.srcDPID')
139 .text(function (d) {
140 if (d.flow) {
141 return d.flow.dataPath.srcPort.dpid.value;
142 }
143 });
144
145 selectedFlowsView.select('.dstDPID')
146 .text(function (d) {
147 if (d.flow) {
148 return d.flow.dataPath.dstPort.dpid.value;
149 }
150 });
151}
152
153function createFlowView() {
154 function rowEnter(d, i) {
155 var row = d3.select(this);
156
157 row.on('click', function () {
158 selectedFlowsData[i].selected = !selectedFlowsData[i].selected;
159 updateFlowView();
160 drawFlows();
161 });
162
163 row.append('div')
164 .classed('flowIndex', true)
165 .text(function () {
166 return i+1;
167 });
168
169 row.append('div')
170 .classed('flowId', true);
171
172 row.append('div')
173 .classed('srcDPID', true);
174
175 row.append('div')
176 .classed('dstDPID', true);
177
178 row.append('div')
179 .classed('iperf', true);
180 }
181
182 var flows = d3.select('#selectedFlows')
183 .selectAll('.selectedFlow')
184 .data(selectedFlowsData)
185 .enter()
186 .append('div')
187 .classed('selectedFlow', true)
188 .each(rowEnter);
189
190
191 return flows;
192}
193
Paul Greysond1a22d92013-03-19 12:15:19 -0700194function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700195 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700196 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
197 d3.select('#activeFlows').text(model.flows.length);
198}
199
200function toRadians (angle) {
201 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700202}
203
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700204var widths = {
205 edge: 6,
206 aggregation: 12,
207 core: 18
208}
209
Paul Greysonc17278a2013-03-23 10:17:12 -0700210function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700211 var rings = [{
212 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700213 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700214 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700215 className: 'edge',
216 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700217 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700218 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700219 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700220 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700221 className: 'aggregation',
222 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700223 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700224 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700225 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700226 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700227 className: 'core',
228 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700229 }];
230
Paul Greysonde7fad52013-03-19 12:47:32 -0700231
232 var aggRanges = {};
233
234 // arrange edge switches at equal increments
235 var k = 360 / rings[0].switches.length;
236 rings[0].switches.forEach(function (s, i) {
237 var angle = k * i;
238
239 rings[0].angles[i] = angle;
240
241 // record the angle for the agg switch layout
242 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700243 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700244 var aggdpid = dpid.join(':');
245 var aggRange = aggRanges[aggdpid];
246 if (!aggRange) {
247 aggRange = aggRanges[aggdpid] = {};
248 aggRange.min = aggRange.max = angle;
249 } else {
250 aggRange.max = angle;
251 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700252 });
253
254 // arrange aggregation switches to "fan out" to edge switches
255 k = 360 / rings[1].switches.length;
256 rings[1].switches.forEach(function (s, i) {
257// rings[1].angles[i] = k * i;
258 var range = aggRanges[s.dpid];
259
Paul Greyson832d2202013-03-21 13:27:56 -0700260 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700261 });
262
Paul Greyson3f890b62013-03-22 17:39:36 -0700263 // find the association between core switches and aggregation switches
264 var aggregationSwitchMap = {};
265 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700266 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700267 });
268
Paul Greyson3f890b62013-03-22 17:39:36 -0700269 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700270 k = 360 / rings[2].switches.length;
271 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700272// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700273 var associatedAggregationSwitches = model.configuration.association[s.dpid];
274 // TODO: go between if there are multiple
275 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
276
277 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700278 });
279
Paul Greyson644d92a2013-03-23 18:00:40 -0700280 // TODO: construct this form initially rather than converting. it works better because
281 // it allows binding by dpid
282 var testRings = [];
283 rings.forEach(function (ring) {
284 var testRing = [];
285 ring.switches.forEach(function (s, i) {
286 var testSwitch = {
287 dpid: s.dpid,
288 state: s.state,
289 radius: ring.radius,
290 width: ring.width,
291 className: ring.className,
292 angle: ring.angles[i],
293 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700294 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700295 testRing.push(testSwitch);
296 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700297
298
Paul Greyson644d92a2013-03-23 18:00:40 -0700299 testRings.push(testRing);
300 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700301
302
Paul Greyson644d92a2013-03-23 18:00:40 -0700303// return rings;
304 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700305}
306
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700307function createLinkMap(model) {
308 var linkMap = {};
309 model.links.forEach(function (link) {
310 var srcDPID = link['src-switch'];
311 var dstDPID = link['dst-switch'];
312
313 var srcMap = linkMap[srcDPID] || {};
314 var dstMap = linkMap[dstDPID] || {};
315
316 srcMap[dstDPID] = true;
317 dstMap[srcDPID] = true;
318
319 linkMap[srcDPID] = srcMap;
320 linkMap[dstDPID] = dstMap;
321 });
322 return linkMap;
323}
324
Paul Greyson56378ed2013-03-26 23:17:36 -0700325updateTopology = function(svg, model) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700326
327 // DRAW THE SWITCHES
328 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
329
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700330 var linkMap = createLinkMap(model);
331// var flowMap = createFlowMap(model);
332
333 function mouseOver(data) {
334 if (data.highlighted) {
335 return;
336 }
337
338 // only highlight valid link or flow destination by checking for class of existing highlighted circle
339 var highlighted = svg.selectAll('circle.highlight')[0];
340 if (highlighted.length == 1) {
341 var s = d3.select(highlighted[0]);
342 // only allow links
343 // edge->edge (flow)
344 // aggregation->core
345 // core->core
346 if (data.className == 'edge' && !s.classed('edge') ||
347 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
348 data.className == 'aggregation' && !s.classed('core')) {
349 return;
350 }
351
352 // don't highlight if there's already a link or flow
353 // var map = linkMap[data.dpid];
354 // console.log(map);
355 // console.log(s.data()[0].dpid);
356 // console.log(map[s.data()[0].dpid]);
357 // if (map && map[s.data()[0].dpid]) {
358 // return;
359 // }
360
361 // the second highlighted switch is the target for a link or flow
362 data.target = true;
363 }
364
365
366 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
367 var node = d3.select(document.getElementById(data.dpid));
368 node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core);
369 data.highlighted = true;
370 node.moveToFront();
371 }
372
373 function mouseOut(data) {
374 if (data.mouseDown)
375 return;
376
377 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
378 var node = d3.select(document.getElementById(data.dpid));
379 node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]);
380 data.highlighted = false;
381 data.target = false;
382 }
383
384 function mouseDown(data) {
385 mouseOver(data);
386 data.mouseDown = true;
387 }
388
Paul Greyson740bdaf2013-03-18 16:10:48 -0700389 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700390 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700391 return;
392 }
393
Paul Greysonc17278a2013-03-23 10:17:12 -0700394 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700395 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700396 .data(data, function (data) {
397 return data.dpid;
398 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700399 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700400 .attr("id", function (data, i) {
401 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700402 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700403 .attr("transform", function(data, i) {
404 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700405 });
406
Paul Greysonc17278a2013-03-23 10:17:12 -0700407 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700408 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700409 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700410 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700411 if (data.scale) {
412 m = m.scale(data.scale);
413 }
414 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700415 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700416 .attr("x", function (data) {
417 return -data.width / 2;
418 })
419 .attr("y", function (data) {
420 return -data.width / 2;
421 })
422 .attr("r", function (data) {
423 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700424 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700425
Paul Greysonc17278a2013-03-23 10:17:12 -0700426 // setup the mouseover behaviors
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700427 nodes.on('mouseover', mouseOver);
428 nodes.on('mouseout', mouseOut);
429 nodes.on('mousedown', mouseDown)
Paul Greyson740bdaf2013-03-18 16:10:48 -0700430 }
431
Paul Greysonc17278a2013-03-23 10:17:12 -0700432 // append switches
433 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700434 .attr("class", "ring")
435 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700436
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700437
Paul Greysonc17278a2013-03-23 10:17:12 -0700438 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700439 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700440 .data(data, function (data) {
441 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700442 });
Paul Greyson644d92a2013-03-23 18:00:40 -0700443 nodes.select('circle').attr('class', function (data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700444 if (data.state === 'ACTIVE') {
Paul Greyson644d92a2013-03-23 18:00:40 -0700445 return data.className + ' ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700446 } else {
447 return data.className + ' ' + 'colorInactive';
448 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700449 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700450 }
451
452 // update switches
453 rings.each(ringUpdate);
454
Paul Greyson968d1b42013-03-23 16:58:41 -0700455
456 // Now setup the labels
457 // This is done separately because SVG draws in node order and we want the labels
458 // always on top
459 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
460
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700461 d3.select(document.body).on('mouseup', function () {
462 function clearHighlight() {
463 svg.selectAll('circle').each(function (data) {
464 data.mouseDown = false;
465 mouseOut(data);
466 })
467 };
468
469
470 var highlighted = svg.selectAll('circle.highlight')[0];
471 if (highlighted.length == 2) {
472 var s1Data = d3.select(highlighted[0]).data()[0];
473 var s2Data = d3.select(highlighted[1]).data()[0];
474
475 var srcData, dstData;
476 if (s1Data.target) {
477 dstData = s1Data;
478 srcData = s2Data;
479 } else {
480 dstData = s2Data;
481 srcData = s1Data;
482 }
483
484 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
485 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
486 if (confirm(prompt)) {
487 alert('do create flow');
488 } else {
489 alert('do not create flow');
490 }
491 } else {
492 var map = linkMap[srcData.dpid];
493 if (map && map[dstData.dpid]) {
494 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
495 if (confirm(prompt)) {
496 linkDown(srcData, dstData);
497 }
498 } else {
499 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
500 if (confirm(prompt)) {
501 linkUp(srcData, dstData);
502 }
503 }
504 }
505
506 clearHighlight();
507 } else {
508 clearHighlight();
509 }
510
511 });
512
Paul Greyson9066ab02013-03-23 18:15:41 -0700513 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700514 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700515 return;
516 }
517
518 // create the nodes
519 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700520 .data(data, function (data) {
521 return data.dpid;
522 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700523 .enter().append("svg:g")
524 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700525 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700526 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700527 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700528 .attr("transform", function(data, i) {
529 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700530 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700531
532 // add the text nodes which show on mouse over
533 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700534 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700535 .attr("x", function (data) {
536 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
537 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700538 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700539 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700540 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700541 }
542 } else {
543 if (data.className == 'edge') {
544 return data.width*3 + 4;
545 } else {
546 return data.width + 4;
547 }
548 }
549 })
550 .attr("y", function (data) {
551 var y;
552 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
553 if (data.className == 'edge') {
554 y = data.width*3/2 + 4;
555 } else {
556 y = data.width/2 + 4;
557 }
558 } else {
559 if (data.className == 'edge') {
560 y = data.width*3/2 + 4;
561 } else {
562 y = data.width/2 + 4;
563 }
564 }
565 return y - 6;
566 })
567 .attr("text-anchor", function (data) {
568 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
569 return "end";
570 } else {
571 return "start";
572 }
573 })
574 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700575 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
576 if (data.scale) {
577 m = m.scale(data.scale);
578 }
579 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
580 })
581 }
582
583 labelRings.enter().append("svg:g")
584 .attr("class", "textRing")
585 .each(labelRingEnter);
586
Paul Greysonc17278a2013-03-23 10:17:12 -0700587 // switches should not change during operation of the ui so no
588 // rings.exit()
589
590
Paul Greysond1a22d92013-03-19 12:15:19 -0700591 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700592
Paul Greysonc17278a2013-03-23 10:17:12 -0700593 // key on link dpids since these will come/go during demo
Paul Greysonb367de22013-03-23 11:09:11 -0700594 var links = d3.select('svg').selectAll('.link').data(model.links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700595 return d['src-switch']+'->'+d['dst-switch'];
596 });
597
598 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700599 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700600 .attr("class", "link");
601
602 links
603 .attr("d", function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700604
Paul Greysond1a22d92013-03-19 12:15:19 -0700605 var src = d3.select(document.getElementById(d['src-switch']));
606 var dst = d3.select(document.getElementById(d['dst-switch']));
607
608 var srcPt = document.querySelector('svg').createSVGPoint();
609 srcPt.x = src.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700610 srcPt.y = src.attr('y');
611 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700612
613 var dstPt = document.querySelector('svg').createSVGPoint();
614 dstPt.x = dst.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700615 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
616 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700617
Paul Greysonb367de22013-03-23 11:09:11 -0700618 var midPt = document.querySelector('svg').createSVGPoint();
619 midPt.x = (srcPt.x + dstPt.x)/2;
620 midPt.y = (srcPt.y + dstPt.y)/2;
621
622 return line([srcPt, midPt, dstPt]);
623 })
624 .attr("marker-mid", function(d) { return "url(#arrow)"; });
Paul Greysonc17278a2013-03-23 10:17:12 -0700625
Paul Greyson56378ed2013-03-26 23:17:36 -0700626
Paul Greysonc17278a2013-03-23 10:17:12 -0700627 // remove old links
628 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700629
Paul Greyson127d7fb2013-03-25 23:39:20 -0700630
631 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700632}
633
634function updateControllers(model) {
635 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700636 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700637 .each(function (c) {
638 controllerColorMap[c] = colors.pop();
639 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
640 })
641 .text(function (d) {
642 return d;
643 });
Paul Greysonbcd3c772013-03-21 13:16:44 -0700644
Paul Greysone262a292013-03-23 10:35:23 -0700645 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700646 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700647 if (model.activeControllers.indexOf(d) != -1) {
648 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700649 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700650 var className = 'controller ' + color;
651 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700652 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700653
Paul Greysone262a292013-03-23 10:35:23 -0700654 // this should never be needed
655 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700656
Paul Greyson3e142162013-03-19 13:56:17 -0700657 controllers.on('click', function (c, index) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700658 var allSelected = true;
659 for (var key in controllerColorMap) {
660 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
661 allSelected = false;
662 break;
663 }
664 }
665 if (allSelected) {
666 for (var key in controllerColorMap) {
667 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
668 }
669 } else {
670 for (var key in controllerColorMap) {
671 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
672 }
673 }
674
675 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
676 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700677 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700678}
679
Paul Greyson127d7fb2013-03-25 23:39:20 -0700680function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700681 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700682 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700683// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700684
Paul Greyson56378ed2013-03-26 23:17:36 -0700685 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700686 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700687
688 // fake flows right now
689 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700690 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700691 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
692 selectedFlowsData[i].flow = newModel.flows[i];
693 selectedFlowsData[i].selected = selected;
694 }
695
696 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700697 updateTopology(svg, newModel);
698 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700699// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700700 }
701 updateHeader(newModel);
702
Paul Greyson56378ed2013-03-26 23:17:36 -0700703 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700704
705 // do it again in 1s
706 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700707 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700708 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700709 });
710}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700711
Paul Greyson38d8bde2013-03-22 22:07:35 -0700712svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700713selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700714// workaround for Chrome v25 bug
715// if executed immediately, the view box transform logic doesn't work properly
716// fixed in Chrome v27
717setTimeout(function () {
718 // workaround for another Chrome v25 bug
719 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700720 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700721 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700722 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700723}, 100);