blob: 3d21b01f4d0e521405bd19905df26390f4dfb834 [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)) {
Paul Greyson347fb742013-03-27 13:40:29 -0700397 d3.select(document.getElementById(data.dpid)).select('circle').classed('pending', true);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700398 switchDown(data);
399 }
400 } else {
401 var prompt = 'Activate ' + data.dpid + '?';
402 if (confirm(prompt)) {
Paul Greyson347fb742013-03-27 13:40:29 -0700403 d3.select(document.getElementById(data.dpid)).select('circle').classed('pending', true);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700404 switchUp(data);
405 }
406 }
407 }
408
Paul Greyson740bdaf2013-03-18 16:10:48 -0700409 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700410 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700411 return;
412 }
413
Paul Greysonc17278a2013-03-23 10:17:12 -0700414 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700415 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700416 .data(data, function (data) {
417 return data.dpid;
418 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700419 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700420 .attr("id", function (data, i) {
421 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700422 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700423 .attr("transform", function(data, i) {
424 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700425 });
426
Paul Greysonc17278a2013-03-23 10:17:12 -0700427 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700428 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700429 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700430 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700431 if (data.scale) {
432 m = m.scale(data.scale);
433 }
434 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700435 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700436 .attr("x", function (data) {
437 return -data.width / 2;
438 })
439 .attr("y", function (data) {
440 return -data.width / 2;
441 })
442 .attr("r", function (data) {
443 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700444 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700445
Paul Greysonc17278a2013-03-23 10:17:12 -0700446 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700447 nodes.on('mouseover', mouseOverSwitch);
448 nodes.on('mouseout', mouseOutSwitch);
449 nodes.on('mouseup', mouseUpSwitch);
450 nodes.on('mousedown', mouseDownSwitch);
451
452 // only do switch up/down for core switches
453 if (i == 2) {
454 nodes.on('dblclick', doubleClickSwitch);
455 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700456 }
457
Paul Greysonc17278a2013-03-23 10:17:12 -0700458 // append switches
459 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700460 .attr("class", "ring")
461 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700462
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700463
Paul Greysonc17278a2013-03-23 10:17:12 -0700464 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700465 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700466 .data(data, function (data) {
467 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700468 });
Paul Greyson347fb742013-03-27 13:40:29 -0700469 nodes.select('circle')
470 .each(function (data) {
471 // if there's a pending state changed and then the state changes, clear the pending class
472 var circle = d3.select(this);
473 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
474 data.state === 'INACTIVE' && circle.classed('active')) {
475 circle.classed('pending', false);
476 }
477 })
478 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700479 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700480 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700481 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700482 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700483 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700484 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700485 }
486
487 // update switches
488 rings.each(ringUpdate);
489
Paul Greyson968d1b42013-03-23 16:58:41 -0700490
491 // Now setup the labels
492 // This is done separately because SVG draws in node order and we want the labels
493 // always on top
494 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
495
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700496 d3.select(document.body).on('mouseup', function () {
497 function clearHighlight() {
498 svg.selectAll('circle').each(function (data) {
499 data.mouseDown = false;
Paul Greyson8d1c6362013-03-27 13:05:24 -0700500 mouseOutSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700501 })
502 };
503
504
505 var highlighted = svg.selectAll('circle.highlight')[0];
506 if (highlighted.length == 2) {
507 var s1Data = d3.select(highlighted[0]).data()[0];
508 var s2Data = d3.select(highlighted[1]).data()[0];
509
510 var srcData, dstData;
511 if (s1Data.target) {
512 dstData = s1Data;
513 srcData = s2Data;
514 } else {
515 dstData = s2Data;
516 srcData = s1Data;
517 }
518
519 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
520 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
521 if (confirm(prompt)) {
522 alert('do create flow');
523 } else {
524 alert('do not create flow');
525 }
526 } else {
527 var map = linkMap[srcData.dpid];
528 if (map && map[dstData.dpid]) {
529 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
530 if (confirm(prompt)) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700531 linkDown(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700532 }
533 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700534 map = linkMap[dstData.dpid];
535 if (map && map[srcData.dpid]) {
536 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
537 if (confirm(prompt)) {
538 linkDown(map[srcData.dpid]);
539 }
540 } else {
541 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
542 if (confirm(prompt)) {
543 linkUp(srcData, dstData);
544 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700545 }
546 }
547 }
548
549 clearHighlight();
550 } else {
551 clearHighlight();
552 }
553
554 });
555
Paul Greyson9066ab02013-03-23 18:15:41 -0700556 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700557 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700558 return;
559 }
560
561 // create the nodes
562 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700563 .data(data, function (data) {
564 return data.dpid;
565 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700566 .enter().append("svg:g")
567 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700568 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700569 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700570 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700571 .attr("transform", function(data, i) {
572 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700573 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700574
575 // add the text nodes which show on mouse over
576 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700577 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700578 .attr("x", function (data) {
579 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
580 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700581 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700582 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700583 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700584 }
585 } else {
586 if (data.className == 'edge') {
587 return data.width*3 + 4;
588 } else {
589 return data.width + 4;
590 }
591 }
592 })
593 .attr("y", function (data) {
594 var y;
595 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
596 if (data.className == 'edge') {
597 y = data.width*3/2 + 4;
598 } else {
599 y = data.width/2 + 4;
600 }
601 } else {
602 if (data.className == 'edge') {
603 y = data.width*3/2 + 4;
604 } else {
605 y = data.width/2 + 4;
606 }
607 }
608 return y - 6;
609 })
610 .attr("text-anchor", function (data) {
611 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
612 return "end";
613 } else {
614 return "start";
615 }
616 })
617 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700618 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
619 if (data.scale) {
620 m = m.scale(data.scale);
621 }
622 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
623 })
624 }
625
626 labelRings.enter().append("svg:g")
627 .attr("class", "textRing")
628 .each(labelRingEnter);
629
Paul Greysonc17278a2013-03-23 10:17:12 -0700630 // switches should not change during operation of the ui so no
631 // rings.exit()
632
633
Paul Greysond1a22d92013-03-19 12:15:19 -0700634 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700635
Paul Greysonc17278a2013-03-23 10:17:12 -0700636 // key on link dpids since these will come/go during demo
Paul Greysonb367de22013-03-23 11:09:11 -0700637 var links = d3.select('svg').selectAll('.link').data(model.links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700638 return d['src-switch']+'->'+d['dst-switch'];
639 });
640
641 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700642 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700643 .attr("class", "link");
644
645 links
646 .attr("d", function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700647
Paul Greysond1a22d92013-03-19 12:15:19 -0700648 var src = d3.select(document.getElementById(d['src-switch']));
649 var dst = d3.select(document.getElementById(d['dst-switch']));
650
651 var srcPt = document.querySelector('svg').createSVGPoint();
652 srcPt.x = src.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700653 srcPt.y = src.attr('y');
654 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700655
656 var dstPt = document.querySelector('svg').createSVGPoint();
657 dstPt.x = dst.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700658 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
659 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700660
Paul Greysonb367de22013-03-23 11:09:11 -0700661 var midPt = document.querySelector('svg').createSVGPoint();
662 midPt.x = (srcPt.x + dstPt.x)/2;
663 midPt.y = (srcPt.y + dstPt.y)/2;
664
665 return line([srcPt, midPt, dstPt]);
666 })
667 .attr("marker-mid", function(d) { return "url(#arrow)"; });
Paul Greysonc17278a2013-03-23 10:17:12 -0700668
Paul Greyson56378ed2013-03-26 23:17:36 -0700669
Paul Greysonc17278a2013-03-23 10:17:12 -0700670 // remove old links
671 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700672
Paul Greyson127d7fb2013-03-25 23:39:20 -0700673
674 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700675}
676
677function updateControllers(model) {
678 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700679 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700680 .each(function (c) {
681 controllerColorMap[c] = colors.pop();
682 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
683 })
684 .text(function (d) {
685 return d;
686 });
Paul Greysonbcd3c772013-03-21 13:16:44 -0700687
Paul Greysone262a292013-03-23 10:35:23 -0700688 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700689 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700690 if (model.activeControllers.indexOf(d) != -1) {
691 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700692 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700693 var className = 'controller ' + color;
694 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700695 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700696
Paul Greysone262a292013-03-23 10:35:23 -0700697 // this should never be needed
698 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700699
Paul Greyson8d1c6362013-03-27 13:05:24 -0700700 controllers.on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700701 var allSelected = true;
702 for (var key in controllerColorMap) {
703 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
704 allSelected = false;
705 break;
706 }
707 }
708 if (allSelected) {
709 for (var key in controllerColorMap) {
710 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
711 }
712 } else {
713 for (var key in controllerColorMap) {
714 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
715 }
716 }
717
718 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
719 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700720 });
Paul Greyson8d1c6362013-03-27 13:05:24 -0700721
722
Paul Greyson740bdaf2013-03-18 16:10:48 -0700723}
724
Paul Greyson127d7fb2013-03-25 23:39:20 -0700725function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700726 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700727 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700728// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700729
Paul Greyson56378ed2013-03-26 23:17:36 -0700730 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700731 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700732
733 // fake flows right now
734 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700735 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700736 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
737 selectedFlowsData[i].flow = newModel.flows[i];
738 selectedFlowsData[i].selected = selected;
739 }
740
741 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700742 updateTopology(svg, newModel);
743 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700744// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700745 }
746 updateHeader(newModel);
747
Paul Greyson56378ed2013-03-26 23:17:36 -0700748 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700749
750 // do it again in 1s
751 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700752 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700753 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700754 });
755}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700756
Paul Greyson38d8bde2013-03-22 22:07:35 -0700757svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700758selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700759// workaround for Chrome v25 bug
760// if executed immediately, the view box transform logic doesn't work properly
761// fixed in Chrome v27
762setTimeout(function () {
763 // workaround for another Chrome v25 bug
764 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700765 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700766 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700767 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700768}, 100);