blob: 491b1f16fba17dfb2022b46ae2912c3d8efc7666 [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 Greyson40c8a592013-03-27 14:10:33 -070020var pendingLinks = {};
Paul Greyson127d7fb2013-03-25 23:39:20 -070021
Paul Greysond1a22d92013-03-19 12:15:19 -070022var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -070023 'color1',
24 'color2',
25 'color3',
26 'color4',
27 'color5',
28 'color6',
29 'color7',
30 'color8',
31 'color9',
32 'color10',
33 'color11',
Paul Greyson127d7fb2013-03-25 23:39:20 -070034 'color12'
35];
Paul Greyson01a5dff2013-03-19 15:50:14 -070036colors.reverse();
Paul Greysond1a22d92013-03-19 12:15:19 -070037
38var controllerColorMap = {};
39
Paul Greyson084779b2013-03-27 13:55:49 -070040function setPending(selection) {
41 selection.classed('pending', false);
42 setTimeout(function () {
43 selection.classed('pending', true);
44 })
45}
Paul Greysond1a22d92013-03-19 12:15:19 -070046
Paul Greyson740bdaf2013-03-18 16:10:48 -070047function createTopologyView() {
Paul Greyson56378ed2013-03-26 23:17:36 -070048
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?
52// updateTopology(svg, model);
53 });
54
Paul Greysonb367de22013-03-23 11:09:11 -070055 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 Greyson45303ac2013-03-23 16:44:01 -070065 .attr("d", "M0,-3L10,0L0,3");
Paul Greysonb367de22013-03-23 11:09:11 -070066
67 return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070068 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070069}
70
Paul Greyson127d7fb2013-03-25 23:39:20 -070071var selectedFlowsData = [
72 {selected: false, flow: null},
73 {selected: false, flow: null},
Paul Greyson127d7fb2013-03-25 23:39:20 -070074 {selected: false, flow: null}
75];
76
77function drawFlows() {
78 // DRAW THE FLOWS
79 var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) {
80 return d.flow ? d.flow.flowId.value : null;
81 });
82
83 flows.enter().append("svg:path")
84 .attr('class', 'flow')
85 .attr('d', function (d) {
86 if (!d.flow) {
87 return;
88 }
89 var pts = [];
90 d.flow.dataPath.flowEntries.forEach(function (flowEntry) {
91 var s = d3.select(document.getElementById(flowEntry.dpid.value));
92 var pt = document.querySelector('svg').createSVGPoint();
93 pt.x = s.attr('x');
94 pt.y = s.attr('y');
95 pt = pt.matrixTransform(s[0][0].getCTM());
96 pts.push(pt);
97 });
98 return line(pts);
99 })
Paul Greysonacb59412013-03-25 23:48:06 -0700100 .attr('stroke-dasharray', '3, 10')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700101 .append('svg:animate')
102 .attr('attributeName', 'stroke-dashoffset')
103 .attr('attributeType', 'xml')
104 .attr('from', '500')
105 .attr('to', '-500')
106 .attr('dur', '20s')
107 .attr('repeatCount', 'indefinite');
108
109 flows.style('visibility', function (d) {
110 if (d) {
111 return d.selected ? '' : 'hidden';
112 }
113 })
114
Paul Greyson56378ed2013-03-26 23:17:36 -0700115 // "marching ants"
116 // TODO: this will only be true if there's an iperf session running
Paul Greyson127d7fb2013-03-25 23:39:20 -0700117 flows.select('animate').attr('from', function (d) {
118 if (d.flow) {
119 if (d.selected) {
120 return '500';
121 } else {
122 return '-500';
123 }
124 }
125 });
126}
127
128function updateFlowView() {
129 selectedFlowsView.data(selectedFlowsData);
130
131 selectedFlowsView.classed('selected', function (d) {
132 if (d.flow) {
133 return d.selected;
134 }
135 });
136
137 selectedFlowsView.select('.flowId')
138 .text(function (d) {
139 if (d.flow) {
140 return d.flow.flowId.value;
141 }
142 });
143
144 selectedFlowsView.select('.srcDPID')
145 .text(function (d) {
146 if (d.flow) {
147 return d.flow.dataPath.srcPort.dpid.value;
148 }
149 });
150
151 selectedFlowsView.select('.dstDPID')
152 .text(function (d) {
153 if (d.flow) {
154 return d.flow.dataPath.dstPort.dpid.value;
155 }
156 });
157}
158
159function createFlowView() {
160 function rowEnter(d, i) {
161 var row = d3.select(this);
162
163 row.on('click', function () {
164 selectedFlowsData[i].selected = !selectedFlowsData[i].selected;
165 updateFlowView();
166 drawFlows();
167 });
168
169 row.append('div')
170 .classed('flowIndex', true)
171 .text(function () {
172 return i+1;
173 });
174
175 row.append('div')
176 .classed('flowId', true);
177
178 row.append('div')
179 .classed('srcDPID', true);
180
181 row.append('div')
182 .classed('dstDPID', true);
183
184 row.append('div')
185 .classed('iperf', true);
186 }
187
188 var flows = d3.select('#selectedFlows')
189 .selectAll('.selectedFlow')
190 .data(selectedFlowsData)
191 .enter()
192 .append('div')
193 .classed('selectedFlow', true)
194 .each(rowEnter);
195
196
197 return flows;
198}
199
Paul Greysond1a22d92013-03-19 12:15:19 -0700200function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700201 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700202 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
203 d3.select('#activeFlows').text(model.flows.length);
204}
205
206function toRadians (angle) {
207 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700208}
209
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700210var widths = {
211 edge: 6,
212 aggregation: 12,
213 core: 18
214}
215
Paul Greysonc17278a2013-03-23 10:17:12 -0700216function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700217 var rings = [{
218 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700219 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700220 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700221 className: 'edge',
222 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700223 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700224 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700225 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700226 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700227 className: 'aggregation',
228 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700229 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700230 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700231 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700232 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700233 className: 'core',
234 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700235 }];
236
Paul Greysonde7fad52013-03-19 12:47:32 -0700237
238 var aggRanges = {};
239
240 // arrange edge switches at equal increments
241 var k = 360 / rings[0].switches.length;
242 rings[0].switches.forEach(function (s, i) {
243 var angle = k * i;
244
245 rings[0].angles[i] = angle;
246
247 // record the angle for the agg switch layout
248 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700249 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700250 var aggdpid = dpid.join(':');
251 var aggRange = aggRanges[aggdpid];
252 if (!aggRange) {
253 aggRange = aggRanges[aggdpid] = {};
254 aggRange.min = aggRange.max = angle;
255 } else {
256 aggRange.max = angle;
257 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700258 });
259
260 // arrange aggregation switches to "fan out" to edge switches
261 k = 360 / rings[1].switches.length;
262 rings[1].switches.forEach(function (s, i) {
263// rings[1].angles[i] = k * i;
264 var range = aggRanges[s.dpid];
265
Paul Greyson832d2202013-03-21 13:27:56 -0700266 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700267 });
268
Paul Greyson3f890b62013-03-22 17:39:36 -0700269 // find the association between core switches and aggregation switches
270 var aggregationSwitchMap = {};
271 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700272 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700273 });
274
Paul Greyson3f890b62013-03-22 17:39:36 -0700275 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700276 k = 360 / rings[2].switches.length;
277 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700278// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700279 var associatedAggregationSwitches = model.configuration.association[s.dpid];
280 // TODO: go between if there are multiple
281 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
282
283 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700284 });
285
Paul Greyson644d92a2013-03-23 18:00:40 -0700286 // TODO: construct this form initially rather than converting. it works better because
287 // it allows binding by dpid
288 var testRings = [];
289 rings.forEach(function (ring) {
290 var testRing = [];
291 ring.switches.forEach(function (s, i) {
292 var testSwitch = {
293 dpid: s.dpid,
294 state: s.state,
295 radius: ring.radius,
296 width: ring.width,
297 className: ring.className,
298 angle: ring.angles[i],
299 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700300 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700301 testRing.push(testSwitch);
302 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700303
304
Paul Greyson644d92a2013-03-23 18:00:40 -0700305 testRings.push(testRing);
306 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700307
308
Paul Greyson644d92a2013-03-23 18:00:40 -0700309// return rings;
310 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700311}
312
Paul Greyson40c8a592013-03-27 14:10:33 -0700313function makeLinkKey(link) {
314 return link['src-switch'] + '=>' + link['dst-switch'];
315}
316
317function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700318 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700319 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700320 var srcDPID = link['src-switch'];
321 var dstDPID = link['dst-switch'];
322
323 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700324
Paul Greyson8d1c6362013-03-27 13:05:24 -0700325 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700326
327 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700328 });
329 return linkMap;
330}
331
Paul Greyson56378ed2013-03-26 23:17:36 -0700332updateTopology = function(svg, model) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700333
334 // DRAW THE SWITCHES
335 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
336
Paul Greyson40c8a592013-03-27 14:10:33 -0700337
338 var links = [];
339 model.links.forEach(function (link) {
340 links.push(link);
341 delete pendingLinks[makeLinkKey(link)]
342 })
343 var linkId;
344 for (linkId in pendingLinks) {
345 links.push(pendingLinks[linkId]);
346 }
347
348 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700349// var flowMap = createFlowMap(model);
350
Paul Greyson8d1c6362013-03-27 13:05:24 -0700351 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700352
353 d3.event.preventDefault();
354
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700355 if (data.highlighted) {
356 return;
357 }
358
Paul Greyson72f18852013-03-27 15:56:11 -0700359
360
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700361 // only highlight valid link or flow destination by checking for class of existing highlighted circle
362 var highlighted = svg.selectAll('circle.highlight')[0];
363 if (highlighted.length == 1) {
364 var s = d3.select(highlighted[0]);
365 // only allow links
366 // edge->edge (flow)
367 // aggregation->core
368 // core->core
369 if (data.className == 'edge' && !s.classed('edge') ||
370 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
371 data.className == 'aggregation' && !s.classed('core')) {
372 return;
373 }
374
375 // don't highlight if there's already a link or flow
376 // var map = linkMap[data.dpid];
377 // console.log(map);
378 // console.log(s.data()[0].dpid);
379 // console.log(map[s.data()[0].dpid]);
380 // if (map && map[s.data()[0].dpid]) {
381 // return;
382 // }
383
384 // the second highlighted switch is the target for a link or flow
385 data.target = true;
386 }
387
388
389 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
390 var node = d3.select(document.getElementById(data.dpid));
391 node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core);
392 data.highlighted = true;
393 node.moveToFront();
394 }
395
Paul Greyson8d1c6362013-03-27 13:05:24 -0700396 function mouseOutSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700397 if (data.mouseDown)
398 return;
399
400 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
401 var node = d3.select(document.getElementById(data.dpid));
402 node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]);
403 data.highlighted = false;
404 data.target = false;
405 }
406
Paul Greyson8d1c6362013-03-27 13:05:24 -0700407 function mouseDownSwitch(data) {
408 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700409 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700410 d3.select('#topology').classed('linking', true);
411
412 if (data.className === 'core') {
413 d3.selectAll('.edge').classed('nodrop', true);
414 }
415 if (data.className === 'edge') {
416 d3.selectAll('.core').classed('nodrop', true);
417 d3.selectAll('.aggregation').classed('nodrop', true);
418 }
419 if (data.className === 'aggregation') {
420 d3.selectAll('.edge').classed('nodrop', true);
421 d3.selectAll('.aggregation').classed('nodrop', true);
422 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700423 }
424
Paul Greyson8d1c6362013-03-27 13:05:24 -0700425 function mouseUpSwitch(data) {
426 if (data.mouseDown) {
427 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700428 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700429 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700430 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700431 }
432 }
433
434 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700435 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700436 if (data.state == 'ACTIVE') {
437 var prompt = 'Deactivate ' + data.dpid + '?';
438 if (confirm(prompt)) {
439 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700440 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700441 }
442 } else {
443 var prompt = 'Activate ' + data.dpid + '?';
444 if (confirm(prompt)) {
445 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700446 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700447 }
448 }
449 }
450
Paul Greyson740bdaf2013-03-18 16:10:48 -0700451 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700452 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700453 return;
454 }
455
Paul Greysonc17278a2013-03-23 10:17:12 -0700456 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700457 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700458 .data(data, function (data) {
459 return data.dpid;
460 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700461 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700462 .attr("id", function (data, i) {
463 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700464 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700465 .attr("transform", function(data, i) {
466 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700467 });
468
Paul Greysonc17278a2013-03-23 10:17:12 -0700469 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700470 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700471 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700472 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700473 if (data.scale) {
474 m = m.scale(data.scale);
475 }
476 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700477 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700478 .attr("x", function (data) {
479 return -data.width / 2;
480 })
481 .attr("y", function (data) {
482 return -data.width / 2;
483 })
484 .attr("r", function (data) {
485 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700486 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700487
Paul Greysonc17278a2013-03-23 10:17:12 -0700488 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700489 nodes.on('mouseover', mouseOverSwitch);
490 nodes.on('mouseout', mouseOutSwitch);
491 nodes.on('mouseup', mouseUpSwitch);
492 nodes.on('mousedown', mouseDownSwitch);
493
494 // only do switch up/down for core switches
495 if (i == 2) {
496 nodes.on('dblclick', doubleClickSwitch);
497 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700498 }
499
Paul Greysonc17278a2013-03-23 10:17:12 -0700500 // append switches
501 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700502 .attr("class", "ring")
503 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700504
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700505
Paul Greysonc17278a2013-03-23 10:17:12 -0700506 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700507 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700508 .data(data, function (data) {
509 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700510 });
Paul Greyson347fb742013-03-27 13:40:29 -0700511 nodes.select('circle')
512 .each(function (data) {
513 // if there's a pending state changed and then the state changes, clear the pending class
514 var circle = d3.select(this);
515 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
516 data.state === 'INACTIVE' && circle.classed('active')) {
517 circle.classed('pending', false);
518 }
519 })
520 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700521 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700522 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700523 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700524 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700525 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700526 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700527 }
528
529 // update switches
530 rings.each(ringUpdate);
531
Paul Greyson968d1b42013-03-23 16:58:41 -0700532
533 // Now setup the labels
534 // This is done separately because SVG draws in node order and we want the labels
535 // always on top
536 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
537
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700538 d3.select(document.body).on('mouseup', function () {
539 function clearHighlight() {
540 svg.selectAll('circle').each(function (data) {
541 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700542 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700543 mouseOutSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700544 })
545 };
546
Paul Greyson72f18852013-03-27 15:56:11 -0700547 d3.selectAll('.nodrop').classed('nodrop', false);
548
Paul Greyson084779b2013-03-27 13:55:49 -0700549 function removeLink(link) {
550 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
551 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
552
553 if (path1) {
554 setPending(d3.select(path1));
555 }
556 if (path2) {
557 setPending(d3.select(path2));
558 }
559
560 linkDown(link);
561 }
562
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700563
564 var highlighted = svg.selectAll('circle.highlight')[0];
565 if (highlighted.length == 2) {
566 var s1Data = d3.select(highlighted[0]).data()[0];
567 var s2Data = d3.select(highlighted[1]).data()[0];
568
569 var srcData, dstData;
570 if (s1Data.target) {
571 dstData = s1Data;
572 srcData = s2Data;
573 } else {
574 dstData = s2Data;
575 srcData = s1Data;
576 }
577
578 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
579 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
580 if (confirm(prompt)) {
581 alert('do create flow');
582 } else {
583 alert('do not create flow');
584 }
585 } else {
586 var map = linkMap[srcData.dpid];
587 if (map && map[dstData.dpid]) {
588 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
589 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700590 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700591 }
592 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700593 map = linkMap[dstData.dpid];
594 if (map && map[srcData.dpid]) {
595 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
596 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700597 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700598 }
599 } else {
600 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
601 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700602 var link1 = {
603 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700604 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700605 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700606 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700607 pending: true
608 };
609 pendingLinks[makeLinkKey(link1)] = link1;
610 var link2 = {
611 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700612 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700613 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700614 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700615 pending: true
616 };
617 pendingLinks[makeLinkKey(link2)] = link2;
618 updateTopology(svg, model);
619
Paul Greyson2913af82013-03-27 14:53:17 -0700620 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700621
622 // remove the pending link after 10s
623 setTimeout(function () {
624 delete pendingLinks[makeLinkKey(link1)];
625 delete pendingLinks[makeLinkKey(link2)];
626
627 updateTopology(svg, model);
628 }, 10000);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700629 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700630 }
631 }
632 }
633
634 clearHighlight();
635 } else {
636 clearHighlight();
637 }
638
639 });
640
Paul Greyson9066ab02013-03-23 18:15:41 -0700641 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700642 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700643 return;
644 }
645
646 // create the nodes
647 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700648 .data(data, function (data) {
649 return data.dpid;
650 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700651 .enter().append("svg:g")
652 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700653 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700654 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700655 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700656 .attr("transform", function(data, i) {
657 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700658 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700659
660 // add the text nodes which show on mouse over
661 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700662 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700663 .attr("x", function (data) {
664 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
665 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700666 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700667 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700668 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700669 }
670 } else {
671 if (data.className == 'edge') {
672 return data.width*3 + 4;
673 } else {
674 return data.width + 4;
675 }
676 }
677 })
678 .attr("y", function (data) {
679 var y;
680 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
681 if (data.className == 'edge') {
682 y = data.width*3/2 + 4;
683 } else {
684 y = data.width/2 + 4;
685 }
686 } else {
687 if (data.className == 'edge') {
688 y = data.width*3/2 + 4;
689 } else {
690 y = data.width/2 + 4;
691 }
692 }
693 return y - 6;
694 })
695 .attr("text-anchor", function (data) {
696 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
697 return "end";
698 } else {
699 return "start";
700 }
701 })
702 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700703 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
704 if (data.scale) {
705 m = m.scale(data.scale);
706 }
707 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
708 })
709 }
710
711 labelRings.enter().append("svg:g")
712 .attr("class", "textRing")
713 .each(labelRingEnter);
714
Paul Greysonc17278a2013-03-23 10:17:12 -0700715 // switches should not change during operation of the ui so no
716 // rings.exit()
717
718
Paul Greysond1a22d92013-03-19 12:15:19 -0700719 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700720
Paul Greysonc17278a2013-03-23 10:17:12 -0700721 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -0700722 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700723 return d['src-switch']+'->'+d['dst-switch'];
724 });
725
726 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700727 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700728 .attr("class", "link");
729
Paul Greyson084779b2013-03-27 13:55:49 -0700730 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700731 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -0700732 })
Paul Greyson56378ed2013-03-26 23:17:36 -0700733 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -0700734 var src = d3.select(document.getElementById(d['src-switch']));
735 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -0700736
Paul Greyson084779b2013-03-27 13:55:49 -0700737 var srcPt = document.querySelector('svg').createSVGPoint();
738 srcPt.x = src.attr('x');
739 srcPt.y = src.attr('y');
740 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700741
Paul Greyson084779b2013-03-27 13:55:49 -0700742 var dstPt = document.querySelector('svg').createSVGPoint();
743 dstPt.x = dst.attr('x');
744 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
745 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700746
Paul Greyson084779b2013-03-27 13:55:49 -0700747 var midPt = document.querySelector('svg').createSVGPoint();
748 midPt.x = (srcPt.x + dstPt.x)/2;
749 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -0700750
Paul Greyson084779b2013-03-27 13:55:49 -0700751 return line([srcPt, midPt, dstPt]);
752 })
Paul Greyson40c8a592013-03-27 14:10:33 -0700753 .attr("marker-mid", function(d) { return "url(#arrow)"; })
754 .classed('pending', function (d) {
755 return d.pending;
756 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700757
Paul Greyson56378ed2013-03-26 23:17:36 -0700758
Paul Greysonc17278a2013-03-23 10:17:12 -0700759 // remove old links
760 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700761
Paul Greyson127d7fb2013-03-25 23:39:20 -0700762
763 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700764}
765
766function updateControllers(model) {
767 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700768 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700769 .each(function (c) {
770 controllerColorMap[c] = colors.pop();
771 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
772 })
773 .text(function (d) {
774 return d;
Paul Greyson2913af82013-03-27 14:53:17 -0700775 })
776 .append('div')
777 .attr('class', 'controllerEye');
Paul Greysonbcd3c772013-03-21 13:16:44 -0700778
Paul Greysone262a292013-03-23 10:35:23 -0700779 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700780 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700781 if (model.activeControllers.indexOf(d) != -1) {
782 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700783 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700784 var className = 'controller ' + color;
785 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700786 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700787
Paul Greysone262a292013-03-23 10:35:23 -0700788 // this should never be needed
789 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700790
Paul Greyson2913af82013-03-27 14:53:17 -0700791 controllers.on('dblclick', function (c) {
792 if (model.activeControllers.indexOf(c) != -1) {
793 var prompt = 'Dectivate ' + c + '?';
794 if (confirm(prompt)) {
795 controllerDown(c);
796 setPending(d3.select(this));
797 };
798 } else {
799 var prompt = 'Activate ' + c + '?';
800 if (confirm(prompt)) {
801 controllerUp(c);
802 setPending(d3.select(this));
803 };
804 }
805 });
806
807 controllers.select('.controllerEye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700808 var allSelected = true;
809 for (var key in controllerColorMap) {
810 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
811 allSelected = false;
812 break;
813 }
814 }
815 if (allSelected) {
816 for (var key in controllerColorMap) {
817 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
818 }
819 } else {
820 for (var key in controllerColorMap) {
821 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
822 }
823 }
824
825 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
826 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700827 });
Paul Greyson8d1c6362013-03-27 13:05:24 -0700828
829
Paul Greyson740bdaf2013-03-18 16:10:48 -0700830}
831
Paul Greyson127d7fb2013-03-25 23:39:20 -0700832function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700833 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700834 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700835// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700836
Paul Greyson56378ed2013-03-26 23:17:36 -0700837 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700838 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700839
840 // fake flows right now
841 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700842 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700843 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
844 selectedFlowsData[i].flow = newModel.flows[i];
845 selectedFlowsData[i].selected = selected;
846 }
847
848 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700849 updateTopology(svg, newModel);
850 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700851// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700852 }
853 updateHeader(newModel);
854
Paul Greyson56378ed2013-03-26 23:17:36 -0700855 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700856
857 // do it again in 1s
858 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700859 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700860 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700861 });
862}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700863
Paul Greyson38d8bde2013-03-22 22:07:35 -0700864svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700865selectedFlowsView = createFlowView();
Paul Greyson72f18852013-03-27 15:56:11 -0700866
Paul Greyson38d8bde2013-03-22 22:07:35 -0700867// workaround for Chrome v25 bug
868// if executed immediately, the view box transform logic doesn't work properly
869// fixed in Chrome v27
870setTimeout(function () {
871 // workaround for another Chrome v25 bug
872 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700873 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700874 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700875 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700876}, 100);