blob: d4cb18f4a786bbc007bfb47ce0bd24f2b15508b2 [file] [log] [blame]
Paul Greyson127d7fb2013-03-25 23:39:20 -07001/*global d3, document∆*/
Paul Greyson740bdaf2013-03-18 16:10:48 -07002
Paul Greyson6d9ed862013-03-23 17:37:15 -07003d3.selection.prototype.moveToFront = function() {
4 return this.each(function(){
5 this.parentNode.appendChild(this);
6 });
7};
Paul Greysond1a22d92013-03-19 12:15:19 -07008
Paul Greyson127d7fb2013-03-25 23:39:20 -07009var line = d3.svg.line()
10 .x(function(d) {
11 return d.x;
12 })
13 .y(function(d) {
14 return d.y;
15 });
16
Paul Greyson56378ed2013-03-26 23:17:36 -070017var model;
Paul Greyson127d7fb2013-03-25 23:39:20 -070018var svg, selectedFlowsView;
Paul Greyson56378ed2013-03-26 23:17:36 -070019var updateTopology;
Paul Greyson127d7fb2013-03-25 23:39:20 -070020
Paul Greysond1a22d92013-03-19 12:15:19 -070021var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -070022 'color1',
23 'color2',
24 'color3',
25 'color4',
26 'color5',
27 'color6',
28 'color7',
29 'color8',
30 'color9',
31 'color10',
32 'color11',
Paul Greyson127d7fb2013-03-25 23:39:20 -070033 'color12'
34];
Paul Greyson01a5dff2013-03-19 15:50:14 -070035colors.reverse();
Paul Greysond1a22d92013-03-19 12:15:19 -070036
37var controllerColorMap = {};
38
39
40
Paul Greyson740bdaf2013-03-18 16:10:48 -070041function createTopologyView() {
Paul Greyson56378ed2013-03-26 23:17:36 -070042
43 window.addEventListener('resize', function () {
44 // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied
45 // either that or is it possible to position the paths so they get the automatic transform as well?
46// updateTopology(svg, model);
47 });
48
Paul Greysonb367de22013-03-23 11:09:11 -070049 var svg = d3.select('#svg-container').append('svg:svg');
50
51 svg.append("svg:defs").append("svg:marker")
52 .attr("id", "arrow")
53 .attr("viewBox", "0 -5 10 10")
54 .attr("refX", -1)
55 .attr("markerWidth", 5)
56 .attr("markerHeight", 5)
57 .attr("orient", "auto")
58 .append("svg:path")
Paul Greyson45303ac2013-03-23 16:44:01 -070059 .attr("d", "M0,-3L10,0L0,3");
Paul Greysonb367de22013-03-23 11:09:11 -070060
61 return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070062 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070063}
64
Paul Greyson127d7fb2013-03-25 23:39:20 -070065var selectedFlowsData = [
66 {selected: false, flow: null},
67 {selected: false, flow: null},
Paul Greyson127d7fb2013-03-25 23:39:20 -070068 {selected: false, flow: null}
69];
70
71function drawFlows() {
72 // DRAW THE FLOWS
73 var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) {
74 return d.flow ? d.flow.flowId.value : null;
75 });
76
77 flows.enter().append("svg:path")
78 .attr('class', 'flow')
79 .attr('d', function (d) {
80 if (!d.flow) {
81 return;
82 }
83 var pts = [];
84 d.flow.dataPath.flowEntries.forEach(function (flowEntry) {
85 var s = d3.select(document.getElementById(flowEntry.dpid.value));
86 var pt = document.querySelector('svg').createSVGPoint();
87 pt.x = s.attr('x');
88 pt.y = s.attr('y');
89 pt = pt.matrixTransform(s[0][0].getCTM());
90 pts.push(pt);
91 });
92 return line(pts);
93 })
Paul Greysonacb59412013-03-25 23:48:06 -070094 .attr('stroke-dasharray', '3, 10')
Paul Greyson127d7fb2013-03-25 23:39:20 -070095 .append('svg:animate')
96 .attr('attributeName', 'stroke-dashoffset')
97 .attr('attributeType', 'xml')
98 .attr('from', '500')
99 .attr('to', '-500')
100 .attr('dur', '20s')
101 .attr('repeatCount', 'indefinite');
102
103 flows.style('visibility', function (d) {
104 if (d) {
105 return d.selected ? '' : 'hidden';
106 }
107 })
108
Paul Greyson56378ed2013-03-26 23:17:36 -0700109 // "marching ants"
110 // TODO: this will only be true if there's an iperf session running
Paul Greyson127d7fb2013-03-25 23:39:20 -0700111 flows.select('animate').attr('from', function (d) {
112 if (d.flow) {
113 if (d.selected) {
114 return '500';
115 } else {
116 return '-500';
117 }
118 }
119 });
120}
121
122function updateFlowView() {
123 selectedFlowsView.data(selectedFlowsData);
124
125 selectedFlowsView.classed('selected', function (d) {
126 if (d.flow) {
127 return d.selected;
128 }
129 });
130
131 selectedFlowsView.select('.flowId')
132 .text(function (d) {
133 if (d.flow) {
134 return d.flow.flowId.value;
135 }
136 });
137
138 selectedFlowsView.select('.srcDPID')
139 .text(function (d) {
140 if (d.flow) {
141 return d.flow.dataPath.srcPort.dpid.value;
142 }
143 });
144
145 selectedFlowsView.select('.dstDPID')
146 .text(function (d) {
147 if (d.flow) {
148 return d.flow.dataPath.dstPort.dpid.value;
149 }
150 });
151}
152
153function createFlowView() {
154 function rowEnter(d, i) {
155 var row = d3.select(this);
156
157 row.on('click', function () {
158 selectedFlowsData[i].selected = !selectedFlowsData[i].selected;
159 updateFlowView();
160 drawFlows();
161 });
162
163 row.append('div')
164 .classed('flowIndex', true)
165 .text(function () {
166 return i+1;
167 });
168
169 row.append('div')
170 .classed('flowId', true);
171
172 row.append('div')
173 .classed('srcDPID', true);
174
175 row.append('div')
176 .classed('dstDPID', true);
177
178 row.append('div')
179 .classed('iperf', true);
180 }
181
182 var flows = d3.select('#selectedFlows')
183 .selectAll('.selectedFlow')
184 .data(selectedFlowsData)
185 .enter()
186 .append('div')
187 .classed('selectedFlow', true)
188 .each(rowEnter);
189
190
191 return flows;
192}
193
Paul Greysond1a22d92013-03-19 12:15:19 -0700194function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700195 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700196 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
197 d3.select('#activeFlows').text(model.flows.length);
198}
199
200function toRadians (angle) {
201 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700202}
203
Paul Greysonc17278a2013-03-23 10:17:12 -0700204function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700205 var rings = [{
206 radius: 3,
Paul Greysonde7fad52013-03-19 12:47:32 -0700207 width: 6,
Paul Greyson952ccb62013-03-18 22:22:08 -0700208 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700209 className: 'edge',
210 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700211 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700212 radius: 2.25,
Paul Greysonde7fad52013-03-19 12:47:32 -0700213 width: 12,
Paul Greyson952ccb62013-03-18 22:22:08 -0700214 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700215 className: 'aggregation',
216 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700217 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700218 radius: 0.75,
Paul Greysonde7fad52013-03-19 12:47:32 -0700219 width: 18,
Paul Greyson952ccb62013-03-18 22:22:08 -0700220 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700221 className: 'core',
222 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700223 }];
224
Paul Greysonde7fad52013-03-19 12:47:32 -0700225
226 var aggRanges = {};
227
228 // arrange edge switches at equal increments
229 var k = 360 / rings[0].switches.length;
230 rings[0].switches.forEach(function (s, i) {
231 var angle = k * i;
232
233 rings[0].angles[i] = angle;
234
235 // record the angle for the agg switch layout
236 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700237 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700238 var aggdpid = dpid.join(':');
239 var aggRange = aggRanges[aggdpid];
240 if (!aggRange) {
241 aggRange = aggRanges[aggdpid] = {};
242 aggRange.min = aggRange.max = angle;
243 } else {
244 aggRange.max = angle;
245 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700246 });
247
248 // arrange aggregation switches to "fan out" to edge switches
249 k = 360 / rings[1].switches.length;
250 rings[1].switches.forEach(function (s, i) {
251// rings[1].angles[i] = k * i;
252 var range = aggRanges[s.dpid];
253
Paul Greyson832d2202013-03-21 13:27:56 -0700254 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700255 });
256
Paul Greyson3f890b62013-03-22 17:39:36 -0700257 // find the association between core switches and aggregation switches
258 var aggregationSwitchMap = {};
259 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700260 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700261 });
262
Paul Greyson3f890b62013-03-22 17:39:36 -0700263 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700264 k = 360 / rings[2].switches.length;
265 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700266// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700267 var associatedAggregationSwitches = model.configuration.association[s.dpid];
268 // TODO: go between if there are multiple
269 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
270
271 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700272 });
273
Paul Greyson644d92a2013-03-23 18:00:40 -0700274 // TODO: construct this form initially rather than converting. it works better because
275 // it allows binding by dpid
276 var testRings = [];
277 rings.forEach(function (ring) {
278 var testRing = [];
279 ring.switches.forEach(function (s, i) {
280 var testSwitch = {
281 dpid: s.dpid,
282 state: s.state,
283 radius: ring.radius,
284 width: ring.width,
285 className: ring.className,
286 angle: ring.angles[i],
287 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700288 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700289 testRing.push(testSwitch);
290 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700291
292
Paul Greyson644d92a2013-03-23 18:00:40 -0700293 testRings.push(testRing);
294 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700295
296
Paul Greyson644d92a2013-03-23 18:00:40 -0700297// return rings;
298 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700299}
300
Paul Greyson56378ed2013-03-26 23:17:36 -0700301updateTopology = function(svg, model) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700302
303 // DRAW THE SWITCHES
304 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
305
Paul Greyson740bdaf2013-03-18 16:10:48 -0700306 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700307 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700308 return;
309 }
310
Paul Greysonc17278a2013-03-23 10:17:12 -0700311 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700312 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700313 .data(data, function (data) {
314 return data.dpid;
315 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700316 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700317 .classed('nolabel', true)
Paul Greyson644d92a2013-03-23 18:00:40 -0700318 .attr("id", function (data, i) {
319 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700320 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700321 .attr("transform", function(data, i) {
322 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700323 });
324
Paul Greysonc17278a2013-03-23 10:17:12 -0700325 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700326 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700327 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700328 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700329 if (data.scale) {
330 m = m.scale(data.scale);
331 }
332 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700333 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700334 .attr("x", function (data) {
335 return -data.width / 2;
336 })
337 .attr("y", function (data) {
338 return -data.width / 2;
339 })
340 .attr("r", function (data) {
341 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700342 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700343
Paul Greysonc17278a2013-03-23 10:17:12 -0700344 // setup the mouseover behaviors
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700345 function showLabel(data, index) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700346 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700347 }
348
349 function hideLabel(data, index) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700350 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700351 }
352
353 nodes.on('mouseover', showLabel);
354 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700355 }
356
Paul Greysonc17278a2013-03-23 10:17:12 -0700357 // append switches
358 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700359 .attr("class", "ring")
360 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700361
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700362
Paul Greysonc17278a2013-03-23 10:17:12 -0700363 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700364 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700365 .data(data, function (data) {
366 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700367 });
Paul Greyson644d92a2013-03-23 18:00:40 -0700368 nodes.select('circle').attr('class', function (data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700369 if (data.state === 'ACTIVE') {
Paul Greyson644d92a2013-03-23 18:00:40 -0700370 return data.className + ' ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700371 } else {
372 return data.className + ' ' + 'colorInactive';
373 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700374 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700375 }
376
377 // update switches
378 rings.each(ringUpdate);
379
Paul Greyson968d1b42013-03-23 16:58:41 -0700380
381 // Now setup the labels
382 // This is done separately because SVG draws in node order and we want the labels
383 // always on top
384 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
385
Paul Greyson9066ab02013-03-23 18:15:41 -0700386 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700387 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700388 return;
389 }
390
391 // create the nodes
392 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700393 .data(data, function (data) {
394 return data.dpid;
395 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700396 .enter().append("svg:g")
397 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700398 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700399 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700400 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700401 .attr("transform", function(data, i) {
402 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson968d1b42013-03-23 16:58:41 -0700403 });
404
405 // add the text nodes which show on mouse over
406 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700407 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700408 .attr("x", function (data) {
409 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
410 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700411 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700412 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700413 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700414 }
415 } else {
416 if (data.className == 'edge') {
417 return data.width*3 + 4;
418 } else {
419 return data.width + 4;
420 }
421 }
422 })
423 .attr("y", function (data) {
424 var y;
425 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
426 if (data.className == 'edge') {
427 y = data.width*3/2 + 4;
428 } else {
429 y = data.width/2 + 4;
430 }
431 } else {
432 if (data.className == 'edge') {
433 y = data.width*3/2 + 4;
434 } else {
435 y = data.width/2 + 4;
436 }
437 }
438 return y - 6;
439 })
440 .attr("text-anchor", function (data) {
441 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
442 return "end";
443 } else {
444 return "start";
445 }
446 })
447 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700448 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
449 if (data.scale) {
450 m = m.scale(data.scale);
451 }
452 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
453 })
454 }
455
456 labelRings.enter().append("svg:g")
457 .attr("class", "textRing")
458 .each(labelRingEnter);
459
460
Paul Greysonc17278a2013-03-23 10:17:12 -0700461 // switches should not change during operation of the ui so no
462 // rings.exit()
463
464
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700465 // do mouseover zoom on edge nodes
Paul Greyson6d9ed862013-03-23 17:37:15 -0700466 function zoom(data, index) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700467 var g = d3.select(document.getElementById(data.dpid)).select('circle');
468 g.transition().duration(100).attr("r", data.width*3);
Paul Greyson6d9ed862013-03-23 17:37:15 -0700469 // TODO: this doesn't work because the data binding is by index
Paul Greyson644d92a2013-03-23 18:00:40 -0700470 d3.select(this.parentNode).moveToFront();
Paul Greyson6d9ed862013-03-23 17:37:15 -0700471 }
Paul Greysonf8f43172013-03-18 23:00:30 -0700472
Paul Greyson6d9ed862013-03-23 17:37:15 -0700473 svg.selectAll('.edge').on('mouseover', zoom);
474 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700475
Paul Greyson6d9ed862013-03-23 17:37:15 -0700476 function unzoom(data, index) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700477 var g = d3.select(document.getElementById(data.dpid)).select('circle');
478 g.transition().duration(100).attr("r", data.width);
Paul Greyson6d9ed862013-03-23 17:37:15 -0700479 }
480 svg.selectAll('.edge').on('mouseout', unzoom);
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700481
482
Paul Greysond1a22d92013-03-19 12:15:19 -0700483 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700484
Paul Greysonc17278a2013-03-23 10:17:12 -0700485 // key on link dpids since these will come/go during demo
Paul Greysonb367de22013-03-23 11:09:11 -0700486 var links = d3.select('svg').selectAll('.link').data(model.links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700487 return d['src-switch']+'->'+d['dst-switch'];
488 });
489
490 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700491 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700492 .attr("class", "link");
493
494 links
495 .attr("d", function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700496
Paul Greysond1a22d92013-03-19 12:15:19 -0700497 var src = d3.select(document.getElementById(d['src-switch']));
498 var dst = d3.select(document.getElementById(d['dst-switch']));
499
500 var srcPt = document.querySelector('svg').createSVGPoint();
501 srcPt.x = src.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700502 srcPt.y = src.attr('y');
503 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700504
505 var dstPt = document.querySelector('svg').createSVGPoint();
506 dstPt.x = dst.attr('x');
Paul Greysonb367de22013-03-23 11:09:11 -0700507 dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable
508 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700509
Paul Greysonb367de22013-03-23 11:09:11 -0700510 var midPt = document.querySelector('svg').createSVGPoint();
511 midPt.x = (srcPt.x + dstPt.x)/2;
512 midPt.y = (srcPt.y + dstPt.y)/2;
513
514 return line([srcPt, midPt, dstPt]);
515 })
516 .attr("marker-mid", function(d) { return "url(#arrow)"; });
Paul Greysonc17278a2013-03-23 10:17:12 -0700517
Paul Greyson56378ed2013-03-26 23:17:36 -0700518
Paul Greysonc17278a2013-03-23 10:17:12 -0700519 // remove old links
520 links.exit().remove();
Paul Greyson644d92a2013-03-23 18:00:40 -0700521
Paul Greyson127d7fb2013-03-25 23:39:20 -0700522
523 drawFlows();
Paul Greysond1a22d92013-03-19 12:15:19 -0700524}
525
526function updateControllers(model) {
527 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700528 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -0700529 .each(function (c) {
530 controllerColorMap[c] = colors.pop();
531 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
532 })
533 .text(function (d) {
534 return d;
535 });
Paul Greysonbcd3c772013-03-21 13:16:44 -0700536
Paul Greysone262a292013-03-23 10:35:23 -0700537 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -0700538 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -0700539 if (model.activeControllers.indexOf(d) != -1) {
540 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -0700541 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700542 var className = 'controller ' + color;
543 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700544 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700545
Paul Greysone262a292013-03-23 10:35:23 -0700546 // this should never be needed
547 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700548
Paul Greyson3e142162013-03-19 13:56:17 -0700549 controllers.on('click', function (c, index) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700550 var allSelected = true;
551 for (var key in controllerColorMap) {
552 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
553 allSelected = false;
554 break;
555 }
556 }
557 if (allSelected) {
558 for (var key in controllerColorMap) {
559 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
560 }
561 } else {
562 for (var key in controllerColorMap) {
563 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
564 }
565 }
566
567 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
568 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700569 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700570}
571
Paul Greyson127d7fb2013-03-25 23:39:20 -0700572function sync(svg, selectedFlowsView) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700573 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700574 updateModel(function (newModel) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700575 console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700576
Paul Greyson56378ed2013-03-26 23:17:36 -0700577 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700578 updateControllers(newModel);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700579
580 // fake flows right now
581 var i;
Paul Greyson56378ed2013-03-26 23:17:36 -0700582 for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700583 var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false;
584 selectedFlowsData[i].flow = newModel.flows[i];
585 selectedFlowsData[i].selected = selected;
586 }
587
588 updateFlowView(newModel);
Paul Greysonb48943b2013-03-19 13:27:57 -0700589 updateTopology(svg, newModel);
590 } else {
591 console.log('no change');
592 }
593 updateHeader(newModel);
594
Paul Greyson56378ed2013-03-26 23:17:36 -0700595 model = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700596
597 // do it again in 1s
598 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700599 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700600 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700601 });
602}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700603
Paul Greyson38d8bde2013-03-22 22:07:35 -0700604svg = createTopologyView();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700605selectedFlowsView = createFlowView();
Paul Greyson38d8bde2013-03-22 22:07:35 -0700606// workaround for Chrome v25 bug
607// if executed immediately, the view box transform logic doesn't work properly
608// fixed in Chrome v27
609setTimeout(function () {
610 // workaround for another Chrome v25 bug
611 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -0700612 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -0700613 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700614 sync(svg, selectedFlowsView);
Paul Greyson38d8bde2013-03-22 22:07:35 -0700615}, 100);