blob: c150b9f4f2a984275366c183e399148427469955 [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 Greyson4b4c8af2013-04-04 09:02:09 -0700235// TODO: cancel the interval when the flow is desel
236function startIPerfForFlow(flow) {
237 var duration = 10; // seconds
238 var interval = 100; // ms. this is defined by the server
239 var updateRate = 1000; // ms
240
241 if (flow.flowId) {
242 console.log('starting iperf for: ' + flow.flowId.value);
243 startIPerf(flow, duration, updateRate/interval);
244 flow.iperfInterval = setInterval(function () {
245 getIPerfData(flow, function (data) {
246 console.log(data);
247 });
248 });
249 }
250}
251
Paul Greyson13f02b92013-03-28 11:29:35 -0700252function updateSelectedFlows() {
253 // make sure that all of the selected flows are either
254 // 1) valid (meaning they are in the latest list of flows)
255 // 2) pending
256 if (model) {
257 var flowMap = {};
258 model.flows.forEach(function (flow) {
259 flowMap[makeFlowKey(flow)] = flow;
260 });
261
262 var newSelectedFlows = [];
263 selectedFlows.forEach(function (flow) {
264 if (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700265 var liveFlow = flowMap[makeFlowKey(flow)];
266 if (liveFlow) {
267 newSelectedFlows.push(liveFlow);
Paul Greysonaa812562013-03-28 12:43:12 -0700268 liveFlow.deletePending = flow.deletePending;
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700269 liveFlow.iperfInterval = flow.iperfInterval;
Paul Greysonaa812562013-03-28 12:43:12 -0700270 } else if (flow.createPending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700271 newSelectedFlows.push(flow);
Paul Greyson13f02b92013-03-28 11:29:35 -0700272 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700273 }
274 });
275 selectedFlows = newSelectedFlows;
276 }
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700277 selectedFlows.forEach(function (flow) {
278 if (!flow.iperfInterval) {
279 startIPerfForFlow(flow);
280 }
281 });
Paul Greyson6f918402013-03-28 12:18:30 -0700282 while (selectedFlows.length < 3) {
283 selectedFlows.push(null);
284 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700285
286 updateSelectedFlowsTable();
287 updateSelectedFlowsTopology();
288}
289
290function selectFlow(flow) {
Paul Greysonc30f75e2013-03-28 11:45:15 -0700291 var flowKey = makeFlowKey(flow);
292 var alreadySelected = false;
293 selectedFlows.forEach(function (f) {
294 if (f && makeFlowKey(f) === flowKey) {
295 alreadySelected = true;
296 }
297 });
298
299 if (!alreadySelected) {
300 selectedFlows.unshift(flow);
301 selectedFlows = selectedFlows.slice(0, 3);
302 updateSelectedFlows();
303 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700304}
305
Paul Greysonaa812562013-03-28 12:43:12 -0700306function deselectFlow(flow, ifCreatePending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700307 var flowKey = makeFlowKey(flow);
308 var newSelectedFlows = [];
309 selectedFlows.forEach(function (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700310 if (!flow ||
311 flowKey !== makeFlowKey(flow) ||
Paul Greysonaa812562013-03-28 12:43:12 -0700312 flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700313 newSelectedFlows.push(flow);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700314 } else {
315 if (flow && flow.iperfInterval) {
316 console.log('clearing iperf interval for: ' + flow.flowId.value);
317 clearInterval(flow.iperfInterval);
318 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700319 }
320 });
321 selectedFlows = newSelectedFlows;
322 while (selectedFlows.length < 3) {
323 selectedFlows.push(null);
324 }
325
326 updateSelectedFlows();
327}
328
Paul Greysonaa812562013-03-28 12:43:12 -0700329function deselectFlowIfCreatePending(flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700330 deselectFlow(flow, true);
331}
332
Paul Greyson29aa98d2013-03-28 00:09:31 -0700333function showFlowChooser() {
334 function rowEnter(d) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700335 var row = d3.select(this);
336
Paul Greyson127d7fb2013-03-25 23:39:20 -0700337 row.append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -0700338 .classed('black-eye', true).
Paul Greyson29aa98d2013-03-28 00:09:31 -0700339 on('click', function () {
Paul Greyson13f02b92013-03-28 11:29:35 -0700340 selectFlow(d);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700341 });
342
343 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700344 .classed('flowId', true)
345 .text(function (d) {
346 return d.flowId.value;
347 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700348
349 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700350 .classed('srcDPID', true)
351 .text(function (d) {
352 return d.dataPath.srcPort.dpid.value;
353 });
354
Paul Greyson127d7fb2013-03-25 23:39:20 -0700355
356 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700357 .classed('dstDPID', true)
358 .text(function (d) {
359 return d.dataPath.dstPort.dpid.value;
360 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700361
Paul Greyson127d7fb2013-03-25 23:39:20 -0700362 }
363
Paul Greyson29aa98d2013-03-28 00:09:31 -0700364 var flows = d3.select('#flowChooser')
365 .append('div')
366 .style('pointer-events', 'auto')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700367 .selectAll('.selectedFlow')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700368 .data(model.flows)
Paul Greyson127d7fb2013-03-25 23:39:20 -0700369 .enter()
370 .append('div')
371 .classed('selectedFlow', true)
372 .each(rowEnter);
373
Paul Greyson29aa98d2013-03-28 00:09:31 -0700374 setTimeout(function () {
375 d3.select(document.body).on('click', function () {
376 d3.select('#flowChooser').html('');
377 d3.select(document.body).on('click', null);
378 });
379 }, 0);
380}
381
Paul Greyson29aa98d2013-03-28 00:09:31 -0700382
Paul Greyson127d7fb2013-03-25 23:39:20 -0700383
Paul Greysond1a22d92013-03-19 12:15:19 -0700384function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700385 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700386 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
387 d3.select('#activeFlows').text(model.flows.length);
388}
389
390function toRadians (angle) {
391 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700392}
393
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700394var widths = {
395 edge: 6,
396 aggregation: 12,
397 core: 18
398}
399
Paul Greysonc17278a2013-03-23 10:17:12 -0700400function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700401 var rings = [{
402 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700403 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700404 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700405 className: 'edge',
406 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700407 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700408 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700409 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700410 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700411 className: 'aggregation',
412 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700413 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700414 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700415 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700416 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700417 className: 'core',
418 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700419 }];
420
Paul Greysonde7fad52013-03-19 12:47:32 -0700421
422 var aggRanges = {};
423
424 // arrange edge switches at equal increments
425 var k = 360 / rings[0].switches.length;
426 rings[0].switches.forEach(function (s, i) {
427 var angle = k * i;
428
429 rings[0].angles[i] = angle;
430
431 // record the angle for the agg switch layout
432 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700433 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700434 var aggdpid = dpid.join(':');
435 var aggRange = aggRanges[aggdpid];
436 if (!aggRange) {
437 aggRange = aggRanges[aggdpid] = {};
438 aggRange.min = aggRange.max = angle;
439 } else {
440 aggRange.max = angle;
441 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700442 });
443
444 // arrange aggregation switches to "fan out" to edge switches
445 k = 360 / rings[1].switches.length;
446 rings[1].switches.forEach(function (s, i) {
447// rings[1].angles[i] = k * i;
448 var range = aggRanges[s.dpid];
449
Paul Greyson832d2202013-03-21 13:27:56 -0700450 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700451 });
452
Paul Greyson3f890b62013-03-22 17:39:36 -0700453 // find the association between core switches and aggregation switches
454 var aggregationSwitchMap = {};
455 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700456 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700457 });
458
Paul Greyson3f890b62013-03-22 17:39:36 -0700459 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700460 k = 360 / rings[2].switches.length;
461 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700462// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700463 var associatedAggregationSwitches = model.configuration.association[s.dpid];
464 // TODO: go between if there are multiple
465 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
466
467 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700468 });
469
Paul Greyson644d92a2013-03-23 18:00:40 -0700470 // TODO: construct this form initially rather than converting. it works better because
471 // it allows binding by dpid
472 var testRings = [];
473 rings.forEach(function (ring) {
474 var testRing = [];
475 ring.switches.forEach(function (s, i) {
476 var testSwitch = {
477 dpid: s.dpid,
478 state: s.state,
479 radius: ring.radius,
480 width: ring.width,
481 className: ring.className,
482 angle: ring.angles[i],
483 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700484 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700485 testRing.push(testSwitch);
486 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700487
488
Paul Greyson644d92a2013-03-23 18:00:40 -0700489 testRings.push(testRing);
490 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700491
492
Paul Greyson644d92a2013-03-23 18:00:40 -0700493// return rings;
494 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700495}
496
Paul Greyson40c8a592013-03-27 14:10:33 -0700497function makeLinkKey(link) {
498 return link['src-switch'] + '=>' + link['dst-switch'];
499}
500
Paul Greyson13f02b92013-03-28 11:29:35 -0700501function makeFlowKey(flow) {
502 return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value;
503}
504
Paul Greyson40c8a592013-03-27 14:10:33 -0700505function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700506 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700507 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700508 var srcDPID = link['src-switch'];
509 var dstDPID = link['dst-switch'];
510
511 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700512
Paul Greyson8d1c6362013-03-27 13:05:24 -0700513 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700514
515 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700516 });
517 return linkMap;
518}
519
Paul Greyson5cc35f02013-03-28 10:07:36 -0700520// removes links from the pending list that are now in the model
521function reconcilePendingLinks(model) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700522 var links = [];
523 model.links.forEach(function (link) {
524 links.push(link);
525 delete pendingLinks[makeLinkKey(link)]
526 })
527 var linkId;
528 for (linkId in pendingLinks) {
529 links.push(pendingLinks[linkId]);
530 }
Paul Greyson5cc35f02013-03-28 10:07:36 -0700531 return links
532}
Paul Greyson40c8a592013-03-27 14:10:33 -0700533
Paul Greyson5cc35f02013-03-28 10:07:36 -0700534updateTopology = function() {
535
536 // DRAW THE SWITCHES
537 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
538
539 var links = reconcilePendingLinks(model);
Paul Greyson40c8a592013-03-27 14:10:33 -0700540 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700541
Paul Greyson8d1c6362013-03-27 13:05:24 -0700542 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700543
544 d3.event.preventDefault();
545
Paul Greyson5cc35f02013-03-28 10:07:36 -0700546 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
547
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700548 if (data.highlighted) {
549 return;
550 }
551
552 // only highlight valid link or flow destination by checking for class of existing highlighted circle
Paul Greyson421bfcd2013-03-27 22:22:09 -0700553 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700554 if (highlighted.length == 1) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700555 var s = d3.select(highlighted[0]).select('circle');
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700556 // only allow links
557 // edge->edge (flow)
558 // aggregation->core
559 // core->core
560 if (data.className == 'edge' && !s.classed('edge') ||
561 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
562 data.className == 'aggregation' && !s.classed('core')) {
563 return;
564 }
565
566 // don't highlight if there's already a link or flow
567 // var map = linkMap[data.dpid];
568 // console.log(map);
569 // console.log(s.data()[0].dpid);
570 // console.log(map[s.data()[0].dpid]);
571 // if (map && map[s.data()[0].dpid]) {
572 // return;
573 // }
574
575 // the second highlighted switch is the target for a link or flow
576 data.target = true;
577 }
578
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700579 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700580 node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700581 data.highlighted = true;
582 node.moveToFront();
583 }
584
Paul Greyson8d1c6362013-03-27 13:05:24 -0700585 function mouseOutSwitch(data) {
Paul Greyson5cc35f02013-03-28 10:07:36 -0700586 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
587
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700588 if (data.mouseDown)
589 return;
590
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700591 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700592 node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700593 data.highlighted = false;
594 data.target = false;
595 }
596
Paul Greyson8d1c6362013-03-27 13:05:24 -0700597 function mouseDownSwitch(data) {
598 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700599 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700600 d3.select('#topology').classed('linking', true);
601
Paul Greyson421bfcd2013-03-27 22:22:09 -0700602 d3.select('svg')
603 .append('svg:path')
604 .attr('id', 'linkVector')
605 .attr('d', function () {
606 var s = d3.select(document.getElementById(data.dpid));
607
608 var pt = document.querySelector('svg').createSVGPoint();
609 pt.x = s.attr('x');
610 pt.y = s.attr('y');
611 pt = pt.matrixTransform(s[0][0].getCTM());
612
613 return line([pt, pt]);
614 });
615
616
Paul Greyson72f18852013-03-27 15:56:11 -0700617 if (data.className === 'core') {
618 d3.selectAll('.edge').classed('nodrop', true);
619 }
620 if (data.className === 'edge') {
621 d3.selectAll('.core').classed('nodrop', true);
622 d3.selectAll('.aggregation').classed('nodrop', true);
623 }
624 if (data.className === 'aggregation') {
625 d3.selectAll('.edge').classed('nodrop', true);
626 d3.selectAll('.aggregation').classed('nodrop', true);
627 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700628 }
629
Paul Greyson8d1c6362013-03-27 13:05:24 -0700630 function mouseUpSwitch(data) {
631 if (data.mouseDown) {
632 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700633 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700634 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700635 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700636 }
637 }
638
639 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700640 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700641 if (data.state == 'ACTIVE') {
642 var prompt = 'Deactivate ' + data.dpid + '?';
643 if (confirm(prompt)) {
644 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700645 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700646 }
647 } else {
648 var prompt = 'Activate ' + data.dpid + '?';
649 if (confirm(prompt)) {
650 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700651 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700652 }
653 }
654 }
655
Paul Greyson740bdaf2013-03-18 16:10:48 -0700656 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700657 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700658 return;
659 }
660
Paul Greysonc17278a2013-03-23 10:17:12 -0700661 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700662 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700663 .data(data, function (data) {
664 return data.dpid;
665 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700666 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700667 .attr("id", function (data, i) {
668 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700669 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700670 .attr("transform", function(data, i) {
671 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700672 });
673
Paul Greysonc17278a2013-03-23 10:17:12 -0700674 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700675 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700676 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700677 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700678 if (data.scale) {
679 m = m.scale(data.scale);
680 }
681 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700682 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700683 .attr("x", function (data) {
684 return -data.width / 2;
685 })
686 .attr("y", function (data) {
687 return -data.width / 2;
688 })
689 .attr("r", function (data) {
690 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700691 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700692
Paul Greysonc17278a2013-03-23 10:17:12 -0700693 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700694 nodes.on('mouseover', mouseOverSwitch);
695 nodes.on('mouseout', mouseOutSwitch);
696 nodes.on('mouseup', mouseUpSwitch);
697 nodes.on('mousedown', mouseDownSwitch);
698
699 // only do switch up/down for core switches
700 if (i == 2) {
701 nodes.on('dblclick', doubleClickSwitch);
702 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700703 }
704
Paul Greysonc17278a2013-03-23 10:17:12 -0700705 // append switches
706 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700707 .attr("class", "ring")
708 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700709
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700710
Paul Greysonc17278a2013-03-23 10:17:12 -0700711 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700712 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700713 .data(data, function (data) {
714 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700715 });
Paul Greyson347fb742013-03-27 13:40:29 -0700716 nodes.select('circle')
717 .each(function (data) {
718 // if there's a pending state changed and then the state changes, clear the pending class
719 var circle = d3.select(this);
720 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
721 data.state === 'INACTIVE' && circle.classed('active')) {
722 circle.classed('pending', false);
723 }
724 })
725 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700726 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700727 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700728 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700729 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700730 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700731 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700732 }
733
734 // update switches
735 rings.each(ringUpdate);
736
Paul Greyson968d1b42013-03-23 16:58:41 -0700737
738 // Now setup the labels
739 // This is done separately because SVG draws in node order and we want the labels
740 // always on top
741 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
742
Paul Greyson421bfcd2013-03-27 22:22:09 -0700743 d3.select(document.body).on('mousemove', function () {
744 if (!d3.select('#topology').classed('linking')) {
745 return;
746 }
747 var linkVector = document.getElementById('linkVector');
748 if (!linkVector) {
749 return;
750 }
751 linkVector = d3.select(linkVector);
752
753 var highlighted = svg.selectAll('.highlight')[0];
754 var s1 = null, s2 = null;
755 if (highlighted.length > 1) {
756 var s1 = d3.select(highlighted[0]);
757 var s2 = d3.select(highlighted[1]);
758
759 } else if (highlighted.length > 0) {
760 var s1 = d3.select(highlighted[0]);
761 }
762 var src = s1;
763 if (s2 && !s2.data()[0].target) {
764 src = s2;
765 }
766 if (src) {
767 linkVector.attr('d', function () {
768 var srcPt = document.querySelector('svg').createSVGPoint();
769 srcPt.x = src.attr('x');
770 srcPt.y = src.attr('y');
771 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
772
773 var svg = document.getElementById('topology');
774 var mouse = d3.mouse(viewbox);
775 var dstPt = document.querySelector('svg').createSVGPoint();
776 dstPt.x = mouse[0];
777 dstPt.y = mouse[1];
778 dstPt = dstPt.matrixTransform(viewbox.getCTM());
779
780 return line([srcPt, dstPt]);
781 });
782 }
783 });
784
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700785 d3.select(document.body).on('mouseup', function () {
786 function clearHighlight() {
787 svg.selectAll('circle').each(function (data) {
788 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700789 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700790 mouseOutSwitch(data);
Paul Greyson421bfcd2013-03-27 22:22:09 -0700791 });
792 d3.select('#linkVector').remove();
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700793 };
794
Paul Greyson72f18852013-03-27 15:56:11 -0700795 d3.selectAll('.nodrop').classed('nodrop', false);
796
Paul Greyson084779b2013-03-27 13:55:49 -0700797 function removeLink(link) {
798 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
799 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
800
801 if (path1) {
802 setPending(d3.select(path1));
803 }
804 if (path2) {
805 setPending(d3.select(path2));
806 }
807
808 linkDown(link);
809 }
810
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700811
Paul Greyson421bfcd2013-03-27 22:22:09 -0700812 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700813 if (highlighted.length == 2) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700814 var s1Data = highlighted[0].__data__;
815 var s2Data = highlighted[1].__data__;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700816
817 var srcData, dstData;
818 if (s1Data.target) {
819 dstData = s1Data;
820 srcData = s2Data;
821 } else {
822 dstData = s2Data;
823 srcData = s1Data;
824 }
825
826 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
827 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
828 if (confirm(prompt)) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700829 addFlow(srcData, dstData);
830
831 var flow = {
832 dataPath: {
833 srcPort: {
834 dpid: {
835 value: srcData.dpid
836 }
837 },
838 dstPort: {
839 dpid: {
840 value: dstData.dpid
841 }
842 }
843 },
Paul Greysonaa812562013-03-28 12:43:12 -0700844 createPending: true
Paul Greyson13f02b92013-03-28 11:29:35 -0700845 };
846
847 selectFlow(flow);
848
849 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700850 deselectFlowIfCreatePending(flow);
Paul Greyson6f918402013-03-28 12:18:30 -0700851 }, pendingTimeout);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700852 }
853 } else {
854 var map = linkMap[srcData.dpid];
855 if (map && map[dstData.dpid]) {
856 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
857 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700858 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700859 }
860 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700861 map = linkMap[dstData.dpid];
862 if (map && map[srcData.dpid]) {
863 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
864 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700865 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700866 }
867 } else {
868 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
869 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700870 var link1 = {
871 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700872 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700873 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700874 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700875 pending: true
876 };
877 pendingLinks[makeLinkKey(link1)] = link1;
878 var link2 = {
879 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700880 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700881 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700882 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700883 pending: true
884 };
885 pendingLinks[makeLinkKey(link2)] = link2;
Paul Greyson5cc35f02013-03-28 10:07:36 -0700886 updateTopology();
Paul Greyson40c8a592013-03-27 14:10:33 -0700887
Paul Greyson2913af82013-03-27 14:53:17 -0700888 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700889
Paul Greyson5cc35f02013-03-28 10:07:36 -0700890 // remove the pending links after 10s
Paul Greyson40c8a592013-03-27 14:10:33 -0700891 setTimeout(function () {
892 delete pendingLinks[makeLinkKey(link1)];
893 delete pendingLinks[makeLinkKey(link2)];
894
Paul Greyson5cc35f02013-03-28 10:07:36 -0700895 updateTopology();
Paul Greyson6f918402013-03-28 12:18:30 -0700896 }, pendingTimeout);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700897 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700898 }
899 }
900 }
901
902 clearHighlight();
903 } else {
904 clearHighlight();
905 }
906
907 });
908
Paul Greyson9066ab02013-03-23 18:15:41 -0700909 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700910 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700911 return;
912 }
913
914 // create the nodes
915 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700916 .data(data, function (data) {
917 return data.dpid;
918 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700919 .enter().append("svg:g")
920 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -0700921 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700922 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -0700923 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700924 .attr("transform", function(data, i) {
925 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700926 })
Paul Greyson968d1b42013-03-23 16:58:41 -0700927
928 // add the text nodes which show on mouse over
929 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -0700930 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -0700931 .attr("x", function (data) {
932 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
933 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700934 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700935 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -0700936 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -0700937 }
938 } else {
939 if (data.className == 'edge') {
940 return data.width*3 + 4;
941 } else {
942 return data.width + 4;
943 }
944 }
945 })
946 .attr("y", function (data) {
947 var y;
948 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
949 if (data.className == 'edge') {
950 y = data.width*3/2 + 4;
951 } else {
952 y = data.width/2 + 4;
953 }
954 } else {
955 if (data.className == 'edge') {
956 y = data.width*3/2 + 4;
957 } else {
958 y = data.width/2 + 4;
959 }
960 }
961 return y - 6;
962 })
963 .attr("text-anchor", function (data) {
964 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
965 return "end";
966 } else {
967 return "start";
968 }
969 })
970 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -0700971 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
972 if (data.scale) {
973 m = m.scale(data.scale);
974 }
975 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
976 })
977 }
978
979 labelRings.enter().append("svg:g")
980 .attr("class", "textRing")
981 .each(labelRingEnter);
982
Paul Greysonc17278a2013-03-23 10:17:12 -0700983 // switches should not change during operation of the ui so no
984 // rings.exit()
985
986
Paul Greysond1a22d92013-03-19 12:15:19 -0700987 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -0700988
Paul Greysonc17278a2013-03-23 10:17:12 -0700989 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -0700990 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700991 return d['src-switch']+'->'+d['dst-switch'];
992 });
993
994 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -0700995 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -0700996 .attr("class", "link");
997
Paul Greyson084779b2013-03-27 13:55:49 -0700998 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700999 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -07001000 })
Paul Greyson56378ed2013-03-26 23:17:36 -07001001 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -07001002 var src = d3.select(document.getElementById(d['src-switch']));
1003 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -07001004
Paul Greyson084779b2013-03-27 13:55:49 -07001005 var srcPt = document.querySelector('svg').createSVGPoint();
1006 srcPt.x = src.attr('x');
1007 srcPt.y = src.attr('y');
1008 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001009
Paul Greyson084779b2013-03-27 13:55:49 -07001010 var dstPt = document.querySelector('svg').createSVGPoint();
1011 dstPt.x = dst.attr('x');
Paul Greyson421bfcd2013-03-27 22:22:09 -07001012 dstPt.y = dst.attr('y');
Paul Greyson084779b2013-03-27 13:55:49 -07001013 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001014
Paul Greyson084779b2013-03-27 13:55:49 -07001015 var midPt = document.querySelector('svg').createSVGPoint();
1016 midPt.x = (srcPt.x + dstPt.x)/2;
1017 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -07001018
Paul Greyson084779b2013-03-27 13:55:49 -07001019 return line([srcPt, midPt, dstPt]);
1020 })
Paul Greyson40c8a592013-03-27 14:10:33 -07001021 .attr("marker-mid", function(d) { return "url(#arrow)"; })
1022 .classed('pending', function (d) {
1023 return d.pending;
1024 });
Paul Greysonc17278a2013-03-23 10:17:12 -07001025
Paul Greyson56378ed2013-03-26 23:17:36 -07001026
Paul Greysonc17278a2013-03-23 10:17:12 -07001027 // remove old links
1028 links.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001029}
1030
Paul Greyson5cc35f02013-03-28 10:07:36 -07001031function updateControllers() {
Paul Greysond1a22d92013-03-19 12:15:19 -07001032 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -07001033 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -07001034 .each(function (c) {
1035 controllerColorMap[c] = colors.pop();
1036 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
1037 })
1038 .text(function (d) {
1039 return d;
Paul Greyson2913af82013-03-27 14:53:17 -07001040 })
1041 .append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -07001042 .attr('class', 'black-eye');
Paul Greysonbcd3c772013-03-21 13:16:44 -07001043
Paul Greysone262a292013-03-23 10:35:23 -07001044 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -07001045 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -07001046 if (model.activeControllers.indexOf(d) != -1) {
1047 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -07001048 }
Paul Greysonbcd3c772013-03-21 13:16:44 -07001049 var className = 'controller ' + color;
1050 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -07001051 });
Paul Greysond1a22d92013-03-19 12:15:19 -07001052
Paul Greysone262a292013-03-23 10:35:23 -07001053 // this should never be needed
1054 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001055
Paul Greyson2913af82013-03-27 14:53:17 -07001056 controllers.on('dblclick', function (c) {
1057 if (model.activeControllers.indexOf(c) != -1) {
1058 var prompt = 'Dectivate ' + c + '?';
1059 if (confirm(prompt)) {
1060 controllerDown(c);
1061 setPending(d3.select(this));
1062 };
1063 } else {
1064 var prompt = 'Activate ' + c + '?';
1065 if (confirm(prompt)) {
1066 controllerUp(c);
1067 setPending(d3.select(this));
1068 };
1069 }
1070 });
1071
Paul Greyson8247c3f2013-03-28 00:24:02 -07001072 controllers.select('.black-eye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -07001073 var allSelected = true;
1074 for (var key in controllerColorMap) {
1075 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
1076 allSelected = false;
1077 break;
1078 }
1079 }
1080 if (allSelected) {
1081 for (var key in controllerColorMap) {
1082 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
1083 }
1084 } else {
1085 for (var key in controllerColorMap) {
1086 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
1087 }
1088 }
1089
1090 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
1091 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -07001092 });
Paul Greyson8d1c6362013-03-27 13:05:24 -07001093
1094
Paul Greyson740bdaf2013-03-18 16:10:48 -07001095}
1096
Paul Greyson29aa98d2013-03-28 00:09:31 -07001097function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -07001098 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -07001099 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001100// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -07001101
Paul Greysone5991b52013-04-04 01:34:04 -07001102 if (newModel) {
1103 var modelChanged = false;
1104 if (!model || JSON.stringify(model) != JSON.stringify(newModel)) {
1105 modelChanged = true;
1106 model = newModel;
1107 } else {
1108 // console.log('no change');
1109 }
Paul Greysonb48943b2013-03-19 13:27:57 -07001110
Paul Greysone5991b52013-04-04 01:34:04 -07001111 if (modelChanged) {
1112 updateControllers();
1113 updateSelectedFlows();
1114 updateTopology();
1115 }
Paul Greyson5cc35f02013-03-28 10:07:36 -07001116
Paul Greysone5991b52013-04-04 01:34:04 -07001117 updateHeader(newModel);
1118 }
Paul Greyson740bdaf2013-03-18 16:10:48 -07001119
1120 // do it again in 1s
1121 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -07001122 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -07001123 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001124 });
1125}
Paul Greyson740bdaf2013-03-18 16:10:48 -07001126
Paul Greyson38d8bde2013-03-22 22:07:35 -07001127svg = createTopologyView();
Paul Greyson29aa98d2013-03-28 00:09:31 -07001128updateSelectedFlows();
1129
1130d3.select('#showFlowChooser').on('click', function () {
1131 showFlowChooser();
1132});
1133
Paul Greyson72f18852013-03-27 15:56:11 -07001134
Paul Greyson38d8bde2013-03-22 22:07:35 -07001135// workaround for Chrome v25 bug
1136// if executed immediately, the view box transform logic doesn't work properly
1137// fixed in Chrome v27
1138setTimeout(function () {
1139 // workaround for another Chrome v25 bug
1140 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -07001141 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -07001142 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson29aa98d2013-03-28 00:09:31 -07001143 sync(svg);
Paul Greyson38d8bde2013-03-22 22:07:35 -07001144}, 100);