blob: 7d708592e5bdb3807d6b1f9667b073c3d8b0a292 [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
Paul Greyson084779b2013-03-27 13:55:49 -070039function setPending(selection) {
40 selection.classed('pending', false);
41 setTimeout(function () {
42 selection.classed('pending', true);
43 })
44}
Paul Greysond1a22d92013-03-19 12:15:19 -070045
Paul Greyson740bdaf2013-03-18 16:10:48 -070046function createTopologyView() {
Paul Greyson56378ed2013-03-26 23:17:36 -070047
48 window.addEventListener('resize', function () {
49 // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied
50 // either that or is it possible to position the paths so they get the automatic transform as well?
51// updateTopology(svg, model);
52 });
53
Paul Greysonb367de22013-03-23 11:09:11 -070054 var svg = d3.select('#svg-container').append('svg:svg');
55
56 svg.append("svg:defs").append("svg:marker")
57 .attr("id", "arrow")
58 .attr("viewBox", "0 -5 10 10")
59 .attr("refX", -1)
60 .attr("markerWidth", 5)
61 .attr("markerHeight", 5)
62 .attr("orient", "auto")
63 .append("svg:path")
Paul Greyson45303ac2013-03-23 16:44:01 -070064 .attr("d", "M0,-3L10,0L0,3");
Paul Greysonb367de22013-03-23 11:09:11 -070065
66 return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070067 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070068}
69
Paul Greyson127d7fb2013-03-25 23:39:20 -070070var selectedFlowsData = [
71 {selected: false, flow: null},
72 {selected: false, flow: null},
Paul Greyson127d7fb2013-03-25 23:39:20 -070073 {selected: false, flow: null}
74];
75
76function drawFlows() {
77 // DRAW THE FLOWS
78 var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) {
79 return d.flow ? d.flow.flowId.value : null;
80 });
81
82 flows.enter().append("svg:path")
83 .attr('class', 'flow')
84 .attr('d', function (d) {
85 if (!d.flow) {
86 return;
87 }
88 var pts = [];
89 d.flow.dataPath.flowEntries.forEach(function (flowEntry) {
90 var s = d3.select(document.getElementById(flowEntry.dpid.value));
91 var pt = document.querySelector('svg').createSVGPoint();
92 pt.x = s.attr('x');
93 pt.y = s.attr('y');
94 pt = pt.matrixTransform(s[0][0].getCTM());
95 pts.push(pt);
96 });
97 return line(pts);
98 })
Paul Greysonacb59412013-03-25 23:48:06 -070099 .attr('stroke-dasharray', '3, 10')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700100 .append('svg:animate')
101 .attr('attributeName', 'stroke-dashoffset')
102 .attr('attributeType', 'xml')
103 .attr('from', '500')
104 .attr('to', '-500')
105 .attr('dur', '20s')
106 .attr('repeatCount', 'indefinite');
107
108 flows.style('visibility', function (d) {
109 if (d) {
110 return d.selected ? '' : 'hidden';
111 }
112 })
113
Paul Greyson56378ed2013-03-26 23:17:36 -0700114 // "marching ants"
115 // TODO: this will only be true if there's an iperf session running
Paul Greyson127d7fb2013-03-25 23:39:20 -0700116 flows.select('animate').attr('from', function (d) {
117 if (d.flow) {
118 if (d.selected) {
119 return '500';
120 } else {
121 return '-500';
122 }
123 }
124 });
125}
126
127function updateFlowView() {
128 selectedFlowsView.data(selectedFlowsData);
129
130 selectedFlowsView.classed('selected', function (d) {
131 if (d.flow) {
132 return d.selected;
133 }
134 });
135
136 selectedFlowsView.select('.flowId')
137 .text(function (d) {
138 if (d.flow) {
139 return d.flow.flowId.value;
140 }
141 });
142
143 selectedFlowsView.select('.srcDPID')
144 .text(function (d) {
145 if (d.flow) {
146 return d.flow.dataPath.srcPort.dpid.value;
147 }
148 });
149
150 selectedFlowsView.select('.dstDPID')
151 .text(function (d) {
152 if (d.flow) {
153 return d.flow.dataPath.dstPort.dpid.value;
154 }
155 });
156}
157
158function createFlowView() {
159 function rowEnter(d, i) {
160 var row = d3.select(this);
161
162 row.on('click', function () {
163 selectedFlowsData[i].selected = !selectedFlowsData[i].selected;
164 updateFlowView();
165 drawFlows();
166 });
167
168 row.append('div')
169 .classed('flowIndex', true)
170 .text(function () {
171 return i+1;
172 });
173
174 row.append('div')
175 .classed('flowId', true);
176
177 row.append('div')
178 .classed('srcDPID', true);
179
180 row.append('div')
181 .classed('dstDPID', true);
182
183 row.append('div')
184 .classed('iperf', true);
185 }
186
187 var flows = d3.select('#selectedFlows')
188 .selectAll('.selectedFlow')
189 .data(selectedFlowsData)
190 .enter()
191 .append('div')
192 .classed('selectedFlow', true)
193 .each(rowEnter);
194
195
196 return flows;
197}
198
Paul Greysond1a22d92013-03-19 12:15:19 -0700199function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700200 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700201 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
202 d3.select('#activeFlows').text(model.flows.length);
203}
204
205function toRadians (angle) {
206 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700207}
208
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700209var widths = {
210 edge: 6,
211 aggregation: 12,
212 core: 18
213}
214
Paul Greysonc17278a2013-03-23 10:17:12 -0700215function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700216 var rings = [{
217 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700218 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700219 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700220 className: 'edge',
221 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700222 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700223 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700224 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700225 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700226 className: 'aggregation',
227 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700228 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700229 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700230 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700231 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700232 className: 'core',
233 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700234 }];
235
Paul Greysonde7fad52013-03-19 12:47:32 -0700236
237 var aggRanges = {};
238
239 // arrange edge switches at equal increments
240 var k = 360 / rings[0].switches.length;
241 rings[0].switches.forEach(function (s, i) {
242 var angle = k * i;
243
244 rings[0].angles[i] = angle;
245
246 // record the angle for the agg switch layout
247 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700248 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700249 var aggdpid = dpid.join(':');
250 var aggRange = aggRanges[aggdpid];
251 if (!aggRange) {
252 aggRange = aggRanges[aggdpid] = {};
253 aggRange.min = aggRange.max = angle;
254 } else {
255 aggRange.max = angle;
256 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700257 });
258
259 // arrange aggregation switches to "fan out" to edge switches
260 k = 360 / rings[1].switches.length;
261 rings[1].switches.forEach(function (s, i) {
262// rings[1].angles[i] = k * i;
263 var range = aggRanges[s.dpid];
264
Paul Greyson832d2202013-03-21 13:27:56 -0700265 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700266 });
267
Paul Greyson3f890b62013-03-22 17:39:36 -0700268 // find the association between core switches and aggregation switches
269 var aggregationSwitchMap = {};
270 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700271 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700272 });
273
Paul Greyson3f890b62013-03-22 17:39:36 -0700274 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700275 k = 360 / rings[2].switches.length;
276 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700277// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700278 var associatedAggregationSwitches = model.configuration.association[s.dpid];
279 // TODO: go between if there are multiple
280 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
281
282 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700283 });
284
Paul Greyson644d92a2013-03-23 18:00:40 -0700285 // TODO: construct this form initially rather than converting. it works better because
286 // it allows binding by dpid
287 var testRings = [];
288 rings.forEach(function (ring) {
289 var testRing = [];
290 ring.switches.forEach(function (s, i) {
291 var testSwitch = {
292 dpid: s.dpid,
293 state: s.state,
294 radius: ring.radius,
295 width: ring.width,
296 className: ring.className,
297 angle: ring.angles[i],
298 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700299 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700300 testRing.push(testSwitch);
301 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700302
303
Paul Greyson644d92a2013-03-23 18:00:40 -0700304 testRings.push(testRing);
305 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700306
307
Paul Greyson644d92a2013-03-23 18:00:40 -0700308// return rings;
309 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700310}
311
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700312function createLinkMap(model) {
313 var linkMap = {};
314 model.links.forEach(function (link) {
315 var srcDPID = link['src-switch'];
316 var dstDPID = link['dst-switch'];
317
318 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700319
Paul Greyson8d1c6362013-03-27 13:05:24 -0700320 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700321
322 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700323 });
324 return linkMap;
325}
326
Paul Greyson56378ed2013-03-26 23:17:36 -0700327updateTopology = function(svg, model) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700328
329 // DRAW THE SWITCHES
330 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
331
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700332 var linkMap = createLinkMap(model);
333// var flowMap = createFlowMap(model);
334
Paul Greyson8d1c6362013-03-27 13:05:24 -0700335 function mouseOverSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700336 if (data.highlighted) {
337 return;
338 }
339
340 // only highlight valid link or flow destination by checking for class of existing highlighted circle
341 var highlighted = svg.selectAll('circle.highlight')[0];
342 if (highlighted.length == 1) {
343 var s = d3.select(highlighted[0]);
344 // only allow links
345 // edge->edge (flow)
346 // aggregation->core
347 // core->core
348 if (data.className == 'edge' && !s.classed('edge') ||
349 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
350 data.className == 'aggregation' && !s.classed('core')) {
351 return;
352 }
353
354 // don't highlight if there's already a link or flow
355 // var map = linkMap[data.dpid];
356 // console.log(map);
357 // console.log(s.data()[0].dpid);
358 // console.log(map[s.data()[0].dpid]);
359 // if (map && map[s.data()[0].dpid]) {
360 // return;
361 // }
362
363 // the second highlighted switch is the target for a link or flow
364 data.target = true;
365 }
366
367
368 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
369 var node = d3.select(document.getElementById(data.dpid));
370 node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core);
371 data.highlighted = true;
372 node.moveToFront();
373 }
374
Paul Greyson8d1c6362013-03-27 13:05:24 -0700375 function mouseOutSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700376 if (data.mouseDown)
377 return;
378
379 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
380 var node = d3.select(document.getElementById(data.dpid));
381 node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]);
382 data.highlighted = false;
383 data.target = false;
384 }
385
Paul Greyson8d1c6362013-03-27 13:05:24 -0700386 function mouseDownSwitch(data) {
387 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700388 data.mouseDown = true;
389 }
390
Paul Greyson8d1c6362013-03-27 13:05:24 -0700391 function mouseUpSwitch(data) {
392 if (data.mouseDown) {
393 data.mouseDown = false;
394 d3.event.stopPropagation();
395 }
396 }
397
398 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700399 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700400 if (data.state == 'ACTIVE') {
401 var prompt = 'Deactivate ' + data.dpid + '?';
402 if (confirm(prompt)) {
403 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700404 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700405 }
406 } else {
407 var prompt = 'Activate ' + data.dpid + '?';
408 if (confirm(prompt)) {
409 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700410 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700411 }
412 }
413 }
414
Paul Greyson740bdaf2013-03-18 16:10:48 -0700415 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700416 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700417 return;
418 }
419
Paul Greysonc17278a2013-03-23 10:17:12 -0700420 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700421 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700422 .data(data, function (data) {
423 return data.dpid;
424 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700425 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700426 .attr("id", function (data, i) {
427 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700428 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700429 .attr("transform", function(data, i) {
430 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700431 });
432
Paul Greysonc17278a2013-03-23 10:17:12 -0700433 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700434 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700435 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700436 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700437 if (data.scale) {
438 m = m.scale(data.scale);
439 }
440 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700441 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700442 .attr("x", function (data) {
443 return -data.width / 2;
444 })
445 .attr("y", function (data) {
446 return -data.width / 2;
447 })
448 .attr("r", function (data) {
449 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700450 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700451
Paul Greysonc17278a2013-03-23 10:17:12 -0700452 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700453 nodes.on('mouseover', mouseOverSwitch);
454 nodes.on('mouseout', mouseOutSwitch);
455 nodes.on('mouseup', mouseUpSwitch);
456 nodes.on('mousedown', mouseDownSwitch);
457
458 // only do switch up/down for core switches
459 if (i == 2) {
460 nodes.on('dblclick', doubleClickSwitch);
461 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700462 }
463
Paul Greysonc17278a2013-03-23 10:17:12 -0700464 // append switches
465 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700466 .attr("class", "ring")
467 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700468
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700469
Paul Greysonc17278a2013-03-23 10:17:12 -0700470 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700471 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700472 .data(data, function (data) {
473 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700474 });
Paul Greyson347fb742013-03-27 13:40:29 -0700475 nodes.select('circle')
476 .each(function (data) {
477 // if there's a pending state changed and then the state changes, clear the pending class
478 var circle = d3.select(this);
479 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
480 data.state === 'INACTIVE' && circle.classed('active')) {
481 circle.classed('pending', false);
482 }
483 })
484 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700485 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700486 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700487 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700488 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700489 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700490 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700491 }
492
493 // update switches
494 rings.each(ringUpdate);
495
Paul Greyson968d1b42013-03-23 16:58:41 -0700496
497 // Now setup the labels
498 // This is done separately because SVG draws in node order and we want the labels
499 // always on top
500 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
501
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700502 d3.select(document.body).on('mouseup', function () {
503 function clearHighlight() {
504 svg.selectAll('circle').each(function (data) {
505 data.mouseDown = false;
Paul Greyson8d1c6362013-03-27 13:05:24 -0700506 mouseOutSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700507 })
508 };
509
Paul Greyson084779b2013-03-27 13:55:49 -0700510 function removeLink(link) {
511 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
512 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
513
514 if (path1) {
515 setPending(d3.select(path1));
516 }
517 if (path2) {
518 setPending(d3.select(path2));
519 }
520
521 linkDown(link);
522 }
523
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700524
525 var highlighted = svg.selectAll('circle.highlight')[0];
526 if (highlighted.length == 2) {
527 var s1Data = d3.select(highlighted[0]).data()[0];
528 var s2Data = d3.select(highlighted[1]).data()[0];
529
530 var srcData, dstData;
531 if (s1Data.target) {
532 dstData = s1Data;
533 srcData = s2Data;
534 } else {
535 dstData = s2Data;
536 srcData = s1Data;
537 }
538
539 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
540 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
541 if (confirm(prompt)) {
542 alert('do create flow');
543 } else {
544 alert('do not create flow');
545 }
546 } else {
547 var map = linkMap[srcData.dpid];
548 if (map && map[dstData.dpid]) {
549 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
550 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700551 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700552 }
553 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700554 map = linkMap[dstData.dpid];
555 if (map && map[srcData.dpid]) {
556 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
557 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700558 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700559 }
560 } else {
561 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
562 if (confirm(prompt)) {
563 linkUp(srcData, dstData);
564 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700565 }
566 }
567 }
568
569 clearHighlight();
570 } else {
571 clearHighlight();
572 }
573
574 });
575
Paul Greyson9066ab02013-03-23 18:15:41 -0700576 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700577 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700578 return;
579 }
580
581 // create the nodes
582 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700583 .data(data, function (data) {
584 return data.dpid;
585 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700586 .enter().append("svg:g")
587 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700588 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700589 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700590 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700591 .attr("transform", function(data, i) {
592 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700593 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700594
595 // add the text nodes which show on mouse over
596 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700597 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700598 .attr("x", function (data) {
599 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
600 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700601 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700602 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700603 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700604 }
605 } else {
606 if (data.className == 'edge') {
607 return data.width*3 + 4;
608 } else {
609 return data.width + 4;
610 }
611 }
612 })
613 .attr("y", function (data) {
614 var y;
615 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
616 if (data.className == 'edge') {
617 y = data.width*3/2 + 4;
618 } else {
619 y = data.width/2 + 4;
620 }
621 } else {
622 if (data.className == 'edge') {
623 y = data.width*3/2 + 4;
624 } else {
625 y = data.width/2 + 4;
626 }
627 }
628 return y - 6;
629 })
630 .attr("text-anchor", function (data) {
631 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
632 return "end";
633 } else {
634 return "start";
635 }
636 })
637 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700638 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
639 if (data.scale) {
640 m = m.scale(data.scale);
641 }
642 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
643 })
644 }
645
646 labelRings.enter().append("svg:g")
647 .attr("class", "textRing")
648 .each(labelRingEnter);
649
Paul Greysonc17278a2013-03-23 10:17:12 -0700650 // switches should not change during operation of the ui so no
651 // rings.exit()
652
653
Paul Greysond1a22d92013-03-19 12:15:19 -0700654 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700655
Paul Greysonc17278a2013-03-23 10:17:12 -0700656 // key on link dpids since these will come/go during demo
Paul Greysonb367de22013-03-23 11:09:11 -0700657 var links = d3.select('svg').selectAll('.link').data(model.links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700658 return d['src-switch']+'->'+d['dst-switch'];
659 });
660
661 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700662 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700663 .attr("class", "link");
664
Paul Greyson084779b2013-03-27 13:55:49 -0700665 links.attr('id', function (d) {
666 return d['src-switch'] + '=>' + d['dst-switch'];
667 })
Paul Greyson56378ed2013-03-26 23:17:36 -0700668 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -0700669 var src = d3.select(document.getElementById(d['src-switch']));
670 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -0700671
Paul Greyson084779b2013-03-27 13:55:49 -0700672 var srcPt = document.querySelector('svg').createSVGPoint();
673 srcPt.x = src.attr('x');
674 srcPt.y = src.attr('y');
675 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700676
Paul Greyson084779b2013-03-27 13:55:49 -0700677 var dstPt = document.querySelector('svg').createSVGPoint();
678 dstPt.x = dst.attr('x');
679 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
680 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700681
Paul Greyson084779b2013-03-27 13:55:49 -0700682 var midPt = document.querySelector('svg').createSVGPoint();
683 midPt.x = (srcPt.x + dstPt.x)/2;
684 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -0700685
Paul Greyson084779b2013-03-27 13:55:49 -0700686 return line([srcPt, midPt, dstPt]);
687 })
688 .attr("marker-mid", function(d) { return "url(#arrow)"; });
Paul Greysonc17278a2013-03-23 10:17:12 -0700689
Paul Greyson56378ed2013-03-26 23:17:36 -0700690
Paul Greysonc17278a2013-03-23 10:17:12 -0700691 // remove old links
692 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700693
Paul Greyson127d7fb2013-03-25 23:39:20 -0700694
695 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700696}
697
698function updateControllers(model) {
699 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700700 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700701 .each(function (c) {
702 controllerColorMap[c] = colors.pop();
703 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
704 })
705 .text(function (d) {
706 return d;
707 });
Paul Greysonbcd3c772013-03-21 13:16:44 -0700708
Paul Greysone262a292013-03-23 10:35:23 -0700709 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700710 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700711 if (model.activeControllers.indexOf(d) != -1) {
712 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700713 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700714 var className = 'controller ' + color;
715 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700716 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700717
Paul Greysone262a292013-03-23 10:35:23 -0700718 // this should never be needed
719 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700720
Paul Greyson8d1c6362013-03-27 13:05:24 -0700721 controllers.on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700722 var allSelected = true;
723 for (var key in controllerColorMap) {
724 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
725 allSelected = false;
726 break;
727 }
728 }
729 if (allSelected) {
730 for (var key in controllerColorMap) {
731 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
732 }
733 } else {
734 for (var key in controllerColorMap) {
735 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
736 }
737 }
738
739 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
740 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700741 });
Paul Greyson8d1c6362013-03-27 13:05:24 -0700742
743
Paul Greyson740bdaf2013-03-18 16:10:48 -0700744}
745
Paul Greyson127d7fb2013-03-25 23:39:20 -0700746function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700747 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700748 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700749// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700750
Paul Greyson56378ed2013-03-26 23:17:36 -0700751 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700752 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700753
754 // fake flows right now
755 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700756 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700757 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
758 selectedFlowsData[i].flow = newModel.flows[i];
759 selectedFlowsData[i].selected = selected;
760 }
761
762 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700763 updateTopology(svg, newModel);
764 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700765// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700766 }
767 updateHeader(newModel);
768
Paul Greyson56378ed2013-03-26 23:17:36 -0700769 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700770
771 // do it again in 1s
772 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700773 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700774 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700775 });
776}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700777
Paul Greyson38d8bde2013-03-22 22:07:35 -0700778svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700779selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700780// workaround for Chrome v25 bug
781// if executed immediately, the view box transform logic doesn't work properly
782// fixed in Chrome v27
783setTimeout(function () {
784 // workaround for another Chrome v25 bug
785 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700786 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700787 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700788 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700789}, 100);