blob: daf767c0495375f6dad4b47bd2d08ba0a8b32010 [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 Greyson4e6dc3a2013-03-27 11:37:14 -0700352 if (data.highlighted) {
353 return;
354 }
355
356 // only highlight valid link or flow destination by checking for class of existing highlighted circle
357 var highlighted = svg.selectAll('circle.highlight')[0];
358 if (highlighted.length == 1) {
359 var s = d3.select(highlighted[0]);
360 // only allow links
361 // edge->edge (flow)
362 // aggregation->core
363 // core->core
364 if (data.className == 'edge' && !s.classed('edge') ||
365 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
366 data.className == 'aggregation' && !s.classed('core')) {
367 return;
368 }
369
370 // don't highlight if there's already a link or flow
371 // var map = linkMap[data.dpid];
372 // console.log(map);
373 // console.log(s.data()[0].dpid);
374 // console.log(map[s.data()[0].dpid]);
375 // if (map && map[s.data()[0].dpid]) {
376 // return;
377 // }
378
379 // the second highlighted switch is the target for a link or flow
380 data.target = true;
381 }
382
383
384 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
385 var node = d3.select(document.getElementById(data.dpid));
386 node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core);
387 data.highlighted = true;
388 node.moveToFront();
389 }
390
Paul Greyson8d1c6362013-03-27 13:05:24 -0700391 function mouseOutSwitch(data) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700392 if (data.mouseDown)
393 return;
394
395 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
396 var node = d3.select(document.getElementById(data.dpid));
397 node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]);
398 data.highlighted = false;
399 data.target = false;
400 }
401
Paul Greyson8d1c6362013-03-27 13:05:24 -0700402 function mouseDownSwitch(data) {
403 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700404 data.mouseDown = true;
405 }
406
Paul Greyson8d1c6362013-03-27 13:05:24 -0700407 function mouseUpSwitch(data) {
408 if (data.mouseDown) {
409 data.mouseDown = false;
410 d3.event.stopPropagation();
411 }
412 }
413
414 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700415 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700416 if (data.state == 'ACTIVE') {
417 var prompt = 'Deactivate ' + data.dpid + '?';
418 if (confirm(prompt)) {
419 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700420 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700421 }
422 } else {
423 var prompt = 'Activate ' + data.dpid + '?';
424 if (confirm(prompt)) {
425 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700426 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700427 }
428 }
429 }
430
Paul Greyson740bdaf2013-03-18 16:10:48 -0700431 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700432 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700433 return;
434 }
435
Paul Greysonc17278a2013-03-23 10:17:12 -0700436 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700437 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700438 .data(data, function (data) {
439 return data.dpid;
440 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700441 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700442 .attr("id", function (data, i) {
443 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700444 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700445 .attr("transform", function(data, i) {
446 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700447 });
448
Paul Greysonc17278a2013-03-23 10:17:12 -0700449 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700450 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700451 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700452 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700453 if (data.scale) {
454 m = m.scale(data.scale);
455 }
456 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700457 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700458 .attr("x", function (data) {
459 return -data.width / 2;
460 })
461 .attr("y", function (data) {
462 return -data.width / 2;
463 })
464 .attr("r", function (data) {
465 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700466 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700467
Paul Greysonc17278a2013-03-23 10:17:12 -0700468 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700469 nodes.on('mouseover', mouseOverSwitch);
470 nodes.on('mouseout', mouseOutSwitch);
471 nodes.on('mouseup', mouseUpSwitch);
472 nodes.on('mousedown', mouseDownSwitch);
473
474 // only do switch up/down for core switches
475 if (i == 2) {
476 nodes.on('dblclick', doubleClickSwitch);
477 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700478 }
479
Paul Greysonc17278a2013-03-23 10:17:12 -0700480 // append switches
481 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700482 .attr("class", "ring")
483 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700484
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700485
Paul Greysonc17278a2013-03-23 10:17:12 -0700486 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700487 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700488 .data(data, function (data) {
489 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700490 });
Paul Greyson347fb742013-03-27 13:40:29 -0700491 nodes.select('circle')
492 .each(function (data) {
493 // if there's a pending state changed and then the state changes, clear the pending class
494 var circle = d3.select(this);
495 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
496 data.state === 'INACTIVE' && circle.classed('active')) {
497 circle.classed('pending', false);
498 }
499 })
500 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700501 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700502 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700503 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700504 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700505 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700506 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700507 }
508
509 // update switches
510 rings.each(ringUpdate);
511
Paul Greyson968d1b42013-03-23 16:58:41 -0700512
513 // Now setup the labels
514 // This is done separately because SVG draws in node order and we want the labels
515 // always on top
516 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
517
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700518 d3.select(document.body).on('mouseup', function () {
519 function clearHighlight() {
520 svg.selectAll('circle').each(function (data) {
521 data.mouseDown = false;
Paul Greyson8d1c6362013-03-27 13:05:24 -0700522 mouseOutSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700523 })
524 };
525
Paul Greyson084779b2013-03-27 13:55:49 -0700526 function removeLink(link) {
527 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
528 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
529
530 if (path1) {
531 setPending(d3.select(path1));
532 }
533 if (path2) {
534 setPending(d3.select(path2));
535 }
536
537 linkDown(link);
538 }
539
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700540
541 var highlighted = svg.selectAll('circle.highlight')[0];
542 if (highlighted.length == 2) {
543 var s1Data = d3.select(highlighted[0]).data()[0];
544 var s2Data = d3.select(highlighted[1]).data()[0];
545
546 var srcData, dstData;
547 if (s1Data.target) {
548 dstData = s1Data;
549 srcData = s2Data;
550 } else {
551 dstData = s2Data;
552 srcData = s1Data;
553 }
554
555 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
556 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
557 if (confirm(prompt)) {
558 alert('do create flow');
559 } else {
560 alert('do not create flow');
561 }
562 } else {
563 var map = linkMap[srcData.dpid];
564 if (map && map[dstData.dpid]) {
565 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
566 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700567 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700568 }
569 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700570 map = linkMap[dstData.dpid];
571 if (map && map[srcData.dpid]) {
572 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
573 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700574 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700575 }
576 } else {
577 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
578 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700579 var link1 = {
580 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700581 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700582 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700583 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700584 pending: true
585 };
586 pendingLinks[makeLinkKey(link1)] = link1;
587 var link2 = {
588 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700589 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700590 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700591 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700592 pending: true
593 };
594 pendingLinks[makeLinkKey(link2)] = link2;
595 updateTopology(svg, model);
596
Paul Greyson2913af82013-03-27 14:53:17 -0700597 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700598
599 // remove the pending link after 10s
600 setTimeout(function () {
601 delete pendingLinks[makeLinkKey(link1)];
602 delete pendingLinks[makeLinkKey(link2)];
603
604 updateTopology(svg, model);
605 }, 10000);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700606 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700607 }
608 }
609 }
610
611 clearHighlight();
612 } else {
613 clearHighlight();
614 }
615
616 });
617
Paul Greyson9066ab02013-03-23 18:15:41 -0700618 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700619 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700620 return;
621 }
622
623 // create the nodes
624 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700625 .data(data, function (data) {
626 return data.dpid;
627 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700628 .enter().append("svg:g")
629 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700630 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700631 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700632 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700633 .attr("transform", function(data, i) {
634 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700635 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700636
637 // add the text nodes which show on mouse over
638 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700639 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700640 .attr("x", function (data) {
641 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
642 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700643 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700644 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700645 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700646 }
647 } else {
648 if (data.className == 'edge') {
649 return data.width*3 + 4;
650 } else {
651 return data.width + 4;
652 }
653 }
654 })
655 .attr("y", function (data) {
656 var y;
657 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
658 if (data.className == 'edge') {
659 y = data.width*3/2 + 4;
660 } else {
661 y = data.width/2 + 4;
662 }
663 } else {
664 if (data.className == 'edge') {
665 y = data.width*3/2 + 4;
666 } else {
667 y = data.width/2 + 4;
668 }
669 }
670 return y - 6;
671 })
672 .attr("text-anchor", function (data) {
673 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
674 return "end";
675 } else {
676 return "start";
677 }
678 })
679 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700680 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
681 if (data.scale) {
682 m = m.scale(data.scale);
683 }
684 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
685 })
686 }
687
688 labelRings.enter().append("svg:g")
689 .attr("class", "textRing")
690 .each(labelRingEnter);
691
Paul Greysonc17278a2013-03-23 10:17:12 -0700692 // switches should not change during operation of the ui so no
693 // rings.exit()
694
695
Paul Greysond1a22d92013-03-19 12:15:19 -0700696 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700697
Paul Greysonc17278a2013-03-23 10:17:12 -0700698 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -0700699 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700700 return d['src-switch']+'->'+d['dst-switch'];
701 });
702
703 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700704 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700705 .attr("class", "link");
706
Paul Greyson084779b2013-03-27 13:55:49 -0700707 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700708 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -0700709 })
Paul Greyson56378ed2013-03-26 23:17:36 -0700710 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -0700711 var src = d3.select(document.getElementById(d['src-switch']));
712 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -0700713
Paul Greyson084779b2013-03-27 13:55:49 -0700714 var srcPt = document.querySelector('svg').createSVGPoint();
715 srcPt.x = src.attr('x');
716 srcPt.y = src.attr('y');
717 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700718
Paul Greyson084779b2013-03-27 13:55:49 -0700719 var dstPt = document.querySelector('svg').createSVGPoint();
720 dstPt.x = dst.attr('x');
721 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
722 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700723
Paul Greyson084779b2013-03-27 13:55:49 -0700724 var midPt = document.querySelector('svg').createSVGPoint();
725 midPt.x = (srcPt.x + dstPt.x)/2;
726 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -0700727
Paul Greyson084779b2013-03-27 13:55:49 -0700728 return line([srcPt, midPt, dstPt]);
729 })
Paul Greyson40c8a592013-03-27 14:10:33 -0700730 .attr("marker-mid", function(d) { return "url(#arrow)"; })
731 .classed('pending', function (d) {
732 return d.pending;
733 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700734
Paul Greyson56378ed2013-03-26 23:17:36 -0700735
Paul Greysonc17278a2013-03-23 10:17:12 -0700736 // remove old links
737 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700738
Paul Greyson127d7fb2013-03-25 23:39:20 -0700739
740 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700741}
742
743function updateControllers(model) {
744 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700745 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700746 .each(function (c) {
747 controllerColorMap[c] = colors.pop();
748 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
749 })
750 .text(function (d) {
751 return d;
Paul Greyson2913af82013-03-27 14:53:17 -0700752 })
753 .append('div')
754 .attr('class', 'controllerEye');
Paul Greysonbcd3c772013-03-21 13:16:44 -0700755
Paul Greysone262a292013-03-23 10:35:23 -0700756 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700757 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700758 if (model.activeControllers.indexOf(d) != -1) {
759 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700760 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700761 var className = 'controller ' + color;
762 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700763 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700764
Paul Greysone262a292013-03-23 10:35:23 -0700765 // this should never be needed
766 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700767
Paul Greyson2913af82013-03-27 14:53:17 -0700768 controllers.on('dblclick', function (c) {
769 if (model.activeControllers.indexOf(c) != -1) {
770 var prompt = 'Dectivate ' + c + '?';
771 if (confirm(prompt)) {
772 controllerDown(c);
773 setPending(d3.select(this));
774 };
775 } else {
776 var prompt = 'Activate ' + c + '?';
777 if (confirm(prompt)) {
778 controllerUp(c);
779 setPending(d3.select(this));
780 };
781 }
782 });
783
784 controllers.select('.controllerEye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700785 var allSelected = true;
786 for (var key in controllerColorMap) {
787 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
788 allSelected = false;
789 break;
790 }
791 }
792 if (allSelected) {
793 for (var key in controllerColorMap) {
794 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
795 }
796 } else {
797 for (var key in controllerColorMap) {
798 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
799 }
800 }
801
802 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
803 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700804 });
Paul Greyson8d1c6362013-03-27 13:05:24 -0700805
806
Paul Greyson740bdaf2013-03-18 16:10:48 -0700807}
808
Paul Greyson127d7fb2013-03-25 23:39:20 -0700809function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700810 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700811 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700812// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700813
Paul Greyson56378ed2013-03-26 23:17:36 -0700814 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700815 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700816
817 // fake flows right now
818 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700819 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700820 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
821 selectedFlowsData[i].flow = newModel.flows[i];
822 selectedFlowsData[i].selected = selected;
823 }
824
825 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700826 updateTopology(svg, newModel);
827 } else {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700828// console.log('no change');
Paul Greysonb48943b2013-03-19 13:27:57 -0700829 }
830 updateHeader(newModel);
831
Paul Greyson56378ed2013-03-26 23:17:36 -0700832 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700833
834 // do it again in 1s
835 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700836 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700837 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700838 });
839}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700840
Paul Greyson38d8bde2013-03-22 22:07:35 -0700841svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700842selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700843// workaround for Chrome v25 bug
844// if executed immediately, the view box transform logic doesn't work properly
845// fixed in Chrome v27
846setTimeout(function () {
847 // workaround for another Chrome v25 bug
848 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700849 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700850 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700851 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700852}, 100);