blob: d2e67bf957bb6aaf7ce97160336a411ab29f58c2 [file] [log] [blame]
Simon Hunt195cb382014-11-03 17:50:51 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Hunt142d0032014-11-04 20:13:09 -080018 ONOS network topology viewer - version 1.1
Simon Hunt195cb382014-11-03 17:50:51 -080019
20 @author Simon Hunt
Simon Hunta6a9fe72014-11-20 11:17:12 -080021 @author Thomas Vachuska
Simon Hunt195cb382014-11-03 17:50:51 -080022 */
23
24(function (onos) {
25 'use strict';
26
Simon Hunt1a9eff92014-11-07 11:06:34 -080027 // shorter names for library APIs
Simon Huntbb282f52014-11-10 11:08:19 -080028 var d3u = onos.lib.d3util,
Simon Hunta6a9fe72014-11-20 11:17:12 -080029 gly = onos.lib.glyphs;
Simon Hunt1a9eff92014-11-07 11:06:34 -080030
Simon Hunt195cb382014-11-03 17:50:51 -080031 // configuration data
32 var config = {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -080033 useLiveData: true,
Simon Huntfc274c92014-11-11 11:05:46 -080034 fnTrace: true,
Simon Hunt195cb382014-11-03 17:50:51 -080035 debugOn: false,
36 debug: {
Simon Hunt99c13842014-11-06 18:23:12 -080037 showNodeXY: true,
38 showKeyHandler: false
Simon Hunt195cb382014-11-03 17:50:51 -080039 },
Simon Hunt12ce12e2014-11-15 21:13:19 -080040 birdDim: 400,
Simon Hunt195cb382014-11-03 17:50:51 -080041 options: {
42 layering: true,
43 collisionPrevention: true,
Simon Hunt142d0032014-11-04 20:13:09 -080044 showBackground: true
Simon Hunt195cb382014-11-03 17:50:51 -080045 },
46 backgroundUrl: 'img/us-map.png',
Thomas Vachuska7d638d32014-11-07 10:24:43 -080047 webSockUrl: 'ws/topology',
Simon Hunt195cb382014-11-03 17:50:51 -080048 data: {
49 live: {
50 jsonUrl: 'rs/topology/graph',
51 detailPrefix: 'rs/topology/graph/',
52 detailSuffix: ''
53 },
54 fake: {
55 jsonUrl: 'json/network2.json',
56 detailPrefix: 'json/',
57 detailSuffix: '.json'
58 }
59 },
Simon Hunt99c13842014-11-06 18:23:12 -080060 labels: {
61 imgPad: 16,
62 padLR: 4,
63 padTB: 3,
64 marginLR: 3,
65 marginTB: 2,
66 port: {
67 gap: 3,
68 width: 18,
69 height: 14
70 }
71 },
Simon Hunt1a9eff92014-11-07 11:06:34 -080072 topo: {
Thomas Vachuska89543292014-11-19 11:28:33 -080073 linkBaseColor: '#666',
Simon Hunt1a9eff92014-11-07 11:06:34 -080074 linkInColor: '#66f',
Thomas Vachuska9edca302014-11-22 17:06:42 -080075 linkInWidth: 12,
Thomas Vachuska89543292014-11-19 11:28:33 -080076 linkOutColor: '#f00',
Thomas Vachuska9edca302014-11-22 17:06:42 -080077 linkOutWidth: 10
Simon Hunt1a9eff92014-11-07 11:06:34 -080078 },
Paul Greyson29cd58f2014-11-18 13:14:57 -080079 icons: {
Simon Huntc72967b2014-11-20 09:21:42 -080080 device: {
Simon Hunt395a70c2014-11-22 23:17:40 -080081 dim: 36,
82 rx: 4,
83 xoff: -20,
84 yoff: -18
85 },
86 host: {
87 defaultRadius: 9,
88 radius: {
89 endstation: 14,
90 bgpSpeaker: 14,
91 router: 14
92 }
Simon Huntc72967b2014-11-20 09:21:42 -080093 }
Thomas Vachuska89543292014-11-19 11:28:33 -080094 },
Simon Hunt195cb382014-11-03 17:50:51 -080095 force: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080096 note_for_links: 'link.type is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -080097 linkDistance: {
Simon Hunt7cd48f32014-11-09 23:42:50 -080098 direct: 100,
99 optical: 120,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800100 hostLink: 3
Simon Huntc7ee0662014-11-05 16:44:37 -0800101 },
102 linkStrength: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800103 direct: 1.0,
104 optical: 1.0,
105 hostLink: 1.0
Simon Huntc7ee0662014-11-05 16:44:37 -0800106 },
Simon Hunt7cd48f32014-11-09 23:42:50 -0800107 note_for_nodes: 'node.class is used to differentiate',
Simon Huntc7ee0662014-11-05 16:44:37 -0800108 charge: {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800109 device: -8000,
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800110 host: -5000
Simon Hunt195cb382014-11-03 17:50:51 -0800111 }
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800112 },
113 // see below in creation of viewBox on main svg
114 logicalSize: 1000
Simon Hunt195cb382014-11-03 17:50:51 -0800115 };
116
Simon Hunt142d0032014-11-04 20:13:09 -0800117 // radio buttons
Simon Hunt9462e8c2014-11-14 17:28:09 -0800118 var layerButtons = [
119 { text: 'All Layers', id: 'all', cb: showAllLayers },
120 { text: 'Packet Only', id: 'pkt', cb: showPacketLayer },
121 { text: 'Optical Only', id: 'opt', cb: showOpticalLayer }
122 ],
123 layerBtnSet,
124 layerBtnDispatch = {
125 all: showAllLayers,
126 pkt: showPacketLayer,
127 opt: showOpticalLayer
128 };
Simon Hunt934c3ce2014-11-05 11:45:07 -0800129
130 // key bindings
131 var keyDispatch = {
Simon Hunt988c6fc2014-11-20 17:43:03 -0800132 // TODO: remove these "development only" bindings
Simon Hunt8f40cce2014-11-23 15:57:30 -0800133 0: testMe,
134 equals: injectStartupEvents,
135 dash: injectTestEvent,
Simon Hunt99c13842014-11-06 18:23:12 -0800136
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800137 E: [equalizeMasters, 'Equalize mastership roles'],
Thomas Vachuska47635c62014-11-22 01:21:36 -0800138 O: [toggleSummary, 'Toggle ONOS summary pane'],
139 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800140 D: [toggleDetails, 'Disable / enable details pane'],
Simon Hunt988c6fc2014-11-20 17:43:03 -0800141 B: [toggleBg, 'Toggle background image'],
Simon Hunt434cf142014-11-24 11:10:28 -0800142 H: [toggleHosts, 'Toggle host visibility'],
Simon Hunt6d9bd032014-11-28 22:16:40 -0800143 M: [toggleOffline, 'Toggle offline visibility'],
Simon Hunt27d322d2014-11-28 10:45:43 -0800144 L: [cycleLabels, 'Cycle device labels'],
Simon Hunt934c3ce2014-11-05 11:45:07 -0800145 P: togglePorts,
Simon Hunt87514342014-11-24 16:41:27 -0800146 U: [unpin, 'Unpin node (hover mouse over)'],
Simon Hunt3c5ca542014-11-29 14:11:43 -0800147 R: [resetPanZoom, 'Reset pan / zoom'],
Thomas Vachuska9edca302014-11-22 17:06:42 -0800148 V: [showTrafficAction, 'Show related traffic'],
Simon Hunt56ef0fe2014-11-21 08:24:43 -0800149 A: [showAllTrafficAction, 'Show all traffic'],
150 F: [showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntc2367d52014-11-29 19:30:23 -0800151 X: [toggleNodeLock, 'Lock / unlock node positions'],
152 Z: [toggleOblique, 'Toggle oblique view'],
Simon Hunt9462e8c2014-11-14 17:28:09 -0800153 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800154 };
Simon Hunt142d0032014-11-04 20:13:09 -0800155
Simon Hunt87514342014-11-24 16:41:27 -0800156 // mouse gestures
157 var gestures = [
158 ['click', 'Select the item and show details'],
159 ['shift-click', 'Toggle selection state'],
160 ['drag', 'Reposition (and pin) device / host'],
161 ['cmd-scroll', 'Zoom in / out'],
162 ['cmd-drag', 'Pan']
163 ];
164
Simon Hunt195cb382014-11-03 17:50:51 -0800165 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800166 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800167 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800168 nodes: [],
169 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800170 lookup: {},
171 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800172 },
Simon Hunt56d51852014-11-09 13:03:35 -0800173 scenario = {
174 evDir: 'json/ev/',
175 evScenario: '/scenario.json',
176 evPrefix: '/ev_',
177 evOnos: '_onos.json',
178 evUi: '_ui.json',
179 ctx: null,
180 params: {},
181 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800182 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800183 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800184 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800185 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800186 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800187 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800188 deviceLabelIndex = 0,
189 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800190 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800191 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800192 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800193 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800194 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800195 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800196 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800197 onosInstances = {},
198 onosOrder = [],
199 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800200 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800201 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800202 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800203 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800204 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800205 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800206 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800207 haveDetails = false,
Simon Huntc2367d52014-11-29 19:30:23 -0800208 nodeLock = false,
209 oblique = false;
Simon Hunt195cb382014-11-03 17:50:51 -0800210
Simon Hunt434cf142014-11-24 11:10:28 -0800211 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800212 var hoverModeAll = 1,
213 hoverModeFlows = 2,
214 hoverModeIntents = 3,
215 hoverMode = hoverModeFlows;
216
Simon Hunt934c3ce2014-11-05 11:45:07 -0800217 // D3 selections
218 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800219 panZoomContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800220 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800221 topoG,
222 nodeG,
223 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800224 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800225 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800226 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800227 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800228 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800229
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800230 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800231 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800232
Paul Greysonfcba0e82014-11-13 10:21:16 -0800233 // the zoom function
234 var zoom;
235
Simon Hunt142d0032014-11-04 20:13:09 -0800236 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800237 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800238
Simon Hunt99c13842014-11-06 18:23:12 -0800239 function note(label, msg) {
240 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800241 }
242
Simon Hunt99c13842014-11-06 18:23:12 -0800243 function debug(what) {
244 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800245 }
246
Simon Huntfc274c92014-11-11 11:05:46 -0800247 function fnTrace(msg, id) {
248 if (config.fnTrace) {
249 console.log('FN: ' + msg + ' [' + id + ']');
250 }
251 }
Simon Hunt99c13842014-11-06 18:23:12 -0800252
Simon Hunta5e89142014-11-14 07:00:33 -0800253 function evTrace(data) {
254 fnTrace(data.event, data.payload.id);
255 }
256
Simon Hunt934c3ce2014-11-05 11:45:07 -0800257 // ==============================
258 // Key Callbacks
259
Simon Hunt27d322d2014-11-28 10:45:43 -0800260 function flash(txt) {
261 network.view.flash(txt);
262 }
263
Simon Hunt99c13842014-11-06 18:23:12 -0800264 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800265 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800266 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800267 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800268 }
269
Simon Hunt56d51852014-11-09 13:03:35 -0800270 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800271 if (config.useLiveData) { return; }
272
Simon Hunt56d51852014-11-09 13:03:35 -0800273 var sc = scenario,
274 evn = ++sc.evNumber,
275 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
276 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800277 uiUrl = pfx + sc.evUi,
278 stack = [
279 { url: onosUrl, cb: handleServerEvent },
280 { url: uiUrl, cb: handleUiEvent }
281 ];
282 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800283 }
284
Simon Hunt7cd48f32014-11-09 23:42:50 -0800285 function recurseFetchEvent(stack, evn) {
286 var v = scenario.view,
287 frame;
288 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800289 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800290 return;
291 }
292 frame = stack.shift();
293
294 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800295 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800296 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800297 // if we didn't find the data, try the next stack frame
298 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800299 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800300 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800301 }
Simon Hunt99c13842014-11-06 18:23:12 -0800302 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800303 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800304 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800305 }
306 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800307
Simon Hunt56d51852014-11-09 13:03:35 -0800308 }
Simon Hunt50128c02014-11-08 13:36:15 -0800309
Simon Hunt56d51852014-11-09 13:03:35 -0800310 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800311 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
312 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800313 }
314
315 function injectStartupEvents(view) {
316 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800317 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800318
319 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800320 injectTestEvent(view);
321 }
322 }
323
Simon Hunt934c3ce2014-11-05 11:45:07 -0800324 function toggleBg() {
325 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800326 bgImg.style('visibility', visVal(vis === 'hidden'));
327 }
328
Simon Huntc2367d52014-11-29 19:30:23 -0800329 function toggleNodeLock() {
330 nodeLock = !nodeLock;
331 flash('Node positions ' + (nodeLock ? 'locked' : 'unlocked'))
332 }
333
334 function toggleOblique() {
335 oblique = !oblique;
336 // TODO: oblique transformation
337 }
338
Simon Hunt434cf142014-11-24 11:10:28 -0800339 function toggleHosts() {
340 showHosts = !showHosts;
341 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800342 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800343 }
344
Simon Hunt6d9bd032014-11-28 22:16:40 -0800345 function toggleOffline() {
346 showOffline = !showOffline;
347 updateOfflineVisibility();
348 flash('Offline devices ' + visVal(showOffline));
349 }
350
Simon Hunt99c13842014-11-06 18:23:12 -0800351 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800352 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800353 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800354
Simon Hunt99c13842014-11-06 18:23:12 -0800355 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800356 if (d.class === 'device') {
357 updateDeviceLabel(d);
358 }
Simon Hunt99c13842014-11-06 18:23:12 -0800359 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800360 }
361
362 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800363 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800364 }
365
Simon Hunt6ac93f32014-11-13 12:17:27 -0800366 function unpin() {
367 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800368 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800369 hovered.fixed = false;
370 hovered.el.classed('fixed', false);
371 network.force.resume();
372 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800373 }
374
Simon Hunt9462e8c2014-11-14 17:28:09 -0800375 function handleEscape(view) {
376 if (oiShowMaster) {
377 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800378 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800379 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800380 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800381 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800382 } else if (summaryPane.isVisible()) {
383 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800384 stopAntTimer();
385 } else {
386 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800387 }
388 }
389
Simon Hunt934c3ce2014-11-05 11:45:07 -0800390 // ==============================
391 // Radio Button Callbacks
392
Simon Hunta5e89142014-11-14 07:00:33 -0800393 var layerLookup = {
394 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800395 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800396 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800397 bgpSpeaker: 'pkt'
398 },
399 device: {
400 switch: 'pkt',
401 roadm: 'opt'
402 },
403 link: {
404 hostLink: 'pkt',
405 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800406 indirect: '',
407 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800408 optical: 'opt'
409 }
410 };
411
412 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800413 var type = d.class === 'link' ? d.type() : d.type,
414 look = layerLookup[d.class],
415 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800416 return lyr === layer;
417 }
418
419 function unsuppressLayer(which) {
420 node.each(function (d) {
421 var node = d.el;
422 if (inLayer(d, which)) {
423 node.classed('suppressed', false);
424 }
425 });
426
427 link.each(function (d) {
428 var link = d.el;
429 if (inLayer(d, which)) {
430 link.classed('suppressed', false);
431 }
432 });
433 }
434
Simon Hunt9462e8c2014-11-14 17:28:09 -0800435 function suppressLayers(b) {
436 node.classed('suppressed', b);
437 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800438// d3.selectAll('svg .port').classed('inactive', false);
439// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800440 }
441
Simon Hunt9462e8c2014-11-14 17:28:09 -0800442 function showAllLayers() {
443 suppressLayers(false);
444 }
445
Simon Hunt195cb382014-11-03 17:50:51 -0800446 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800447 node.classed('suppressed', true);
448 link.classed('suppressed', true);
449 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800450 }
451
452 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800453 node.classed('suppressed', true);
454 link.classed('suppressed', true);
455 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800456 }
457
Simon Hunt9462e8c2014-11-14 17:28:09 -0800458 function restoreLayerState() {
459 layerBtnDispatch[layerBtnSet.selected()]();
460 }
461
Simon Hunt142d0032014-11-04 20:13:09 -0800462 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800463 // Private functions
464
Simon Hunt99c13842014-11-06 18:23:12 -0800465 function safeId(s) {
466 return s.replace(/[^a-z0-9]/gi, '-');
467 }
468
Simon Huntc7ee0662014-11-05 16:44:37 -0800469 // set the size of the given element to that of the view (reduced if padded)
470 function setSize(el, view, pad) {
471 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800472 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800473 width: view.width() - padding,
474 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800475 });
476 }
477
Simon Hunt8257f4c2014-11-16 19:34:54 -0800478 function makeNodeKey(d, what) {
479 var port = what + 'Port';
480 return d[what] + '/' + d[port];
481 }
482
483 function makeLinkKey(d, flipped) {
484 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
485 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
486 return one + '-' + two;
487 }
488
Simon Hunt269670f2014-11-17 16:17:43 -0800489 function findLinkById(id) {
490 // check to see if this is a reverse lookup, else default to given id
491 var key = network.revLinkToKey[id] || id;
492 return key && network.lookup[key];
493 }
494
Simon Hunt8257f4c2014-11-16 19:34:54 -0800495 function findLink(linkData, op) {
496 var key = makeLinkKey(linkData),
497 keyrev = makeLinkKey(linkData, 1),
498 link = network.lookup[key],
499 linkRev = network.lookup[keyrev],
500 result = {},
501 ldata = link || linkRev,
502 rawLink;
503
504 if (op === 'add') {
505 if (link) {
506 // trying to add a link that we already know about
507 result.ldata = link;
508 result.badLogic = 'addLink: link already added';
509
510 } else if (linkRev) {
511 // we found the reverse of the link to be added
512 result.ldata = linkRev;
513 if (linkRev.fromTarget) {
514 result.badLogic = 'addLink: link already added';
515 }
516 }
517 } else if (op === 'update') {
518 if (!ldata) {
519 result.badLogic = 'updateLink: link not found';
520 } else {
521 rawLink = link ? ldata.fromSource : ldata.fromTarget;
522 result.updateWith = function (data) {
523 $.extend(rawLink, data);
524 restyleLinkElement(ldata);
525 }
526 }
527 } else if (op === 'remove') {
528 if (!ldata) {
529 result.badLogic = 'removeLink: link not found';
530 } else {
531 rawLink = link ? ldata.fromSource : ldata.fromTarget;
532
533 if (!rawLink) {
534 result.badLogic = 'removeLink: link not found';
535
536 } else {
537 result.removeRawLink = function () {
538 if (link) {
539 // remove fromSource
540 ldata.fromSource = null;
541 if (ldata.fromTarget) {
542 // promote target into source position
543 ldata.fromSource = ldata.fromTarget;
544 ldata.fromTarget = null;
545 ldata.key = keyrev;
546 delete network.lookup[key];
547 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800548 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800549 }
550 } else {
551 // remove fromTarget
552 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800553 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800554 }
555 if (ldata.fromSource) {
556 restyleLinkElement(ldata);
557 } else {
558 removeLinkElement(ldata);
559 }
560 }
561 }
562 }
563 }
564 return result;
565 }
566
567 function addLinkUpdate(ldata, link) {
568 // add link event, but we already have the reverse link installed
569 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800570 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800571 restyleLinkElement(ldata);
572 }
573
574 var allLinkTypes = 'direct indirect optical tunnel',
575 defaultLinkType = 'direct';
576
577 function restyleLinkElement(ldata) {
578 // this fn's job is to look at raw links and decide what svg classes
579 // need to be applied to the line element in the DOM
580 var el = ldata.el,
581 type = ldata.type(),
582 lw = ldata.linkWidth(),
583 online = ldata.online();
584
585 el.classed('link', true);
586 el.classed('inactive', !online);
587 el.classed(allLinkTypes, false);
588 if (type) {
589 el.classed(type, true);
590 }
591 el.transition()
592 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800593 .attr('stroke-width', linkScale(lw))
594 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800595 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800596
Simon Hunt99c13842014-11-06 18:23:12 -0800597 // ==============================
598 // Event handlers for server-pushed events
599
Simon Huntbb282f52014-11-10 11:08:19 -0800600 function logicError(msg) {
601 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800602 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800603 }
604
Simon Hunt99c13842014-11-06 18:23:12 -0800605 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800606 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800607 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800608 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800609 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800610
Simon Huntfcfb46c2014-11-19 12:53:38 -0800611 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800612 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800613 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800614 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800615
Simon Hunt7b403bc2014-11-22 19:01:00 -0800616 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800617 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800618 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800619 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800620
Simon Hunt61d04042014-11-11 17:27:16 -0800621 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800622 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800623 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800624 };
625
Simon Hunta5e89142014-11-14 07:00:33 -0800626 function addInstance(data) {
627 evTrace(data);
628 var inst = data.payload,
629 id = inst.id;
630 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800631 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800632 return;
633 }
634 onosInstances[id] = inst;
635 onosOrder.push(inst);
636 updateInstances();
637 }
638
Simon Hunt99c13842014-11-06 18:23:12 -0800639 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800640 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800641 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800642 id = device.id,
643 d;
644
645 if (network.lookup[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800646 updateDevice(data);
Simon Huntca867ac2014-11-28 18:07:35 -0800647 return;
648 }
649
650 d = createDeviceNode(device);
651 network.nodes.push(d);
652 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800653 updateNodes();
654 network.force.start();
655 }
656
Simon Hunt99c13842014-11-06 18:23:12 -0800657 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800658 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800659 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800660 result = findLink(link, 'add'),
661 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800662 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800663
664 if (bad) {
665 logicError(bad + ': ' + link.id);
666 return;
667 }
668
Simon Huntca867ac2014-11-28 18:07:35 -0800669 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800670 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800671 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800672 return;
673 }
674
675 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800676 d = createLink(link);
677 if (d) {
678 network.links.push(d);
679 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800680 updateLinks();
681 network.force.start();
682 }
683 }
684
Simon Hunt56d51852014-11-09 13:03:35 -0800685 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800686 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800687 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800688 id = host.id,
689 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800690 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800691
692 if (network.lookup[id]) {
693 logicError('Host already added: ' + id);
694 return;
695 }
696
697 d = createHostNode(host);
698 network.nodes.push(d);
699 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800700 updateNodes();
701
702 lnk = createHostLink(host);
703 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800704 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800705 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800706 network.lookup[d.ingress] = lnk;
707 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800708 updateLinks();
709 }
710 network.force.start();
711 }
712
Simon Hunt44031102014-11-11 13:20:36 -0800713 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800714
715 function updateInstance(data) {
716 evTrace(data);
717 var inst = data.payload,
718 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800719 d = onosInstances[id];
720 if (d) {
721 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800722 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800723 } else {
724 logicError('updateInstance lookup fail. ID = "' + id + '"');
725 }
726 }
727
Simon Huntbb282f52014-11-10 11:08:19 -0800728 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800729 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800730 var device = data.payload,
731 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800732 d = network.lookup[id],
733 wasOnline;
734
Simon Hunt62c47542014-11-22 22:16:32 -0800735 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800736 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800737 $.extend(d, device);
738 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800739 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800740 }
741 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800742 if (wasOnline !== d.online) {
743 findAttachedLinks(d.id).forEach(restyleLinkElement);
744 updateOfflineVisibility(d);
745 }
Simon Huntbb282f52014-11-10 11:08:19 -0800746 } else {
747 logicError('updateDevice lookup fail. ID = "' + id + '"');
748 }
749 }
750
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800751 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800752 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800753 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800754 result = findLink(link, 'update'),
755 bad = result.badLogic;
756 if (bad) {
757 logicError(bad + ': ' + link.id);
758 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800759 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800760 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800761 }
762
Simon Hunt7cd48f32014-11-09 23:42:50 -0800763 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800764 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800765 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800766 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800767 d = network.lookup[id];
768 if (d) {
769 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800770 if (positionNode(d, true)) {
771 sendUpdateMeta(d, true);
772 }
773 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800774 } else {
775 logicError('updateHost lookup fail. ID = "' + id + '"');
776 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800777 }
778
Simon Hunt44031102014-11-11 13:20:36 -0800779 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800780 function removeInstance(data) {
781 evTrace(data);
782 var inst = data.payload,
783 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800784 d = onosInstances[id];
785 if (d) {
786 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800787 if (idx >= 0) {
788 onosOrder.splice(idx, 1);
789 }
790 delete onosInstances[id];
791 updateInstances();
792 } else {
793 logicError('updateInstance lookup fail. ID = "' + id + '"');
794 }
795 }
796
Simon Huntca867ac2014-11-28 18:07:35 -0800797 function removeDevice(data) {
798 evTrace(data);
799 var device = data.payload,
800 id = device.id,
801 d = network.lookup[id];
802 if (d) {
803 removeDeviceElement(d);
804 } else {
805 logicError('removeDevice lookup fail. ID = "' + id + '"');
806 }
807 }
808
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800809 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800810 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800811 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800812 result = findLink(link, 'remove'),
813 bad = result.badLogic;
814 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -0800815 // may have already removed link, if attached to removed device
816 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800817 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800818 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800819 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800820 }
821
Simon Hunt44031102014-11-11 13:20:36 -0800822 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800823 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800824 var host = data.payload,
825 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800826 d = network.lookup[id];
827 if (d) {
828 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -0800829 } else {
Simon Huntca867ac2014-11-28 18:07:35 -0800830 // may have already removed host, if attached to removed device
831 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -0800832 }
833 }
834
Simon Huntca867ac2014-11-28 18:07:35 -0800835 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -0800836 function showSummary(data) {
837 evTrace(data);
838 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800839 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800840 }
841
Simon Hunt61d04042014-11-11 17:27:16 -0800842 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800843 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -0800844 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800845 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -0800846 if (useDetails) {
847 showDetailPane();
848 }
Simon Hunt61d04042014-11-11 17:27:16 -0800849 }
850
Simon Huntb53e0682014-11-12 13:32:01 -0800851 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800852 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800853 var paths = data.payload.paths,
854 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800855
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800856 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800857 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800858 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800859 // Remove all previous labels.
860 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800861
Simon Hunte2575b62014-11-18 15:25:53 -0800862 // Now hilight all links in the paths payload, and attach
863 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800864 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800865 var n = p.links.length,
866 i,
867 ldata;
868
Thomas Vachuska4731f122014-11-20 04:56:19 -0800869 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800870 for (i=0; i<n; i++) {
871 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800872 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800873 ldata.el.classed(p.class, true);
874 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800875 }
Simon Hunte2575b62014-11-18 15:25:53 -0800876 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800877 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800878
Simon Hunte2575b62014-11-18 15:25:53 -0800879 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800880
881 if (hasTraffic && !antTimer) {
882 startAntTimer();
883 } else if (!hasTraffic && antTimer) {
884 stopAntTimer();
885 }
Simon Huntb53e0682014-11-12 13:32:01 -0800886 }
887
Simon Hunt56d51852014-11-09 13:03:35 -0800888 // ...............................
889
Simon Hunt99c13842014-11-06 18:23:12 -0800890 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800891 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800892 }
893
894 function handleServerEvent(data) {
895 var fn = eventDispatch[data.event] || unknownEvent;
896 fn(data);
897 }
898
899 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800900 // Out-going messages...
901
Simon Huntb53e0682014-11-12 13:32:01 -0800902 function nSel() {
903 return selectOrder.length;
904 }
Simon Hunt61d04042014-11-11 17:27:16 -0800905 function getSel(idx) {
906 return selections[selectOrder[idx]];
907 }
Simon Huntb53e0682014-11-12 13:32:01 -0800908 function allSelectionsClass(cls) {
909 for (var i=0, n=nSel(); i<n; i++) {
910 if (getSel(i).obj.class !== cls) {
911 return false;
912 }
913 }
914 return true;
915 }
Simon Hunt61d04042014-11-11 17:27:16 -0800916
Thomas Vachuska47635c62014-11-22 01:21:36 -0800917 function toggleInstances() {
918 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800919 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800920 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800921 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800922 }
923 }
924
Simon Huntb0ecfa52014-11-23 21:05:12 -0800925 function showInstances() {
926 oiBox.show();
927 colorAffinity = true;
928 updateDeviceColors();
929 }
930
931 function hideInstances() {
932 oiBox.hide();
933 colorAffinity = false;
934 cancelAffinity();
935 updateDeviceColors();
936 }
937
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800938 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800939 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -0800940 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800941 }
942
Thomas Vachuska47635c62014-11-22 01:21:36 -0800943 function toggleSummary() {
944 if (!summaryPane.isVisible()) {
945 requestSummary();
946 } else {
947 cancelSummary();
948 }
949 }
950
951 // request overall summary data
952 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -0800953 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -0800954 }
955
956 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -0800957 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -0800958 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800959 }
960
Simon Hunt27d322d2014-11-28 10:45:43 -0800961 function toggleDetails() {
962 useDetails = !useDetails;
963 if (useDetails) {
964 flash('Enable details pane');
965 if (haveDetails) {
966 showDetailPane();
967 }
968 } else {
969 flash('Disable details pane');
970 hideDetailPane();
971 }
972 }
973
Simon Hunt06811b72014-11-25 18:54:48 -0800974 // encapsulate interaction between summary and details panes
975 function showSummaryPane() {
976 if (detailPane.isVisible()) {
977 detailPane.down(summaryPane.show);
978 } else {
979 summaryPane.show();
980 }
981 }
982
983 function hideSummaryPane() {
984 summaryPane.hide(function () {
985 if (detailPane.isVisible()) {
986 detailPane.up();
987 }
988 });
989 }
990
991 function showDetailPane() {
992 if (summaryPane.isVisible()) {
993 detailPane.down(detailPane.show);
994 } else {
995 detailPane.up(detailPane.show);
996 }
997 }
998
999 function hideDetailPane() {
1000 detailPane.hide();
1001 }
1002
1003
Simon Hunt61d04042014-11-11 17:27:16 -08001004 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -08001005 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -08001006 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001007 var data = getSel(0).obj;
1008 sendMessage('requestDetails', {
1009 id: data.id,
1010 class: data.class
1011 });
Simon Hunt61d04042014-11-11 17:27:16 -08001012 }
1013
Thomas Vachuska9edca302014-11-22 17:06:42 -08001014 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001015 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001016 one: selectOrder[0],
1017 two: selectOrder[1],
1018 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001019 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001020 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001021 }
1022
Thomas Vachuska9edca302014-11-22 17:06:42 -08001023 function addMultiSourceIntentAction() {
1024 sendMessage('addMultiSourceIntent', {
1025 src: selectOrder.slice(0, selectOrder.length - 1),
1026 dst: selectOrder[selectOrder.length - 1],
1027 ids: selectOrder
1028 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001029 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001030 }
1031
Thomas Vachuska9edca302014-11-22 17:06:42 -08001032
Thomas Vachuska47635c62014-11-22 01:21:36 -08001033 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001034 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001035 }
1036
Thomas Vachuska9edca302014-11-22 17:06:42 -08001037 function requestTrafficForMode() {
1038 if (hoverMode === hoverModeAll) {
1039 requestAllTraffic();
1040 } else if (hoverMode === hoverModeFlows) {
1041 requestDeviceLinkFlows();
1042 } else if (hoverMode === hoverModeIntents) {
1043 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001044 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001045 }
Simon Huntd72bc702014-11-13 18:38:04 -08001046
Thomas Vachuska9edca302014-11-22 17:06:42 -08001047 function showTrafficAction() {
1048 hoverMode = hoverModeIntents;
1049 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001050 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001051 }
1052
1053 function requestSelectTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001054 function hoverValid() {
1055 return hoverMode === hoverModeIntents &&
1056 hovered &&
1057 (hovered.class === 'host' || hovered.class === 'device');
1058 }
1059
Thomas Vachuska9edca302014-11-22 17:06:42 -08001060 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001061 sendMessage('requestTraffic', {
1062 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001063 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001064 });
1065 }
Simon Huntd72bc702014-11-13 18:38:04 -08001066 }
1067
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001068
Thomas Vachuska29617e52014-11-20 03:17:46 -08001069 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001070 hoverMode = hoverModeFlows;
1071 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001072 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001073 }
1074
Thomas Vachuska9edca302014-11-22 17:06:42 -08001075 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001076 function hoverValid() {
1077 return hoverMode === hoverModeFlows &&
1078 hovered && (hovered.class === 'device');
1079 }
1080
Thomas Vachuska9edca302014-11-22 17:06:42 -08001081 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001082 sendMessage('requestDeviceLinkFlows', {
1083 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001084 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001085 });
1086 }
1087 }
1088
1089
1090 function showAllTrafficAction() {
1091 hoverMode = hoverModeAll;
1092 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001093 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001094 }
1095
1096 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001097 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001098 }
1099
1100 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001101 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001102 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001103 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001104 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001105 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001106 }
Simon Huntd72bc702014-11-13 18:38:04 -08001107
Simon Hunta6a9fe72014-11-20 11:17:12 -08001108
Simon Hunt61d04042014-11-11 17:27:16 -08001109 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001110 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001111
Simon Hunt7b403bc2014-11-22 19:01:00 -08001112 var instCfg = {
1113 rectPad: 8,
1114 nodeOx: 9,
1115 nodeOy: 9,
1116 nodeDim: 40,
1117 birdOx: 19,
1118 birdOy: 21,
1119 birdDim: 21,
1120 uiDy: 45,
1121 titleDy: 30,
1122 textYOff: 20,
1123 textYSpc: 15
1124 };
1125
1126 function viewBox(dim) {
1127 return '0 0 ' + dim.w + ' ' + dim.h;
1128 }
1129
1130 function instRectAttr(dim) {
1131 var pad = instCfg.rectPad;
1132 return {
1133 x: pad,
1134 y: pad,
1135 width: dim.w - pad*2,
1136 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001137 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001138 };
1139 }
1140
1141 function computeDim(self) {
1142 var css = window.getComputedStyle(self);
1143 return {
1144 w: stripPx(css.width),
1145 h: stripPx(css.height)
1146 };
Simon Huntb82f6902014-11-22 11:53:15 -08001147 }
Simon Hunta5e89142014-11-14 07:00:33 -08001148
1149 function updateInstances() {
1150 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001151 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001152 instDim = {w:0,h:0},
1153 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001154
Simon Hunt7b403bc2014-11-22 19:01:00 -08001155 function nSw(n) {
1156 return '# Switches: ' + n;
1157 }
Simon Hunta5e89142014-11-14 07:00:33 -08001158
Simon Huntb82f6902014-11-22 11:53:15 -08001159 // operate on existing onos instances if necessary
1160 onoses.each(function (d) {
1161 var el = d3.select(this),
1162 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001163 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001164
1165 // update online state
1166 el.classed('online', d.online);
1167
1168 // update ui-attached state
1169 svg.select('use.uiBadge').remove();
1170 if (d.uiAttached) {
1171 attachUiBadge(svg);
1172 }
1173
Simon Hunt7b403bc2014-11-22 19:01:00 -08001174 function updAttr(id, value) {
1175 svg.select('text.instLabel.'+id).text(value);
1176 }
1177
1178 updAttr('ip', d.ip);
1179 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001180 });
1181
1182
1183 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001184 var entering = onoses.enter()
1185 .append('div')
1186 .attr('class', 'onosInst')
1187 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001188 .on('click', clickInst);
1189
Simon Huntb82f6902014-11-22 11:53:15 -08001190 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001191 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001192 rectAttr,
1193 svg;
1194 instDim = computeDim(this);
1195 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001196
Simon Hunt7b403bc2014-11-22 19:01:00 -08001197 svg = el.append('svg').attr({
1198 width: instDim.w,
1199 height: instDim.h,
1200 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001201 });
Simon Huntb82f6902014-11-22 11:53:15 -08001202
Simon Hunt7b403bc2014-11-22 19:01:00 -08001203 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001204
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001205 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1206 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001207
1208 if (d.uiAttached) {
1209 attachUiBadge(svg);
1210 }
1211
Simon Hunt7b403bc2014-11-22 19:01:00 -08001212 var left = c.nodeOx + c.nodeDim,
1213 len = rectAttr.width - left,
1214 hlen = len / 2,
1215 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001216
Simon Hunt7b403bc2014-11-22 19:01:00 -08001217 // title
1218 svg.append('text')
1219 .attr({
1220 class: 'instTitle',
1221 x: midline,
1222 y: c.titleDy
1223 })
1224 .text(d.id);
1225
1226 // a couple of attributes
1227 var ty = c.titleDy + c.textYOff;
1228
1229 function addAttr(id, label) {
1230 svg.append('text').attr({
1231 class: 'instLabel ' + id,
1232 x: midline,
1233 y: ty
1234 }).text(label);
1235 ty += c.textYSpc;
1236 }
1237
1238 addAttr('ip', d.ip);
1239 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001240 });
Simon Hunta5e89142014-11-14 07:00:33 -08001241
1242 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001243 // set the affinity colors...
1244 onoses.each(function (d) {
1245 var el = d3.select(this),
1246 rect = el.select('svg').select('rect'),
1247 col = instColor(d.id, d.online);
1248 rect.style('fill', col);
1249 });
Simon Hunta5e89142014-11-14 07:00:33 -08001250
Simon Hunt7b403bc2014-11-22 19:01:00 -08001251 // adjust the panel size appropriately...
1252 oiBox.width(instDim.w * onosOrder.length);
1253 oiBox.height(instDim.h);
1254
1255 // remove any outgoing instances
1256 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001257 }
1258
Simon Hunt8f40cce2014-11-23 15:57:30 -08001259 function instColor(id, online) {
1260 return cat7.get(id, !online, network.view.getTheme());
1261 }
1262
Simon Hunt9462e8c2014-11-14 17:28:09 -08001263 function clickInst(d) {
1264 var el = d3.select(this),
1265 aff = el.classed('affinity');
1266 if (!aff) {
1267 setAffinity(el, d);
1268 } else {
1269 cancelAffinity();
1270 }
1271 }
1272
1273 function setAffinity(el, d) {
1274 d3.selectAll('.onosInst')
1275 .classed('mastership', true)
1276 .classed('affinity', false);
1277 el.classed('affinity', true);
1278
1279 suppressLayers(true);
1280 node.each(function (n) {
1281 if (n.master === d.id) {
1282 n.el.classed('suppressed', false);
1283 }
1284 });
1285 oiShowMaster = true;
1286 }
1287
1288 function cancelAffinity() {
1289 d3.selectAll('.onosInst')
1290 .classed('mastership affinity', false);
1291 restoreLayerState();
1292 oiShowMaster = false;
1293 }
1294
Simon Hunt7b403bc2014-11-22 19:01:00 -08001295 // TODO: these should be moved out to utility module.
1296 function stripPx(s) {
1297 return s.replace(/px$/,'');
1298 }
1299
1300 function appendUse(svg, ox, oy, dim, iid, cls) {
1301 var use = svg.append('use').attr({
1302 transform: translate(ox,oy),
1303 'xlink:href': iid,
1304 width: dim,
1305 height: dim
1306 });
1307 if (cls) {
1308 use.classed(cls, true);
1309 }
1310 return use;
1311 }
1312
1313 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1314 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1315 }
1316
1317 function appendBadge(svg, ox, oy, dim, iid, cls) {
1318 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1319 }
1320
1321 function attachUiBadge(svg) {
1322 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1323 }
1324
Simon Hunt434cf142014-11-24 11:10:28 -08001325 function visVal(b) {
1326 return b ? 'visible' : 'hidden';
1327 }
1328
Simon Hunta5e89142014-11-14 07:00:33 -08001329 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001330 // force layout modification functions
1331
1332 function translate(x, y) {
1333 return 'translate(' + x + ',' + y + ')';
1334 }
1335
Simon Hunte2575b62014-11-18 15:25:53 -08001336 function rotate(deg) {
1337 return 'rotate(' + deg + ')';
1338 }
1339
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001340 function missMsg(what, id) {
1341 return '\n[' + what + '] "' + id + '" missing ';
1342 }
1343
1344 function linkEndPoints(srcId, dstId) {
1345 var srcNode = network.lookup[srcId],
1346 dstNode = network.lookup[dstId],
1347 sMiss = !srcNode ? missMsg('src', srcId) : '',
1348 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1349
1350 if (sMiss || dMiss) {
1351 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1352 return null;
1353 }
1354 return {
1355 source: srcNode,
1356 target: dstNode,
1357 x1: srcNode.x,
1358 y1: srcNode.y,
1359 x2: dstNode.x,
1360 y2: dstNode.y
1361 };
1362 }
1363
Simon Hunt56d51852014-11-09 13:03:35 -08001364 function createHostLink(host) {
1365 var src = host.id,
1366 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001367 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001368 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001369
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001370 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001371 return null;
1372 }
1373
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001374 // Synthesize link ...
1375 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001376 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001377 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001378
1379 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001380 online: function () {
1381 // hostlink target is edge switch
1382 return lnk.target.online;
1383 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001384 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001385 });
Simon Hunt99c13842014-11-06 18:23:12 -08001386 return lnk;
1387 }
1388
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001389 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001390 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001391
1392 if (!lnk) {
1393 return null;
1394 }
1395
Simon Hunt8257f4c2014-11-16 19:34:54 -08001396 $.extend(lnk, {
1397 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001398 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001399 fromSource: link,
1400
1401 // functions to aggregate dual link state
1402 type: function () {
1403 var s = lnk.fromSource,
1404 t = lnk.fromTarget;
1405 return (s && s.type) || (t && t.type) || defaultLinkType;
1406 },
1407 online: function () {
1408 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001409 t = lnk.fromTarget,
1410 both = lnk.source.online && lnk.target.online;
1411 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001412 },
1413 linkWidth: function () {
1414 var s = lnk.fromSource,
1415 t = lnk.fromTarget,
1416 ws = (s && s.linkWidth) || 0,
1417 wt = (t && t.linkWidth) || 0;
1418 return Math.max(ws, wt);
1419 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001420 });
1421 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001422 }
1423
Simon Hunte2575b62014-11-18 15:25:53 -08001424 function removeLinkLabels() {
1425 network.links.forEach(function (d) {
1426 d.label = '';
1427 });
1428 }
1429
Simon Hunt434cf142014-11-24 11:10:28 -08001430 function showHostVis(el) {
1431 el.style('visibility', visVal(showHosts));
1432 }
1433
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001434 var widthRatio = 1.4,
1435 linkScale = d3.scale.linear()
1436 .domain([1, 12])
1437 .range([widthRatio, 12 * widthRatio])
1438 .clamp(true);
1439
Simon Hunt99c13842014-11-06 18:23:12 -08001440 function updateLinks() {
1441 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001442 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001443
1444 // operate on existing links, if necessary
1445 // link .foo() .bar() ...
1446
1447 // operate on entering links:
1448 var entering = link.enter()
1449 .append('line')
1450 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001451 x1: function (d) { return d.x1; },
1452 y1: function (d) { return d.y1; },
1453 x2: function (d) { return d.x2; },
1454 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001455 stroke: config.topo.linkInColor,
1456 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001457 });
1458
1459 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001460 entering.each(function (d) {
1461 var link = d3.select(this);
1462 // provide ref to element selection from backing data....
1463 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001464 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001465 if (d.type() === 'hostLink') {
1466 showHostVis(link);
1467 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001468 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001469
1470 // operate on both existing and new links, if necessary
1471 //link .foo() .bar() ...
1472
Simon Hunte2575b62014-11-18 15:25:53 -08001473 // apply or remove labels
1474 var labelData = getLabelData();
1475 applyLinkLabels(labelData);
1476
Thomas Vachuska4830d392014-11-09 17:09:56 -08001477 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001478 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001479 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001480 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001481 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001482 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001483 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001484 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001485 stroke: config.topo.linkOutColor,
1486 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001487 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001488 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001489 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001490
1491 // NOTE: invoke a single tick to force the labels to position
1492 // onto their links.
1493 tick();
1494 }
1495
1496 function getLabelData() {
1497 // create the backing data for showing labels..
1498 var data = [];
1499 link.each(function (d) {
1500 if (d.label) {
1501 data.push({
1502 id: 'lab-' + d.key,
1503 key: d.key,
1504 label: d.label,
1505 ldata: d
1506 });
1507 }
1508 });
1509 return data;
1510 }
1511
1512 var linkLabelOffset = '0.3em';
1513
1514 function applyLinkLabels(data) {
1515 var entering;
1516
1517 linkLabel = linkLabelG.selectAll('.linkLabel')
1518 .data(data, function (d) { return d.id; });
1519
Simon Hunt56a2ea42014-11-19 12:39:31 -08001520 // for elements already existing, we need to update the text
1521 // and adjust the rectangle size to fit
1522 linkLabel.each(function (d) {
1523 var el = d3.select(this),
1524 rect = el.select('rect'),
1525 text = el.select('text');
1526 text.text(d.label);
1527 rect.attr(rectAroundText(el));
1528 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001529
Simon Hunte2575b62014-11-18 15:25:53 -08001530 entering = linkLabel.enter().append('g')
1531 .classed('linkLabel', true)
1532 .attr('id', function (d) { return d.id; });
1533
1534 entering.each(function (d) {
1535 var el = d3.select(this),
1536 rect,
1537 text,
1538 parms = {
1539 x1: d.ldata.x1,
1540 y1: d.ldata.y1,
1541 x2: d.ldata.x2,
1542 y2: d.ldata.y2
1543 };
1544
1545 d.el = el;
1546 rect = el.append('rect');
1547 text = el.append('text').text(d.label);
1548 rect.attr(rectAroundText(el));
1549 text.attr('dy', linkLabelOffset);
1550
1551 el.attr('transform', transformLabel(parms));
1552 });
1553
1554 // Remove any links that are no longer required.
1555 linkLabel.exit().remove();
1556 }
1557
1558 function rectAroundText(el) {
1559 var text = el.select('text'),
1560 box = text.node().getBBox();
1561
1562 // translate the bbox so that it is centered on [x,y]
1563 box.x = -box.width / 2;
1564 box.y = -box.height / 2;
1565
1566 // add padding
1567 box.x -= 1;
1568 box.width += 2;
1569 return box;
1570 }
1571
1572 function transformLabel(p) {
1573 var dx = p.x2 - p.x1,
1574 dy = p.y2 - p.y1,
1575 xMid = dx/2 + p.x1,
1576 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001577 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001578 }
1579
1580 function createDeviceNode(device) {
1581 // start with the object as is
1582 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001583 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001584 svgCls = type ? 'node device ' + type : 'node device',
1585 labels = device.labels || [];
1586
Simon Hunt99c13842014-11-06 18:23:12 -08001587 // Augment as needed...
1588 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001589 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001590 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001591 return node;
1592 }
1593
Simon Hunt56d51852014-11-09 13:03:35 -08001594 function createHostNode(host) {
1595 // start with the object as is
1596 var node = host;
1597
1598 // Augment as needed...
1599 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001600 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001601 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001602 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001603 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001604 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001605 return node;
1606 }
1607
Simon Hunt62c47542014-11-22 22:16:32 -08001608 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001609 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001610 x = meta && meta.x,
1611 y = meta && meta.y,
1612 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001613
Simon Huntac9e24f2014-11-12 10:12:21 -08001614 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001615 if (x && y) {
1616 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001617 node.px = node.x = x;
1618 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001619 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001620 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001621
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001622 var location = node.location;
1623 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001624 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001625 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001626 node.px = node.x = coord[0];
1627 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001628 return true;
1629 }
1630
1631 // if this is a node update (not a node add).. skip randomizer
1632 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001633 return;
1634 }
1635
Simon Huntac9e24f2014-11-12 10:12:21 -08001636 // Note: Placing incoming unpinned nodes at exactly the same point
1637 // (center of the view) causes them to explode outwards when
1638 // the force layout kicks in. So, we spread them out a bit
1639 // initially, to provide a more serene layout convergence.
1640 // Additionally, if the node is a host, we place it near
1641 // the device it is connected to.
1642
1643 function spread(s) {
1644 return Math.floor((Math.random() * s) - s/2);
1645 }
1646
1647 function randDim(dim) {
1648 return dim / 2 + spread(dim * 0.7071);
1649 }
1650
1651 function rand() {
1652 return {
1653 x: randDim(network.view.width()),
1654 y: randDim(network.view.height())
1655 };
1656 }
1657
1658 function near(node) {
1659 var min = 12,
1660 dx = spread(12),
1661 dy = spread(12);
1662 return {
1663 x: node.x + min + dx,
1664 y: node.y + min + dy
1665 };
1666 }
1667
1668 function getDevice(cp) {
1669 var d = network.lookup[cp.device];
1670 return d || rand();
1671 }
1672
1673 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1674 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001675 }
1676
Simon Hunt99c13842014-11-06 18:23:12 -08001677
Simon Huntc72967b2014-11-20 09:21:42 -08001678 function iconGlyphUrl(d) {
1679 var which = d.type || 'unknown';
1680 return '#' + which;
1681 }
1682
Simon Hunt99c13842014-11-06 18:23:12 -08001683 // returns the newly computed bounding box of the rectangle
1684 function adjustRectToFitText(n) {
1685 var text = n.select('text'),
1686 box = text.node().getBBox(),
1687 lab = config.labels;
1688
1689 text.attr('text-anchor', 'middle')
1690 .attr('y', '-0.8em')
1691 .attr('x', lab.imgPad/2);
1692
1693 // translate the bbox so that it is centered on [x,y]
1694 box.x = -box.width / 2;
1695 box.y = -box.height / 2;
1696
1697 // add padding
1698 box.x -= (lab.padLR + lab.imgPad/2);
1699 box.width += lab.padLR * 2 + lab.imgPad;
1700 box.y -= lab.padTB;
1701 box.height += lab.padTB * 2;
1702
1703 return box;
1704 }
1705
Simon Hunt1a9eff92014-11-07 11:06:34 -08001706 function mkSvgClass(d) {
1707 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1708 }
1709
Simon Hunt7cd48f32014-11-09 23:42:50 -08001710 function hostLabel(d) {
1711 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1712 return d.labels[idx];
1713 }
1714 function deviceLabel(d) {
1715 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1716 return d.labels[idx];
1717 }
Simon Huntc72967b2014-11-20 09:21:42 -08001718 function trimLabel(label) {
1719 return (label && label.trim()) || '';
1720 }
1721
1722 function emptyBox() {
1723 return {
1724 x: -2,
1725 y: -2,
1726 width: 4,
1727 height: 4
1728 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001729 }
1730
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001731 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001732 var label = trimLabel(deviceLabel(d)),
1733 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001734 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001735 box,
1736 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001737 dy,
1738 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001739
1740 node.select('text')
1741 .text(label)
1742 .style('opacity', 0)
1743 .transition()
1744 .style('opacity', 1);
1745
Simon Huntc72967b2014-11-20 09:21:42 -08001746 if (noLabel) {
1747 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001748 dx = -cfg.dim/2;
1749 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001750 } else {
1751 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001752 dx = box.x + cfg.xoff;
1753 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001754 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001755
1756 node.select('rect')
1757 .transition()
1758 .attr(box);
1759
Simon Huntc72967b2014-11-20 09:21:42 -08001760 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001761 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001762 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001763 }
1764
1765 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001766 var label = trimLabel(hostLabel(d));
1767 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001768 }
1769
Simon Hunt434cf142014-11-24 11:10:28 -08001770 function updateHostVisibility() {
1771 var v = visVal(showHosts);
1772 nodeG.selectAll('.host').style('visibility', v);
1773 linkG.selectAll('.hostLink').style('visibility', v);
1774 }
1775
Simon Hunt6d9bd032014-11-28 22:16:40 -08001776 function findOfflineNodes() {
1777 var a = [];
1778 network.nodes.forEach(function (d) {
1779 if (d.class === 'device' && !d.online) {
1780 a.push(d);
1781 }
1782 });
1783 return a;
1784 }
1785
1786 function updateOfflineVisibility(dev) {
1787 var so = showOffline,
1788 sh = showHosts,
1789 vb = 'visibility',
1790 v, off, al, ah, db, b;
1791
1792 function updAtt(show) {
1793 al.forEach(function (d) {
1794 b = show && ((d.type() !== 'hostLink') || sh);
1795 d.el.style(vb, visVal(b));
1796 });
1797 ah.forEach(function (d) {
1798 b = show && sh;
1799 d.el.style(vb, visVal(b));
1800 });
1801 }
1802
1803 if (dev) {
1804 // updating a specific device that just toggled off/on-line
1805 db = dev.online || so;
1806 dev.el.style(vb, visVal(db));
1807 al = findAttachedLinks(dev.id);
1808 ah = findAttachedHosts(dev.id);
1809 updAtt(db);
1810 } else {
1811 // updating all offline devices
1812 v = visVal(so);
1813 off = findOfflineNodes();
1814 off.forEach(function (d) {
1815 d.el.style(vb, v);
1816 al = findAttachedLinks(d.id);
1817 ah = findAttachedHosts(d.id);
1818 updAtt(so);
1819 });
1820 }
1821 }
1822
Simon Hunt6ac93f32014-11-13 12:17:27 -08001823 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001824 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001825 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001826 }
1827
1828 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001829 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001830 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001831 }
Simon Huntbb282f52014-11-10 11:08:19 -08001832
Simon Hunteb1514d2014-11-20 09:57:29 -08001833 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001834 var dim = radius * 1.5,
1835 xlate = -dim / 2;
1836
Simon Hunteb1514d2014-11-20 09:57:29 -08001837 node.append('use').attr({
1838 class: 'glyphIcon hostIcon',
1839 transform: translate(xlate,xlate),
1840 'xlink:href': iid,
1841 width: dim,
1842 height: dim
1843 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001844 }
1845
Simon Hunt99c13842014-11-06 18:23:12 -08001846 function updateNodes() {
1847 node = nodeG.selectAll('.node')
1848 .data(network.nodes, function (d) { return d.id; });
1849
Simon Hunt62c47542014-11-22 22:16:32 -08001850 // operate on existing nodes...
1851 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001852 var node = d.el;
1853 node.classed('online', d.online);
1854 updateDeviceLabel(d);
1855 positionNode(d, true);
1856 });
1857
1858 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001859 updateHostLabel(d);
1860 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08001861 });
Simon Hunt99c13842014-11-06 18:23:12 -08001862
1863 // operate on entering nodes:
1864 var entering = node.enter()
1865 .append('g')
1866 .attr({
1867 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001868 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001869 transform: function (d) { return translate(d.x, d.y); },
1870 opacity: 0
1871 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001872 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001873 .on('mouseover', nodeMouseOver)
1874 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001875 .transition()
1876 .attr('opacity', 1);
1877
1878 // augment device nodes...
1879 entering.filter('.device').each(function (d) {
1880 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001881 label = trimLabel(deviceLabel(d)),
1882 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001883 box;
1884
Simon Hunt7cd48f32014-11-09 23:42:50 -08001885 // provide ref to element from backing data....
1886 d.el = node;
1887
Simon Hunt62c47542014-11-22 22:16:32 -08001888 node.append('rect').attr({ rx: 5, ry: 5 });
1889 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001890 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001891 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001892 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001893 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001894
Thomas Vachuska89543292014-11-19 11:28:33 -08001895
Simon Hunt56d51852014-11-09 13:03:35 -08001896 // augment host nodes...
1897 entering.filter('.host').each(function (d) {
1898 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001899 cfg = config.icons.host,
1900 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001901 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001902 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001903
Simon Hunt7cd48f32014-11-09 23:42:50 -08001904 // provide ref to element from backing data....
1905 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001906 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001907
Simon Hunt62c47542014-11-22 22:16:32 -08001908 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001909 if (iid) {
1910 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001911 }
Simon Hunt56d51852014-11-09 13:03:35 -08001912 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001913 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001914 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001915 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001916 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001917
Simon Hunt99c13842014-11-06 18:23:12 -08001918 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001919 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001920
Simon Hunt99c13842014-11-06 18:23:12 -08001921 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001922 // Note that the node is removed after 2 seconds.
1923 // Sub element animations should be shorter than 2 seconds.
1924 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001925 .transition()
1926 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001927 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001928 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001929
1930 // host node exits....
1931 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08001932 var node = d.el;
1933 node.select('use')
1934 .style('opacity', 0.5)
1935 .transition()
1936 .duration(800)
1937 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001938
1939 node.select('text')
1940 .style('opacity', 0.5)
1941 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08001942 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08001943 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001944
Thomas Vachuska89543292014-11-19 11:28:33 -08001945 node.select('circle')
1946 .style('stroke-fill', '#555')
1947 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001948 .style('opacity', 0.5)
1949 .transition()
1950 .duration(1500)
1951 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001952 });
1953
Simon Huntca867ac2014-11-28 18:07:35 -08001954 // device node exits....
1955 exiting.filter('.device').each(function (d) {
1956 var node = d.el;
1957 node.select('use')
1958 .style('opacity', 0.5)
1959 .transition()
1960 .duration(800)
1961 .style('opacity', 0);
1962
1963 node.selectAll('rect')
1964 .style('stroke-fill', '#555')
1965 .style('fill', '#888')
1966 .style('opacity', 0.5);
1967 });
Simon Hunt62c47542014-11-22 22:16:32 -08001968
1969 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001970 }
1971
Simon Hunt95dad922014-11-24 09:43:31 -08001972 var dCol = {
1973 black: '#000',
1974 paleblue: '#acf',
1975 offwhite: '#ddd',
1976 midgrey: '#888',
1977 lightgrey: '#bbb',
1978 orange: '#f90'
1979 };
1980
Simon Huntb0ecfa52014-11-23 21:05:12 -08001981 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001982 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001983 light: {
1984 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001985 glyph: dCol.black,
1986 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001987 },
1988 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001989 glyph: dCol.midgrey,
1990 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001991 }
1992 },
Simon Hunt95dad922014-11-24 09:43:31 -08001993 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001994 dark: {
1995 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001996 glyph: dCol.black,
1997 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001998 },
1999 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08002000 glyph: dCol.midgrey,
2001 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08002002 }
2003 }
2004 };
2005
2006 function devBaseColor(d) {
2007 var t = network.view.getTheme(),
2008 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08002009 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002010 }
2011
2012 function setDeviceColor(d) {
2013 var o = d.online,
2014 s = d.el.classed('selected'),
2015 c = devBaseColor(d),
2016 a = instColor(d.master, o),
2017 g, r,
2018 icon = d.el.select('g.deviceIcon');
2019
2020 if (s) {
2021 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002022 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002023 } else if (colorAffinity) {
2024 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002025 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002026 } else {
2027 g = c.glyph;
2028 r = c.rect;
2029 }
2030
2031 icon.select('use')
2032 .style('fill', g);
2033 icon.select('rect')
2034 .style('fill', r);
2035 }
2036
Simon Huntc72967b2014-11-20 09:21:42 -08002037 function addDeviceIcon(node, box, noLabel, iid) {
2038 var cfg = config.icons.device,
2039 dx,
2040 dy,
2041 g;
2042
2043 if (noLabel) {
2044 box = emptyBox();
2045 dx = -cfg.dim/2;
2046 dy = -cfg.dim/2;
2047 } else {
2048 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002049 dx = box.x + cfg.xoff;
2050 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002051 }
2052
Simon Hunteb1514d2014-11-20 09:57:29 -08002053 g = node.append('g')
2054 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002055 .attr('transform', translate(dx, dy));
2056
2057 g.append('rect').attr({
2058 x: 0,
2059 y: 0,
2060 rx: cfg.rx,
2061 width: cfg.dim,
2062 height: cfg.dim
2063 });
2064
2065 g.append('use').attr({
2066 'xlink:href': iid,
2067 width: cfg.dim,
2068 height: cfg.dim
2069 });
2070
Simon Huntc72967b2014-11-20 09:21:42 -08002071 }
2072
Simon Hunt7b403bc2014-11-22 19:01:00 -08002073 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002074 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002075 idx, n, d;
2076 for (idx = 0, n = array.length; idx < n; idx++) {
2077 d = array[idx];
2078 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002079 return idx;
2080 }
2081 }
2082 return -1;
2083 }
2084
Simon Huntca867ac2014-11-28 18:07:35 -08002085 function removeLinkElement(d) {
2086 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002087 removed;
2088 if (idx >=0) {
2089 // remove from links array
2090 removed = network.links.splice(idx, 1);
2091 // remove from lookup cache
2092 delete network.lookup[removed[0].key];
2093 updateLinks();
2094 network.force.resume();
2095 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002096 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002097
Simon Huntca867ac2014-11-28 18:07:35 -08002098 function removeHostElement(d, upd) {
2099 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002100 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002101 removeLinkElement(d.linkData);
2102
2103 // remove hostLink bindings
2104 delete lu[d.ingress];
2105 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002106
2107 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002108 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002109 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002110 var idx = find(d.id, network.nodes);
2111 network.nodes.splice(idx, 1);
2112 // remove from SVG
2113 // NOTE: upd is false if we were called from removeDeviceElement()
2114 if (upd) {
2115 updateNodes();
2116 network.force.resume();
2117 }
2118 }
2119
2120
2121 function removeDeviceElement(d) {
2122 var id = d.id;
2123 // first, remove associated hosts and links..
2124 findAttachedHosts(id).forEach(removeHostElement);
2125 findAttachedLinks(id).forEach(removeLinkElement);
2126
2127 // remove from lookup cache
2128 delete network.lookup[id];
2129 // remove from nodes array
2130 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002131 network.nodes.splice(idx, 1);
2132 // remove from SVG
2133 updateNodes();
2134 network.force.resume();
2135 }
2136
Simon Huntca867ac2014-11-28 18:07:35 -08002137 function findAttachedHosts(devId) {
2138 var hosts = [];
2139 network.nodes.forEach(function (d) {
2140 if (d.class === 'host' && d.cp.device === devId) {
2141 hosts.push(d);
2142 }
2143 });
2144 return hosts;
2145 }
2146
2147 function findAttachedLinks(devId) {
2148 var links = [];
2149 network.links.forEach(function (d) {
2150 if (d.source.id === devId || d.target.id === devId) {
2151 links.push(d);
2152 }
2153 });
2154 return links;
2155 }
Simon Hunt44031102014-11-11 13:20:36 -08002156
Simon Huntc7ee0662014-11-05 16:44:37 -08002157 function tick() {
2158 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08002159 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08002160 });
2161
2162 link.attr({
2163 x1: function (d) { return d.source.x; },
2164 y1: function (d) { return d.source.y; },
2165 x2: function (d) { return d.target.x; },
2166 y2: function (d) { return d.target.y; }
2167 });
Simon Hunte2575b62014-11-18 15:25:53 -08002168
2169 linkLabel.each(function (d) {
2170 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002171 var lnk = findLinkById(d.key);
2172
2173 if (lnk) {
2174 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002175 x1: lnk.source.x,
2176 y1: lnk.source.y,
2177 x2: lnk.target.x,
2178 y2: lnk.target.y
2179 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002180 el.attr('transform', transformLabel(parms));
2181 }
Simon Hunte2575b62014-11-18 15:25:53 -08002182 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002183 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002184
2185 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002186 // Web-Socket for live data
2187
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002188 function findGuiSuccessor() {
2189 var idx = -1;
2190 onosOrder.forEach(function (d, i) {
2191 if (d.uiAttached) {
2192 idx = i;
2193 }
2194 });
2195
2196 for (var i = 0; i < onosOrder.length - 1; i++) {
2197 var ni = (idx + 1 + i) % onosOrder.length;
2198 if (onosOrder[ni].online) {
2199 return onosOrder[ni].ip;
2200 }
2201 }
2202 return null;
2203 }
2204
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002205 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002206 var url = document.location.toString()
2207 .replace(/\#.*/, '')
2208 .replace('http://', 'ws://')
2209 .replace('https://', 'wss://')
2210 .replace('index.html', config.webSockUrl);
2211 if (guiSuccessor) {
2212 url = url.replace(location.hostname, guiSuccessor);
2213 }
2214 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002215 }
2216
2217 webSock = {
2218 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002219 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002220
2221 connect : function() {
2222 webSock.ws = new WebSocket(webSockUrl());
2223
2224 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002225 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002226 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002227 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002228 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002229 };
2230
2231 webSock.ws.onmessage = function(m) {
2232 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002233 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002234 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002235 }
2236 };
2237
2238 webSock.ws.onclose = function(m) {
2239 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002240 guiSuccessor = findGuiSuccessor();
2241 if (guiSuccessor && webSock.retries < onosOrder.length) {
2242 webSock.retries++;
2243 webSock.connect();
2244 } else {
2245 noWebSock(true);
2246 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002247 };
2248 },
2249
2250 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002251 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002252 webSock._send(text);
2253 }
2254 },
2255
2256 _send : function(message) {
2257 if (webSock.ws) {
2258 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002259 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002260 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002261 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002262 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002263 }
2264 }
2265
2266 };
2267
Simon Hunt0c6d4192014-11-12 12:07:10 -08002268 function noWebSock(b) {
2269 mask.style('display',b ? 'block' : 'none');
2270 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002271
2272 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002273 var p = payload || {},
2274 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002275 event: evType,
2276 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002277 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002278 },
2279 asText = JSON.stringify(toSend);
2280 wsTraceTx(asText);
2281 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002282
2283 // Temporary measure for debugging UI behavior ...
2284 if (!config.useLiveData) {
2285 handleTestSend(toSend);
2286 }
Simon Huntbb282f52014-11-10 11:08:19 -08002287 }
2288
2289 function wsTraceTx(msg) {
2290 wsTrace('tx', msg);
2291 }
2292 function wsTraceRx(msg) {
2293 wsTrace('rx', msg);
2294 }
2295 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002296 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002297 }
2298
Simon Huntc76ae892014-11-18 17:31:51 -08002299 // NOTE: Temporary hardcoded example for showing detail pane
2300 // while we fine-
2301 // Probably should not merge this change...
2302 function handleTestSend(msg) {
2303 if (msg.event === 'requestDetails') {
2304 showDetails({
2305 event: 'showDetails',
2306 sid: 1001,
2307 payload: {
2308 "id": "of:0000ffffffffff09",
2309 "type": "roadm",
2310 "propOrder": [
2311 "Name",
2312 "Vendor",
2313 "H/W Version",
2314 "S/W Version",
2315 "-",
2316 "Latitude",
2317 "Longitude",
2318 "Ports"
2319 ],
2320 "props": {
2321 "Name": null,
2322 "Vendor": "Linc",
2323 "H/W Version": "OE",
2324 "S/W Version": "?",
2325 "-": "",
2326 "Latitude": "40.8",
2327 "Longitude": "73.1",
2328 "Ports": "2"
2329 }
2330 }
2331 });
2332 }
2333 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002334
2335 // ==============================
2336 // Selection stuff
2337
2338 function selectObject(obj, el) {
2339 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002340 srcEv = d3.event.sourceEvent,
2341 meta = srcEv.metaKey,
2342 shift = srcEv.shiftKey;
2343
Simon Hunt3c5ca542014-11-29 14:11:43 -08002344 // if the meta key is pressed, we are panning/zooming, so ignore
2345 if (meta) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002346 return;
2347 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002348
2349 if (el) {
2350 n = d3.select(el);
2351 } else {
2352 node.each(function(d) {
2353 if (d == obj) {
2354 n = d3.select(el = this);
2355 }
2356 });
2357 }
2358 if (!n) return;
2359
Simon Hunt01095ff2014-11-13 16:37:29 -08002360 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002361 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002362 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002363 return;
2364 }
2365
Simon Hunt01095ff2014-11-13 16:37:29 -08002366 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002367 deselectAll();
2368 }
2369
Simon Huntc31d5692014-11-12 13:27:18 -08002370 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002371 selectOrder.push(obj.id);
2372
2373 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002374 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002375 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002376 }
2377
2378 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002379 var obj = selections[id],
2380 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002381 if (obj) {
2382 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002383 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002384 idx = $.inArray(id, selectOrder);
2385 if (idx >= 0) {
2386 selectOrder.splice(idx, 1);
2387 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002388 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002389 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002390 }
2391
2392 function deselectAll() {
2393 // deselect all nodes in the network...
2394 node.classed('selected', false);
2395 selections = {};
2396 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002397 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002398 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002399 }
2400
Simon Huntb0ecfa52014-11-23 21:05:12 -08002401 function updateDeviceColors(d) {
2402 if (d) {
2403 setDeviceColor(d);
2404 } else {
2405 node.filter('.device').each(function (d) {
2406 setDeviceColor(d);
2407 });
2408 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002409 }
2410
Simon Hunt61d04042014-11-11 17:27:16 -08002411 // update the state of the detail pane, based on current selections
2412 function updateDetailPane() {
2413 var nSel = selectOrder.length;
2414 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002415 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002416 } else if (nSel === 1) {
2417 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002418 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002419 } else {
2420 multiSelect();
2421 }
2422 }
2423
Thomas Vachuska9edca302014-11-22 17:06:42 -08002424 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002425 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002426 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002427 cancelTraffic();
2428 }
2429
Simon Hunt61d04042014-11-11 17:27:16 -08002430 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002431 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002432 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002433 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002434 }
2435
2436 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002437 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002438 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002439 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002440 }
2441
2442 function addSep(tbody) {
2443 var tr = tbody.append('tr');
2444 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2445 }
2446
2447 function addProp(tbody, label, value) {
2448 var tr = tbody.append('tr');
2449
2450 tr.append('td')
2451 .attr('class', 'label')
2452 .text(label + ' :');
2453
2454 tr.append('td')
2455 .attr('class', 'value')
2456 .text(value);
2457 }
2458
2459 function populateMultiSelect() {
2460 detailPane.empty();
2461
Simon Hunta3dd9572014-11-20 15:22:41 -08002462 var title = detailPane.append('h3'),
2463 table = detailPane.append('table'),
2464 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002465
Thomas Vachuska4731f122014-11-20 04:56:19 -08002466 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002467
2468 selectOrder.forEach(function (d, i) {
2469 addProp(tbody, i+1, d);
2470 });
Simon Huntd72bc702014-11-13 18:38:04 -08002471
2472 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002473 }
2474
Thomas Vachuska47635c62014-11-22 01:21:36 -08002475 // TODO: refactor to consolidate with populateDetails
2476 function populateSummary(data) {
2477 summaryPane.empty();
2478
2479 var svg = summaryPane.append('svg'),
2480 iid = iconGlyphUrl(data);
2481
2482 var title = summaryPane.append('h2'),
2483 table = summaryPane.append('table'),
2484 tbody = table.append('tbody');
2485
2486 appendGlyph(svg, 0, 0, 40, iid);
2487
2488 svg.append('use')
2489 .attr({
2490 class: 'birdBadge',
2491 transform: translate(8,12),
2492 'xlink:href': '#bird',
2493 width: 24,
2494 height: 24,
2495 fill: '#fff'
2496 });
2497
2498 title.text('ONOS Summary');
2499
2500 data.propOrder.forEach(function(p) {
2501 if (p === '-') {
2502 addSep(tbody);
2503 } else {
2504 addProp(tbody, p, data.props[p]);
2505 }
2506 });
2507 }
2508
Simon Hunt61d04042014-11-11 17:27:16 -08002509 function populateDetails(data) {
2510 detailPane.empty();
2511
Simon Hunta6a9fe72014-11-20 11:17:12 -08002512 var svg = detailPane.append('svg'),
2513 iid = iconGlyphUrl(data);
2514
Simon Hunta3dd9572014-11-20 15:22:41 -08002515 var title = detailPane.append('h2'),
2516 table = detailPane.append('table'),
2517 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002518
Simon Hunta6a9fe72014-11-20 11:17:12 -08002519 appendGlyph(svg, 0, 0, 40, iid);
2520 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002521
2522 data.propOrder.forEach(function(p) {
2523 if (p === '-') {
2524 addSep(tbody);
2525 } else {
2526 addProp(tbody, p, data.props[p]);
2527 }
2528 });
Simon Huntd72bc702014-11-13 18:38:04 -08002529
Thomas Vachuska4731f122014-11-20 04:56:19 -08002530 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002531 }
2532
Thomas Vachuska4731f122014-11-20 04:56:19 -08002533 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002534 detailPane.append('hr');
2535 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002536 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2537
2538 if (data.type === 'switch') {
2539 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2540 }
Simon Huntd72bc702014-11-13 18:38:04 -08002541 }
2542
2543 function addMultiSelectActions() {
2544 detailPane.append('hr');
2545 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002546 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002547 // if exactly two hosts are selected, also want 'add host intent'
2548 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002549 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2550 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2551 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002552 }
2553 }
2554
Simon Hunta5e89142014-11-14 07:00:33 -08002555 function addAction(panel, text, cb) {
2556 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002557 .classed('actionBtn', true)
2558 .text(text)
2559 .on('click', cb);
2560 }
2561
2562
Simon Hunt3c5ca542014-11-29 14:11:43 -08002563 // === Pan and Zoom behaviors...
2564
2565 function panZoom(translate, scale) {
2566 panZoomContainer.attr('transform',
2567 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002568 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002569 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002570 }
2571
Simon Hunt3c5ca542014-11-29 14:11:43 -08002572 function resetPanZoom() {
2573 panZoom([0,0], 1);
2574 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002575 }
2576
Simon Hunt3c5ca542014-11-29 14:11:43 -08002577 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002578 function zoomed() {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002579 // pan zoom active when meta key is pressed...
2580 if (d3.event.sourceEvent.metaKey) {
2581 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002582 }
2583 }
2584
2585 zoom = d3.behavior.zoom()
2586 .translate([0, 0])
2587 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002588 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002589 .on("zoom", zoomed);
2590
2591 svg.call(zoom);
2592 }
2593
Simon Hunt61d04042014-11-11 17:27:16 -08002594 // ==============================
2595 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002596
2597 function prepareScenario(view, ctx, dbg) {
2598 var sc = scenario,
2599 urlSc = sc.evDir + ctx + sc.evScenario;
2600
2601 if (!ctx) {
2602 view.alert("No scenario specified (null ctx)");
2603 return;
2604 }
2605
2606 sc.view = view;
2607 sc.ctx = ctx;
2608 sc.debug = dbg;
2609 sc.evNumber = 0;
2610
2611 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002612 var p = data && data.params || {},
2613 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002614 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002615
Simon Hunt56d51852014-11-09 13:03:35 -08002616 if (err) {
2617 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2618 } else {
2619 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002620 if (desc) {
2621 intro += '\n\n ' + desc.join('\n ');
2622 }
2623 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002624 }
2625 });
2626
2627 }
2628
Simon Hunt7fa116d2014-11-17 14:16:55 -08002629 function loadGlyphs(svg) {
2630 var defs = svg.append('defs');
2631 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002632 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002633 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002634 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002635
Simon Hunt395a70c2014-11-22 23:17:40 -08002636 function sendUpdateMeta(d, store) {
2637 var metaUi = {},
2638 ll;
2639
2640 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002641 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002642 metaUi = {
2643 x: d.x,
2644 y: d.y,
2645 lng: ll[0],
2646 lat: ll[1]
2647 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002648 }
Simon Hunt62c47542014-11-22 22:16:32 -08002649 d.metaUi = metaUi;
2650 sendMessage('updateMeta', {
2651 id: d.id,
2652 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002653 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002654 });
2655 }
2656
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002657 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002658 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002659
Simon Huntf67722a2014-11-10 09:32:06 -08002660 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002661 var w = view.width(),
2662 h = view.height(),
Simon Hunt1b18aa52014-11-29 17:57:55 -08002663 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002664
Simon Hunt142d0032014-11-04 20:13:09 -08002665 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002666 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2667 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002668 setSize(svg, view);
2669
Simon Hunt6e18fe32014-11-29 13:35:41 -08002670 // load glyphs and filters...
Simon Hunt7fa116d2014-11-17 14:16:55 -08002671 loadGlyphs(svg);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002672 d3u.appendGlow(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002673
Simon Hunt3c5ca542014-11-29 14:11:43 -08002674 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2675 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002676
Simon Huntc7ee0662014-11-05 16:44:37 -08002677 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002678 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002679 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002680
Simon Hunte2575b62014-11-18 15:25:53 -08002681 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002682 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002683 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002684 nodeG = topoG.append('g').attr('id', 'nodes');
2685
Simon Hunte2575b62014-11-18 15:25:53 -08002686 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002687 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002688 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002689 node = nodeG.selectAll('.node');
2690
Simon Hunt7cd48f32014-11-09 23:42:50 -08002691 function chrg(d) {
2692 return fcfg.charge[d.class] || -12000;
2693 }
Simon Hunt99c13842014-11-06 18:23:12 -08002694 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002695 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002696 }
2697 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002698 // 0.0 - 1.0
2699 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002700 }
2701
Simon Hunt1a9eff92014-11-07 11:06:34 -08002702 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002703 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002704 }
2705
2706 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002707 // once we've finished moving, pin the node in position
2708 d.fixed = true;
2709 d3.select(self).classed('fixed', true);
2710 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002711 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002712 } else {
2713 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002714 }
2715 }
2716
Simon Hunt6e18fe32014-11-29 13:35:41 -08002717 // predicate that indicates when dragging is active
2718 function dragEnabled() {
2719 // meta key pressed means we are zooming/panning (so disable drag)
Simon Huntc2367d52014-11-29 19:30:23 -08002720 return !nodeLock && !d3.event.sourceEvent.metaKey;
2721 }
2722
2723 // predicate that indicates when clicking is active
2724 function clickEnabled() {
2725 return true;
Simon Hunt6e18fe32014-11-29 13:35:41 -08002726 }
2727
Simon Huntc7ee0662014-11-05 16:44:37 -08002728 // set up the force layout
2729 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002730 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002731 .nodes(network.nodes)
2732 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002733 .gravity(0.4)
2734 .friction(0.7)
2735 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002736 .linkDistance(ldist)
2737 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002738 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002739
Simon Hunt01095ff2014-11-13 16:37:29 -08002740 network.drag = d3u.createDragBehavior(network.force,
Simon Huntc2367d52014-11-29 19:30:23 -08002741 selectCb, atDragEnd, dragEnabled, clickEnabled);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002742
Simon Hunt0c6d4192014-11-12 12:07:10 -08002743
2744 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002745 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002746
2747 function para(sel, text) {
2748 sel.append('p').text(text);
2749 }
2750
Simon Hunt0c6d4192014-11-12 12:07:10 -08002751 mask = view.$div.append('div').attr('id','topo-mask');
2752 para(mask, 'Oops!');
2753 para(mask, 'Web-socket connection to server closed...');
2754 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002755
2756 mask.append('svg')
2757 .attr({
2758 id: 'mask-bird',
2759 width: w,
2760 height: h
2761 })
2762 .append('g')
2763 .attr('transform', birdTranslate(w, h))
2764 .style('opacity', 0.3)
2765 .append('use')
2766 .attr({
2767 'xlink:href': '#bird',
2768 width: config.birdDim,
2769 height: config.birdDim,
2770 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002771 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002772 }
Simon Hunt195cb382014-11-03 17:50:51 -08002773
Simon Hunt01095ff2014-11-13 16:37:29 -08002774
Simon Hunt56d51852014-11-09 13:03:35 -08002775 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002776 // resize, in case the window was resized while we were not loaded
2777 resize(view, ctx, flags);
2778
Simon Hunt99c13842014-11-06 18:23:12 -08002779 // cache the view token, so network topo functions can access it
2780 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002781 config.useLiveData = !flags.local;
2782
2783 if (!config.useLiveData) {
2784 prepareScenario(view, ctx, flags.debug);
2785 }
Simon Hunt99c13842014-11-06 18:23:12 -08002786
2787 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002788 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002789 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002790 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002791
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002792 // patch in our "button bar" for now
2793 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002794 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002795
Simon Huntd3b7d512014-11-12 15:48:41 -08002796 // Load map data asynchronously; complete startup after that..
2797 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002798 }
2799
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002800 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002801 if (!antTimer) {
2802 var pulses = [5, 3, 1.2, 3],
2803 pulse = 0;
2804 antTimer = setInterval(function () {
2805 pulse = pulse + 1;
2806 pulse = pulse === pulses.length ? 0 : pulse;
2807 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2808 }, 200);
2809 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002810 }
2811
2812 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002813 if (antTimer) {
2814 clearInterval(antTimer);
2815 antTimer = null;
2816 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002817 }
2818
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002819 function unload(view, ctx, flags) {
2820 stopAntTimer();
2821 }
2822
Simon Huntd3b7d512014-11-12 15:48:41 -08002823 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002824 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002825 geoJson;
2826
2827 function loadGeoJsonData() {
2828 d3.json(geoJsonUrl, function (err, data) {
2829 if (err) {
2830 // fall back to USA map background
2831 loadStaticMap();
2832 } else {
2833 geoJson = data;
2834 loadGeoMap();
2835 }
2836
2837 // finally, connect to the server...
2838 if (config.useLiveData) {
2839 webSock.connect();
2840 }
2841 });
2842 }
2843
2844 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002845 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002846 }
2847
2848 function loadStaticMap() {
2849 fnTrace('loadStaticMap', config.backgroundUrl);
2850 var w = network.view.width(),
2851 h = network.view.height();
2852
2853 // load the background image
2854 bgImg = svg.insert('svg:image', '#topo-G')
2855 .attr({
2856 id: 'topo-bg',
2857 width: w,
2858 height: h,
2859 'xlink:href': config.backgroundUrl
2860 })
2861 .style({
2862 visibility: showBg()
2863 });
2864 }
2865
Simon Hunt1b18aa52014-11-29 17:57:55 -08002866 function setProjForView(path, topoData) {
2867 var dim = config.logicalSize;
2868
2869 // start with unit scale, no translation..
2870 geoMapProj.scale(1).translate([0, 0]);
2871
2872 // figure out dimensions of map data..
2873 var b = path.bounds(topoData),
2874 x1 = b[0][0],
2875 y1 = b[0][1],
2876 x2 = b[1][0],
2877 y2 = b[1][1],
2878 dx = x2 - x1,
2879 dy = y2 - y1,
2880 x = (x1 + x2) / 2,
2881 y = (y1 + y2) / 2;
2882
2883 // size map to 95% of minimum dimension to fill space..
2884 var s = .95 / Math.min(dx / dim, dy / dim);
2885 var t = [dim / 2 - s * x, dim / 2 - s * y];
2886
2887 // set new scale, translation on the projection..
2888 geoMapProj.scale(s).translate(t);
2889 }
2890
Simon Huntd3b7d512014-11-12 15:48:41 -08002891 function loadGeoMap() {
2892 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002893
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002894 // extracts the topojson data into geocoordinate-based geometry
2895 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002896
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002897 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08002898 geoMapProj = d3.geo.mercator();
2899 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08002900
Simon Hunt1b18aa52014-11-29 17:57:55 -08002901 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002902
Simon Hunt3c5ca542014-11-29 14:11:43 -08002903 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002904 bgImg.attr('id', 'map').selectAll('path')
2905 .data(topoData.features)
2906 .enter()
2907 .append('path')
2908 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002909 }
2910
Simon Huntf67722a2014-11-10 09:32:06 -08002911 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002912 var w = view.width(),
2913 h = view.height();
2914
Simon Hunt934c3ce2014-11-05 11:45:07 -08002915 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002916
2917 d3.select('#mask-bird').attr({ width: w, height: h})
2918 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002919 }
2920
Simon Hunt8f40cce2014-11-23 15:57:30 -08002921 function theme(view, ctx, flags) {
2922 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002923 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002924 }
2925
Simon Hunt12ce12e2014-11-15 21:13:19 -08002926 function birdTranslate(w, h) {
2927 var bdim = config.birdDim;
2928 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2929 }
Simon Hunt142d0032014-11-04 20:13:09 -08002930
Simon Hunt06811b72014-11-25 18:54:48 -08002931 function isF(f) { return $.isFunction(f) ? f : null; }
2932 function noop() {}
2933
2934 function augmentDetailPane() {
2935 var dp = detailPane;
2936 dp.ypos = { up: 64, down: 320, current: 320};
2937
2938 dp._move = function (y, cb) {
2939 var endCb = isF(cb) || noop,
2940 yp = dp.ypos;
2941 if (yp.current !== y) {
2942 yp.current = y;
2943 dp.el.transition().duration(300)
2944 .each('end', endCb)
2945 .style('top', yp.current + 'px');
2946 } else {
2947 endCb();
2948 }
2949 };
2950
2951 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2952 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2953 }
2954
Simon Hunt142d0032014-11-04 20:13:09 -08002955 // ==============================
2956 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002957
Simon Hunt25248912014-11-04 11:25:48 -08002958 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002959 preload: preload,
2960 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002961 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002962 resize: resize,
2963 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002964 });
2965
Thomas Vachuska47635c62014-11-22 01:21:36 -08002966 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002967 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002968 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002969 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002970 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002971
Simon Hunt195cb382014-11-03 17:50:51 -08002972}(ONOS));