blob: 695e4bb292f6eb7ec0765059561dcdfa29e2f3e5 [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 Greyson29aa98d2013-03-28 00:09:31 -070018var svg;
Paul Greyson56378ed2013-03-26 23:17:36 -070019var updateTopology;
Paul Greyson40c8a592013-03-27 14:10:33 -070020var pendingLinks = {};
Paul Greyson6f918402013-03-28 12:18:30 -070021var selectedFlows = [];
22
Paul Greyson97cc9f52013-04-04 01:45:40 -070023var pendingTimeout = 30000;
Paul Greyson127d7fb2013-03-25 23:39:20 -070024
Paul Greysond1a22d92013-03-19 12:15:19 -070025var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -070026 'color1',
27 'color2',
28 'color3',
29 'color4',
Paul Greyson3e142162013-03-19 13:56:17 -070030 'color7',
31 'color8',
32 'color9',
Paul Greyson5cc35f02013-03-28 10:07:36 -070033// '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?
Paul Greyson5cc35f02013-03-28 10:07:36 -070052// updateTopology();
Paul Greyson56378ed2013-03-26 23:17:36 -070053 });
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 Greyson13f02b92013-03-28 11:29:35 -070071function updateSelectedFlowsTopology() {
Paul Greyson127d7fb2013-03-25 23:39:20 -070072 // DRAW THE FLOWS
Paul Greyson97cc9f52013-04-04 01:45:40 -070073 var topologyFlows = [];
74 selectedFlows.forEach(function (flow) {
75 if (flow) {
76 topologyFlows.push(flow);
77 }
78 });
79
80 var flows = d3.select('svg').selectAll('.flow').data(topologyFlows);
Paul Greyson127d7fb2013-03-25 23:39:20 -070081
Paul Greyson29aa98d2013-03-28 00:09:31 -070082 flows.enter().append("svg:path").attr('class', 'flow')
83 .attr('stroke-dasharray', '4, 10')
84 .append('svg:animate')
85 .attr('attributeName', 'stroke-dashoffset')
86 .attr('attributeType', 'xml')
87 .attr('from', '500')
88 .attr('to', '-500')
89 .attr('dur', '20s')
90 .attr('repeatCount', 'indefinite');
91
Paul Greyson97cc9f52013-04-04 01:45:40 -070092 flows.exit().remove();
Paul Greyson29aa98d2013-03-28 00:09:31 -070093
94 flows.attr('d', function (d) {
Paul Greyson13f02b92013-03-28 11:29:35 -070095 if (!d) {
96 return;
97 }
98 var pts = [];
Paul Greyson280a2752013-04-04 03:21:59 -070099 if (!d.dataPath.flowEntries || !d.dataPath.flowEntries.length) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700100 // create a temporary vector to indicate the pending flow
101 var s1 = d3.select(document.getElementById(d.dataPath.srcPort.dpid.value));
102 var s2 = d3.select(document.getElementById(d.dataPath.dstPort.dpid.value));
103
104 var pt1 = document.querySelector('svg').createSVGPoint();
105 pt1.x = s1.attr('x');
106 pt1.y = s1.attr('y');
107 pt1 = pt1.matrixTransform(s1[0][0].getCTM());
108 pts.push(pt1);
109
110 var pt2 = document.querySelector('svg').createSVGPoint();
111 pt2.x = s2.attr('x');
112 pt2.y = s2.attr('y');
113 pt2 = pt2.matrixTransform(s2[0][0].getCTM());
114 pts.push(pt2);
115
116 } else {
117 d.dataPath.flowEntries.forEach(function (flowEntry) {
118 var s = d3.select(document.getElementById(flowEntry.dpid.value));
119 // s[0] is null if the flow entry refers to a non-existent switch
120 if (s[0][0]) {
121 var pt = document.querySelector('svg').createSVGPoint();
122 pt.x = s.attr('x');
123 pt.y = s.attr('y');
124 pt = pt.matrixTransform(s[0][0].getCTM());
125 pts.push(pt);
126 } else {
127 console.log('flow refers to non-existent switch: ' + flowEntry.dpid.value);
128 }
129 });
130 }
131 return line(pts);
132 })
Paul Greyson71b550d2013-03-28 11:56:19 -0700133 .attr('id', function (d) {
134 if (d) {
135 return makeFlowKey(d);
136 }
137 })
Paul Greyson13f02b92013-03-28 11:29:35 -0700138 .classed('pending', function (d) {
Paul Greysonaa812562013-03-28 12:43:12 -0700139 return d && (d.createPending || d.deletePending);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700140 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700141
Paul Greyson56378ed2013-03-26 23:17:36 -0700142 // "marching ants"
Paul Greyson29aa98d2013-03-28 00:09:31 -0700143 flows.select('animate').attr('from', 500);
144
Paul Greyson13f02b92013-03-28 11:29:35 -0700145}
146
147function updateSelectedFlowsTable() {
148 function rowEnter(d) {
149 var row = d3.select(this);
Paul Greyson6f918402013-03-28 12:18:30 -0700150 row.append('div').classed('deleteFlow', true);
Paul Greyson13f02b92013-03-28 11:29:35 -0700151 row.append('div').classed('flowId', true);
152 row.append('div').classed('srcDPID', true);
153 row.append('div').classed('dstDPID', true);
154 row.append('div').classed('iperf', true);
Paul Greyson71b550d2013-03-28 11:56:19 -0700155
156 row.on('mouseover', function (d) {
157 if (d) {
158 var path = document.getElementById(makeFlowKey(d));
159 d3.select(path).classed('highlight', true);
160 }
161 });
162 row.on('mouseout', function (d) {
163 if (d) {
164 var path = document.getElementById(makeFlowKey(d));
165 d3.select(path).classed('highlight', false);
166 }
167 })
Paul Greyson13f02b92013-03-28 11:29:35 -0700168 }
169
170 function rowUpdate(d) {
171 var row = d3.select(this);
Paul Greyson6f918402013-03-28 12:18:30 -0700172 row.select('.deleteFlow').on('click', function () {
Paul Greysonbe8bf872013-04-04 01:53:45 -0700173 selectedFlows[selectedFlows.indexOf(d)] = null;
174 updateSelectedFlows();
175 });
176 row.on('dblclick', function () {
Paul Greyson6f918402013-03-28 12:18:30 -0700177 if (d) {
178 var prompt = 'Delete flow ' + d.flowId.value + '?';
179 if (confirm(prompt)) {
180 deleteFlow(d);
Paul Greysonaa812562013-03-28 12:43:12 -0700181 d.deletePending = true;
Paul Greyson6f918402013-03-28 12:18:30 -0700182 updateSelectedFlows();
183
184 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700185 d.deletePending = false;
Paul Greyson6f918402013-03-28 12:18:30 -0700186 updateSelectedFlows();
187 }, pendingTimeout)
188 };
189 }
190 });
191
Paul Greyson13f02b92013-03-28 11:29:35 -0700192 row.select('.flowId')
193 .text(function (d) {
194 if (d) {
195 if (d.flowId) {
196 return d.flowId.value;
197 } else {
198 return '0x--';
199 }
200 }
201 })
Paul Greysoncb5d30d2013-04-04 02:00:56 -0700202 .classed('pending', function (d) {
203 return d && (d.createPending || d.deletePending);
204 });
Paul Greyson13f02b92013-03-28 11:29:35 -0700205
206 row.select('.srcDPID')
207 .text(function (d) {
208 if (d) {
209 return d.dataPath.srcPort.dpid.value;
210 }
211 });
212
213 row.select('.dstDPID')
214 .text(function (d) {
215 if (d) {
216 return d.dataPath.dstPort.dpid.value;
217 }
218 });
219 }
220
221 var flows = d3.select('#selectedFlows')
222 .selectAll('.selectedFlow')
223 .data(selectedFlows);
224
225 flows.enter()
226 .append('div')
227 .classed('selectedFlow', true)
228 .each(rowEnter);
229
230 flows.each(rowUpdate);
231
Paul Greyson29aa98d2013-03-28 00:09:31 -0700232 flows.exit().remove();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700233}
234
Paul Greyson13f02b92013-03-28 11:29:35 -0700235function updateSelectedFlows() {
236 // make sure that all of the selected flows are either
237 // 1) valid (meaning they are in the latest list of flows)
238 // 2) pending
239 if (model) {
240 var flowMap = {};
241 model.flows.forEach(function (flow) {
242 flowMap[makeFlowKey(flow)] = flow;
243 });
244
245 var newSelectedFlows = [];
246 selectedFlows.forEach(function (flow) {
247 if (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700248 var liveFlow = flowMap[makeFlowKey(flow)];
249 if (liveFlow) {
250 newSelectedFlows.push(liveFlow);
Paul Greysonaa812562013-03-28 12:43:12 -0700251 liveFlow.deletePending = flow.deletePending;
252 } else if (flow.createPending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700253 newSelectedFlows.push(flow);
Paul Greyson13f02b92013-03-28 11:29:35 -0700254 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700255 }
256 });
257 selectedFlows = newSelectedFlows;
258 }
Paul Greyson6f918402013-03-28 12:18:30 -0700259 while (selectedFlows.length < 3) {
260 selectedFlows.push(null);
261 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700262
263 updateSelectedFlowsTable();
264 updateSelectedFlowsTopology();
265}
266
267function selectFlow(flow) {
Paul Greysonc30f75e2013-03-28 11:45:15 -0700268 var flowKey = makeFlowKey(flow);
269 var alreadySelected = false;
270 selectedFlows.forEach(function (f) {
271 if (f && makeFlowKey(f) === flowKey) {
272 alreadySelected = true;
273 }
274 });
275
276 if (!alreadySelected) {
277 selectedFlows.unshift(flow);
278 selectedFlows = selectedFlows.slice(0, 3);
279 updateSelectedFlows();
280 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700281}
282
Paul Greysonaa812562013-03-28 12:43:12 -0700283function deselectFlow(flow, ifCreatePending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700284 var flowKey = makeFlowKey(flow);
285 var newSelectedFlows = [];
286 selectedFlows.forEach(function (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700287 if (!flow ||
288 flowKey !== makeFlowKey(flow) ||
Paul Greysonaa812562013-03-28 12:43:12 -0700289 flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700290 newSelectedFlows.push(flow);
291 }
292 });
293 selectedFlows = newSelectedFlows;
294 while (selectedFlows.length < 3) {
295 selectedFlows.push(null);
296 }
297
298 updateSelectedFlows();
299}
300
Paul Greysonaa812562013-03-28 12:43:12 -0700301function deselectFlowIfCreatePending(flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700302 deselectFlow(flow, true);
303}
304
Paul Greyson29aa98d2013-03-28 00:09:31 -0700305function showFlowChooser() {
306 function rowEnter(d) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700307 var row = d3.select(this);
308
Paul Greyson127d7fb2013-03-25 23:39:20 -0700309 row.append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -0700310 .classed('black-eye', true).
Paul Greyson29aa98d2013-03-28 00:09:31 -0700311 on('click', function () {
Paul Greyson13f02b92013-03-28 11:29:35 -0700312 selectFlow(d);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700313 });
314
315 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700316 .classed('flowId', true)
317 .text(function (d) {
318 return d.flowId.value;
319 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700320
321 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700322 .classed('srcDPID', true)
323 .text(function (d) {
324 return d.dataPath.srcPort.dpid.value;
325 });
326
Paul Greyson127d7fb2013-03-25 23:39:20 -0700327
328 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700329 .classed('dstDPID', true)
330 .text(function (d) {
331 return d.dataPath.dstPort.dpid.value;
332 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700333
Paul Greyson127d7fb2013-03-25 23:39:20 -0700334 }
335
Paul Greyson29aa98d2013-03-28 00:09:31 -0700336 var flows = d3.select('#flowChooser')
337 .append('div')
338 .style('pointer-events', 'auto')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700339 .selectAll('.selectedFlow')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700340 .data(model.flows)
Paul Greyson127d7fb2013-03-25 23:39:20 -0700341 .enter()
342 .append('div')
343 .classed('selectedFlow', true)
344 .each(rowEnter);
345
Paul Greyson29aa98d2013-03-28 00:09:31 -0700346 setTimeout(function () {
347 d3.select(document.body).on('click', function () {
348 d3.select('#flowChooser').html('');
349 d3.select(document.body).on('click', null);
350 });
351 }, 0);
352}
353
Paul Greyson29aa98d2013-03-28 00:09:31 -0700354
Paul Greyson127d7fb2013-03-25 23:39:20 -0700355
Paul Greysond1a22d92013-03-19 12:15:19 -0700356function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700357 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700358 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
359 d3.select('#activeFlows').text(model.flows.length);
360}
361
362function toRadians (angle) {
363 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700364}
365
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700366var widths = {
367 edge: 6,
368 aggregation: 12,
369 core: 18
370}
371
Paul Greysonc17278a2013-03-23 10:17:12 -0700372function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700373 var rings = [{
374 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700375 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700376 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700377 className: 'edge',
378 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700379 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700380 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700381 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700382 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700383 className: 'aggregation',
384 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700385 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700386 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700387 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700388 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700389 className: 'core',
390 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700391 }];
392
Paul Greysonde7fad52013-03-19 12:47:32 -0700393
394 var aggRanges = {};
395
396 // arrange edge switches at equal increments
397 var k = 360 / rings[0].switches.length;
398 rings[0].switches.forEach(function (s, i) {
399 var angle = k * i;
400
401 rings[0].angles[i] = angle;
402
403 // record the angle for the agg switch layout
404 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700405 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700406 var aggdpid = dpid.join(':');
407 var aggRange = aggRanges[aggdpid];
408 if (!aggRange) {
409 aggRange = aggRanges[aggdpid] = {};
410 aggRange.min = aggRange.max = angle;
411 } else {
412 aggRange.max = angle;
413 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700414 });
415
416 // arrange aggregation switches to "fan out" to edge switches
417 k = 360 / rings[1].switches.length;
418 rings[1].switches.forEach(function (s, i) {
419// rings[1].angles[i] = k * i;
420 var range = aggRanges[s.dpid];
421
Paul Greyson832d2202013-03-21 13:27:56 -0700422 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700423 });
424
Paul Greyson3f890b62013-03-22 17:39:36 -0700425 // find the association between core switches and aggregation switches
426 var aggregationSwitchMap = {};
427 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700428 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700429 });
430
Paul Greyson3f890b62013-03-22 17:39:36 -0700431 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700432 k = 360 / rings[2].switches.length;
433 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700434// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700435 var associatedAggregationSwitches = model.configuration.association[s.dpid];
436 // TODO: go between if there are multiple
437 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
438
439 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700440 });
441
Paul Greyson644d92a2013-03-23 18:00:40 -0700442 // TODO: construct this form initially rather than converting. it works better because
443 // it allows binding by dpid
444 var testRings = [];
445 rings.forEach(function (ring) {
446 var testRing = [];
447 ring.switches.forEach(function (s, i) {
448 var testSwitch = {
449 dpid: s.dpid,
450 state: s.state,
451 radius: ring.radius,
452 width: ring.width,
453 className: ring.className,
454 angle: ring.angles[i],
455 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700456 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700457 testRing.push(testSwitch);
458 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700459
460
Paul Greyson644d92a2013-03-23 18:00:40 -0700461 testRings.push(testRing);
462 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700463
464
Paul Greyson644d92a2013-03-23 18:00:40 -0700465// return rings;
466 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700467}
468
Paul Greyson40c8a592013-03-27 14:10:33 -0700469function makeLinkKey(link) {
470 return link['src-switch'] + '=>' + link['dst-switch'];
471}
472
Paul Greyson13f02b92013-03-28 11:29:35 -0700473function makeFlowKey(flow) {
474 return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value;
475}
476
Paul Greyson40c8a592013-03-27 14:10:33 -0700477function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700478 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700479 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700480 var srcDPID = link['src-switch'];
481 var dstDPID = link['dst-switch'];
482
483 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700484
Paul Greyson8d1c6362013-03-27 13:05:24 -0700485 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700486
487 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700488 });
489 return linkMap;
490}
491
Paul Greyson5cc35f02013-03-28 10:07:36 -0700492// removes links from the pending list that are now in the model
493function reconcilePendingLinks(model) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700494 var links = [];
495 model.links.forEach(function (link) {
496 links.push(link);
497 delete pendingLinks[makeLinkKey(link)]
498 })
499 var linkId;
500 for (linkId in pendingLinks) {
501 links.push(pendingLinks[linkId]);
502 }
Paul Greyson5cc35f02013-03-28 10:07:36 -0700503 return links
504}
Paul Greyson40c8a592013-03-27 14:10:33 -0700505
Paul Greyson5cc35f02013-03-28 10:07:36 -0700506updateTopology = function() {
507
508 // DRAW THE SWITCHES
509 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
510
511 var links = reconcilePendingLinks(model);
Paul Greyson40c8a592013-03-27 14:10:33 -0700512 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700513
Paul Greyson8d1c6362013-03-27 13:05:24 -0700514 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700515
516 d3.event.preventDefault();
517
Paul Greyson5cc35f02013-03-28 10:07:36 -0700518 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
519
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700520 if (data.highlighted) {
521 return;
522 }
523
524 // only highlight valid link or flow destination by checking for class of existing highlighted circle
Paul Greyson421bfcd2013-03-27 22:22:09 -0700525 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700526 if (highlighted.length == 1) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700527 var s = d3.select(highlighted[0]).select('circle');
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700528 // only allow links
529 // edge->edge (flow)
530 // aggregation->core
531 // core->core
532 if (data.className == 'edge' && !s.classed('edge') ||
533 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
534 data.className == 'aggregation' && !s.classed('core')) {
535 return;
536 }
537
538 // don't highlight if there's already a link or flow
539 // var map = linkMap[data.dpid];
540 // console.log(map);
541 // console.log(s.data()[0].dpid);
542 // console.log(map[s.data()[0].dpid]);
543 // if (map && map[s.data()[0].dpid]) {
544 // return;
545 // }
546
547 // the second highlighted switch is the target for a link or flow
548 data.target = true;
549 }
550
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700551 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700552 node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700553 data.highlighted = true;
554 node.moveToFront();
555 }
556
Paul Greyson8d1c6362013-03-27 13:05:24 -0700557 function mouseOutSwitch(data) {
Paul Greyson5cc35f02013-03-28 10:07:36 -0700558 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
559
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700560 if (data.mouseDown)
561 return;
562
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700563 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700564 node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700565 data.highlighted = false;
566 data.target = false;
567 }
568
Paul Greyson8d1c6362013-03-27 13:05:24 -0700569 function mouseDownSwitch(data) {
570 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700571 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700572 d3.select('#topology').classed('linking', true);
573
Paul Greyson421bfcd2013-03-27 22:22:09 -0700574 d3.select('svg')
575 .append('svg:path')
576 .attr('id', 'linkVector')
577 .attr('d', function () {
578 var s = d3.select(document.getElementById(data.dpid));
579
580 var pt = document.querySelector('svg').createSVGPoint();
581 pt.x = s.attr('x');
582 pt.y = s.attr('y');
583 pt = pt.matrixTransform(s[0][0].getCTM());
584
585 return line([pt, pt]);
586 });
587
588
Paul Greyson72f18852013-03-27 15:56:11 -0700589 if (data.className === 'core') {
590 d3.selectAll('.edge').classed('nodrop', true);
591 }
592 if (data.className === 'edge') {
593 d3.selectAll('.core').classed('nodrop', true);
594 d3.selectAll('.aggregation').classed('nodrop', true);
595 }
596 if (data.className === 'aggregation') {
597 d3.selectAll('.edge').classed('nodrop', true);
598 d3.selectAll('.aggregation').classed('nodrop', true);
599 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700600 }
601
Paul Greyson8d1c6362013-03-27 13:05:24 -0700602 function mouseUpSwitch(data) {
603 if (data.mouseDown) {
604 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700605 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700606 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700607 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700608 }
609 }
610
611 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700612 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700613 if (data.state == 'ACTIVE') {
614 var prompt = 'Deactivate ' + data.dpid + '?';
615 if (confirm(prompt)) {
616 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700617 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700618 }
619 } else {
620 var prompt = 'Activate ' + data.dpid + '?';
621 if (confirm(prompt)) {
622 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700623 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700624 }
625 }
626 }
627
Paul Greyson740bdaf2013-03-18 16:10:48 -0700628 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700629 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700630 return;
631 }
632
Paul Greysonc17278a2013-03-23 10:17:12 -0700633 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700634 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700635 .data(data, function (data) {
636 return data.dpid;
637 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700638 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700639 .attr("id", function (data, i) {
640 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700641 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700642 .attr("transform", function(data, i) {
643 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700644 });
645
Paul Greysonc17278a2013-03-23 10:17:12 -0700646 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700647 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700648 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700649 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700650 if (data.scale) {
651 m = m.scale(data.scale);
652 }
653 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700654 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700655 .attr("x", function (data) {
656 return -data.width / 2;
657 })
658 .attr("y", function (data) {
659 return -data.width / 2;
660 })
661 .attr("r", function (data) {
662 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700663 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700664
Paul Greysonc17278a2013-03-23 10:17:12 -0700665 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700666 nodes.on('mouseover', mouseOverSwitch);
667 nodes.on('mouseout', mouseOutSwitch);
668 nodes.on('mouseup', mouseUpSwitch);
669 nodes.on('mousedown', mouseDownSwitch);
670
671 // only do switch up/down for core switches
672 if (i == 2) {
673 nodes.on('dblclick', doubleClickSwitch);
674 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700675 }
676
Paul Greysonc17278a2013-03-23 10:17:12 -0700677 // append switches
678 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700679 .attr("class", "ring")
680 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700681
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700682
Paul Greysonc17278a2013-03-23 10:17:12 -0700683 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700684 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700685 .data(data, function (data) {
686 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700687 });
Paul Greyson347fb742013-03-27 13:40:29 -0700688 nodes.select('circle')
689 .each(function (data) {
690 // if there's a pending state changed and then the state changes, clear the pending class
691 var circle = d3.select(this);
692 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
693 data.state === 'INACTIVE' && circle.classed('active')) {
694 circle.classed('pending', false);
695 }
696 })
697 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700698 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700699 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700700 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700701 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700702 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700703 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700704 }
705
706 // update switches
707 rings.each(ringUpdate);
708
Paul Greyson968d1b42013-03-23 16:58:41 -0700709
710 // Now setup the labels
711 // This is done separately because SVG draws in node order and we want the labels
712 // always on top
713 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
714
Paul Greyson421bfcd2013-03-27 22:22:09 -0700715 d3.select(document.body).on('mousemove', function () {
716 if (!d3.select('#topology').classed('linking')) {
717 return;
718 }
719 var linkVector = document.getElementById('linkVector');
720 if (!linkVector) {
721 return;
722 }
723 linkVector = d3.select(linkVector);
724
725 var highlighted = svg.selectAll('.highlight')[0];
726 var s1 = null, s2 = null;
727 if (highlighted.length > 1) {
728 var s1 = d3.select(highlighted[0]);
729 var s2 = d3.select(highlighted[1]);
730
731 } else if (highlighted.length > 0) {
732 var s1 = d3.select(highlighted[0]);
733 }
734 var src = s1;
735 if (s2 && !s2.data()[0].target) {
736 src = s2;
737 }
738 if (src) {
739 linkVector.attr('d', function () {
740 var srcPt = document.querySelector('svg').createSVGPoint();
741 srcPt.x = src.attr('x');
742 srcPt.y = src.attr('y');
743 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
744
745 var svg = document.getElementById('topology');
746 var mouse = d3.mouse(viewbox);
747 var dstPt = document.querySelector('svg').createSVGPoint();
748 dstPt.x = mouse[0];
749 dstPt.y = mouse[1];
750 dstPt = dstPt.matrixTransform(viewbox.getCTM());
751
752 return line([srcPt, dstPt]);
753 });
754 }
755 });
756
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700757 d3.select(document.body).on('mouseup', function () {
758 function clearHighlight() {
759 svg.selectAll('circle').each(function (data) {
760 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700761 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700762 mouseOutSwitch(data);
Paul Greyson421bfcd2013-03-27 22:22:09 -0700763 });
764 d3.select('#linkVector').remove();
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700765 };
766
Paul Greyson72f18852013-03-27 15:56:11 -0700767 d3.selectAll('.nodrop').classed('nodrop', false);
768
Paul Greyson084779b2013-03-27 13:55:49 -0700769 function removeLink(link) {
770 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
771 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
772
773 if (path1) {
774 setPending(d3.select(path1));
775 }
776 if (path2) {
777 setPending(d3.select(path2));
778 }
779
780 linkDown(link);
781 }
782
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700783
Paul Greyson421bfcd2013-03-27 22:22:09 -0700784 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700785 if (highlighted.length == 2) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700786 var s1Data = highlighted[0].__data__;
787 var s2Data = highlighted[1].__data__;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700788
789 var srcData, dstData;
790 if (s1Data.target) {
791 dstData = s1Data;
792 srcData = s2Data;
793 } else {
794 dstData = s2Data;
795 srcData = s1Data;
796 }
797
798 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
799 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
800 if (confirm(prompt)) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700801 addFlow(srcData, dstData);
802
803 var flow = {
804 dataPath: {
805 srcPort: {
806 dpid: {
807 value: srcData.dpid
808 }
809 },
810 dstPort: {
811 dpid: {
812 value: dstData.dpid
813 }
814 }
815 },
Paul Greysonaa812562013-03-28 12:43:12 -0700816 createPending: true
Paul Greyson13f02b92013-03-28 11:29:35 -0700817 };
818
819 selectFlow(flow);
820
821 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700822 deselectFlowIfCreatePending(flow);
Paul Greyson6f918402013-03-28 12:18:30 -0700823 }, pendingTimeout);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700824 }
825 } else {
826 var map = linkMap[srcData.dpid];
827 if (map && map[dstData.dpid]) {
828 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
829 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700830 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700831 }
832 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700833 map = linkMap[dstData.dpid];
834 if (map && map[srcData.dpid]) {
835 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
836 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700837 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700838 }
839 } else {
840 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
841 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700842 var link1 = {
843 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700844 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700845 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700846 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700847 pending: true
848 };
849 pendingLinks[makeLinkKey(link1)] = link1;
850 var link2 = {
851 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700852 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700853 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700854 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700855 pending: true
856 };
857 pendingLinks[makeLinkKey(link2)] = link2;
Paul Greyson5cc35f02013-03-28 10:07:36 -0700858 updateTopology();
Paul Greyson40c8a592013-03-27 14:10:33 -0700859
Paul Greyson2913af82013-03-27 14:53:17 -0700860 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700861
Paul Greyson5cc35f02013-03-28 10:07:36 -0700862 // remove the pending links after 10s
Paul Greyson40c8a592013-03-27 14:10:33 -0700863 setTimeout(function () {
864 delete pendingLinks[makeLinkKey(link1)];
865 delete pendingLinks[makeLinkKey(link2)];
866
Paul Greyson5cc35f02013-03-28 10:07:36 -0700867 updateTopology();
Paul Greyson6f918402013-03-28 12:18:30 -0700868 }, pendingTimeout);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700869 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700870 }
871 }
872 }
873
874 clearHighlight();
875 } else {
876 clearHighlight();
877 }
878
879 });
880
Paul Greyson9066ab02013-03-23 18:15:41 -0700881 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700882 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700883 return;
884 }
885
886 // create the nodes
887 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700888 .data(data, function (data) {
889 return data.dpid;
890 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700891 .enter().append("svg:g")
892 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700893 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700894 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700895 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700896 .attr("transform", function(data, i) {
897 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700898 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700899
900 // add the text nodes which show on mouse over
901 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700902 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700903 .attr("x", function (data) {
904 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
905 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700906 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700907 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700908 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700909 }
910 } else {
911 if (data.className == 'edge') {
912 return data.width*3 + 4;
913 } else {
914 return data.width + 4;
915 }
916 }
917 })
918 .attr("y", function (data) {
919 var y;
920 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
921 if (data.className == 'edge') {
922 y = data.width*3/2 + 4;
923 } else {
924 y = data.width/2 + 4;
925 }
926 } else {
927 if (data.className == 'edge') {
928 y = data.width*3/2 + 4;
929 } else {
930 y = data.width/2 + 4;
931 }
932 }
933 return y - 6;
934 })
935 .attr("text-anchor", function (data) {
936 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
937 return "end";
938 } else {
939 return "start";
940 }
941 })
942 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700943 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
944 if (data.scale) {
945 m = m.scale(data.scale);
946 }
947 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
948 })
949 }
950
951 labelRings.enter().append("svg:g")
952 .attr("class", "textRing")
953 .each(labelRingEnter);
954
Paul Greysonc17278a2013-03-23 10:17:12 -0700955 // switches should not change during operation of the ui so no
956 // rings.exit()
957
958
Paul Greysond1a22d92013-03-19 12:15:19 -0700959 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700960
Paul Greysonc17278a2013-03-23 10:17:12 -0700961 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -0700962 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700963 return d['src-switch']+'->'+d['dst-switch'];
964 });
965
966 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700967 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700968 .attr("class", "link");
969
Paul Greyson084779b2013-03-27 13:55:49 -0700970 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700971 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -0700972 })
Paul Greyson56378ed2013-03-26 23:17:36 -0700973 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -0700974 var src = d3.select(document.getElementById(d['src-switch']));
975 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -0700976
Paul Greyson084779b2013-03-27 13:55:49 -0700977 var srcPt = document.querySelector('svg').createSVGPoint();
978 srcPt.x = src.attr('x');
979 srcPt.y = src.attr('y');
980 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700981
Paul Greyson084779b2013-03-27 13:55:49 -0700982 var dstPt = document.querySelector('svg').createSVGPoint();
983 dstPt.x = dst.attr('x');
Paul Greyson421bfcd2013-03-27 22:22:09 -0700984 dstPt.y = dst.attr('y');
Paul Greyson084779b2013-03-27 13:55:49 -0700985 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700986
Paul Greyson084779b2013-03-27 13:55:49 -0700987 var midPt = document.querySelector('svg').createSVGPoint();
988 midPt.x = (srcPt.x + dstPt.x)/2;
989 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -0700990
Paul Greyson084779b2013-03-27 13:55:49 -0700991 return line([srcPt, midPt, dstPt]);
992 })
Paul Greyson40c8a592013-03-27 14:10:33 -0700993 .attr("marker-mid", function(d) { return "url(#arrow)"; })
994 .classed('pending', function (d) {
995 return d.pending;
996 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700997
Paul Greyson56378ed2013-03-26 23:17:36 -0700998
Paul Greysonc17278a2013-03-23 10:17:12 -0700999 // remove old links
1000 links.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001001}
1002
Paul Greyson5cc35f02013-03-28 10:07:36 -07001003function updateControllers() {
Paul Greysond1a22d92013-03-19 12:15:19 -07001004 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -07001005 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -07001006 .each(function (c) {
1007 controllerColorMap[c] = colors.pop();
1008 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
1009 })
1010 .text(function (d) {
1011 return d;
Paul Greyson2913af82013-03-27 14:53:17 -07001012 })
1013 .append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -07001014 .attr('class', 'black-eye');
Paul Greysonbcd3c772013-03-21 13:16:44 -07001015
Paul Greysone262a292013-03-23 10:35:23 -07001016 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -07001017 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -07001018 if (model.activeControllers.indexOf(d) != -1) {
1019 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -07001020 }
Paul Greysonbcd3c772013-03-21 13:16:44 -07001021 var className = 'controller ' + color;
1022 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -07001023 });
Paul Greysond1a22d92013-03-19 12:15:19 -07001024
Paul Greysone262a292013-03-23 10:35:23 -07001025 // this should never be needed
1026 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001027
Paul Greyson2913af82013-03-27 14:53:17 -07001028 controllers.on('dblclick', function (c) {
1029 if (model.activeControllers.indexOf(c) != -1) {
1030 var prompt = 'Dectivate ' + c + '?';
1031 if (confirm(prompt)) {
1032 controllerDown(c);
1033 setPending(d3.select(this));
1034 };
1035 } else {
1036 var prompt = 'Activate ' + c + '?';
1037 if (confirm(prompt)) {
1038 controllerUp(c);
1039 setPending(d3.select(this));
1040 };
1041 }
1042 });
1043
Paul Greyson8247c3f2013-03-28 00:24:02 -07001044 controllers.select('.black-eye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -07001045 var allSelected = true;
1046 for (var key in controllerColorMap) {
1047 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
1048 allSelected = false;
1049 break;
1050 }
1051 }
1052 if (allSelected) {
1053 for (var key in controllerColorMap) {
1054 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
1055 }
1056 } else {
1057 for (var key in controllerColorMap) {
1058 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
1059 }
1060 }
1061
1062 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
1063 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -07001064 });
Paul Greyson8d1c6362013-03-27 13:05:24 -07001065
1066
Paul Greyson740bdaf2013-03-18 16:10:48 -07001067}
1068
Paul Greyson29aa98d2013-03-28 00:09:31 -07001069function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -07001070 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -07001071 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001072// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -07001073
Paul Greysone5991b52013-04-04 01:34:04 -07001074 if (newModel) {
1075 var modelChanged = false;
1076 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
1077 modelChanged = true;
1078 model = newModel;
1079 } else {
1080 // console.log('no change');
1081 }
Paul Greysonb48943b2013-03-19 13:27:57 -07001082
Paul Greysone5991b52013-04-04 01:34:04 -07001083 if (modelChanged) {
1084 updateControllers();
1085 updateSelectedFlows();
1086 updateTopology();
1087 }
Paul Greyson5cc35f02013-03-28 10:07:36 -07001088
Paul Greysone5991b52013-04-04 01:34:04 -07001089 updateHeader(newModel);
1090 }
Paul Greyson740bdaf2013-03-18 16:10:48 -07001091
1092 // do it again in 1s
1093 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -07001094 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -07001095 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001096 });
1097}
Paul Greyson740bdaf2013-03-18 16:10:48 -07001098
Paul Greyson38d8bde2013-03-22 22:07:35 -07001099svg = createTopologyView();
Paul Greyson29aa98d2013-03-28 00:09:31 -07001100updateSelectedFlows();
1101
1102d3.select('#showFlowChooser').on('click', function () {
1103 showFlowChooser();
1104});
1105
Paul Greyson72f18852013-03-27 15:56:11 -07001106
Paul Greyson38d8bde2013-03-22 22:07:35 -07001107// workaround for Chrome v25 bug
1108// if executed immediately, the view box transform logic doesn't work properly
1109// fixed in Chrome v27
1110setTimeout(function () {
1111 // workaround for another Chrome v25 bug
1112 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -07001113 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -07001114 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson29aa98d2013-03-28 00:09:31 -07001115 sync(svg);
Paul Greyson38d8bde2013-03-22 22:07:35 -07001116}, 100);