blob: 92dd37e3dc806f556995ae858091059aaef80024 [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] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700314
Paul Greyson8d1c6362013-03-27 13:05:24 -0700315 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700316
317 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700318 });
319 return linkMap;
320}
321
Paul Greyson56378ed2013-03-26 23:17:36 -0700322updateTopology = function(svg, model) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700323
324 // DRAW THE SWITCHES
325 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
326
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700327 var linkMap = createLinkMap(model);
328// var flowMap = createFlowMap(model);
329
Paul Greyson8d1c6362013-03-27 13:05:24 -0700330 function mouseOverSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700331 if (data.highlighted) {
332 return;
333 }
334
335 // only highlight valid link or flow destination by checking for class of existing highlighted circle
336 var highlighted = svg.selectAll('circle.highlight')[0];
337 if (highlighted.length == 1) {
338 var s = d3.select(highlighted[0]);
339 // only allow links
340 // edge->edge (flow)
341 // aggregation->core
342 // core->core
343 if (data.className == 'edge' && !s.classed('edge') ||
344 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
345 data.className == 'aggregation' && !s.classed('core')) {
346 return;
347 }
348
349 // don't highlight if there's already a link or flow
350 // var map = linkMap[data.dpid];
351 // console.log(map);
352 // console.log(s.data()[0].dpid);
353 // console.log(map[s.data()[0].dpid]);
354 // if (map && map[s.data()[0].dpid]) {
355 // return;
356 // }
357
358 // the second highlighted switch is the target for a link or flow
359 data.target = true;
360 }
361
362
363 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
364 var node = d3.select(document.getElementById(data.dpid));
365 node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core);
366 data.highlighted = true;
367 node.moveToFront();
368 }
369
Paul Greyson8d1c6362013-03-27 13:05:24 -0700370 function mouseOutSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700371 if (data.mouseDown)
372 return;
373
374 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
375 var node = d3.select(document.getElementById(data.dpid));
376 node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]);
377 data.highlighted = false;
378 data.target = false;
379 }
380
Paul Greyson8d1c6362013-03-27 13:05:24 -0700381 function mouseDownSwitch(data) {
382 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700383 data.mouseDown = true;
384 }
385
Paul Greyson8d1c6362013-03-27 13:05:24 -0700386 function mouseUpSwitch(data) {
387 if (data.mouseDown) {
388 data.mouseDown = false;
389 d3.event.stopPropagation();
390 }
391 }
392
393 function doubleClickSwitch(data) {
394 if (data.state == 'ACTIVE') {
395 var prompt = 'Deactivate ' + data.dpid + '?';
396 if (confirm(prompt)) {
397 switchDown(data);
398 }
399 } else {
400 var prompt = 'Activate ' + data.dpid + '?';
401 if (confirm(prompt)) {
402 switchUp(data);
403 }
404 }
405 }
406
Paul Greyson740bdaf2013-03-18 16:10:48 -0700407 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700408 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700409 return;
410 }
411
Paul Greysonc17278a2013-03-23 10:17:12 -0700412 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700413 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700414 .data(data, function (data) {
415 return data.dpid;
416 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700417 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700418 .attr("id", function (data, i) {
419 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700420 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700421 .attr("transform", function(data, i) {
422 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700423 });
424
Paul Greysonc17278a2013-03-23 10:17:12 -0700425 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700426 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700427 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700428 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700429 if (data.scale) {
430 m = m.scale(data.scale);
431 }
432 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700433 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700434 .attr("x", function (data) {
435 return -data.width / 2;
436 })
437 .attr("y", function (data) {
438 return -data.width / 2;
439 })
440 .attr("r", function (data) {
441 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700442 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700443
Paul Greysonc17278a2013-03-23 10:17:12 -0700444 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700445 nodes.on('mouseover', mouseOverSwitch);
446 nodes.on('mouseout', mouseOutSwitch);
447 nodes.on('mouseup', mouseUpSwitch);
448 nodes.on('mousedown', mouseDownSwitch);
449
450 // only do switch up/down for core switches
451 if (i == 2) {
452 nodes.on('dblclick', doubleClickSwitch);
453 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700454 }
455
Paul Greysonc17278a2013-03-23 10:17:12 -0700456 // append switches
457 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700458 .attr("class", "ring")
459 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700460
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700461
Paul Greysonc17278a2013-03-23 10:17:12 -0700462 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700463 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700464 .data(data, function (data) {
465 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700466 });
Paul Greyson644d92a2013-03-23 18:00:40 -0700467 nodes.select('circle').attr('class', function (data, i) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700468 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700469 return data.className + ' ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700470 } else {
471 return data.className + ' ' + 'colorInactive';
472 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700473 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700474 }
475
476 // update switches
477 rings.each(ringUpdate);
478
Paul Greyson968d1b42013-03-23 16:58:41 -0700479
480 // Now setup the labels
481 // This is done separately because SVG draws in node order and we want the labels
482 // always on top
483 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
484
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700485 d3.select(document.body).on('mouseup', function () {
486 function clearHighlight() {
487 svg.selectAll('circle').each(function (data) {
488 data.mouseDown = false;
Paul Greyson8d1c6362013-03-27 13:05:24 -0700489 mouseOutSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700490 })
491 };
492
493
494 var highlighted = svg.selectAll('circle.highlight')[0];
495 if (highlighted.length == 2) {
496 var s1Data = d3.select(highlighted[0]).data()[0];
497 var s2Data = d3.select(highlighted[1]).data()[0];
498
499 var srcData, dstData;
500 if (s1Data.target) {
501 dstData = s1Data;
502 srcData = s2Data;
503 } else {
504 dstData = s2Data;
505 srcData = s1Data;
506 }
507
508 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
509 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
510 if (confirm(prompt)) {
511 alert('do create flow');
512 } else {
513 alert('do not create flow');
514 }
515 } else {
516 var map = linkMap[srcData.dpid];
517 if (map && map[dstData.dpid]) {
518 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
519 if (confirm(prompt)) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700520 linkDown(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700521 }
522 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700523 map = linkMap[dstData.dpid];
524 if (map && map[srcData.dpid]) {
525 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
526 if (confirm(prompt)) {
527 linkDown(map[srcData.dpid]);
528 }
529 } else {
530 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
531 if (confirm(prompt)) {
532 linkUp(srcData, dstData);
533 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700534 }
535 }
536 }
537
538 clearHighlight();
539 } else {
540 clearHighlight();
541 }
542
543 });
544
Paul Greyson9066ab02013-03-23 18:15:41 -0700545 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700546 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700547 return;
548 }
549
550 // create the nodes
551 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700552 .data(data, function (data) {
553 return data.dpid;
554 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700555 .enter().append("svg:g")
556 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700557 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700558 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700559 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700560 .attr("transform", function(data, i) {
561 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700562 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700563
564 // add the text nodes which show on mouse over
565 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700566 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700567 .attr("x", function (data) {
568 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
569 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700570 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700571 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700572 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700573 }
574 } else {
575 if (data.className == 'edge') {
576 return data.width*3 + 4;
577 } else {
578 return data.width + 4;
579 }
580 }
581 })
582 .attr("y", function (data) {
583 var y;
584 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
585 if (data.className == 'edge') {
586 y = data.width*3/2 + 4;
587 } else {
588 y = data.width/2 + 4;
589 }
590 } else {
591 if (data.className == 'edge') {
592 y = data.width*3/2 + 4;
593 } else {
594 y = data.width/2 + 4;
595 }
596 }
597 return y - 6;
598 })
599 .attr("text-anchor", function (data) {
600 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
601 return "end";
602 } else {
603 return "start";
604 }
605 })
606 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700607 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
608 if (data.scale) {
609 m = m.scale(data.scale);
610 }
611 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
612 })
613 }
614
615 labelRings.enter().append("svg:g")
616 .attr("class", "textRing")
617 .each(labelRingEnter);
618
Paul Greysonc17278a2013-03-23 10:17:12 -0700619 // switches should not change during operation of the ui so no
620 // rings.exit()
621
622
Paul Greysond1a22d92013-03-19 12:15:19 -0700623 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700624
Paul Greysonc17278a2013-03-23 10:17:12 -0700625 // key on link dpids since these will come/go during demo
Paul Greysonb367de22013-03-23 11:09:11 -0700626 var links = d3.select('svg').selectAll('.link').data(model.links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700627 return d['src-switch']+'->'+d['dst-switch'];
628 });
629
630 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700631 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700632 .attr("class", "link");
633
634 links
635 .attr("d", function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700636
Paul Greysond1a22d92013-03-19 12:15:19 -0700637 var src = d3.select(document.getElementById(d['src-switch']));
638 var dst = d3.select(document.getElementById(d['dst-switch']));
639
640 var srcPt = document.querySelector('svg').createSVGPoint();
641 srcPt.x = src.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700642 srcPt.y = src.attr('y');
643 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700644
645 var dstPt = document.querySelector('svg').createSVGPoint();
646 dstPt.x = dst.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700647 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
648 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700649
Paul Greysonb367de22013-03-23 11:09:11 -0700650 var midPt = document.querySelector('svg').createSVGPoint();
651 midPt.x = (srcPt.x + dstPt.x)/2;
652 midPt.y = (srcPt.y + dstPt.y)/2;
653
654 return line([srcPt, midPt, dstPt]);
655 })
656 .attr("marker-mid", function(d) { return "url(#arrow)"; });
Paul Greysonc17278a2013-03-23 10:17:12 -0700657
Paul Greyson56378ed2013-03-26 23:17:36 -0700658
Paul Greysonc17278a2013-03-23 10:17:12 -0700659 // remove old links
660 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700661
Paul Greyson127d7fb2013-03-25 23:39:20 -0700662
663 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700664}
665
666function updateControllers(model) {
667 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700668 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700669 .each(function (c) {
670 controllerColorMap[c] = colors.pop();
671 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
672 })
673 .text(function (d) {
674 return d;
675 });
Paul Greysonbcd3c772013-03-21 13:16:44 -0700676
Paul Greysone262a292013-03-23 10:35:23 -0700677 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700678 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700679 if (model.activeControllers.indexOf(d) != -1) {
680 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700681 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700682 var className = 'controller ' + color;
683 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700684 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700685
Paul Greysone262a292013-03-23 10:35:23 -0700686 // this should never be needed
687 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700688
Paul Greyson8d1c6362013-03-27 13:05:24 -0700689 controllers.on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700690 var allSelected = true;
691 for (var key in controllerColorMap) {
692 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
693 allSelected = false;
694 break;
695 }
696 }
697 if (allSelected) {
698 for (var key in controllerColorMap) {
699 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
700 }
701 } else {
702 for (var key in controllerColorMap) {
703 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
704 }
705 }
706
707 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
708 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700709 });
Paul Greyson8d1c6362013-03-27 13:05:24 -0700710
711
Paul Greyson740bdaf2013-03-18 16:10:48 -0700712}
713
Paul Greyson127d7fb2013-03-25 23:39:20 -0700714function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700715 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700716 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700717// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700718
Paul Greyson56378ed2013-03-26 23:17:36 -0700719 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700720 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700721
722 // fake flows right now
723 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700724 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700725 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
726 selectedFlowsData[i].flow = newModel.flows[i];
727 selectedFlowsData[i].selected = selected;
728 }
729
730 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700731 updateTopology(svg, newModel);
732 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700733// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700734 }
735 updateHeader(newModel);
736
Paul Greyson56378ed2013-03-26 23:17:36 -0700737 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700738
739 // do it again in 1s
740 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700741 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700742 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700743 });
744}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700745
Paul Greyson38d8bde2013-03-22 22:07:35 -0700746svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700747selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700748// workaround for Chrome v25 bug
749// if executed immediately, the view box transform logic doesn't work properly
750// fixed in Chrome v27
751setTimeout(function () {
752 // workaround for another Chrome v25 bug
753 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700754 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700755 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700756 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700757}, 100);