blob: d6fa9830dadaa154ef7790c8fef794e1f1897ea3 [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 Greysonbb403b02013-04-04 17:11:24 -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 }
Paul Greysonbb403b02013-04-04 17:11:24 -0700131 if (pts.length) {
132 return line(pts);
133 } else {
134 return "M0,0";
135 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700136 })
Paul Greyson71b550d2013-03-28 11:56:19 -0700137 .attr('id', function (d) {
138 if (d) {
139 return makeFlowKey(d);
140 }
141 })
Paul Greyson13f02b92013-03-28 11:29:35 -0700142 .classed('pending', function (d) {
Paul Greysonaa812562013-03-28 12:43:12 -0700143 return d && (d.createPending || d.deletePending);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700144 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700145
Paul Greyson56378ed2013-03-26 23:17:36 -0700146 // "marching ants"
Paul Greyson29aa98d2013-03-28 00:09:31 -0700147 flows.select('animate').attr('from', 500);
148
Paul Greyson13f02b92013-03-28 11:29:35 -0700149}
150
151function updateSelectedFlowsTable() {
152 function rowEnter(d) {
153 var row = d3.select(this);
Paul Greyson6f918402013-03-28 12:18:30 -0700154 row.append('div').classed('deleteFlow', true);
Paul Greyson13f02b92013-03-28 11:29:35 -0700155 row.append('div').classed('flowId', true);
156 row.append('div').classed('srcDPID', true);
157 row.append('div').classed('dstDPID', true);
158 row.append('div').classed('iperf', true);
Paul Greyson71b550d2013-03-28 11:56:19 -0700159
Paul Greyson95db7a12013-04-04 14:57:58 -0700160 row.select('.iperf')
161 .append('div')
162 .attr('class', 'iperf-container')
163 .append('svg:svg')
164 .attr('viewBox', '0 0 1000 32')
165 .attr('preserveAspectRatio', 'none')
166 .append('svg:g')
167 .append('svg:path')
168 .attr('class', 'iperfdata');
169
Paul Greyson71b550d2013-03-28 11:56:19 -0700170 row.on('mouseover', function (d) {
171 if (d) {
172 var path = document.getElementById(makeFlowKey(d));
173 d3.select(path).classed('highlight', true);
174 }
175 });
176 row.on('mouseout', function (d) {
177 if (d) {
178 var path = document.getElementById(makeFlowKey(d));
179 d3.select(path).classed('highlight', false);
180 }
Paul Greyson95db7a12013-04-04 14:57:58 -0700181 });
Paul Greyson13f02b92013-03-28 11:29:35 -0700182 }
183
184 function rowUpdate(d) {
185 var row = d3.select(this);
Paul Greyson95db7a12013-04-04 14:57:58 -0700186 row.attr('id', function (d) {
187 if (d) {
188 return makeSelectedFlowKey(d);
189 }
190 });
Paul Greyson2c35f572013-04-04 16:23:48 -0700191
Paul Greysonbb403b02013-04-04 17:11:24 -0700192 if (!d || !hasIPerf(d)) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700193 row.select('.iperfdata')
194 .attr('d', 'M0,0');
195 }
196
Paul Greyson6f918402013-03-28 12:18:30 -0700197 row.select('.deleteFlow').on('click', function () {
Paul Greyson2c35f572013-04-04 16:23:48 -0700198 deselectFlow(d);
Paul Greysonbe8bf872013-04-04 01:53:45 -0700199 });
200 row.on('dblclick', function () {
Paul Greyson6f918402013-03-28 12:18:30 -0700201 if (d) {
202 var prompt = 'Delete flow ' + d.flowId.value + '?';
203 if (confirm(prompt)) {
204 deleteFlow(d);
Paul Greysonaa812562013-03-28 12:43:12 -0700205 d.deletePending = true;
Paul Greyson6f918402013-03-28 12:18:30 -0700206 updateSelectedFlows();
207
208 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700209 d.deletePending = false;
Paul Greyson6f918402013-03-28 12:18:30 -0700210 updateSelectedFlows();
211 }, pendingTimeout)
212 };
213 }
214 });
215
Paul Greyson13f02b92013-03-28 11:29:35 -0700216 row.select('.flowId')
217 .text(function (d) {
218 if (d) {
219 if (d.flowId) {
220 return d.flowId.value;
221 } else {
222 return '0x--';
223 }
224 }
225 })
Paul Greysoncb5d30d2013-04-04 02:00:56 -0700226 .classed('pending', function (d) {
227 return d && (d.createPending || d.deletePending);
228 });
Paul Greyson13f02b92013-03-28 11:29:35 -0700229
230 row.select('.srcDPID')
231 .text(function (d) {
232 if (d) {
233 return d.dataPath.srcPort.dpid.value;
234 }
235 });
236
237 row.select('.dstDPID')
238 .text(function (d) {
239 if (d) {
240 return d.dataPath.dstPort.dpid.value;
241 }
242 });
243 }
244
245 var flows = d3.select('#selectedFlows')
246 .selectAll('.selectedFlow')
247 .data(selectedFlows);
248
249 flows.enter()
250 .append('div')
251 .classed('selectedFlow', true)
252 .each(rowEnter);
253
254 flows.each(rowUpdate);
255
Paul Greyson29aa98d2013-03-28 00:09:31 -0700256 flows.exit().remove();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700257}
258
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700259// TODO: cancel the interval when the flow is desel
260function startIPerfForFlow(flow) {
Paul Greyson95db7a12013-04-04 14:57:58 -0700261 var duration = 10000; // seconds
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700262 var interval = 100; // ms. this is defined by the server
Paul Greysonbb403b02013-04-04 17:11:24 -0700263 var updateRate = 2000; // ms
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700264
Paul Greyson2c35f572013-04-04 16:23:48 -0700265 function makePoints() {
266 var pts = [];
267 var i;
268 for (i=0; i < 100; ++i) {
269 var sample = flow.iperfData.samples[i];
270 var height = 32 * sample/50000000;
271 if (height > 32)
272 height = 32;
273 pts.push({
Paul Greysonbb403b02013-04-04 17:11:24 -0700274 x: i * 1000/99,
Paul Greyson2c35f572013-04-04 16:23:48 -0700275 y: 32 - height
276 })
277 }
278 return pts;
279 }
280
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700281 if (flow.flowId) {
282 console.log('starting iperf for: ' + flow.flowId.value);
283 startIPerf(flow, duration, updateRate/interval);
Paul Greyson2c35f572013-04-04 16:23:48 -0700284 flow.iperfDisplayInterval = setInterval(function () {
285 if (flow.iperfData) {
Paul Greysonbb403b02013-04-04 17:11:24 -0700286 while (flow.iperfData.samples.length < 100) {
287 flow.iperfData.samples.push(0);
Paul Greyson2c35f572013-04-04 16:23:48 -0700288 }
289 var iperfPath = d3.select(document.getElementById(makeSelectedFlowKey(flow))).select('path');
290 iperfPath.attr('d', line(makePoints()));
291 flow.iperfData.samples.shift();
292 }
293
294
295 }, interval);
296 flow.iperfFetchInterval = setInterval(function () {
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700297 getIPerfData(flow, function (data) {
Paul Greyson95db7a12013-04-04 14:57:58 -0700298 try {
Paul Greyson2c35f572013-04-04 16:23:48 -0700299 if (!flow.iperfData) {
300 flow.iperfData = {
301 samples: []
302 };
303 var i;
304 for (i = 0; i < 100; ++i) {
305 flow.iperfData.samples.push(0);
306 }
307 }
308
Paul Greyson95db7a12013-04-04 14:57:58 -0700309 var iperfData = JSON.parse(data);
310 // if the data is fresh
Paul Greysonbb403b02013-04-04 17:11:24 -0700311 if (flow.iperfData.timestamp && iperfData.timestamp != flow.iperfData.timestamp) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700312 iperfData.samples.forEach(function (s) {
313 flow.iperfData.samples.push(s);
314 });
Paul Greyson95db7a12013-04-04 14:57:58 -0700315 }
Paul Greyson2c35f572013-04-04 16:23:48 -0700316 flow.iperfData.timestamp = iperfData.timestamp;
Paul Greyson95db7a12013-04-04 14:57:58 -0700317 } catch (e) {
318 console.log('bad iperf data: ' + data);
319 }
320// console.log(data);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700321 });
Paul Greysonbb403b02013-04-04 17:11:24 -0700322 }, updateRate/2); // over sample to avoid gaps
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700323 }
324}
325
Paul Greyson13f02b92013-03-28 11:29:35 -0700326function updateSelectedFlows() {
327 // make sure that all of the selected flows are either
328 // 1) valid (meaning they are in the latest list of flows)
329 // 2) pending
330 if (model) {
331 var flowMap = {};
332 model.flows.forEach(function (flow) {
333 flowMap[makeFlowKey(flow)] = flow;
334 });
335
336 var newSelectedFlows = [];
337 selectedFlows.forEach(function (flow) {
338 if (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700339 var liveFlow = flowMap[makeFlowKey(flow)];
340 if (liveFlow) {
341 newSelectedFlows.push(liveFlow);
Paul Greysonaa812562013-03-28 12:43:12 -0700342 liveFlow.deletePending = flow.deletePending;
Paul Greyson2c35f572013-04-04 16:23:48 -0700343 liveFlow.iperfFetchInterval = flow.iperfFetchInterval;
344 liveFlow.iperfDisplayInterval = flow.iperfDisplayInterval;
Paul Greysonaa812562013-03-28 12:43:12 -0700345 } else if (flow.createPending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700346 newSelectedFlows.push(flow);
Paul Greysonbb403b02013-04-04 17:11:24 -0700347 } else if (hasIPerf(flow)) {
348 clearIPerf(flow);
Paul Greyson13f02b92013-03-28 11:29:35 -0700349 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700350 }
351 });
352 selectedFlows = newSelectedFlows;
353 }
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700354 selectedFlows.forEach(function (flow) {
Paul Greysonbb403b02013-04-04 17:11:24 -0700355 if (!hasIPerf(flow)) {
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700356 startIPerfForFlow(flow);
357 }
358 });
Paul Greyson6f918402013-03-28 12:18:30 -0700359 while (selectedFlows.length < 3) {
360 selectedFlows.push(null);
361 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700362
363 updateSelectedFlowsTable();
364 updateSelectedFlowsTopology();
365}
366
367function selectFlow(flow) {
Paul Greysonc30f75e2013-03-28 11:45:15 -0700368 var flowKey = makeFlowKey(flow);
369 var alreadySelected = false;
370 selectedFlows.forEach(function (f) {
371 if (f && makeFlowKey(f) === flowKey) {
372 alreadySelected = true;
373 }
374 });
375
376 if (!alreadySelected) {
377 selectedFlows.unshift(flow);
378 selectedFlows = selectedFlows.slice(0, 3);
379 updateSelectedFlows();
380 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700381}
382
Paul Greysonbb403b02013-04-04 17:11:24 -0700383function hasIPerf(flow) {
384 return flow && flow.iperfFetchInterval;
385}
386
387function clearIPerf(flow) {
388 console.log('clearing iperf interval for: ' + flow.flowId.value);
389 clearInterval(flow.iperfFetchInterval);
390 delete flow.iperfFetchInterval;
391 clearInterval(flow.iperfDisplayInterval);
392 delete flow.iperfDisplayInterval;
393 delete flow.iperfData;
394}
395
Paul Greysonaa812562013-03-28 12:43:12 -0700396function deselectFlow(flow, ifCreatePending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700397 var flowKey = makeFlowKey(flow);
398 var newSelectedFlows = [];
399 selectedFlows.forEach(function (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700400 if (!flow ||
401 flowKey !== makeFlowKey(flow) ||
Paul Greysonaa812562013-03-28 12:43:12 -0700402 flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700403 newSelectedFlows.push(flow);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700404 } else {
Paul Greysonbb403b02013-04-04 17:11:24 -0700405 if (hasIPerf(flow)) {
406 clearIPerf(flow);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700407 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700408 }
409 });
410 selectedFlows = newSelectedFlows;
411 while (selectedFlows.length < 3) {
412 selectedFlows.push(null);
413 }
414
415 updateSelectedFlows();
416}
417
Paul Greysonaa812562013-03-28 12:43:12 -0700418function deselectFlowIfCreatePending(flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700419 deselectFlow(flow, true);
420}
421
Paul Greyson29aa98d2013-03-28 00:09:31 -0700422function showFlowChooser() {
423 function rowEnter(d) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700424 var row = d3.select(this);
425
Paul Greyson127d7fb2013-03-25 23:39:20 -0700426 row.append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -0700427 .classed('black-eye', true).
Paul Greyson29aa98d2013-03-28 00:09:31 -0700428 on('click', function () {
Paul Greyson13f02b92013-03-28 11:29:35 -0700429 selectFlow(d);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700430 });
431
432 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700433 .classed('flowId', true)
434 .text(function (d) {
435 return d.flowId.value;
436 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700437
438 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700439 .classed('srcDPID', true)
440 .text(function (d) {
441 return d.dataPath.srcPort.dpid.value;
442 });
443
Paul Greyson127d7fb2013-03-25 23:39:20 -0700444
445 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700446 .classed('dstDPID', true)
447 .text(function (d) {
448 return d.dataPath.dstPort.dpid.value;
449 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700450
Paul Greyson127d7fb2013-03-25 23:39:20 -0700451 }
452
Paul Greyson29aa98d2013-03-28 00:09:31 -0700453 var flows = d3.select('#flowChooser')
454 .append('div')
455 .style('pointer-events', 'auto')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700456 .selectAll('.selectedFlow')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700457 .data(model.flows)
Paul Greyson127d7fb2013-03-25 23:39:20 -0700458 .enter()
459 .append('div')
460 .classed('selectedFlow', true)
461 .each(rowEnter);
462
Paul Greyson29aa98d2013-03-28 00:09:31 -0700463 setTimeout(function () {
464 d3.select(document.body).on('click', function () {
465 d3.select('#flowChooser').html('');
466 d3.select(document.body).on('click', null);
467 });
468 }, 0);
469}
470
Paul Greyson29aa98d2013-03-28 00:09:31 -0700471
Paul Greyson127d7fb2013-03-25 23:39:20 -0700472
Paul Greysond1a22d92013-03-19 12:15:19 -0700473function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700474 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700475 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
476 d3.select('#activeFlows').text(model.flows.length);
477}
478
479function toRadians (angle) {
480 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700481}
482
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700483var widths = {
484 edge: 6,
485 aggregation: 12,
486 core: 18
487}
488
Paul Greysonc17278a2013-03-23 10:17:12 -0700489function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700490 var rings = [{
491 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700492 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700493 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700494 className: 'edge',
495 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700496 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700497 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700498 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700499 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700500 className: 'aggregation',
501 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700502 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700503 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700504 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700505 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700506 className: 'core',
507 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700508 }];
509
Paul Greysonde7fad52013-03-19 12:47:32 -0700510
511 var aggRanges = {};
512
513 // arrange edge switches at equal increments
514 var k = 360 / rings[0].switches.length;
515 rings[0].switches.forEach(function (s, i) {
516 var angle = k * i;
517
518 rings[0].angles[i] = angle;
519
520 // record the angle for the agg switch layout
521 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700522 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700523 var aggdpid = dpid.join(':');
524 var aggRange = aggRanges[aggdpid];
525 if (!aggRange) {
526 aggRange = aggRanges[aggdpid] = {};
527 aggRange.min = aggRange.max = angle;
528 } else {
529 aggRange.max = angle;
530 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700531 });
532
533 // arrange aggregation switches to "fan out" to edge switches
534 k = 360 / rings[1].switches.length;
535 rings[1].switches.forEach(function (s, i) {
536// rings[1].angles[i] = k * i;
537 var range = aggRanges[s.dpid];
538
Paul Greyson832d2202013-03-21 13:27:56 -0700539 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700540 });
541
Paul Greyson3f890b62013-03-22 17:39:36 -0700542 // find the association between core switches and aggregation switches
543 var aggregationSwitchMap = {};
544 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700545 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700546 });
547
Paul Greyson3f890b62013-03-22 17:39:36 -0700548 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700549 k = 360 / rings[2].switches.length;
550 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700551// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700552 var associatedAggregationSwitches = model.configuration.association[s.dpid];
553 // TODO: go between if there are multiple
554 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
555
556 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700557 });
558
Paul Greyson644d92a2013-03-23 18:00:40 -0700559 // TODO: construct this form initially rather than converting. it works better because
560 // it allows binding by dpid
561 var testRings = [];
562 rings.forEach(function (ring) {
563 var testRing = [];
564 ring.switches.forEach(function (s, i) {
565 var testSwitch = {
566 dpid: s.dpid,
567 state: s.state,
568 radius: ring.radius,
569 width: ring.width,
570 className: ring.className,
571 angle: ring.angles[i],
572 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700573 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700574 testRing.push(testSwitch);
575 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700576
577
Paul Greyson644d92a2013-03-23 18:00:40 -0700578 testRings.push(testRing);
579 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700580
581
Paul Greyson644d92a2013-03-23 18:00:40 -0700582// return rings;
583 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700584}
585
Paul Greyson40c8a592013-03-27 14:10:33 -0700586function makeLinkKey(link) {
587 return link['src-switch'] + '=>' + link['dst-switch'];
588}
589
Paul Greyson13f02b92013-03-28 11:29:35 -0700590function makeFlowKey(flow) {
591 return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value;
592}
593
Paul Greyson95db7a12013-04-04 14:57:58 -0700594function makeSelectedFlowKey(flow) {
595 return 'S' + makeFlowKey(flow);
596}
597
Paul Greyson40c8a592013-03-27 14:10:33 -0700598function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700599 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700600 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700601 var srcDPID = link['src-switch'];
602 var dstDPID = link['dst-switch'];
603
604 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700605
Paul Greyson8d1c6362013-03-27 13:05:24 -0700606 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700607
608 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700609 });
610 return linkMap;
611}
612
Paul Greyson5cc35f02013-03-28 10:07:36 -0700613// removes links from the pending list that are now in the model
614function reconcilePendingLinks(model) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700615 var links = [];
616 model.links.forEach(function (link) {
617 links.push(link);
618 delete pendingLinks[makeLinkKey(link)]
619 })
620 var linkId;
621 for (linkId in pendingLinks) {
622 links.push(pendingLinks[linkId]);
623 }
Paul Greyson5cc35f02013-03-28 10:07:36 -0700624 return links
625}
Paul Greyson40c8a592013-03-27 14:10:33 -0700626
Paul Greyson5cc35f02013-03-28 10:07:36 -0700627updateTopology = function() {
628
629 // DRAW THE SWITCHES
630 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
631
632 var links = reconcilePendingLinks(model);
Paul Greyson40c8a592013-03-27 14:10:33 -0700633 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700634
Paul Greyson8d1c6362013-03-27 13:05:24 -0700635 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700636
637 d3.event.preventDefault();
638
Paul Greyson5cc35f02013-03-28 10:07:36 -0700639 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
640
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700641 if (data.highlighted) {
642 return;
643 }
644
645 // only highlight valid link or flow destination by checking for class of existing highlighted circle
Paul Greyson421bfcd2013-03-27 22:22:09 -0700646 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700647 if (highlighted.length == 1) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700648 var s = d3.select(highlighted[0]).select('circle');
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700649 // only allow links
650 // edge->edge (flow)
651 // aggregation->core
652 // core->core
653 if (data.className == 'edge' && !s.classed('edge') ||
654 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
655 data.className == 'aggregation' && !s.classed('core')) {
656 return;
657 }
658
659 // don't highlight if there's already a link or flow
660 // var map = linkMap[data.dpid];
661 // console.log(map);
662 // console.log(s.data()[0].dpid);
663 // console.log(map[s.data()[0].dpid]);
664 // if (map && map[s.data()[0].dpid]) {
665 // return;
666 // }
667
668 // the second highlighted switch is the target for a link or flow
669 data.target = true;
670 }
671
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700672 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700673 node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700674 data.highlighted = true;
675 node.moveToFront();
676 }
677
Paul Greyson8d1c6362013-03-27 13:05:24 -0700678 function mouseOutSwitch(data) {
Paul Greyson5cc35f02013-03-28 10:07:36 -0700679 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
680
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700681 if (data.mouseDown)
682 return;
683
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700684 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700685 node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700686 data.highlighted = false;
687 data.target = false;
688 }
689
Paul Greyson8d1c6362013-03-27 13:05:24 -0700690 function mouseDownSwitch(data) {
691 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700692 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700693 d3.select('#topology').classed('linking', true);
694
Paul Greyson421bfcd2013-03-27 22:22:09 -0700695 d3.select('svg')
696 .append('svg:path')
697 .attr('id', 'linkVector')
698 .attr('d', function () {
699 var s = d3.select(document.getElementById(data.dpid));
700
701 var pt = document.querySelector('svg').createSVGPoint();
702 pt.x = s.attr('x');
703 pt.y = s.attr('y');
704 pt = pt.matrixTransform(s[0][0].getCTM());
705
706 return line([pt, pt]);
707 });
708
709
Paul Greyson72f18852013-03-27 15:56:11 -0700710 if (data.className === 'core') {
711 d3.selectAll('.edge').classed('nodrop', true);
712 }
713 if (data.className === 'edge') {
714 d3.selectAll('.core').classed('nodrop', true);
715 d3.selectAll('.aggregation').classed('nodrop', true);
716 }
717 if (data.className === 'aggregation') {
718 d3.selectAll('.edge').classed('nodrop', true);
719 d3.selectAll('.aggregation').classed('nodrop', true);
720 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700721 }
722
Paul Greyson8d1c6362013-03-27 13:05:24 -0700723 function mouseUpSwitch(data) {
724 if (data.mouseDown) {
725 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700726 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700727 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700728 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700729 }
730 }
731
732 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700733 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700734 if (data.state == 'ACTIVE') {
735 var prompt = 'Deactivate ' + data.dpid + '?';
736 if (confirm(prompt)) {
737 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700738 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700739 }
740 } else {
741 var prompt = 'Activate ' + data.dpid + '?';
742 if (confirm(prompt)) {
743 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700744 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700745 }
746 }
747 }
748
Paul Greyson740bdaf2013-03-18 16:10:48 -0700749 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700750 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700751 return;
752 }
753
Paul Greysonc17278a2013-03-23 10:17:12 -0700754 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700755 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700756 .data(data, function (data) {
757 return data.dpid;
758 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700759 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700760 .attr("id", function (data, i) {
761 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700762 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700763 .attr("transform", function(data, i) {
764 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700765 });
766
Paul Greysonc17278a2013-03-23 10:17:12 -0700767 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700768 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700769 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700770 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700771 if (data.scale) {
772 m = m.scale(data.scale);
773 }
774 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700775 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700776 .attr("x", function (data) {
777 return -data.width / 2;
778 })
779 .attr("y", function (data) {
780 return -data.width / 2;
781 })
782 .attr("r", function (data) {
783 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700784 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700785
Paul Greysonc17278a2013-03-23 10:17:12 -0700786 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700787 nodes.on('mouseover', mouseOverSwitch);
788 nodes.on('mouseout', mouseOutSwitch);
789 nodes.on('mouseup', mouseUpSwitch);
790 nodes.on('mousedown', mouseDownSwitch);
791
792 // only do switch up/down for core switches
793 if (i == 2) {
794 nodes.on('dblclick', doubleClickSwitch);
795 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700796 }
797
Paul Greysonc17278a2013-03-23 10:17:12 -0700798 // append switches
799 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700800 .attr("class", "ring")
801 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700802
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700803
Paul Greysonc17278a2013-03-23 10:17:12 -0700804 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700805 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700806 .data(data, function (data) {
807 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700808 });
Paul Greyson347fb742013-03-27 13:40:29 -0700809 nodes.select('circle')
810 .each(function (data) {
811 // if there's a pending state changed and then the state changes, clear the pending class
812 var circle = d3.select(this);
813 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
814 data.state === 'INACTIVE' && circle.classed('active')) {
815 circle.classed('pending', false);
816 }
817 })
818 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700819 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700820 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700821 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700822 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700823 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700824 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700825 }
826
827 // update switches
828 rings.each(ringUpdate);
829
Paul Greyson968d1b42013-03-23 16:58:41 -0700830
831 // Now setup the labels
832 // This is done separately because SVG draws in node order and we want the labels
833 // always on top
834 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
835
Paul Greyson421bfcd2013-03-27 22:22:09 -0700836 d3.select(document.body).on('mousemove', function () {
837 if (!d3.select('#topology').classed('linking')) {
838 return;
839 }
840 var linkVector = document.getElementById('linkVector');
841 if (!linkVector) {
842 return;
843 }
844 linkVector = d3.select(linkVector);
845
846 var highlighted = svg.selectAll('.highlight')[0];
847 var s1 = null, s2 = null;
848 if (highlighted.length > 1) {
849 var s1 = d3.select(highlighted[0]);
850 var s2 = d3.select(highlighted[1]);
851
852 } else if (highlighted.length > 0) {
853 var s1 = d3.select(highlighted[0]);
854 }
855 var src = s1;
856 if (s2 && !s2.data()[0].target) {
857 src = s2;
858 }
859 if (src) {
860 linkVector.attr('d', function () {
861 var srcPt = document.querySelector('svg').createSVGPoint();
862 srcPt.x = src.attr('x');
863 srcPt.y = src.attr('y');
864 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
865
866 var svg = document.getElementById('topology');
867 var mouse = d3.mouse(viewbox);
868 var dstPt = document.querySelector('svg').createSVGPoint();
869 dstPt.x = mouse[0];
870 dstPt.y = mouse[1];
871 dstPt = dstPt.matrixTransform(viewbox.getCTM());
872
873 return line([srcPt, dstPt]);
874 });
875 }
876 });
877
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700878 d3.select(document.body).on('mouseup', function () {
879 function clearHighlight() {
880 svg.selectAll('circle').each(function (data) {
881 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700882 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700883 mouseOutSwitch(data);
Paul Greyson421bfcd2013-03-27 22:22:09 -0700884 });
885 d3.select('#linkVector').remove();
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700886 };
887
Paul Greyson72f18852013-03-27 15:56:11 -0700888 d3.selectAll('.nodrop').classed('nodrop', false);
889
Paul Greyson084779b2013-03-27 13:55:49 -0700890 function removeLink(link) {
891 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
892 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
893
894 if (path1) {
895 setPending(d3.select(path1));
896 }
897 if (path2) {
898 setPending(d3.select(path2));
899 }
900
901 linkDown(link);
902 }
903
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700904
Paul Greyson421bfcd2013-03-27 22:22:09 -0700905 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700906 if (highlighted.length == 2) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700907 var s1Data = highlighted[0].__data__;
908 var s2Data = highlighted[1].__data__;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700909
910 var srcData, dstData;
911 if (s1Data.target) {
912 dstData = s1Data;
913 srcData = s2Data;
914 } else {
915 dstData = s2Data;
916 srcData = s1Data;
917 }
918
919 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
920 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
921 if (confirm(prompt)) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700922 addFlow(srcData, dstData);
923
924 var flow = {
925 dataPath: {
926 srcPort: {
927 dpid: {
928 value: srcData.dpid
929 }
930 },
931 dstPort: {
932 dpid: {
933 value: dstData.dpid
934 }
935 }
936 },
Paul Greysonaa812562013-03-28 12:43:12 -0700937 createPending: true
Paul Greyson13f02b92013-03-28 11:29:35 -0700938 };
939
940 selectFlow(flow);
941
942 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700943 deselectFlowIfCreatePending(flow);
Paul Greyson6f918402013-03-28 12:18:30 -0700944 }, pendingTimeout);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700945 }
946 } else {
947 var map = linkMap[srcData.dpid];
948 if (map && map[dstData.dpid]) {
949 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
950 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700951 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700952 }
953 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700954 map = linkMap[dstData.dpid];
955 if (map && map[srcData.dpid]) {
956 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
957 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700958 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700959 }
960 } else {
961 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
962 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700963 var link1 = {
964 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700965 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700966 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700967 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700968 pending: true
969 };
970 pendingLinks[makeLinkKey(link1)] = link1;
971 var link2 = {
972 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700973 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700974 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700975 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700976 pending: true
977 };
978 pendingLinks[makeLinkKey(link2)] = link2;
Paul Greyson5cc35f02013-03-28 10:07:36 -0700979 updateTopology();
Paul Greyson40c8a592013-03-27 14:10:33 -0700980
Paul Greyson2913af82013-03-27 14:53:17 -0700981 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700982
Paul Greyson5cc35f02013-03-28 10:07:36 -0700983 // remove the pending links after 10s
Paul Greyson40c8a592013-03-27 14:10:33 -0700984 setTimeout(function () {
985 delete pendingLinks[makeLinkKey(link1)];
986 delete pendingLinks[makeLinkKey(link2)];
987
Paul Greyson5cc35f02013-03-28 10:07:36 -0700988 updateTopology();
Paul Greyson6f918402013-03-28 12:18:30 -0700989 }, pendingTimeout);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700990 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700991 }
992 }
993 }
994
995 clearHighlight();
996 } else {
997 clearHighlight();
998 }
999
1000 });
1001
Paul Greyson9066ab02013-03-23 18:15:41 -07001002 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -07001003 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -07001004 return;
1005 }
1006
1007 // create the nodes
1008 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -07001009 .data(data, function (data) {
1010 return data.dpid;
1011 })
Paul Greyson968d1b42013-03-23 16:58:41 -07001012 .enter().append("svg:g")
1013 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -07001014 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -07001015 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -07001016 })
Paul Greyson644d92a2013-03-23 18:00:40 -07001017 .attr("transform", function(data, i) {
1018 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001019 })
Paul Greyson968d1b42013-03-23 16:58:41 -07001020
1021 // add the text nodes which show on mouse over
1022 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -07001023 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -07001024 .attr("x", function (data) {
1025 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1026 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -07001027 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -07001028 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -07001029 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -07001030 }
1031 } else {
1032 if (data.className == 'edge') {
1033 return data.width*3 + 4;
1034 } else {
1035 return data.width + 4;
1036 }
1037 }
1038 })
1039 .attr("y", function (data) {
1040 var y;
1041 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1042 if (data.className == 'edge') {
1043 y = data.width*3/2 + 4;
1044 } else {
1045 y = data.width/2 + 4;
1046 }
1047 } else {
1048 if (data.className == 'edge') {
1049 y = data.width*3/2 + 4;
1050 } else {
1051 y = data.width/2 + 4;
1052 }
1053 }
1054 return y - 6;
1055 })
1056 .attr("text-anchor", function (data) {
1057 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1058 return "end";
1059 } else {
1060 return "start";
1061 }
1062 })
1063 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -07001064 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
1065 if (data.scale) {
1066 m = m.scale(data.scale);
1067 }
1068 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
1069 })
1070 }
1071
1072 labelRings.enter().append("svg:g")
1073 .attr("class", "textRing")
1074 .each(labelRingEnter);
1075
Paul Greysonc17278a2013-03-23 10:17:12 -07001076 // switches should not change during operation of the ui so no
1077 // rings.exit()
1078
1079
Paul Greysond1a22d92013-03-19 12:15:19 -07001080 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -07001081
Paul Greysonc17278a2013-03-23 10:17:12 -07001082 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -07001083 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -07001084 return d['src-switch']+'->'+d['dst-switch'];
1085 });
1086
1087 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -07001088 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -07001089 .attr("class", "link");
1090
Paul Greyson084779b2013-03-27 13:55:49 -07001091 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -07001092 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -07001093 })
Paul Greyson56378ed2013-03-26 23:17:36 -07001094 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -07001095 var src = d3.select(document.getElementById(d['src-switch']));
1096 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -07001097
Paul Greyson084779b2013-03-27 13:55:49 -07001098 var srcPt = document.querySelector('svg').createSVGPoint();
1099 srcPt.x = src.attr('x');
1100 srcPt.y = src.attr('y');
1101 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001102
Paul Greyson084779b2013-03-27 13:55:49 -07001103 var dstPt = document.querySelector('svg').createSVGPoint();
1104 dstPt.x = dst.attr('x');
Paul Greyson421bfcd2013-03-27 22:22:09 -07001105 dstPt.y = dst.attr('y');
Paul Greyson084779b2013-03-27 13:55:49 -07001106 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001107
Paul Greyson084779b2013-03-27 13:55:49 -07001108 var midPt = document.querySelector('svg').createSVGPoint();
1109 midPt.x = (srcPt.x + dstPt.x)/2;
1110 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -07001111
Paul Greyson084779b2013-03-27 13:55:49 -07001112 return line([srcPt, midPt, dstPt]);
1113 })
Paul Greyson40c8a592013-03-27 14:10:33 -07001114 .attr("marker-mid", function(d) { return "url(#arrow)"; })
1115 .classed('pending', function (d) {
1116 return d.pending;
1117 });
Paul Greysonc17278a2013-03-23 10:17:12 -07001118
Paul Greyson56378ed2013-03-26 23:17:36 -07001119
Paul Greysonc17278a2013-03-23 10:17:12 -07001120 // remove old links
1121 links.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001122}
1123
Paul Greyson5cc35f02013-03-28 10:07:36 -07001124function updateControllers() {
Paul Greysond1a22d92013-03-19 12:15:19 -07001125 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -07001126 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -07001127 .each(function (c) {
1128 controllerColorMap[c] = colors.pop();
1129 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
1130 })
1131 .text(function (d) {
1132 return d;
Paul Greyson2913af82013-03-27 14:53:17 -07001133 })
1134 .append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -07001135 .attr('class', 'black-eye');
Paul Greysonbcd3c772013-03-21 13:16:44 -07001136
Paul Greysone262a292013-03-23 10:35:23 -07001137 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -07001138 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -07001139 if (model.activeControllers.indexOf(d) != -1) {
1140 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -07001141 }
Paul Greysonbcd3c772013-03-21 13:16:44 -07001142 var className = 'controller ' + color;
1143 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -07001144 });
Paul Greysond1a22d92013-03-19 12:15:19 -07001145
Paul Greysone262a292013-03-23 10:35:23 -07001146 // this should never be needed
1147 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001148
Paul Greyson2913af82013-03-27 14:53:17 -07001149 controllers.on('dblclick', function (c) {
1150 if (model.activeControllers.indexOf(c) != -1) {
1151 var prompt = 'Dectivate ' + c + '?';
1152 if (confirm(prompt)) {
1153 controllerDown(c);
1154 setPending(d3.select(this));
1155 };
1156 } else {
1157 var prompt = 'Activate ' + c + '?';
1158 if (confirm(prompt)) {
1159 controllerUp(c);
1160 setPending(d3.select(this));
1161 };
1162 }
1163 });
1164
Paul Greyson8247c3f2013-03-28 00:24:02 -07001165 controllers.select('.black-eye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -07001166 var allSelected = true;
1167 for (var key in controllerColorMap) {
1168 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
1169 allSelected = false;
1170 break;
1171 }
1172 }
1173 if (allSelected) {
1174 for (var key in controllerColorMap) {
1175 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
1176 }
1177 } else {
1178 for (var key in controllerColorMap) {
1179 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
1180 }
1181 }
1182
1183 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
1184 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -07001185 });
Paul Greyson8d1c6362013-03-27 13:05:24 -07001186
1187
Paul Greyson740bdaf2013-03-18 16:10:48 -07001188}
1189
Paul Greyson2c35f572013-04-04 16:23:48 -07001190var modelString;
Paul Greyson29aa98d2013-03-28 00:09:31 -07001191function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -07001192 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -07001193 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001194// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -07001195
Paul Greysone5991b52013-04-04 01:34:04 -07001196 if (newModel) {
1197 var modelChanged = false;
Paul Greyson2c35f572013-04-04 16:23:48 -07001198 var newModelString = JSON.stringify(newModel);
1199 if (!modelString || newModelString != modelString) {
Paul Greysone5991b52013-04-04 01:34:04 -07001200 modelChanged = true;
1201 model = newModel;
Paul Greyson2c35f572013-04-04 16:23:48 -07001202 modelString = newModelString;
Paul Greysone5991b52013-04-04 01:34:04 -07001203 } else {
1204 // console.log('no change');
1205 }
Paul Greysonb48943b2013-03-19 13:27:57 -07001206
Paul Greysone5991b52013-04-04 01:34:04 -07001207 if (modelChanged) {
1208 updateControllers();
1209 updateSelectedFlows();
1210 updateTopology();
1211 }
Paul Greyson5cc35f02013-03-28 10:07:36 -07001212
Paul Greysone5991b52013-04-04 01:34:04 -07001213 updateHeader(newModel);
1214 }
Paul Greyson740bdaf2013-03-18 16:10:48 -07001215
1216 // do it again in 1s
1217 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -07001218 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -07001219 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001220 });
1221}
Paul Greyson740bdaf2013-03-18 16:10:48 -07001222
Paul Greyson38d8bde2013-03-22 22:07:35 -07001223svg = createTopologyView();
Paul Greyson29aa98d2013-03-28 00:09:31 -07001224updateSelectedFlows();
1225
1226d3.select('#showFlowChooser').on('click', function () {
1227 showFlowChooser();
1228});
1229
Paul Greyson72f18852013-03-27 15:56:11 -07001230
Paul Greyson38d8bde2013-03-22 22:07:35 -07001231// workaround for Chrome v25 bug
1232// if executed immediately, the view box transform logic doesn't work properly
1233// fixed in Chrome v27
1234setTimeout(function () {
1235 // workaround for another Chrome v25 bug
1236 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -07001237 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -07001238 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson29aa98d2013-03-28 00:09:31 -07001239 sync(svg);
Paul Greyson38d8bde2013-03-22 22:07:35 -07001240}, 100);