blob: b9acbb2905eff5f0caa26da253c06fb8107e5641 [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 Greyson6f918402013-03-28 12:18:30 -070099 if (!d.dataPath.flowEntries) {
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 () {
173 if (d) {
174 var prompt = 'Delete flow ' + d.flowId.value + '?';
175 if (confirm(prompt)) {
176 deleteFlow(d);
Paul Greysonaa812562013-03-28 12:43:12 -0700177 d.deletePending = true;
Paul Greyson6f918402013-03-28 12:18:30 -0700178 updateSelectedFlows();
179
180 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700181 d.deletePending = false;
Paul Greyson6f918402013-03-28 12:18:30 -0700182 updateSelectedFlows();
183 }, pendingTimeout)
184 };
185 }
186 });
187
Paul Greyson13f02b92013-03-28 11:29:35 -0700188 row.select('.flowId')
189 .text(function (d) {
190 if (d) {
191 if (d.flowId) {
192 return d.flowId.value;
193 } else {
194 return '0x--';
195 }
196 }
197 })
Paul Greysonaa812562013-03-28 12:43:12 -0700198 .classed('pending', d && (d.deletePending || d.createPending));
Paul Greyson13f02b92013-03-28 11:29:35 -0700199
200 row.select('.srcDPID')
201 .text(function (d) {
202 if (d) {
203 return d.dataPath.srcPort.dpid.value;
204 }
205 });
206
207 row.select('.dstDPID')
208 .text(function (d) {
209 if (d) {
210 return d.dataPath.dstPort.dpid.value;
211 }
212 });
213 }
214
215 var flows = d3.select('#selectedFlows')
216 .selectAll('.selectedFlow')
217 .data(selectedFlows);
218
219 flows.enter()
220 .append('div')
221 .classed('selectedFlow', true)
222 .each(rowEnter);
223
224 flows.each(rowUpdate);
225
Paul Greyson29aa98d2013-03-28 00:09:31 -0700226 flows.exit().remove();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700227}
228
Paul Greyson13f02b92013-03-28 11:29:35 -0700229function updateSelectedFlows() {
230 // make sure that all of the selected flows are either
231 // 1) valid (meaning they are in the latest list of flows)
232 // 2) pending
233 if (model) {
234 var flowMap = {};
235 model.flows.forEach(function (flow) {
236 flowMap[makeFlowKey(flow)] = flow;
237 });
238
239 var newSelectedFlows = [];
240 selectedFlows.forEach(function (flow) {
241 if (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700242 var liveFlow = flowMap[makeFlowKey(flow)];
243 if (liveFlow) {
244 newSelectedFlows.push(liveFlow);
Paul Greysonaa812562013-03-28 12:43:12 -0700245 liveFlow.deletePending = flow.deletePending;
246 } else if (flow.createPending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700247 newSelectedFlows.push(flow);
Paul Greyson13f02b92013-03-28 11:29:35 -0700248 }
249 } else {
250 newSelectedFlows.push(null);
251 }
252 });
253 selectedFlows = newSelectedFlows;
254 }
Paul Greyson6f918402013-03-28 12:18:30 -0700255 while (selectedFlows.length < 3) {
256 selectedFlows.push(null);
257 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700258
259 updateSelectedFlowsTable();
260 updateSelectedFlowsTopology();
261}
262
263function selectFlow(flow) {
Paul Greysonc30f75e2013-03-28 11:45:15 -0700264 var flowKey = makeFlowKey(flow);
265 var alreadySelected = false;
266 selectedFlows.forEach(function (f) {
267 if (f && makeFlowKey(f) === flowKey) {
268 alreadySelected = true;
269 }
270 });
271
272 if (!alreadySelected) {
273 selectedFlows.unshift(flow);
274 selectedFlows = selectedFlows.slice(0, 3);
275 updateSelectedFlows();
276 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700277}
278
Paul Greysonaa812562013-03-28 12:43:12 -0700279function deselectFlow(flow, ifCreatePending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700280 var flowKey = makeFlowKey(flow);
281 var newSelectedFlows = [];
282 selectedFlows.forEach(function (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700283 if (!flow ||
284 flowKey !== makeFlowKey(flow) ||
Paul Greysonaa812562013-03-28 12:43:12 -0700285 flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700286 newSelectedFlows.push(flow);
287 }
288 });
289 selectedFlows = newSelectedFlows;
290 while (selectedFlows.length < 3) {
291 selectedFlows.push(null);
292 }
293
294 updateSelectedFlows();
295}
296
Paul Greysonaa812562013-03-28 12:43:12 -0700297function deselectFlowIfCreatePending(flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700298 deselectFlow(flow, true);
299}
300
Paul Greyson29aa98d2013-03-28 00:09:31 -0700301function showFlowChooser() {
302 function rowEnter(d) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700303 var row = d3.select(this);
304
Paul Greyson127d7fb2013-03-25 23:39:20 -0700305 row.append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -0700306 .classed('black-eye', true).
Paul Greyson29aa98d2013-03-28 00:09:31 -0700307 on('click', function () {
Paul Greyson13f02b92013-03-28 11:29:35 -0700308 selectFlow(d);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700309 });
310
311 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700312 .classed('flowId', true)
313 .text(function (d) {
314 return d.flowId.value;
315 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700316
317 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700318 .classed('srcDPID', true)
319 .text(function (d) {
320 return d.dataPath.srcPort.dpid.value;
321 });
322
Paul Greyson127d7fb2013-03-25 23:39:20 -0700323
324 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700325 .classed('dstDPID', true)
326 .text(function (d) {
327 return d.dataPath.dstPort.dpid.value;
328 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700329
Paul Greyson127d7fb2013-03-25 23:39:20 -0700330 }
331
Paul Greyson29aa98d2013-03-28 00:09:31 -0700332 var flows = d3.select('#flowChooser')
333 .append('div')
334 .style('pointer-events', 'auto')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700335 .selectAll('.selectedFlow')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700336 .data(model.flows)
Paul Greyson127d7fb2013-03-25 23:39:20 -0700337 .enter()
338 .append('div')
339 .classed('selectedFlow', true)
340 .each(rowEnter);
341
Paul Greyson29aa98d2013-03-28 00:09:31 -0700342 setTimeout(function () {
343 d3.select(document.body).on('click', function () {
344 d3.select('#flowChooser').html('');
345 d3.select(document.body).on('click', null);
346 });
347 }, 0);
348}
349
Paul Greyson29aa98d2013-03-28 00:09:31 -0700350
Paul Greyson127d7fb2013-03-25 23:39:20 -0700351
Paul Greysond1a22d92013-03-19 12:15:19 -0700352function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700353 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700354 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
355 d3.select('#activeFlows').text(model.flows.length);
356}
357
358function toRadians (angle) {
359 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700360}
361
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700362var widths = {
363 edge: 6,
364 aggregation: 12,
365 core: 18
366}
367
Paul Greysonc17278a2013-03-23 10:17:12 -0700368function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700369 var rings = [{
370 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700371 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700372 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700373 className: 'edge',
374 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700375 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700376 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700377 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700378 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700379 className: 'aggregation',
380 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700381 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700382 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700383 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700384 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700385 className: 'core',
386 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700387 }];
388
Paul Greysonde7fad52013-03-19 12:47:32 -0700389
390 var aggRanges = {};
391
392 // arrange edge switches at equal increments
393 var k = 360 / rings[0].switches.length;
394 rings[0].switches.forEach(function (s, i) {
395 var angle = k * i;
396
397 rings[0].angles[i] = angle;
398
399 // record the angle for the agg switch layout
400 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700401 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700402 var aggdpid = dpid.join(':');
403 var aggRange = aggRanges[aggdpid];
404 if (!aggRange) {
405 aggRange = aggRanges[aggdpid] = {};
406 aggRange.min = aggRange.max = angle;
407 } else {
408 aggRange.max = angle;
409 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700410 });
411
412 // arrange aggregation switches to "fan out" to edge switches
413 k = 360 / rings[1].switches.length;
414 rings[1].switches.forEach(function (s, i) {
415// rings[1].angles[i] = k * i;
416 var range = aggRanges[s.dpid];
417
Paul Greyson832d2202013-03-21 13:27:56 -0700418 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700419 });
420
Paul Greyson3f890b62013-03-22 17:39:36 -0700421 // find the association between core switches and aggregation switches
422 var aggregationSwitchMap = {};
423 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700424 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700425 });
426
Paul Greyson3f890b62013-03-22 17:39:36 -0700427 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700428 k = 360 / rings[2].switches.length;
429 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700430// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700431 var associatedAggregationSwitches = model.configuration.association[s.dpid];
432 // TODO: go between if there are multiple
433 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
434
435 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700436 });
437
Paul Greyson644d92a2013-03-23 18:00:40 -0700438 // TODO: construct this form initially rather than converting. it works better because
439 // it allows binding by dpid
440 var testRings = [];
441 rings.forEach(function (ring) {
442 var testRing = [];
443 ring.switches.forEach(function (s, i) {
444 var testSwitch = {
445 dpid: s.dpid,
446 state: s.state,
447 radius: ring.radius,
448 width: ring.width,
449 className: ring.className,
450 angle: ring.angles[i],
451 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700452 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700453 testRing.push(testSwitch);
454 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700455
456
Paul Greyson644d92a2013-03-23 18:00:40 -0700457 testRings.push(testRing);
458 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700459
460
Paul Greyson644d92a2013-03-23 18:00:40 -0700461// return rings;
462 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700463}
464
Paul Greyson40c8a592013-03-27 14:10:33 -0700465function makeLinkKey(link) {
466 return link['src-switch'] + '=>' + link['dst-switch'];
467}
468
Paul Greyson13f02b92013-03-28 11:29:35 -0700469function makeFlowKey(flow) {
470 return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value;
471}
472
Paul Greyson40c8a592013-03-27 14:10:33 -0700473function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700474 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700475 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700476 var srcDPID = link['src-switch'];
477 var dstDPID = link['dst-switch'];
478
479 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700480
Paul Greyson8d1c6362013-03-27 13:05:24 -0700481 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700482
483 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700484 });
485 return linkMap;
486}
487
Paul Greyson5cc35f02013-03-28 10:07:36 -0700488// removes links from the pending list that are now in the model
489function reconcilePendingLinks(model) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700490 var links = [];
491 model.links.forEach(function (link) {
492 links.push(link);
493 delete pendingLinks[makeLinkKey(link)]
494 })
495 var linkId;
496 for (linkId in pendingLinks) {
497 links.push(pendingLinks[linkId]);
498 }
Paul Greyson5cc35f02013-03-28 10:07:36 -0700499 return links
500}
Paul Greyson40c8a592013-03-27 14:10:33 -0700501
Paul Greyson5cc35f02013-03-28 10:07:36 -0700502updateTopology = function() {
503
504 // DRAW THE SWITCHES
505 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
506
507 var links = reconcilePendingLinks(model);
Paul Greyson40c8a592013-03-27 14:10:33 -0700508 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700509
Paul Greyson8d1c6362013-03-27 13:05:24 -0700510 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700511
512 d3.event.preventDefault();
513
Paul Greyson5cc35f02013-03-28 10:07:36 -0700514 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
515
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700516 if (data.highlighted) {
517 return;
518 }
519
520 // only highlight valid link or flow destination by checking for class of existing highlighted circle
Paul Greyson421bfcd2013-03-27 22:22:09 -0700521 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700522 if (highlighted.length == 1) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700523 var s = d3.select(highlighted[0]).select('circle');
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700524 // only allow links
525 // edge->edge (flow)
526 // aggregation->core
527 // core->core
528 if (data.className == 'edge' && !s.classed('edge') ||
529 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
530 data.className == 'aggregation' && !s.classed('core')) {
531 return;
532 }
533
534 // don't highlight if there's already a link or flow
535 // var map = linkMap[data.dpid];
536 // console.log(map);
537 // console.log(s.data()[0].dpid);
538 // console.log(map[s.data()[0].dpid]);
539 // if (map && map[s.data()[0].dpid]) {
540 // return;
541 // }
542
543 // the second highlighted switch is the target for a link or flow
544 data.target = true;
545 }
546
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700547 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700548 node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700549 data.highlighted = true;
550 node.moveToFront();
551 }
552
Paul Greyson8d1c6362013-03-27 13:05:24 -0700553 function mouseOutSwitch(data) {
Paul Greyson5cc35f02013-03-28 10:07:36 -0700554 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
555
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700556 if (data.mouseDown)
557 return;
558
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700559 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700560 node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700561 data.highlighted = false;
562 data.target = false;
563 }
564
Paul Greyson8d1c6362013-03-27 13:05:24 -0700565 function mouseDownSwitch(data) {
566 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700567 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700568 d3.select('#topology').classed('linking', true);
569
Paul Greyson421bfcd2013-03-27 22:22:09 -0700570 d3.select('svg')
571 .append('svg:path')
572 .attr('id', 'linkVector')
573 .attr('d', function () {
574 var s = d3.select(document.getElementById(data.dpid));
575
576 var pt = document.querySelector('svg').createSVGPoint();
577 pt.x = s.attr('x');
578 pt.y = s.attr('y');
579 pt = pt.matrixTransform(s[0][0].getCTM());
580
581 return line([pt, pt]);
582 });
583
584
Paul Greyson72f18852013-03-27 15:56:11 -0700585 if (data.className === 'core') {
586 d3.selectAll('.edge').classed('nodrop', true);
587 }
588 if (data.className === 'edge') {
589 d3.selectAll('.core').classed('nodrop', true);
590 d3.selectAll('.aggregation').classed('nodrop', true);
591 }
592 if (data.className === 'aggregation') {
593 d3.selectAll('.edge').classed('nodrop', true);
594 d3.selectAll('.aggregation').classed('nodrop', true);
595 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700596 }
597
Paul Greyson8d1c6362013-03-27 13:05:24 -0700598 function mouseUpSwitch(data) {
599 if (data.mouseDown) {
600 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700601 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700602 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700603 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700604 }
605 }
606
607 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700608 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700609 if (data.state == 'ACTIVE') {
610 var prompt = 'Deactivate ' + data.dpid + '?';
611 if (confirm(prompt)) {
612 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700613 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700614 }
615 } else {
616 var prompt = 'Activate ' + data.dpid + '?';
617 if (confirm(prompt)) {
618 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700619 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700620 }
621 }
622 }
623
Paul Greyson740bdaf2013-03-18 16:10:48 -0700624 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700625 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700626 return;
627 }
628
Paul Greysonc17278a2013-03-23 10:17:12 -0700629 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700630 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700631 .data(data, function (data) {
632 return data.dpid;
633 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700634 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700635 .attr("id", function (data, i) {
636 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700637 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700638 .attr("transform", function(data, i) {
639 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700640 });
641
Paul Greysonc17278a2013-03-23 10:17:12 -0700642 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700643 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700644 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700645 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700646 if (data.scale) {
647 m = m.scale(data.scale);
648 }
649 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700650 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700651 .attr("x", function (data) {
652 return -data.width / 2;
653 })
654 .attr("y", function (data) {
655 return -data.width / 2;
656 })
657 .attr("r", function (data) {
658 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700659 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700660
Paul Greysonc17278a2013-03-23 10:17:12 -0700661 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700662 nodes.on('mouseover', mouseOverSwitch);
663 nodes.on('mouseout', mouseOutSwitch);
664 nodes.on('mouseup', mouseUpSwitch);
665 nodes.on('mousedown', mouseDownSwitch);
666
667 // only do switch up/down for core switches
668 if (i == 2) {
669 nodes.on('dblclick', doubleClickSwitch);
670 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700671 }
672
Paul Greysonc17278a2013-03-23 10:17:12 -0700673 // append switches
674 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700675 .attr("class", "ring")
676 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700677
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700678
Paul Greysonc17278a2013-03-23 10:17:12 -0700679 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700680 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700681 .data(data, function (data) {
682 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700683 });
Paul Greyson347fb742013-03-27 13:40:29 -0700684 nodes.select('circle')
685 .each(function (data) {
686 // if there's a pending state changed and then the state changes, clear the pending class
687 var circle = d3.select(this);
688 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
689 data.state === 'INACTIVE' && circle.classed('active')) {
690 circle.classed('pending', false);
691 }
692 })
693 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700694 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700695 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700696 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700697 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700698 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700699 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700700 }
701
702 // update switches
703 rings.each(ringUpdate);
704
Paul Greyson968d1b42013-03-23 16:58:41 -0700705
706 // Now setup the labels
707 // This is done separately because SVG draws in node order and we want the labels
708 // always on top
709 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
710
Paul Greyson421bfcd2013-03-27 22:22:09 -0700711 d3.select(document.body).on('mousemove', function () {
712 if (!d3.select('#topology').classed('linking')) {
713 return;
714 }
715 var linkVector = document.getElementById('linkVector');
716 if (!linkVector) {
717 return;
718 }
719 linkVector = d3.select(linkVector);
720
721 var highlighted = svg.selectAll('.highlight')[0];
722 var s1 = null, s2 = null;
723 if (highlighted.length > 1) {
724 var s1 = d3.select(highlighted[0]);
725 var s2 = d3.select(highlighted[1]);
726
727 } else if (highlighted.length > 0) {
728 var s1 = d3.select(highlighted[0]);
729 }
730 var src = s1;
731 if (s2 && !s2.data()[0].target) {
732 src = s2;
733 }
734 if (src) {
735 linkVector.attr('d', function () {
736 var srcPt = document.querySelector('svg').createSVGPoint();
737 srcPt.x = src.attr('x');
738 srcPt.y = src.attr('y');
739 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
740
741 var svg = document.getElementById('topology');
742 var mouse = d3.mouse(viewbox);
743 var dstPt = document.querySelector('svg').createSVGPoint();
744 dstPt.x = mouse[0];
745 dstPt.y = mouse[1];
746 dstPt = dstPt.matrixTransform(viewbox.getCTM());
747
748 return line([srcPt, dstPt]);
749 });
750 }
751 });
752
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700753 d3.select(document.body).on('mouseup', function () {
754 function clearHighlight() {
755 svg.selectAll('circle').each(function (data) {
756 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700757 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700758 mouseOutSwitch(data);
Paul Greyson421bfcd2013-03-27 22:22:09 -0700759 });
760 d3.select('#linkVector').remove();
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700761 };
762
Paul Greyson72f18852013-03-27 15:56:11 -0700763 d3.selectAll('.nodrop').classed('nodrop', false);
764
Paul Greyson084779b2013-03-27 13:55:49 -0700765 function removeLink(link) {
766 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
767 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
768
769 if (path1) {
770 setPending(d3.select(path1));
771 }
772 if (path2) {
773 setPending(d3.select(path2));
774 }
775
776 linkDown(link);
777 }
778
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700779
Paul Greyson421bfcd2013-03-27 22:22:09 -0700780 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700781 if (highlighted.length == 2) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700782 var s1Data = highlighted[0].__data__;
783 var s2Data = highlighted[1].__data__;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700784
785 var srcData, dstData;
786 if (s1Data.target) {
787 dstData = s1Data;
788 srcData = s2Data;
789 } else {
790 dstData = s2Data;
791 srcData = s1Data;
792 }
793
794 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
795 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
796 if (confirm(prompt)) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700797 addFlow(srcData, dstData);
798
799 var flow = {
800 dataPath: {
801 srcPort: {
802 dpid: {
803 value: srcData.dpid
804 }
805 },
806 dstPort: {
807 dpid: {
808 value: dstData.dpid
809 }
810 }
811 },
Paul Greysonaa812562013-03-28 12:43:12 -0700812 createPending: true
Paul Greyson13f02b92013-03-28 11:29:35 -0700813 };
814
815 selectFlow(flow);
816
817 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700818 deselectFlowIfCreatePending(flow);
Paul Greyson6f918402013-03-28 12:18:30 -0700819 }, pendingTimeout);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700820 }
821 } else {
822 var map = linkMap[srcData.dpid];
823 if (map && map[dstData.dpid]) {
824 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
825 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700826 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700827 }
828 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700829 map = linkMap[dstData.dpid];
830 if (map && map[srcData.dpid]) {
831 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
832 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700833 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700834 }
835 } else {
836 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
837 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700838 var link1 = {
839 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700840 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700841 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700842 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700843 pending: true
844 };
845 pendingLinks[makeLinkKey(link1)] = link1;
846 var link2 = {
847 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700848 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700849 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700850 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700851 pending: true
852 };
853 pendingLinks[makeLinkKey(link2)] = link2;
Paul Greyson5cc35f02013-03-28 10:07:36 -0700854 updateTopology();
Paul Greyson40c8a592013-03-27 14:10:33 -0700855
Paul Greyson2913af82013-03-27 14:53:17 -0700856 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700857
Paul Greyson5cc35f02013-03-28 10:07:36 -0700858 // remove the pending links after 10s
Paul Greyson40c8a592013-03-27 14:10:33 -0700859 setTimeout(function () {
860 delete pendingLinks[makeLinkKey(link1)];
861 delete pendingLinks[makeLinkKey(link2)];
862
Paul Greyson5cc35f02013-03-28 10:07:36 -0700863 updateTopology();
Paul Greyson6f918402013-03-28 12:18:30 -0700864 }, pendingTimeout);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700865 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700866 }
867 }
868 }
869
870 clearHighlight();
871 } else {
872 clearHighlight();
873 }
874
875 });
876
Paul Greyson9066ab02013-03-23 18:15:41 -0700877 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700878 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700879 return;
880 }
881
882 // create the nodes
883 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700884 .data(data, function (data) {
885 return data.dpid;
886 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700887 .enter().append("svg:g")
888 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700889 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700890 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700891 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700892 .attr("transform", function(data, i) {
893 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700894 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700895
896 // add the text nodes which show on mouse over
897 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700898 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700899 .attr("x", function (data) {
900 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
901 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700902 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700903 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700904 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700905 }
906 } else {
907 if (data.className == 'edge') {
908 return data.width*3 + 4;
909 } else {
910 return data.width + 4;
911 }
912 }
913 })
914 .attr("y", function (data) {
915 var y;
916 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
917 if (data.className == 'edge') {
918 y = data.width*3/2 + 4;
919 } else {
920 y = data.width/2 + 4;
921 }
922 } else {
923 if (data.className == 'edge') {
924 y = data.width*3/2 + 4;
925 } else {
926 y = data.width/2 + 4;
927 }
928 }
929 return y - 6;
930 })
931 .attr("text-anchor", function (data) {
932 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
933 return "end";
934 } else {
935 return "start";
936 }
937 })
938 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700939 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
940 if (data.scale) {
941 m = m.scale(data.scale);
942 }
943 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
944 })
945 }
946
947 labelRings.enter().append("svg:g")
948 .attr("class", "textRing")
949 .each(labelRingEnter);
950
Paul Greysonc17278a2013-03-23 10:17:12 -0700951 // switches should not change during operation of the ui so no
952 // rings.exit()
953
954
Paul Greysond1a22d92013-03-19 12:15:19 -0700955 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700956
Paul Greysonc17278a2013-03-23 10:17:12 -0700957 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -0700958 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700959 return d['src-switch']+'->'+d['dst-switch'];
960 });
961
962 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700963 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700964 .attr("class", "link");
965
Paul Greyson084779b2013-03-27 13:55:49 -0700966 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700967 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -0700968 })
Paul Greyson56378ed2013-03-26 23:17:36 -0700969 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -0700970 var src = d3.select(document.getElementById(d['src-switch']));
971 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -0700972
Paul Greyson084779b2013-03-27 13:55:49 -0700973 var srcPt = document.querySelector('svg').createSVGPoint();
974 srcPt.x = src.attr('x');
975 srcPt.y = src.attr('y');
976 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700977
Paul Greyson084779b2013-03-27 13:55:49 -0700978 var dstPt = document.querySelector('svg').createSVGPoint();
979 dstPt.x = dst.attr('x');
Paul Greyson421bfcd2013-03-27 22:22:09 -0700980 dstPt.y = dst.attr('y');
Paul Greyson084779b2013-03-27 13:55:49 -0700981 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -0700982
Paul Greyson084779b2013-03-27 13:55:49 -0700983 var midPt = document.querySelector('svg').createSVGPoint();
984 midPt.x = (srcPt.x + dstPt.x)/2;
985 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -0700986
Paul Greyson084779b2013-03-27 13:55:49 -0700987 return line([srcPt, midPt, dstPt]);
988 })
Paul Greyson40c8a592013-03-27 14:10:33 -0700989 .attr("marker-mid", function(d) { return "url(#arrow)"; })
990 .classed('pending', function (d) {
991 return d.pending;
992 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700993
Paul Greyson56378ed2013-03-26 23:17:36 -0700994
Paul Greysonc17278a2013-03-23 10:17:12 -0700995 // remove old links
996 links.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -0700997}
998
Paul Greyson5cc35f02013-03-28 10:07:36 -0700999function updateControllers() {
Paul Greysond1a22d92013-03-19 12:15:19 -07001000 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -07001001 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -07001002 .each(function (c) {
1003 controllerColorMap[c] = colors.pop();
1004 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
1005 })
1006 .text(function (d) {
1007 return d;
Paul Greyson2913af82013-03-27 14:53:17 -07001008 })
1009 .append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -07001010 .attr('class', 'black-eye');
Paul Greysonbcd3c772013-03-21 13:16:44 -07001011
Paul Greysone262a292013-03-23 10:35:23 -07001012 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -07001013 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -07001014 if (model.activeControllers.indexOf(d) != -1) {
1015 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -07001016 }
Paul Greysonbcd3c772013-03-21 13:16:44 -07001017 var className = 'controller ' + color;
1018 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -07001019 });
Paul Greysond1a22d92013-03-19 12:15:19 -07001020
Paul Greysone262a292013-03-23 10:35:23 -07001021 // this should never be needed
1022 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001023
Paul Greyson2913af82013-03-27 14:53:17 -07001024 controllers.on('dblclick', function (c) {
1025 if (model.activeControllers.indexOf(c) != -1) {
1026 var prompt = 'Dectivate ' + c + '?';
1027 if (confirm(prompt)) {
1028 controllerDown(c);
1029 setPending(d3.select(this));
1030 };
1031 } else {
1032 var prompt = 'Activate ' + c + '?';
1033 if (confirm(prompt)) {
1034 controllerUp(c);
1035 setPending(d3.select(this));
1036 };
1037 }
1038 });
1039
Paul Greyson8247c3f2013-03-28 00:24:02 -07001040 controllers.select('.black-eye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -07001041 var allSelected = true;
1042 for (var key in controllerColorMap) {
1043 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
1044 allSelected = false;
1045 break;
1046 }
1047 }
1048 if (allSelected) {
1049 for (var key in controllerColorMap) {
1050 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
1051 }
1052 } else {
1053 for (var key in controllerColorMap) {
1054 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
1055 }
1056 }
1057
1058 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
1059 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -07001060 });
Paul Greyson8d1c6362013-03-27 13:05:24 -07001061
1062
Paul Greyson740bdaf2013-03-18 16:10:48 -07001063}
1064
Paul Greyson29aa98d2013-03-28 00:09:31 -07001065function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -07001066 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -07001067 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001068// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -07001069
Paul Greysone5991b52013-04-04 01:34:04 -07001070 if (newModel) {
1071 var modelChanged = false;
1072 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
1073 modelChanged = true;
1074 model = newModel;
1075 } else {
1076 // console.log('no change');
1077 }
Paul Greysonb48943b2013-03-19 13:27:57 -07001078
Paul Greysone5991b52013-04-04 01:34:04 -07001079 if (modelChanged) {
1080 updateControllers();
1081 updateSelectedFlows();
1082 updateTopology();
1083 }
Paul Greyson5cc35f02013-03-28 10:07:36 -07001084
Paul Greysone5991b52013-04-04 01:34:04 -07001085 updateHeader(newModel);
1086 }
Paul Greyson740bdaf2013-03-18 16:10:48 -07001087
1088 // do it again in 1s
1089 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -07001090 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -07001091 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001092 });
1093}
Paul Greyson740bdaf2013-03-18 16:10:48 -07001094
Paul Greyson38d8bde2013-03-22 22:07:35 -07001095svg = createTopologyView();
Paul Greyson29aa98d2013-03-28 00:09:31 -07001096updateSelectedFlows();
1097
1098d3.select('#showFlowChooser').on('click', function () {
1099 showFlowChooser();
1100});
1101
Paul Greyson72f18852013-03-27 15:56:11 -07001102
Paul Greyson38d8bde2013-03-22 22:07:35 -07001103// workaround for Chrome v25 bug
1104// if executed immediately, the view box transform logic doesn't work properly
1105// fixed in Chrome v27
1106setTimeout(function () {
1107 // workaround for another Chrome v25 bug
1108 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -07001109 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -07001110 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson29aa98d2013-03-28 00:09:31 -07001111 sync(svg);
Paul Greyson38d8bde2013-03-22 22:07:35 -07001112}, 100);