blob: 97918f0baa5c79b5420841c5f47b46c61b3af4fe [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 Hunt9462e8c2014-11-14 17:28:09 -0800151 esc: handleEscape
Simon Hunt934c3ce2014-11-05 11:45:07 -0800152 };
Simon Hunt142d0032014-11-04 20:13:09 -0800153
Simon Hunt87514342014-11-24 16:41:27 -0800154 // mouse gestures
155 var gestures = [
156 ['click', 'Select the item and show details'],
157 ['shift-click', 'Toggle selection state'],
158 ['drag', 'Reposition (and pin) device / host'],
159 ['cmd-scroll', 'Zoom in / out'],
160 ['cmd-drag', 'Pan']
161 ];
162
Simon Hunt195cb382014-11-03 17:50:51 -0800163 // state variables
Simon Hunt99c13842014-11-06 18:23:12 -0800164 var network = {
Simon Hunt50128c02014-11-08 13:36:15 -0800165 view: null, // view token reference
Simon Hunt99c13842014-11-06 18:23:12 -0800166 nodes: [],
167 links: [],
Simon Hunt269670f2014-11-17 16:17:43 -0800168 lookup: {},
169 revLinkToKey: {}
Simon Hunt99c13842014-11-06 18:23:12 -0800170 },
Simon Hunt56d51852014-11-09 13:03:35 -0800171 scenario = {
172 evDir: 'json/ev/',
173 evScenario: '/scenario.json',
174 evPrefix: '/ev_',
175 evOnos: '_onos.json',
176 evUi: '_ui.json',
177 ctx: null,
178 params: {},
179 evNumber: 0,
Simon Hunt434cf142014-11-24 11:10:28 -0800180 view: null
Simon Hunt56d51852014-11-09 13:03:35 -0800181 },
Thomas Vachuska7d638d32014-11-07 10:24:43 -0800182 webSock,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800183 sid = 0,
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800184 deviceLabelCount = 3,
Simon Hunt209155e2014-11-21 12:16:09 -0800185 hostLabelCount = 2,
Simon Hunt56d51852014-11-09 13:03:35 -0800186 deviceLabelIndex = 0,
187 hostLabelIndex = 0,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800188 selections = {},
Simon Hunta5e89142014-11-14 07:00:33 -0800189 selectOrder = [],
Simon Hunt6ac93f32014-11-13 12:17:27 -0800190 hovered = null,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800191 summaryPane,
Simon Hunta5e89142014-11-14 07:00:33 -0800192 detailPane,
Simon Hunta255a2c2014-11-13 22:29:35 -0800193 antTimer = null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800194 guiSuccessor = null,
Simon Hunta5e89142014-11-14 07:00:33 -0800195 onosInstances = {},
196 onosOrder = [],
197 oiBox,
Simon Hunt9462e8c2014-11-14 17:28:09 -0800198 oiShowMaster = false,
Simon Hunt8f40cce2014-11-23 15:57:30 -0800199 portLabelsOn = false,
Simon Huntb0ecfa52014-11-23 21:05:12 -0800200 cat7 = d3u.cat7(),
Simon Hunt434cf142014-11-24 11:10:28 -0800201 colorAffinity = false,
Simon Hunt27d322d2014-11-28 10:45:43 -0800202 showHosts = false,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800203 showOffline = true,
Simon Hunt27d322d2014-11-28 10:45:43 -0800204 useDetails = true,
Simon Hunt6e18fe32014-11-29 13:35:41 -0800205 haveDetails = false,
206 dragAllowed = true;
Simon Hunt195cb382014-11-03 17:50:51 -0800207
Simon Hunt434cf142014-11-24 11:10:28 -0800208 // constants
Thomas Vachuska9edca302014-11-22 17:06:42 -0800209 var hoverModeAll = 1,
210 hoverModeFlows = 2,
211 hoverModeIntents = 3,
212 hoverMode = hoverModeFlows;
213
Simon Hunt934c3ce2014-11-05 11:45:07 -0800214 // D3 selections
215 var svg,
Simon Hunt3c5ca542014-11-29 14:11:43 -0800216 panZoomContainer,
Simon Hunt934c3ce2014-11-05 11:45:07 -0800217 bgImg,
Simon Huntc7ee0662014-11-05 16:44:37 -0800218 topoG,
219 nodeG,
220 linkG,
Simon Hunte2575b62014-11-18 15:25:53 -0800221 linkLabelG,
Simon Huntc7ee0662014-11-05 16:44:37 -0800222 node,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800223 link,
Simon Hunte2575b62014-11-18 15:25:53 -0800224 linkLabel,
Simon Hunt0c6d4192014-11-12 12:07:10 -0800225 mask;
Simon Hunt195cb382014-11-03 17:50:51 -0800226
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800227 // the projection for the map background
Simon Hunt1b18aa52014-11-29 17:57:55 -0800228 var geoMapProj;
Paul Greyson6cb8ca02014-11-12 18:09:02 -0800229
Paul Greysonfcba0e82014-11-13 10:21:16 -0800230 // the zoom function
231 var zoom;
232
Simon Hunt142d0032014-11-04 20:13:09 -0800233 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800234 // For Debugging / Development
Simon Hunt195cb382014-11-03 17:50:51 -0800235
Simon Hunt99c13842014-11-06 18:23:12 -0800236 function note(label, msg) {
237 console.log('NOTE: ' + label + ': ' + msg);
Simon Hunt195cb382014-11-03 17:50:51 -0800238 }
239
Simon Hunt99c13842014-11-06 18:23:12 -0800240 function debug(what) {
241 return config.debugOn && config.debug[what];
Simon Hunt934c3ce2014-11-05 11:45:07 -0800242 }
243
Simon Huntfc274c92014-11-11 11:05:46 -0800244 function fnTrace(msg, id) {
245 if (config.fnTrace) {
246 console.log('FN: ' + msg + ' [' + id + ']');
247 }
248 }
Simon Hunt99c13842014-11-06 18:23:12 -0800249
Simon Hunta5e89142014-11-14 07:00:33 -0800250 function evTrace(data) {
251 fnTrace(data.event, data.payload.id);
252 }
253
Simon Hunt934c3ce2014-11-05 11:45:07 -0800254 // ==============================
255 // Key Callbacks
256
Simon Hunt27d322d2014-11-28 10:45:43 -0800257 function flash(txt) {
258 network.view.flash(txt);
259 }
260
Simon Hunt99c13842014-11-06 18:23:12 -0800261 function testMe(view) {
Simon Hunt8f40cce2014-11-23 15:57:30 -0800262 //view.alert('Theme is ' + view.getTheme());
Simon Hunta3dd9572014-11-20 15:22:41 -0800263 //view.flash('This is some text');
Simon Hunt8f40cce2014-11-23 15:57:30 -0800264 cat7.testCard(svg);
Simon Hunt99c13842014-11-06 18:23:12 -0800265 }
266
Simon Hunt56d51852014-11-09 13:03:35 -0800267 function injectTestEvent(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800268 if (config.useLiveData) { return; }
269
Simon Hunt56d51852014-11-09 13:03:35 -0800270 var sc = scenario,
271 evn = ++sc.evNumber,
272 pfx = sc.evDir + sc.ctx + sc.evPrefix + evn,
273 onosUrl = pfx + sc.evOnos,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800274 uiUrl = pfx + sc.evUi,
275 stack = [
276 { url: onosUrl, cb: handleServerEvent },
277 { url: uiUrl, cb: handleUiEvent }
278 ];
279 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800280 }
281
Simon Hunt7cd48f32014-11-09 23:42:50 -0800282 function recurseFetchEvent(stack, evn) {
283 var v = scenario.view,
284 frame;
285 if (stack.length === 0) {
Simon Huntfc274c92014-11-11 11:05:46 -0800286 v.alert('Oops!\n\nNo event #' + evn + ' found.');
Simon Hunt7cd48f32014-11-09 23:42:50 -0800287 return;
288 }
289 frame = stack.shift();
290
291 d3.json(frame.url, function (err, data) {
Simon Hunt99c13842014-11-06 18:23:12 -0800292 if (err) {
Simon Hunt56d51852014-11-09 13:03:35 -0800293 if (err.status === 404) {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800294 // if we didn't find the data, try the next stack frame
295 recurseFetchEvent(stack, evn);
Simon Hunt56d51852014-11-09 13:03:35 -0800296 } else {
Simon Hunt7cd48f32014-11-09 23:42:50 -0800297 v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err);
Simon Hunt56d51852014-11-09 13:03:35 -0800298 }
Simon Hunt99c13842014-11-06 18:23:12 -0800299 } else {
Simon Hunt1712ed82014-11-17 12:56:00 -0800300 wsTrace('test', JSON.stringify(data));
Simon Hunt7cd48f32014-11-09 23:42:50 -0800301 frame.cb(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800302 }
303 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800304
Simon Hunt56d51852014-11-09 13:03:35 -0800305 }
Simon Hunt50128c02014-11-08 13:36:15 -0800306
Simon Hunt56d51852014-11-09 13:03:35 -0800307 function handleUiEvent(data) {
Simon Huntbb282f52014-11-10 11:08:19 -0800308 scenario.view.alert('UI Tx: ' + data.event + '\n\n' +
309 JSON.stringify(data));
Simon Hunt56d51852014-11-09 13:03:35 -0800310 }
311
312 function injectStartupEvents(view) {
313 var last = scenario.params.lastAuto || 0;
Simon Hunt434cf142014-11-24 11:10:28 -0800314 if (config.useLiveData) { return; }
Simon Hunt56d51852014-11-09 13:03:35 -0800315
316 while (scenario.evNumber < last) {
Simon Hunt1a9eff92014-11-07 11:06:34 -0800317 injectTestEvent(view);
318 }
319 }
320
Simon Hunt934c3ce2014-11-05 11:45:07 -0800321 function toggleBg() {
322 var vis = bgImg.style('visibility');
Simon Hunt434cf142014-11-24 11:10:28 -0800323 bgImg.style('visibility', visVal(vis === 'hidden'));
324 }
325
326 function toggleHosts() {
327 showHosts = !showHosts;
328 updateHostVisibility();
Simon Hunt27d322d2014-11-28 10:45:43 -0800329 flash('Hosts ' + visVal(showHosts));
Simon Hunt934c3ce2014-11-05 11:45:07 -0800330 }
331
Simon Hunt6d9bd032014-11-28 22:16:40 -0800332 function toggleOffline() {
333 showOffline = !showOffline;
334 updateOfflineVisibility();
335 flash('Offline devices ' + visVal(showOffline));
336 }
337
Simon Hunt99c13842014-11-06 18:23:12 -0800338 function cycleLabels() {
Thomas Vachuska60d72bf2014-11-21 13:02:00 -0800339 deviceLabelIndex = (deviceLabelIndex === 2)
Simon Huntbb282f52014-11-10 11:08:19 -0800340 ? 0 : deviceLabelIndex + 1;
Simon Hunt5f36d342014-11-08 21:33:14 -0800341
Simon Hunt99c13842014-11-06 18:23:12 -0800342 network.nodes.forEach(function (d) {
Simon Huntbb282f52014-11-10 11:08:19 -0800343 if (d.class === 'device') {
344 updateDeviceLabel(d);
345 }
Simon Hunt99c13842014-11-06 18:23:12 -0800346 });
Simon Hunt934c3ce2014-11-05 11:45:07 -0800347 }
348
349 function togglePorts(view) {
Simon Hunt434cf142014-11-24 11:10:28 -0800350 //view.alert('togglePorts() callback')
Simon Hunt934c3ce2014-11-05 11:45:07 -0800351 }
352
Simon Hunt6ac93f32014-11-13 12:17:27 -0800353 function unpin() {
354 if (hovered) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800355 sendUpdateMeta(hovered);
Simon Hunt6ac93f32014-11-13 12:17:27 -0800356 hovered.fixed = false;
357 hovered.el.classed('fixed', false);
358 network.force.resume();
359 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800360 }
361
Simon Hunt9462e8c2014-11-14 17:28:09 -0800362 function handleEscape(view) {
363 if (oiShowMaster) {
364 cancelAffinity();
Simon Hunt27d322d2014-11-28 10:45:43 -0800365 } else if (haveDetails) {
Simon Hunt9462e8c2014-11-14 17:28:09 -0800366 deselectAll();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800367 } else if (oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800368 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800369 } else if (summaryPane.isVisible()) {
370 cancelSummary();
Thomas Vachuska5bde31f2014-11-25 15:29:18 -0800371 stopAntTimer();
372 } else {
373 hoverMode = hoverModeFlows;
Simon Hunt9462e8c2014-11-14 17:28:09 -0800374 }
375 }
376
Simon Hunt934c3ce2014-11-05 11:45:07 -0800377 // ==============================
378 // Radio Button Callbacks
379
Simon Hunta5e89142014-11-14 07:00:33 -0800380 var layerLookup = {
381 host: {
Simon Hunt209155e2014-11-21 12:16:09 -0800382 endstation: 'pkt', // default, if host event does not define type
Thomas Vachuska89543292014-11-19 11:28:33 -0800383 router: 'pkt',
Simon Hunta5e89142014-11-14 07:00:33 -0800384 bgpSpeaker: 'pkt'
385 },
386 device: {
387 switch: 'pkt',
388 roadm: 'opt'
389 },
390 link: {
391 hostLink: 'pkt',
392 direct: 'pkt',
Simon Hunt8257f4c2014-11-16 19:34:54 -0800393 indirect: '',
394 tunnel: '',
Simon Hunta5e89142014-11-14 07:00:33 -0800395 optical: 'opt'
396 }
397 };
398
399 function inLayer(d, layer) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800400 var type = d.class === 'link' ? d.type() : d.type,
401 look = layerLookup[d.class],
402 lyr = look && look[type];
Simon Hunta5e89142014-11-14 07:00:33 -0800403 return lyr === layer;
404 }
405
406 function unsuppressLayer(which) {
407 node.each(function (d) {
408 var node = d.el;
409 if (inLayer(d, which)) {
410 node.classed('suppressed', false);
411 }
412 });
413
414 link.each(function (d) {
415 var link = d.el;
416 if (inLayer(d, which)) {
417 link.classed('suppressed', false);
418 }
419 });
420 }
421
Simon Hunt9462e8c2014-11-14 17:28:09 -0800422 function suppressLayers(b) {
423 node.classed('suppressed', b);
424 link.classed('suppressed', b);
Simon Hunt142d0032014-11-04 20:13:09 -0800425// d3.selectAll('svg .port').classed('inactive', false);
426// d3.selectAll('svg .portText').classed('inactive', false);
Simon Hunt195cb382014-11-03 17:50:51 -0800427 }
428
Simon Hunt9462e8c2014-11-14 17:28:09 -0800429 function showAllLayers() {
430 suppressLayers(false);
431 }
432
Simon Hunt195cb382014-11-03 17:50:51 -0800433 function showPacketLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800434 node.classed('suppressed', true);
435 link.classed('suppressed', true);
436 unsuppressLayer('pkt');
Simon Hunt195cb382014-11-03 17:50:51 -0800437 }
438
439 function showOpticalLayer() {
Simon Hunta5e89142014-11-14 07:00:33 -0800440 node.classed('suppressed', true);
441 link.classed('suppressed', true);
442 unsuppressLayer('opt');
Simon Hunt195cb382014-11-03 17:50:51 -0800443 }
444
Simon Hunt9462e8c2014-11-14 17:28:09 -0800445 function restoreLayerState() {
446 layerBtnDispatch[layerBtnSet.selected()]();
447 }
448
Simon Hunt142d0032014-11-04 20:13:09 -0800449 // ==============================
Simon Hunt934c3ce2014-11-05 11:45:07 -0800450 // Private functions
451
Simon Hunt99c13842014-11-06 18:23:12 -0800452 function safeId(s) {
453 return s.replace(/[^a-z0-9]/gi, '-');
454 }
455
Simon Huntc7ee0662014-11-05 16:44:37 -0800456 // set the size of the given element to that of the view (reduced if padded)
457 function setSize(el, view, pad) {
458 var padding = pad ? pad * 2 : 0;
Simon Hunt934c3ce2014-11-05 11:45:07 -0800459 el.attr({
Simon Huntc7ee0662014-11-05 16:44:37 -0800460 width: view.width() - padding,
461 height: view.height() - padding
Simon Hunt934c3ce2014-11-05 11:45:07 -0800462 });
463 }
464
Simon Hunt8257f4c2014-11-16 19:34:54 -0800465 function makeNodeKey(d, what) {
466 var port = what + 'Port';
467 return d[what] + '/' + d[port];
468 }
469
470 function makeLinkKey(d, flipped) {
471 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
472 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
473 return one + '-' + two;
474 }
475
Simon Hunt269670f2014-11-17 16:17:43 -0800476 function findLinkById(id) {
477 // check to see if this is a reverse lookup, else default to given id
478 var key = network.revLinkToKey[id] || id;
479 return key && network.lookup[key];
480 }
481
Simon Hunt8257f4c2014-11-16 19:34:54 -0800482 function findLink(linkData, op) {
483 var key = makeLinkKey(linkData),
484 keyrev = makeLinkKey(linkData, 1),
485 link = network.lookup[key],
486 linkRev = network.lookup[keyrev],
487 result = {},
488 ldata = link || linkRev,
489 rawLink;
490
491 if (op === 'add') {
492 if (link) {
493 // trying to add a link that we already know about
494 result.ldata = link;
495 result.badLogic = 'addLink: link already added';
496
497 } else if (linkRev) {
498 // we found the reverse of the link to be added
499 result.ldata = linkRev;
500 if (linkRev.fromTarget) {
501 result.badLogic = 'addLink: link already added';
502 }
503 }
504 } else if (op === 'update') {
505 if (!ldata) {
506 result.badLogic = 'updateLink: link not found';
507 } else {
508 rawLink = link ? ldata.fromSource : ldata.fromTarget;
509 result.updateWith = function (data) {
510 $.extend(rawLink, data);
511 restyleLinkElement(ldata);
512 }
513 }
514 } else if (op === 'remove') {
515 if (!ldata) {
516 result.badLogic = 'removeLink: link not found';
517 } else {
518 rawLink = link ? ldata.fromSource : ldata.fromTarget;
519
520 if (!rawLink) {
521 result.badLogic = 'removeLink: link not found';
522
523 } else {
524 result.removeRawLink = function () {
525 if (link) {
526 // remove fromSource
527 ldata.fromSource = null;
528 if (ldata.fromTarget) {
529 // promote target into source position
530 ldata.fromSource = ldata.fromTarget;
531 ldata.fromTarget = null;
532 ldata.key = keyrev;
533 delete network.lookup[key];
534 network.lookup[keyrev] = ldata;
Simon Hunt269670f2014-11-17 16:17:43 -0800535 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800536 }
537 } else {
538 // remove fromTarget
539 ldata.fromTarget = null;
Simon Hunt269670f2014-11-17 16:17:43 -0800540 delete network.revLinkToKey[keyrev];
Simon Hunt8257f4c2014-11-16 19:34:54 -0800541 }
542 if (ldata.fromSource) {
543 restyleLinkElement(ldata);
544 } else {
545 removeLinkElement(ldata);
546 }
547 }
548 }
549 }
550 }
551 return result;
552 }
553
554 function addLinkUpdate(ldata, link) {
555 // add link event, but we already have the reverse link installed
556 ldata.fromTarget = link;
Simon Hunt269670f2014-11-17 16:17:43 -0800557 network.revLinkToKey[link.id] = ldata.key;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800558 restyleLinkElement(ldata);
559 }
560
561 var allLinkTypes = 'direct indirect optical tunnel',
562 defaultLinkType = 'direct';
563
564 function restyleLinkElement(ldata) {
565 // this fn's job is to look at raw links and decide what svg classes
566 // need to be applied to the line element in the DOM
567 var el = ldata.el,
568 type = ldata.type(),
569 lw = ldata.linkWidth(),
570 online = ldata.online();
571
572 el.classed('link', true);
573 el.classed('inactive', !online);
574 el.classed(allLinkTypes, false);
575 if (type) {
576 el.classed(type, true);
577 }
578 el.transition()
579 .duration(1000)
Thomas Vachuska89543292014-11-19 11:28:33 -0800580 .attr('stroke-width', linkScale(lw))
581 .attr('stroke', config.topo.linkBaseColor);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800582 }
Simon Hunt934c3ce2014-11-05 11:45:07 -0800583
Simon Hunt99c13842014-11-06 18:23:12 -0800584 // ==============================
585 // Event handlers for server-pushed events
586
Simon Huntbb282f52014-11-10 11:08:19 -0800587 function logicError(msg) {
588 // TODO, report logic error to server, via websock, so it can be logged
Simon Huntfc274c92014-11-11 11:05:46 -0800589 console.warn(msg);
Simon Huntbb282f52014-11-10 11:08:19 -0800590 }
591
Simon Hunt99c13842014-11-06 18:23:12 -0800592 var eventDispatch = {
Simon Hunta5e89142014-11-14 07:00:33 -0800593 addInstance: addInstance,
Simon Hunt99c13842014-11-06 18:23:12 -0800594 addDevice: addDevice,
Thomas Vachuskad1be50d2014-11-08 16:10:20 -0800595 addLink: addLink,
Simon Hunt56d51852014-11-09 13:03:35 -0800596 addHost: addHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800597
Simon Huntfcfb46c2014-11-19 12:53:38 -0800598 updateInstance: updateInstance,
Simon Huntbb282f52014-11-10 11:08:19 -0800599 updateDevice: updateDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800600 updateLink: updateLink,
Simon Hunt7cd48f32014-11-09 23:42:50 -0800601 updateHost: updateHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800602
Simon Hunt7b403bc2014-11-22 19:01:00 -0800603 removeInstance: removeInstance,
Simon Huntca867ac2014-11-28 18:07:35 -0800604 removeDevice: removeDevice,
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800605 removeLink: removeLink,
Simon Hunt44031102014-11-11 13:20:36 -0800606 removeHost: removeHost,
Simon Huntb53e0682014-11-12 13:32:01 -0800607
Simon Hunt61d04042014-11-11 17:27:16 -0800608 showDetails: showDetails,
Thomas Vachuska47635c62014-11-22 01:21:36 -0800609 showSummary: showSummary,
Simon Huntb53e0682014-11-12 13:32:01 -0800610 showTraffic: showTraffic
Simon Hunt99c13842014-11-06 18:23:12 -0800611 };
612
Simon Hunta5e89142014-11-14 07:00:33 -0800613 function addInstance(data) {
614 evTrace(data);
615 var inst = data.payload,
616 id = inst.id;
617 if (onosInstances[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800618 updateInstance(data);
Simon Hunta5e89142014-11-14 07:00:33 -0800619 return;
620 }
621 onosInstances[id] = inst;
622 onosOrder.push(inst);
623 updateInstances();
624 }
625
Simon Hunt99c13842014-11-06 18:23:12 -0800626 function addDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800627 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800628 var device = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800629 id = device.id,
630 d;
631
632 if (network.lookup[id]) {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -0800633 updateDevice(data);
Simon Huntca867ac2014-11-28 18:07:35 -0800634 return;
635 }
636
637 d = createDeviceNode(device);
638 network.nodes.push(d);
639 network.lookup[id] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800640 updateNodes();
641 network.force.start();
642 }
643
Simon Hunt99c13842014-11-06 18:23:12 -0800644 function addLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800645 evTrace(data);
Simon Hunt99c13842014-11-06 18:23:12 -0800646 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800647 result = findLink(link, 'add'),
648 bad = result.badLogic,
Simon Huntca867ac2014-11-28 18:07:35 -0800649 d = result.ldata;
Simon Hunt8257f4c2014-11-16 19:34:54 -0800650
651 if (bad) {
652 logicError(bad + ': ' + link.id);
653 return;
654 }
655
Simon Huntca867ac2014-11-28 18:07:35 -0800656 if (d) {
Simon Hunt8257f4c2014-11-16 19:34:54 -0800657 // we already have a backing store link for src/dst nodes
Simon Huntca867ac2014-11-28 18:07:35 -0800658 addLinkUpdate(d, link);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800659 return;
660 }
661
662 // no backing store link yet
Simon Huntca867ac2014-11-28 18:07:35 -0800663 d = createLink(link);
664 if (d) {
665 network.links.push(d);
666 network.lookup[d.key] = d;
Simon Hunt99c13842014-11-06 18:23:12 -0800667 updateLinks();
668 network.force.start();
669 }
670 }
671
Simon Hunt56d51852014-11-09 13:03:35 -0800672 function addHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800673 evTrace(data);
Simon Hunt56d51852014-11-09 13:03:35 -0800674 var host = data.payload,
Simon Huntca867ac2014-11-28 18:07:35 -0800675 id = host.id,
676 d,
Simon Hunt56d51852014-11-09 13:03:35 -0800677 lnk;
Simon Huntca867ac2014-11-28 18:07:35 -0800678
679 if (network.lookup[id]) {
680 logicError('Host already added: ' + id);
681 return;
682 }
683
684 d = createHostNode(host);
685 network.nodes.push(d);
686 network.lookup[host.id] = d;
Simon Hunt56d51852014-11-09 13:03:35 -0800687 updateNodes();
688
689 lnk = createHostLink(host);
690 if (lnk) {
Simon Huntca867ac2014-11-28 18:07:35 -0800691 d.linkData = lnk; // cache ref on its host
Simon Hunt56d51852014-11-09 13:03:35 -0800692 network.links.push(lnk);
Simon Huntca867ac2014-11-28 18:07:35 -0800693 network.lookup[d.ingress] = lnk;
694 network.lookup[d.egress] = lnk;
Simon Hunt56d51852014-11-09 13:03:35 -0800695 updateLinks();
696 }
697 network.force.start();
698 }
699
Simon Hunt44031102014-11-11 13:20:36 -0800700 // TODO: fold updateX(...) methods into one base method; remove duplication
Simon Hunt56a2ea42014-11-19 12:39:31 -0800701
702 function updateInstance(data) {
703 evTrace(data);
704 var inst = data.payload,
705 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800706 d = onosInstances[id];
707 if (d) {
708 $.extend(d, inst);
Simon Hunt56a2ea42014-11-19 12:39:31 -0800709 updateInstances();
Simon Hunt56a2ea42014-11-19 12:39:31 -0800710 } else {
711 logicError('updateInstance lookup fail. ID = "' + id + '"');
712 }
713 }
714
Simon Huntbb282f52014-11-10 11:08:19 -0800715 function updateDevice(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800716 evTrace(data);
Simon Huntbb282f52014-11-10 11:08:19 -0800717 var device = data.payload,
718 id = device.id,
Simon Hunt6d9bd032014-11-28 22:16:40 -0800719 d = network.lookup[id],
720 wasOnline;
721
Simon Hunt62c47542014-11-22 22:16:32 -0800722 if (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -0800723 wasOnline = d.online;
Simon Hunt62c47542014-11-22 22:16:32 -0800724 $.extend(d, device);
725 if (positionNode(d, true)) {
Simon Hunt395a70c2014-11-22 23:17:40 -0800726 sendUpdateMeta(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -0800727 }
728 updateNodes();
Simon Hunt6d9bd032014-11-28 22:16:40 -0800729 if (wasOnline !== d.online) {
730 findAttachedLinks(d.id).forEach(restyleLinkElement);
731 updateOfflineVisibility(d);
732 }
Simon Huntbb282f52014-11-10 11:08:19 -0800733 } else {
734 logicError('updateDevice lookup fail. ID = "' + id + '"');
735 }
736 }
737
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800738 function updateLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800739 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800740 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800741 result = findLink(link, 'update'),
742 bad = result.badLogic;
743 if (bad) {
744 logicError(bad + ': ' + link.id);
745 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800746 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800747 result.updateWith(link);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800748 }
749
Simon Hunt7cd48f32014-11-09 23:42:50 -0800750 function updateHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800751 evTrace(data);
Simon Hunt7cd48f32014-11-09 23:42:50 -0800752 var host = data.payload,
Simon Huntbb282f52014-11-10 11:08:19 -0800753 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800754 d = network.lookup[id];
755 if (d) {
756 $.extend(d, host);
Simon Hunt6d9bd032014-11-28 22:16:40 -0800757 if (positionNode(d, true)) {
758 sendUpdateMeta(d, true);
759 }
760 updateNodes(d);
Simon Huntbb282f52014-11-10 11:08:19 -0800761 } else {
762 logicError('updateHost lookup fail. ID = "' + id + '"');
763 }
Simon Hunt7cd48f32014-11-09 23:42:50 -0800764 }
765
Simon Hunt44031102014-11-11 13:20:36 -0800766 // TODO: fold removeX(...) methods into base method - remove dup code
Simon Hunt7b403bc2014-11-22 19:01:00 -0800767 function removeInstance(data) {
768 evTrace(data);
769 var inst = data.payload,
770 id = inst.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800771 d = onosInstances[id];
772 if (d) {
773 var idx = find(id, onosOrder);
Simon Hunt7b403bc2014-11-22 19:01:00 -0800774 if (idx >= 0) {
775 onosOrder.splice(idx, 1);
776 }
777 delete onosInstances[id];
778 updateInstances();
779 } else {
780 logicError('updateInstance lookup fail. ID = "' + id + '"');
781 }
782 }
783
Simon Huntca867ac2014-11-28 18:07:35 -0800784 function removeDevice(data) {
785 evTrace(data);
786 var device = data.payload,
787 id = device.id,
788 d = network.lookup[id];
789 if (d) {
790 removeDeviceElement(d);
791 } else {
792 logicError('removeDevice lookup fail. ID = "' + id + '"');
793 }
794 }
795
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800796 function removeLink(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800797 evTrace(data);
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800798 var link = data.payload,
Simon Hunt8257f4c2014-11-16 19:34:54 -0800799 result = findLink(link, 'remove'),
800 bad = result.badLogic;
801 if (bad) {
Simon Huntca867ac2014-11-28 18:07:35 -0800802 // may have already removed link, if attached to removed device
803 console.warn(bad + ': ' + link.id);
Simon Hunt8257f4c2014-11-16 19:34:54 -0800804 return;
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800805 }
Simon Hunt8257f4c2014-11-16 19:34:54 -0800806 result.removeRawLink();
Simon Hunt3f03d4a2014-11-10 20:14:37 -0800807 }
808
Simon Hunt44031102014-11-11 13:20:36 -0800809 function removeHost(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800810 evTrace(data);
Simon Hunt44031102014-11-11 13:20:36 -0800811 var host = data.payload,
812 id = host.id,
Simon Huntca867ac2014-11-28 18:07:35 -0800813 d = network.lookup[id];
814 if (d) {
815 removeHostElement(d, true);
Simon Hunt44031102014-11-11 13:20:36 -0800816 } else {
Simon Huntca867ac2014-11-28 18:07:35 -0800817 // may have already removed host, if attached to removed device
818 console.warn('removeHost lookup fail. ID = "' + id + '"');
Simon Hunt44031102014-11-11 13:20:36 -0800819 }
820 }
821
Simon Huntca867ac2014-11-28 18:07:35 -0800822 // the following events are server responses to user actions
Thomas Vachuska47635c62014-11-22 01:21:36 -0800823 function showSummary(data) {
824 evTrace(data);
825 populateSummary(data.payload);
Simon Hunt06811b72014-11-25 18:54:48 -0800826 showSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800827 }
828
Simon Hunt61d04042014-11-11 17:27:16 -0800829 function showDetails(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800830 evTrace(data);
Simon Hunt27d322d2014-11-28 10:45:43 -0800831 haveDetails = true;
Simon Hunt61d04042014-11-11 17:27:16 -0800832 populateDetails(data.payload);
Simon Hunt27d322d2014-11-28 10:45:43 -0800833 if (useDetails) {
834 showDetailPane();
835 }
Simon Hunt61d04042014-11-11 17:27:16 -0800836 }
837
Simon Huntb53e0682014-11-12 13:32:01 -0800838 function showTraffic(data) {
Simon Hunta5e89142014-11-14 07:00:33 -0800839 evTrace(data);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800840 var paths = data.payload.paths,
841 hasTraffic = false;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800842
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800843 // Revert any links hilighted previously.
Thomas Vachuska4731f122014-11-20 04:56:19 -0800844 link.style('stroke-width', null)
Thomas Vachuskaa3148a72014-11-19 21:38:35 -0800845 .classed('primary secondary animated optical', false);
Simon Hunte2575b62014-11-18 15:25:53 -0800846 // Remove all previous labels.
847 removeLinkLabels();
Thomas Vachuska3266abf2014-11-13 09:28:46 -0800848
Simon Hunte2575b62014-11-18 15:25:53 -0800849 // Now hilight all links in the paths payload, and attach
850 // labels to them, if they are defined.
Simon Hunta255a2c2014-11-13 22:29:35 -0800851 paths.forEach(function (p) {
Simon Hunte2575b62014-11-18 15:25:53 -0800852 var n = p.links.length,
853 i,
854 ldata;
855
Thomas Vachuska4731f122014-11-20 04:56:19 -0800856 hasTraffic = hasTraffic || p.traffic;
Simon Hunte2575b62014-11-18 15:25:53 -0800857 for (i=0; i<n; i++) {
858 ldata = findLinkById(p.links[i]);
Thomas Vachuska4731f122014-11-20 04:56:19 -0800859 if (ldata && ldata.el) {
Simon Hunte2575b62014-11-18 15:25:53 -0800860 ldata.el.classed(p.class, true);
861 ldata.label = p.labels[i];
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800862 }
Simon Hunte2575b62014-11-18 15:25:53 -0800863 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800864 });
Thomas Vachuska4731f122014-11-20 04:56:19 -0800865
Simon Hunte2575b62014-11-18 15:25:53 -0800866 updateLinks();
Thomas Vachuska4731f122014-11-20 04:56:19 -0800867
868 if (hasTraffic && !antTimer) {
869 startAntTimer();
870 } else if (!hasTraffic && antTimer) {
871 stopAntTimer();
872 }
Simon Huntb53e0682014-11-12 13:32:01 -0800873 }
874
Simon Hunt56d51852014-11-09 13:03:35 -0800875 // ...............................
876
Simon Hunt99c13842014-11-06 18:23:12 -0800877 function unknownEvent(data) {
Simon Hunt434cf142014-11-24 11:10:28 -0800878 console.warn('Unknown event type: "' + data.event + '"', data);
Simon Hunt99c13842014-11-06 18:23:12 -0800879 }
880
881 function handleServerEvent(data) {
882 var fn = eventDispatch[data.event] || unknownEvent;
883 fn(data);
884 }
885
886 // ==============================
Simon Hunt61d04042014-11-11 17:27:16 -0800887 // Out-going messages...
888
Simon Huntb53e0682014-11-12 13:32:01 -0800889 function nSel() {
890 return selectOrder.length;
891 }
Simon Hunt61d04042014-11-11 17:27:16 -0800892 function getSel(idx) {
893 return selections[selectOrder[idx]];
894 }
Simon Huntb53e0682014-11-12 13:32:01 -0800895 function allSelectionsClass(cls) {
896 for (var i=0, n=nSel(); i<n; i++) {
897 if (getSel(i).obj.class !== cls) {
898 return false;
899 }
900 }
901 return true;
902 }
Simon Hunt61d04042014-11-11 17:27:16 -0800903
Thomas Vachuska47635c62014-11-22 01:21:36 -0800904 function toggleInstances() {
905 if (!oiBox.isVisible()) {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800906 showInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800907 } else {
Simon Huntb0ecfa52014-11-23 21:05:12 -0800908 hideInstances();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800909 }
910 }
911
Simon Huntb0ecfa52014-11-23 21:05:12 -0800912 function showInstances() {
913 oiBox.show();
914 colorAffinity = true;
915 updateDeviceColors();
916 }
917
918 function hideInstances() {
919 oiBox.hide();
920 colorAffinity = false;
921 cancelAffinity();
922 updateDeviceColors();
923 }
924
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800925 function equalizeMasters() {
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800926 sendMessage('equalizeMasters');
Simon Huntc1cc81c2014-11-29 14:59:01 -0800927 flash('Equalizing master roles');
Thomas Vachuska1e68bdd2014-11-29 13:53:10 -0800928 }
929
Thomas Vachuska47635c62014-11-22 01:21:36 -0800930 function toggleSummary() {
931 if (!summaryPane.isVisible()) {
932 requestSummary();
933 } else {
934 cancelSummary();
935 }
936 }
937
938 // request overall summary data
939 function requestSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -0800940 sendMessage('requestSummary');
Thomas Vachuska47635c62014-11-22 01:21:36 -0800941 }
942
943 function cancelSummary() {
Simon Huntc1cc81c2014-11-29 14:59:01 -0800944 sendMessage('cancelSummary');
Simon Hunt06811b72014-11-25 18:54:48 -0800945 hideSummaryPane();
Thomas Vachuska47635c62014-11-22 01:21:36 -0800946 }
947
Simon Hunt27d322d2014-11-28 10:45:43 -0800948 function toggleDetails() {
949 useDetails = !useDetails;
950 if (useDetails) {
951 flash('Enable details pane');
952 if (haveDetails) {
953 showDetailPane();
954 }
955 } else {
956 flash('Disable details pane');
957 hideDetailPane();
958 }
959 }
960
Simon Hunt06811b72014-11-25 18:54:48 -0800961 // encapsulate interaction between summary and details panes
962 function showSummaryPane() {
963 if (detailPane.isVisible()) {
964 detailPane.down(summaryPane.show);
965 } else {
966 summaryPane.show();
967 }
968 }
969
970 function hideSummaryPane() {
971 summaryPane.hide(function () {
972 if (detailPane.isVisible()) {
973 detailPane.up();
974 }
975 });
976 }
977
978 function showDetailPane() {
979 if (summaryPane.isVisible()) {
980 detailPane.down(detailPane.show);
981 } else {
982 detailPane.up(detailPane.show);
983 }
984 }
985
986 function hideDetailPane() {
987 detailPane.hide();
988 }
989
990
Simon Hunt61d04042014-11-11 17:27:16 -0800991 // request details for the selected element
Simon Huntd72bc702014-11-13 18:38:04 -0800992 // invoked from selection of a single node.
Simon Hunt61d04042014-11-11 17:27:16 -0800993 function requestDetails() {
Simon Huntc1cc81c2014-11-29 14:59:01 -0800994 var data = getSel(0).obj;
995 sendMessage('requestDetails', {
996 id: data.id,
997 class: data.class
998 });
Simon Hunt61d04042014-11-11 17:27:16 -0800999 }
1000
Thomas Vachuska9edca302014-11-22 17:06:42 -08001001 function addHostIntentAction() {
Simon Huntd72bc702014-11-13 18:38:04 -08001002 sendMessage('addHostIntent', {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001003 one: selectOrder[0],
1004 two: selectOrder[1],
1005 ids: selectOrder
Simon Huntd72bc702014-11-13 18:38:04 -08001006 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001007 flash('Host-to-Host flow added');
Simon Huntd72bc702014-11-13 18:38:04 -08001008 }
1009
Thomas Vachuska9edca302014-11-22 17:06:42 -08001010 function addMultiSourceIntentAction() {
1011 sendMessage('addMultiSourceIntent', {
1012 src: selectOrder.slice(0, selectOrder.length - 1),
1013 dst: selectOrder[selectOrder.length - 1],
1014 ids: selectOrder
1015 });
Simon Hunt27d322d2014-11-28 10:45:43 -08001016 flash('Multi-Source flow added');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001017 }
1018
Thomas Vachuska9edca302014-11-22 17:06:42 -08001019
Thomas Vachuska47635c62014-11-22 01:21:36 -08001020 function cancelTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001021 sendMessage('cancelTraffic');
Thomas Vachuska47635c62014-11-22 01:21:36 -08001022 }
1023
Thomas Vachuska9edca302014-11-22 17:06:42 -08001024 function requestTrafficForMode() {
1025 if (hoverMode === hoverModeAll) {
1026 requestAllTraffic();
1027 } else if (hoverMode === hoverModeFlows) {
1028 requestDeviceLinkFlows();
1029 } else if (hoverMode === hoverModeIntents) {
1030 requestSelectTraffic();
Simon Huntd72bc702014-11-13 18:38:04 -08001031 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001032 }
Simon Huntd72bc702014-11-13 18:38:04 -08001033
Thomas Vachuska9edca302014-11-22 17:06:42 -08001034 function showTrafficAction() {
1035 hoverMode = hoverModeIntents;
1036 requestSelectTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001037 flash('Related Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001038 }
1039
1040 function requestSelectTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001041 function hoverValid() {
1042 return hoverMode === hoverModeIntents &&
1043 hovered &&
1044 (hovered.class === 'host' || hovered.class === 'device');
1045 }
1046
Thomas Vachuska9edca302014-11-22 17:06:42 -08001047 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001048 sendMessage('requestTraffic', {
1049 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001050 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001051 });
1052 }
Simon Huntd72bc702014-11-13 18:38:04 -08001053 }
1054
Thomas Vachuska5fedb7a2014-11-20 00:55:08 -08001055
Thomas Vachuska29617e52014-11-20 03:17:46 -08001056 function showDeviceLinkFlowsAction() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001057 hoverMode = hoverModeFlows;
1058 requestDeviceLinkFlows();
Simon Hunt27d322d2014-11-28 10:45:43 -08001059 flash('Device Flows');
Thomas Vachuska29617e52014-11-20 03:17:46 -08001060 }
1061
Thomas Vachuska9edca302014-11-22 17:06:42 -08001062 function requestDeviceLinkFlows() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001063 function hoverValid() {
1064 return hoverMode === hoverModeFlows &&
1065 hovered && (hovered.class === 'device');
1066 }
1067
Thomas Vachuska9edca302014-11-22 17:06:42 -08001068 if (validateSelectionContext()) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08001069 sendMessage('requestDeviceLinkFlows', {
1070 ids: selectOrder,
Simon Huntc1cc81c2014-11-29 14:59:01 -08001071 hover: hoverValid() ? hovered.id : ''
Thomas Vachuska9edca302014-11-22 17:06:42 -08001072 });
1073 }
1074 }
1075
1076
1077 function showAllTrafficAction() {
1078 hoverMode = hoverModeAll;
1079 requestAllTraffic();
Simon Hunt27d322d2014-11-28 10:45:43 -08001080 flash('All Traffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001081 }
1082
1083 function requestAllTraffic() {
Simon Huntc1cc81c2014-11-29 14:59:01 -08001084 sendMessage('requestAllTraffic');
Thomas Vachuska9edca302014-11-22 17:06:42 -08001085 }
1086
1087 function validateSelectionContext() {
Thomas Vachuska29617e52014-11-20 03:17:46 -08001088 if (!hovered && nSel() === 0) {
Thomas Vachuska47635c62014-11-22 01:21:36 -08001089 cancelTraffic();
Thomas Vachuska9edca302014-11-22 17:06:42 -08001090 return false;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001091 }
Thomas Vachuska9edca302014-11-22 17:06:42 -08001092 return true;
Thomas Vachuska29617e52014-11-20 03:17:46 -08001093 }
Simon Huntd72bc702014-11-13 18:38:04 -08001094
Simon Hunta6a9fe72014-11-20 11:17:12 -08001095
Simon Hunt61d04042014-11-11 17:27:16 -08001096 // ==============================
Simon Hunta5e89142014-11-14 07:00:33 -08001097 // onos instance panel functions
Simon Huntb82f6902014-11-22 11:53:15 -08001098
Simon Hunt7b403bc2014-11-22 19:01:00 -08001099 var instCfg = {
1100 rectPad: 8,
1101 nodeOx: 9,
1102 nodeOy: 9,
1103 nodeDim: 40,
1104 birdOx: 19,
1105 birdOy: 21,
1106 birdDim: 21,
1107 uiDy: 45,
1108 titleDy: 30,
1109 textYOff: 20,
1110 textYSpc: 15
1111 };
1112
1113 function viewBox(dim) {
1114 return '0 0 ' + dim.w + ' ' + dim.h;
1115 }
1116
1117 function instRectAttr(dim) {
1118 var pad = instCfg.rectPad;
1119 return {
1120 x: pad,
1121 y: pad,
1122 width: dim.w - pad*2,
1123 height: dim.h - pad*2,
Simon Huntb0ecfa52014-11-23 21:05:12 -08001124 rx: 6
Simon Hunt7b403bc2014-11-22 19:01:00 -08001125 };
1126 }
1127
1128 function computeDim(self) {
1129 var css = window.getComputedStyle(self);
1130 return {
1131 w: stripPx(css.width),
1132 h: stripPx(css.height)
1133 };
Simon Huntb82f6902014-11-22 11:53:15 -08001134 }
Simon Hunta5e89142014-11-14 07:00:33 -08001135
1136 function updateInstances() {
1137 var onoses = oiBox.el.selectAll('.onosInst')
Simon Huntb82f6902014-11-22 11:53:15 -08001138 .data(onosOrder, function (d) { return d.id; }),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001139 instDim = {w:0,h:0},
1140 c = instCfg;
Simon Hunta5e89142014-11-14 07:00:33 -08001141
Simon Hunt7b403bc2014-11-22 19:01:00 -08001142 function nSw(n) {
1143 return '# Switches: ' + n;
1144 }
Simon Hunta5e89142014-11-14 07:00:33 -08001145
Simon Huntb82f6902014-11-22 11:53:15 -08001146 // operate on existing onos instances if necessary
1147 onoses.each(function (d) {
1148 var el = d3.select(this),
1149 svg = el.select('svg');
Simon Hunt7b403bc2014-11-22 19:01:00 -08001150 instDim = computeDim(this);
Simon Huntb82f6902014-11-22 11:53:15 -08001151
1152 // update online state
1153 el.classed('online', d.online);
1154
1155 // update ui-attached state
1156 svg.select('use.uiBadge').remove();
1157 if (d.uiAttached) {
1158 attachUiBadge(svg);
1159 }
1160
Simon Hunt7b403bc2014-11-22 19:01:00 -08001161 function updAttr(id, value) {
1162 svg.select('text.instLabel.'+id).text(value);
1163 }
1164
1165 updAttr('ip', d.ip);
1166 updAttr('ns', nSw(d.switches));
Simon Huntb82f6902014-11-22 11:53:15 -08001167 });
1168
1169
1170 // operate on new onos instances
Simon Hunta5e89142014-11-14 07:00:33 -08001171 var entering = onoses.enter()
1172 .append('div')
1173 .attr('class', 'onosInst')
1174 .classed('online', function (d) { return d.online; })
Simon Hunt9c15eca2014-11-15 18:37:59 -08001175 .on('click', clickInst);
1176
Simon Huntb82f6902014-11-22 11:53:15 -08001177 entering.each(function (d) {
Simon Hunt9c15eca2014-11-15 18:37:59 -08001178 var el = d3.select(this),
Simon Hunt7b403bc2014-11-22 19:01:00 -08001179 rectAttr,
1180 svg;
1181 instDim = computeDim(this);
1182 rectAttr = instRectAttr(instDim);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001183
Simon Hunt7b403bc2014-11-22 19:01:00 -08001184 svg = el.append('svg').attr({
1185 width: instDim.w,
1186 height: instDim.h,
1187 viewBox: viewBox(instDim)
Simon Hunt95908012014-11-20 10:20:26 -08001188 });
Simon Huntb82f6902014-11-22 11:53:15 -08001189
Simon Hunt7b403bc2014-11-22 19:01:00 -08001190 svg.append('rect').attr(rectAttr);
Simon Hunt9c15eca2014-11-15 18:37:59 -08001191
Thomas Vachuskae02e11c2014-11-24 16:13:52 -08001192 //appendGlyph(svg, c.nodeOx, c.nodeOy, c.nodeDim, '#node');
1193 appendBadge(svg, 14, 14, 28, '#bird');
Simon Huntb82f6902014-11-22 11:53:15 -08001194
1195 if (d.uiAttached) {
1196 attachUiBadge(svg);
1197 }
1198
Simon Hunt7b403bc2014-11-22 19:01:00 -08001199 var left = c.nodeOx + c.nodeDim,
1200 len = rectAttr.width - left,
1201 hlen = len / 2,
1202 midline = hlen + left;
Simon Hunt9c15eca2014-11-15 18:37:59 -08001203
Simon Hunt7b403bc2014-11-22 19:01:00 -08001204 // title
1205 svg.append('text')
1206 .attr({
1207 class: 'instTitle',
1208 x: midline,
1209 y: c.titleDy
1210 })
1211 .text(d.id);
1212
1213 // a couple of attributes
1214 var ty = c.titleDy + c.textYOff;
1215
1216 function addAttr(id, label) {
1217 svg.append('text').attr({
1218 class: 'instLabel ' + id,
1219 x: midline,
1220 y: ty
1221 }).text(label);
1222 ty += c.textYSpc;
1223 }
1224
1225 addAttr('ip', d.ip);
1226 addAttr('ns', nSw(d.switches));
Simon Hunt9c15eca2014-11-15 18:37:59 -08001227 });
Simon Hunta5e89142014-11-14 07:00:33 -08001228
1229 // operate on existing + new onoses here
Simon Hunt8f40cce2014-11-23 15:57:30 -08001230 // set the affinity colors...
1231 onoses.each(function (d) {
1232 var el = d3.select(this),
1233 rect = el.select('svg').select('rect'),
1234 col = instColor(d.id, d.online);
1235 rect.style('fill', col);
1236 });
Simon Hunta5e89142014-11-14 07:00:33 -08001237
Simon Hunt7b403bc2014-11-22 19:01:00 -08001238 // adjust the panel size appropriately...
1239 oiBox.width(instDim.w * onosOrder.length);
1240 oiBox.height(instDim.h);
1241
1242 // remove any outgoing instances
1243 onoses.exit().remove();
Simon Hunta5e89142014-11-14 07:00:33 -08001244 }
1245
Simon Hunt8f40cce2014-11-23 15:57:30 -08001246 function instColor(id, online) {
1247 return cat7.get(id, !online, network.view.getTheme());
1248 }
1249
Simon Hunt9462e8c2014-11-14 17:28:09 -08001250 function clickInst(d) {
1251 var el = d3.select(this),
1252 aff = el.classed('affinity');
1253 if (!aff) {
1254 setAffinity(el, d);
1255 } else {
1256 cancelAffinity();
1257 }
1258 }
1259
1260 function setAffinity(el, d) {
1261 d3.selectAll('.onosInst')
1262 .classed('mastership', true)
1263 .classed('affinity', false);
1264 el.classed('affinity', true);
1265
1266 suppressLayers(true);
1267 node.each(function (n) {
1268 if (n.master === d.id) {
1269 n.el.classed('suppressed', false);
1270 }
1271 });
1272 oiShowMaster = true;
1273 }
1274
1275 function cancelAffinity() {
1276 d3.selectAll('.onosInst')
1277 .classed('mastership affinity', false);
1278 restoreLayerState();
1279 oiShowMaster = false;
1280 }
1281
Simon Hunt7b403bc2014-11-22 19:01:00 -08001282 // TODO: these should be moved out to utility module.
1283 function stripPx(s) {
1284 return s.replace(/px$/,'');
1285 }
1286
1287 function appendUse(svg, ox, oy, dim, iid, cls) {
1288 var use = svg.append('use').attr({
1289 transform: translate(ox,oy),
1290 'xlink:href': iid,
1291 width: dim,
1292 height: dim
1293 });
1294 if (cls) {
1295 use.classed(cls, true);
1296 }
1297 return use;
1298 }
1299
1300 function appendGlyph(svg, ox, oy, dim, iid, cls) {
1301 appendUse(svg, ox, oy, dim, iid, cls).classed('glyphIcon', true);
1302 }
1303
1304 function appendBadge(svg, ox, oy, dim, iid, cls) {
1305 appendUse(svg, ox, oy, dim, iid,cls ).classed('badgeIcon', true);
1306 }
1307
1308 function attachUiBadge(svg) {
1309 appendBadge(svg, 12, instCfg.uiDy, 30, '#uiAttached', 'uiBadge');
1310 }
1311
Simon Hunt434cf142014-11-24 11:10:28 -08001312 function visVal(b) {
1313 return b ? 'visible' : 'hidden';
1314 }
1315
Simon Hunta5e89142014-11-14 07:00:33 -08001316 // ==============================
Simon Hunt99c13842014-11-06 18:23:12 -08001317 // force layout modification functions
1318
1319 function translate(x, y) {
1320 return 'translate(' + x + ',' + y + ')';
1321 }
1322
Simon Hunte2575b62014-11-18 15:25:53 -08001323 function rotate(deg) {
1324 return 'rotate(' + deg + ')';
1325 }
1326
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001327 function missMsg(what, id) {
1328 return '\n[' + what + '] "' + id + '" missing ';
1329 }
1330
1331 function linkEndPoints(srcId, dstId) {
1332 var srcNode = network.lookup[srcId],
1333 dstNode = network.lookup[dstId],
1334 sMiss = !srcNode ? missMsg('src', srcId) : '',
1335 dMiss = !dstNode ? missMsg('dst', dstId) : '';
1336
1337 if (sMiss || dMiss) {
1338 logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
1339 return null;
1340 }
1341 return {
1342 source: srcNode,
1343 target: dstNode,
1344 x1: srcNode.x,
1345 y1: srcNode.y,
1346 x2: dstNode.x,
1347 y2: dstNode.y
1348 };
1349 }
1350
Simon Hunt56d51852014-11-09 13:03:35 -08001351 function createHostLink(host) {
1352 var src = host.id,
1353 dst = host.cp.device,
Simon Hunt7cd48f32014-11-09 23:42:50 -08001354 id = host.ingress,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001355 lnk = linkEndPoints(src, dst);
Simon Hunt56d51852014-11-09 13:03:35 -08001356
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001357 if (!lnk) {
Simon Hunt56d51852014-11-09 13:03:35 -08001358 return null;
1359 }
1360
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001361 // Synthesize link ...
1362 $.extend(lnk, {
Simon Hunt8257f4c2014-11-16 19:34:54 -08001363 key: id,
Simon Hunt56d51852014-11-09 13:03:35 -08001364 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001365
1366 type: function () { return 'hostLink'; },
Simon Hunt6d9bd032014-11-28 22:16:40 -08001367 online: function () {
1368 // hostlink target is edge switch
1369 return lnk.target.online;
1370 },
Simon Hunt8257f4c2014-11-16 19:34:54 -08001371 linkWidth: function () { return 1; }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001372 });
Simon Hunt99c13842014-11-06 18:23:12 -08001373 return lnk;
1374 }
1375
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001376 function createLink(link) {
Simon Hunta6a9fe72014-11-20 11:17:12 -08001377 var lnk = linkEndPoints(link.src, link.dst);
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001378
1379 if (!lnk) {
1380 return null;
1381 }
1382
Simon Hunt8257f4c2014-11-16 19:34:54 -08001383 $.extend(lnk, {
1384 key: link.id,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001385 class: 'link',
Simon Hunt8257f4c2014-11-16 19:34:54 -08001386 fromSource: link,
1387
1388 // functions to aggregate dual link state
1389 type: function () {
1390 var s = lnk.fromSource,
1391 t = lnk.fromTarget;
1392 return (s && s.type) || (t && t.type) || defaultLinkType;
1393 },
1394 online: function () {
1395 var s = lnk.fromSource,
Simon Hunt6d9bd032014-11-28 22:16:40 -08001396 t = lnk.fromTarget,
1397 both = lnk.source.online && lnk.target.online;
1398 return both && ((s && s.online) || (t && t.online));
Simon Hunt8257f4c2014-11-16 19:34:54 -08001399 },
1400 linkWidth: function () {
1401 var s = lnk.fromSource,
1402 t = lnk.fromTarget,
1403 ws = (s && s.linkWidth) || 0,
1404 wt = (t && t.linkWidth) || 0;
1405 return Math.max(ws, wt);
1406 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001407 });
1408 return lnk;
Simon Hunt1a9eff92014-11-07 11:06:34 -08001409 }
1410
Simon Hunte2575b62014-11-18 15:25:53 -08001411 function removeLinkLabels() {
1412 network.links.forEach(function (d) {
1413 d.label = '';
1414 });
1415 }
1416
Simon Hunt434cf142014-11-24 11:10:28 -08001417 function showHostVis(el) {
1418 el.style('visibility', visVal(showHosts));
1419 }
1420
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001421 var widthRatio = 1.4,
1422 linkScale = d3.scale.linear()
1423 .domain([1, 12])
1424 .range([widthRatio, 12 * widthRatio])
1425 .clamp(true);
1426
Simon Hunt99c13842014-11-06 18:23:12 -08001427 function updateLinks() {
1428 link = linkG.selectAll('.link')
Simon Hunt8257f4c2014-11-16 19:34:54 -08001429 .data(network.links, function (d) { return d.key; });
Simon Hunt99c13842014-11-06 18:23:12 -08001430
1431 // operate on existing links, if necessary
1432 // link .foo() .bar() ...
1433
1434 // operate on entering links:
1435 var entering = link.enter()
1436 .append('line')
1437 .attr({
Simon Hunt99c13842014-11-06 18:23:12 -08001438 x1: function (d) { return d.x1; },
1439 y1: function (d) { return d.y1; },
1440 x2: function (d) { return d.x2; },
1441 y2: function (d) { return d.y2; },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001442 stroke: config.topo.linkInColor,
1443 'stroke-width': config.topo.linkInWidth
Simon Hunt99c13842014-11-06 18:23:12 -08001444 });
1445
1446 // augment links
Simon Hunt7cd48f32014-11-09 23:42:50 -08001447 entering.each(function (d) {
1448 var link = d3.select(this);
1449 // provide ref to element selection from backing data....
1450 d.el = link;
Simon Hunt8257f4c2014-11-16 19:34:54 -08001451 restyleLinkElement(d);
Simon Hunt434cf142014-11-24 11:10:28 -08001452 if (d.type() === 'hostLink') {
1453 showHostVis(link);
1454 }
Simon Hunt7cd48f32014-11-09 23:42:50 -08001455 });
Thomas Vachuska4830d392014-11-09 17:09:56 -08001456
1457 // operate on both existing and new links, if necessary
1458 //link .foo() .bar() ...
1459
Simon Hunte2575b62014-11-18 15:25:53 -08001460 // apply or remove labels
1461 var labelData = getLabelData();
1462 applyLinkLabels(labelData);
1463
Thomas Vachuska4830d392014-11-09 17:09:56 -08001464 // operate on exiting links:
Thomas Vachuska4830d392014-11-09 17:09:56 -08001465 link.exit()
Simon Hunt6d9bd032014-11-28 22:16:40 -08001466 .attr('stroke-dasharray', '3 3')
Simon Hunt13bf9c82014-11-18 07:26:44 -08001467 .style('opacity', 0.5)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001468 .transition()
Simon Huntea80eb42014-11-11 13:46:57 -08001469 .duration(1500)
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001470 .attr({
Simon Hunt6d9bd032014-11-28 22:16:40 -08001471 'stroke-dasharray': '3 12',
Simon Hunt13bf9c82014-11-18 07:26:44 -08001472 stroke: config.topo.linkOutColor,
1473 'stroke-width': config.topo.linkOutWidth
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001474 })
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001475 .style('opacity', 0.0)
Thomas Vachuska4830d392014-11-09 17:09:56 -08001476 .remove();
Simon Hunte2575b62014-11-18 15:25:53 -08001477
1478 // NOTE: invoke a single tick to force the labels to position
1479 // onto their links.
1480 tick();
1481 }
1482
1483 function getLabelData() {
1484 // create the backing data for showing labels..
1485 var data = [];
1486 link.each(function (d) {
1487 if (d.label) {
1488 data.push({
1489 id: 'lab-' + d.key,
1490 key: d.key,
1491 label: d.label,
1492 ldata: d
1493 });
1494 }
1495 });
1496 return data;
1497 }
1498
1499 var linkLabelOffset = '0.3em';
1500
1501 function applyLinkLabels(data) {
1502 var entering;
1503
1504 linkLabel = linkLabelG.selectAll('.linkLabel')
1505 .data(data, function (d) { return d.id; });
1506
Simon Hunt56a2ea42014-11-19 12:39:31 -08001507 // for elements already existing, we need to update the text
1508 // and adjust the rectangle size to fit
1509 linkLabel.each(function (d) {
1510 var el = d3.select(this),
1511 rect = el.select('rect'),
1512 text = el.select('text');
1513 text.text(d.label);
1514 rect.attr(rectAroundText(el));
1515 });
Thomas Vachuskaf75b7ab2014-11-19 12:15:55 -08001516
Simon Hunte2575b62014-11-18 15:25:53 -08001517 entering = linkLabel.enter().append('g')
1518 .classed('linkLabel', true)
1519 .attr('id', function (d) { return d.id; });
1520
1521 entering.each(function (d) {
1522 var el = d3.select(this),
1523 rect,
1524 text,
1525 parms = {
1526 x1: d.ldata.x1,
1527 y1: d.ldata.y1,
1528 x2: d.ldata.x2,
1529 y2: d.ldata.y2
1530 };
1531
1532 d.el = el;
1533 rect = el.append('rect');
1534 text = el.append('text').text(d.label);
1535 rect.attr(rectAroundText(el));
1536 text.attr('dy', linkLabelOffset);
1537
1538 el.attr('transform', transformLabel(parms));
1539 });
1540
1541 // Remove any links that are no longer required.
1542 linkLabel.exit().remove();
1543 }
1544
1545 function rectAroundText(el) {
1546 var text = el.select('text'),
1547 box = text.node().getBBox();
1548
1549 // translate the bbox so that it is centered on [x,y]
1550 box.x = -box.width / 2;
1551 box.y = -box.height / 2;
1552
1553 // add padding
1554 box.x -= 1;
1555 box.width += 2;
1556 return box;
1557 }
1558
1559 function transformLabel(p) {
1560 var dx = p.x2 - p.x1,
1561 dy = p.y2 - p.y1,
1562 xMid = dx/2 + p.x1,
1563 yMid = dy/2 + p.y1;
Simon Hunte2575b62014-11-18 15:25:53 -08001564 return translate(xMid, yMid);
Simon Hunt99c13842014-11-06 18:23:12 -08001565 }
1566
1567 function createDeviceNode(device) {
1568 // start with the object as is
1569 var node = device,
Simon Huntbb282f52014-11-10 11:08:19 -08001570 type = device.type,
Simon Huntc72967b2014-11-20 09:21:42 -08001571 svgCls = type ? 'node device ' + type : 'node device',
1572 labels = device.labels || [];
1573
Simon Hunt99c13842014-11-06 18:23:12 -08001574 // Augment as needed...
1575 node.class = 'device';
Simon Huntbb282f52014-11-10 11:08:19 -08001576 node.svgClass = device.online ? svgCls + ' online' : svgCls;
Simon Hunt99c13842014-11-06 18:23:12 -08001577 positionNode(node);
Simon Hunt99c13842014-11-06 18:23:12 -08001578 return node;
1579 }
1580
Simon Hunt56d51852014-11-09 13:03:35 -08001581 function createHostNode(host) {
1582 // start with the object as is
1583 var node = host;
1584
1585 // Augment as needed...
1586 node.class = 'host';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001587 if (!node.type) {
Simon Hunt209155e2014-11-21 12:16:09 -08001588 node.type = 'endstation';
Simon Hunt7cd48f32014-11-09 23:42:50 -08001589 }
Simon Hunt7fa116d2014-11-17 14:16:55 -08001590 node.svgClass = 'node host ' + node.type;
Simon Hunt56d51852014-11-09 13:03:35 -08001591 positionNode(node);
Simon Hunt56d51852014-11-09 13:03:35 -08001592 return node;
1593 }
1594
Simon Hunt62c47542014-11-22 22:16:32 -08001595 function positionNode(node, forUpdate) {
Simon Hunt99c13842014-11-06 18:23:12 -08001596 var meta = node.metaUi,
Simon Huntac9e24f2014-11-12 10:12:21 -08001597 x = meta && meta.x,
1598 y = meta && meta.y,
1599 xy;
Simon Hunt99c13842014-11-06 18:23:12 -08001600
Simon Huntac9e24f2014-11-12 10:12:21 -08001601 // If we have [x,y] already, use that...
Simon Hunt99c13842014-11-06 18:23:12 -08001602 if (x && y) {
1603 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001604 node.px = node.x = x;
1605 node.py = node.y = y;
Simon Huntac9e24f2014-11-12 10:12:21 -08001606 return;
Simon Hunt99c13842014-11-06 18:23:12 -08001607 }
Simon Huntac9e24f2014-11-12 10:12:21 -08001608
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001609 var location = node.location;
1610 if (location && location.type === 'latlng') {
Simon Hunt1b18aa52014-11-29 17:57:55 -08001611 var coord = geoMapProj([location.lng, location.lat]);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001612 node.fixed = true;
Simon Hunt62c47542014-11-22 22:16:32 -08001613 node.px = node.x = coord[0];
1614 node.py = node.y = coord[1];
Simon Hunt62c47542014-11-22 22:16:32 -08001615 return true;
1616 }
1617
1618 // if this is a node update (not a node add).. skip randomizer
1619 if (forUpdate) {
Paul Greyson6cb8ca02014-11-12 18:09:02 -08001620 return;
1621 }
1622
Simon Huntac9e24f2014-11-12 10:12:21 -08001623 // Note: Placing incoming unpinned nodes at exactly the same point
1624 // (center of the view) causes them to explode outwards when
1625 // the force layout kicks in. So, we spread them out a bit
1626 // initially, to provide a more serene layout convergence.
1627 // Additionally, if the node is a host, we place it near
1628 // the device it is connected to.
1629
1630 function spread(s) {
1631 return Math.floor((Math.random() * s) - s/2);
1632 }
1633
1634 function randDim(dim) {
1635 return dim / 2 + spread(dim * 0.7071);
1636 }
1637
1638 function rand() {
1639 return {
1640 x: randDim(network.view.width()),
1641 y: randDim(network.view.height())
1642 };
1643 }
1644
1645 function near(node) {
1646 var min = 12,
1647 dx = spread(12),
1648 dy = spread(12);
1649 return {
1650 x: node.x + min + dx,
1651 y: node.y + min + dy
1652 };
1653 }
1654
1655 function getDevice(cp) {
1656 var d = network.lookup[cp.device];
1657 return d || rand();
1658 }
1659
1660 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
1661 $.extend(node, xy);
Simon Hunt99c13842014-11-06 18:23:12 -08001662 }
1663
Simon Hunt99c13842014-11-06 18:23:12 -08001664
Simon Huntc72967b2014-11-20 09:21:42 -08001665 function iconGlyphUrl(d) {
1666 var which = d.type || 'unknown';
1667 return '#' + which;
1668 }
1669
Simon Hunt99c13842014-11-06 18:23:12 -08001670 // returns the newly computed bounding box of the rectangle
1671 function adjustRectToFitText(n) {
1672 var text = n.select('text'),
1673 box = text.node().getBBox(),
1674 lab = config.labels;
1675
1676 text.attr('text-anchor', 'middle')
1677 .attr('y', '-0.8em')
1678 .attr('x', lab.imgPad/2);
1679
1680 // translate the bbox so that it is centered on [x,y]
1681 box.x = -box.width / 2;
1682 box.y = -box.height / 2;
1683
1684 // add padding
1685 box.x -= (lab.padLR + lab.imgPad/2);
1686 box.width += lab.padLR * 2 + lab.imgPad;
1687 box.y -= lab.padTB;
1688 box.height += lab.padTB * 2;
1689
1690 return box;
1691 }
1692
Simon Hunt1a9eff92014-11-07 11:06:34 -08001693 function mkSvgClass(d) {
1694 return d.fixed ? d.svgClass + ' fixed' : d.svgClass;
1695 }
1696
Simon Hunt7cd48f32014-11-09 23:42:50 -08001697 function hostLabel(d) {
1698 var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0;
1699 return d.labels[idx];
1700 }
1701 function deviceLabel(d) {
1702 var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0;
1703 return d.labels[idx];
1704 }
Simon Huntc72967b2014-11-20 09:21:42 -08001705 function trimLabel(label) {
1706 return (label && label.trim()) || '';
1707 }
1708
1709 function emptyBox() {
1710 return {
1711 x: -2,
1712 y: -2,
1713 width: 4,
1714 height: 4
1715 };
Simon Hunt7cd48f32014-11-09 23:42:50 -08001716 }
1717
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001718 function updateDeviceLabel(d) {
Simon Huntc72967b2014-11-20 09:21:42 -08001719 var label = trimLabel(deviceLabel(d)),
1720 noLabel = !label,
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001721 node = d.el,
Simon Huntc72967b2014-11-20 09:21:42 -08001722 box,
1723 dx,
Simon Hunt395a70c2014-11-22 23:17:40 -08001724 dy,
1725 cfg = config.icons.device;
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001726
1727 node.select('text')
1728 .text(label)
1729 .style('opacity', 0)
1730 .transition()
1731 .style('opacity', 1);
1732
Simon Huntc72967b2014-11-20 09:21:42 -08001733 if (noLabel) {
1734 box = emptyBox();
Simon Hunt395a70c2014-11-22 23:17:40 -08001735 dx = -cfg.dim/2;
1736 dy = -cfg.dim/2;
Simon Huntc72967b2014-11-20 09:21:42 -08001737 } else {
1738 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08001739 dx = box.x + cfg.xoff;
1740 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08001741 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001742
1743 node.select('rect')
1744 .transition()
1745 .attr(box);
1746
Simon Huntc72967b2014-11-20 09:21:42 -08001747 node.select('g.deviceIcon')
Thomas Vachuska89543292014-11-19 11:28:33 -08001748 .transition()
Simon Huntc72967b2014-11-20 09:21:42 -08001749 .attr('transform', translate(dx, dy));
Simon Hunt3f03d4a2014-11-10 20:14:37 -08001750 }
1751
1752 function updateHostLabel(d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001753 var label = trimLabel(hostLabel(d));
1754 d.el.select('text').text(label);
Simon Huntbb282f52014-11-10 11:08:19 -08001755 }
1756
Simon Hunt434cf142014-11-24 11:10:28 -08001757 function updateHostVisibility() {
1758 var v = visVal(showHosts);
1759 nodeG.selectAll('.host').style('visibility', v);
1760 linkG.selectAll('.hostLink').style('visibility', v);
1761 }
1762
Simon Hunt6d9bd032014-11-28 22:16:40 -08001763 function findOfflineNodes() {
1764 var a = [];
1765 network.nodes.forEach(function (d) {
1766 if (d.class === 'device' && !d.online) {
1767 a.push(d);
1768 }
1769 });
1770 return a;
1771 }
1772
1773 function updateOfflineVisibility(dev) {
1774 var so = showOffline,
1775 sh = showHosts,
1776 vb = 'visibility',
1777 v, off, al, ah, db, b;
1778
1779 function updAtt(show) {
1780 al.forEach(function (d) {
1781 b = show && ((d.type() !== 'hostLink') || sh);
1782 d.el.style(vb, visVal(b));
1783 });
1784 ah.forEach(function (d) {
1785 b = show && sh;
1786 d.el.style(vb, visVal(b));
1787 });
1788 }
1789
1790 if (dev) {
1791 // updating a specific device that just toggled off/on-line
1792 db = dev.online || so;
1793 dev.el.style(vb, visVal(db));
1794 al = findAttachedLinks(dev.id);
1795 ah = findAttachedHosts(dev.id);
1796 updAtt(db);
1797 } else {
1798 // updating all offline devices
1799 v = visVal(so);
1800 off = findOfflineNodes();
1801 off.forEach(function (d) {
1802 d.el.style(vb, v);
1803 al = findAttachedLinks(d.id);
1804 ah = findAttachedHosts(d.id);
1805 updAtt(so);
1806 });
1807 }
1808 }
1809
Simon Hunt6ac93f32014-11-13 12:17:27 -08001810 function nodeMouseOver(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001811 hovered = d;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001812 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001813 }
1814
1815 function nodeMouseOut(d) {
Simon Hunt6ac93f32014-11-13 12:17:27 -08001816 hovered = null;
Thomas Vachuska9edca302014-11-22 17:06:42 -08001817 requestTrafficForMode();
Simon Hunt6ac93f32014-11-13 12:17:27 -08001818 }
Simon Huntbb282f52014-11-10 11:08:19 -08001819
Simon Hunteb1514d2014-11-20 09:57:29 -08001820 function addHostIcon(node, radius, iid) {
Thomas Vachuska89543292014-11-19 11:28:33 -08001821 var dim = radius * 1.5,
1822 xlate = -dim / 2;
1823
Simon Hunteb1514d2014-11-20 09:57:29 -08001824 node.append('use').attr({
1825 class: 'glyphIcon hostIcon',
1826 transform: translate(xlate,xlate),
1827 'xlink:href': iid,
1828 width: dim,
1829 height: dim
1830 });
Thomas Vachuska89543292014-11-19 11:28:33 -08001831 }
1832
Simon Hunt99c13842014-11-06 18:23:12 -08001833 function updateNodes() {
1834 node = nodeG.selectAll('.node')
1835 .data(network.nodes, function (d) { return d.id; });
1836
Simon Hunt62c47542014-11-22 22:16:32 -08001837 // operate on existing nodes...
1838 node.filter('.device').each(function (d) {
Simon Hunt62c47542014-11-22 22:16:32 -08001839 var node = d.el;
1840 node.classed('online', d.online);
1841 updateDeviceLabel(d);
1842 positionNode(d, true);
1843 });
1844
1845 node.filter('.host').each(function (d) {
Simon Hunt6d9bd032014-11-28 22:16:40 -08001846 updateHostLabel(d);
1847 positionNode(d, true);
Simon Hunt62c47542014-11-22 22:16:32 -08001848 });
Simon Hunt99c13842014-11-06 18:23:12 -08001849
1850 // operate on entering nodes:
1851 var entering = node.enter()
1852 .append('g')
1853 .attr({
1854 id: function (d) { return safeId(d.id); },
Simon Hunt1a9eff92014-11-07 11:06:34 -08001855 class: mkSvgClass,
Simon Hunt99c13842014-11-06 18:23:12 -08001856 transform: function (d) { return translate(d.x, d.y); },
1857 opacity: 0
1858 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08001859 .call(network.drag)
Simon Hunt6ac93f32014-11-13 12:17:27 -08001860 .on('mouseover', nodeMouseOver)
1861 .on('mouseout', nodeMouseOut)
Simon Hunt99c13842014-11-06 18:23:12 -08001862 .transition()
1863 .attr('opacity', 1);
1864
1865 // augment device nodes...
1866 entering.filter('.device').each(function (d) {
1867 var node = d3.select(this),
Simon Huntc72967b2014-11-20 09:21:42 -08001868 label = trimLabel(deviceLabel(d)),
1869 noLabel = !label,
Simon Hunt99c13842014-11-06 18:23:12 -08001870 box;
1871
Simon Hunt7cd48f32014-11-09 23:42:50 -08001872 // provide ref to element from backing data....
1873 d.el = node;
1874
Simon Hunt62c47542014-11-22 22:16:32 -08001875 node.append('rect').attr({ rx: 5, ry: 5 });
1876 node.append('text').text(label).attr('dy', '1.1em');
Simon Hunt99c13842014-11-06 18:23:12 -08001877 box = adjustRectToFitText(node);
Simon Hunta3dd9572014-11-20 15:22:41 -08001878 node.select('rect').attr(box);
Simon Huntc72967b2014-11-20 09:21:42 -08001879 addDeviceIcon(node, box, noLabel, iconGlyphUrl(d));
Simon Huntc7ee0662014-11-05 16:44:37 -08001880 });
Simon Hunt934c3ce2014-11-05 11:45:07 -08001881
Thomas Vachuska89543292014-11-19 11:28:33 -08001882
Simon Hunt56d51852014-11-09 13:03:35 -08001883 // augment host nodes...
1884 entering.filter('.host').each(function (d) {
1885 var node = d3.select(this),
Simon Hunt395a70c2014-11-22 23:17:40 -08001886 cfg = config.icons.host,
1887 r = cfg.radius[d.type] || cfg.defaultRadius,
Thomas Vachuska89543292014-11-19 11:28:33 -08001888 textDy = r + 10,
Simon Hunteb1514d2014-11-20 09:57:29 -08001889 iid = iconGlyphUrl(d);
Simon Hunt56d51852014-11-09 13:03:35 -08001890
Simon Hunt7cd48f32014-11-09 23:42:50 -08001891 // provide ref to element from backing data....
1892 d.el = node;
Simon Hunt434cf142014-11-24 11:10:28 -08001893 showHostVis(node);
Simon Hunt7cd48f32014-11-09 23:42:50 -08001894
Simon Hunt62c47542014-11-22 22:16:32 -08001895 node.append('circle').attr('r', r);
Simon Hunteb1514d2014-11-20 09:57:29 -08001896 if (iid) {
1897 addHostIcon(node, r, iid);
Simon Hunt7fa116d2014-11-17 14:16:55 -08001898 }
Simon Hunt56d51852014-11-09 13:03:35 -08001899 node.append('text')
Simon Hunt7cd48f32014-11-09 23:42:50 -08001900 .text(hostLabel)
Thomas Vachuska89543292014-11-19 11:28:33 -08001901 .attr('dy', textDy)
Simon Hunt7cd48f32014-11-09 23:42:50 -08001902 .attr('text-anchor', 'middle');
Simon Hunt56d51852014-11-09 13:03:35 -08001903 });
Simon Huntc7ee0662014-11-05 16:44:37 -08001904
Simon Hunt99c13842014-11-06 18:23:12 -08001905 // operate on both existing and new nodes, if necessary
Simon Huntb0ecfa52014-11-23 21:05:12 -08001906 updateDeviceColors();
Simon Huntc7ee0662014-11-05 16:44:37 -08001907
Simon Hunt99c13842014-11-06 18:23:12 -08001908 // operate on exiting nodes:
Simon Huntea80eb42014-11-11 13:46:57 -08001909 // Note that the node is removed after 2 seconds.
1910 // Sub element animations should be shorter than 2 seconds.
1911 var exiting = node.exit()
Simon Hunt44031102014-11-11 13:20:36 -08001912 .transition()
1913 .duration(2000)
Simon Huntea80eb42014-11-11 13:46:57 -08001914 .style('opacity', 0)
Simon Hunt99c13842014-11-06 18:23:12 -08001915 .remove();
Simon Huntea80eb42014-11-11 13:46:57 -08001916
1917 // host node exits....
1918 exiting.filter('.host').each(function (d) {
Simon Huntca867ac2014-11-28 18:07:35 -08001919 var node = d.el;
1920 node.select('use')
1921 .style('opacity', 0.5)
1922 .transition()
1923 .duration(800)
1924 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001925
1926 node.select('text')
1927 .style('opacity', 0.5)
1928 .transition()
Simon Huntca867ac2014-11-28 18:07:35 -08001929 .duration(800)
Simon Huntea80eb42014-11-11 13:46:57 -08001930 .style('opacity', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001931
Thomas Vachuska89543292014-11-19 11:28:33 -08001932 node.select('circle')
1933 .style('stroke-fill', '#555')
1934 .style('fill', '#888')
Simon Huntea80eb42014-11-11 13:46:57 -08001935 .style('opacity', 0.5)
1936 .transition()
1937 .duration(1500)
1938 .attr('r', 0);
Simon Huntea80eb42014-11-11 13:46:57 -08001939 });
1940
Simon Huntca867ac2014-11-28 18:07:35 -08001941 // device node exits....
1942 exiting.filter('.device').each(function (d) {
1943 var node = d.el;
1944 node.select('use')
1945 .style('opacity', 0.5)
1946 .transition()
1947 .duration(800)
1948 .style('opacity', 0);
1949
1950 node.selectAll('rect')
1951 .style('stroke-fill', '#555')
1952 .style('fill', '#888')
1953 .style('opacity', 0.5);
1954 });
Simon Hunt62c47542014-11-22 22:16:32 -08001955
1956 network.force.resume();
Simon Huntc7ee0662014-11-05 16:44:37 -08001957 }
1958
Simon Hunt95dad922014-11-24 09:43:31 -08001959 var dCol = {
1960 black: '#000',
1961 paleblue: '#acf',
1962 offwhite: '#ddd',
1963 midgrey: '#888',
1964 lightgrey: '#bbb',
1965 orange: '#f90'
1966 };
1967
Simon Huntb0ecfa52014-11-23 21:05:12 -08001968 // note: these are the device icon colors without affinity
Simon Hunt95dad922014-11-24 09:43:31 -08001969 var dColTheme = {
Simon Huntb0ecfa52014-11-23 21:05:12 -08001970 light: {
1971 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001972 glyph: dCol.black,
1973 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001974 },
1975 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001976 glyph: dCol.midgrey,
1977 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001978 }
1979 },
Simon Hunt95dad922014-11-24 09:43:31 -08001980 // TODO: theme
Simon Huntb0ecfa52014-11-23 21:05:12 -08001981 dark: {
1982 online: {
Simon Hunt95dad922014-11-24 09:43:31 -08001983 glyph: dCol.black,
1984 rect: dCol.paleblue
Simon Huntb0ecfa52014-11-23 21:05:12 -08001985 },
1986 offline: {
Simon Hunt95dad922014-11-24 09:43:31 -08001987 glyph: dCol.midgrey,
1988 rect: dCol.lightgrey
Simon Huntb0ecfa52014-11-23 21:05:12 -08001989 }
1990 }
1991 };
1992
1993 function devBaseColor(d) {
1994 var t = network.view.getTheme(),
1995 o = d.online ? 'online' : 'offline';
Simon Hunt95dad922014-11-24 09:43:31 -08001996 return dColTheme[t][o];
Simon Huntb0ecfa52014-11-23 21:05:12 -08001997 }
1998
1999 function setDeviceColor(d) {
2000 var o = d.online,
2001 s = d.el.classed('selected'),
2002 c = devBaseColor(d),
2003 a = instColor(d.master, o),
2004 g, r,
2005 icon = d.el.select('g.deviceIcon');
2006
2007 if (s) {
2008 g = c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002009 r = dColTheme.sel;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002010 } else if (colorAffinity) {
2011 g = o ? a : c.glyph;
Simon Hunt95dad922014-11-24 09:43:31 -08002012 r = o ? dCol.offwhite : a;
Simon Huntb0ecfa52014-11-23 21:05:12 -08002013 } else {
2014 g = c.glyph;
2015 r = c.rect;
2016 }
2017
2018 icon.select('use')
2019 .style('fill', g);
2020 icon.select('rect')
2021 .style('fill', r);
2022 }
2023
Simon Huntc72967b2014-11-20 09:21:42 -08002024 function addDeviceIcon(node, box, noLabel, iid) {
2025 var cfg = config.icons.device,
2026 dx,
2027 dy,
2028 g;
2029
2030 if (noLabel) {
2031 box = emptyBox();
2032 dx = -cfg.dim/2;
2033 dy = -cfg.dim/2;
2034 } else {
2035 box = adjustRectToFitText(node);
Simon Hunt395a70c2014-11-22 23:17:40 -08002036 dx = box.x + cfg.xoff;
2037 dy = box.y + cfg.yoff;
Simon Huntc72967b2014-11-20 09:21:42 -08002038 }
2039
Simon Hunteb1514d2014-11-20 09:57:29 -08002040 g = node.append('g')
2041 .attr('class', 'glyphIcon deviceIcon')
Simon Huntc72967b2014-11-20 09:21:42 -08002042 .attr('transform', translate(dx, dy));
2043
2044 g.append('rect').attr({
2045 x: 0,
2046 y: 0,
2047 rx: cfg.rx,
2048 width: cfg.dim,
2049 height: cfg.dim
2050 });
2051
2052 g.append('use').attr({
2053 'xlink:href': iid,
2054 width: cfg.dim,
2055 height: cfg.dim
2056 });
2057
Simon Huntc72967b2014-11-20 09:21:42 -08002058 }
2059
Simon Hunt7b403bc2014-11-22 19:01:00 -08002060 function find(key, array, tag) {
Simon Huntca867ac2014-11-28 18:07:35 -08002061 var _tag = tag || 'id',
Simon Hunt7b403bc2014-11-22 19:01:00 -08002062 idx, n, d;
2063 for (idx = 0, n = array.length; idx < n; idx++) {
2064 d = array[idx];
2065 if (d[_tag] === key) {
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002066 return idx;
2067 }
2068 }
2069 return -1;
2070 }
2071
Simon Huntca867ac2014-11-28 18:07:35 -08002072 function removeLinkElement(d) {
2073 var idx = find(d.key, network.links, 'key'),
Simon Hunt8257f4c2014-11-16 19:34:54 -08002074 removed;
2075 if (idx >=0) {
2076 // remove from links array
2077 removed = network.links.splice(idx, 1);
2078 // remove from lookup cache
2079 delete network.lookup[removed[0].key];
2080 updateLinks();
2081 network.force.resume();
2082 }
Simon Hunt3f03d4a2014-11-10 20:14:37 -08002083 }
Simon Huntc7ee0662014-11-05 16:44:37 -08002084
Simon Huntca867ac2014-11-28 18:07:35 -08002085 function removeHostElement(d, upd) {
2086 var lu = network.lookup;
Simon Hunt44031102014-11-11 13:20:36 -08002087 // first, remove associated hostLink...
Simon Huntca867ac2014-11-28 18:07:35 -08002088 removeLinkElement(d.linkData);
2089
2090 // remove hostLink bindings
2091 delete lu[d.ingress];
2092 delete lu[d.egress];
Simon Hunt44031102014-11-11 13:20:36 -08002093
2094 // remove from lookup cache
Simon Huntca867ac2014-11-28 18:07:35 -08002095 delete lu[d.id];
Simon Hunt44031102014-11-11 13:20:36 -08002096 // remove from nodes array
Simon Huntca867ac2014-11-28 18:07:35 -08002097 var idx = find(d.id, network.nodes);
2098 network.nodes.splice(idx, 1);
2099 // remove from SVG
2100 // NOTE: upd is false if we were called from removeDeviceElement()
2101 if (upd) {
2102 updateNodes();
2103 network.force.resume();
2104 }
2105 }
2106
2107
2108 function removeDeviceElement(d) {
2109 var id = d.id;
2110 // first, remove associated hosts and links..
2111 findAttachedHosts(id).forEach(removeHostElement);
2112 findAttachedLinks(id).forEach(removeLinkElement);
2113
2114 // remove from lookup cache
2115 delete network.lookup[id];
2116 // remove from nodes array
2117 var idx = find(id, network.nodes);
Simon Hunt44031102014-11-11 13:20:36 -08002118 network.nodes.splice(idx, 1);
2119 // remove from SVG
2120 updateNodes();
2121 network.force.resume();
2122 }
2123
Simon Huntca867ac2014-11-28 18:07:35 -08002124 function findAttachedHosts(devId) {
2125 var hosts = [];
2126 network.nodes.forEach(function (d) {
2127 if (d.class === 'host' && d.cp.device === devId) {
2128 hosts.push(d);
2129 }
2130 });
2131 return hosts;
2132 }
2133
2134 function findAttachedLinks(devId) {
2135 var links = [];
2136 network.links.forEach(function (d) {
2137 if (d.source.id === devId || d.target.id === devId) {
2138 links.push(d);
2139 }
2140 });
2141 return links;
2142 }
Simon Hunt44031102014-11-11 13:20:36 -08002143
Simon Huntc7ee0662014-11-05 16:44:37 -08002144 function tick() {
2145 node.attr({
Simon Hunt99c13842014-11-06 18:23:12 -08002146 transform: function (d) { return translate(d.x, d.y); }
Simon Huntc7ee0662014-11-05 16:44:37 -08002147 });
2148
2149 link.attr({
2150 x1: function (d) { return d.source.x; },
2151 y1: function (d) { return d.source.y; },
2152 x2: function (d) { return d.target.x; },
2153 y2: function (d) { return d.target.y; }
2154 });
Simon Hunte2575b62014-11-18 15:25:53 -08002155
2156 linkLabel.each(function (d) {
2157 var el = d3.select(this);
Thomas Vachuska4731f122014-11-20 04:56:19 -08002158 var lnk = findLinkById(d.key);
2159
2160 if (lnk) {
2161 var parms = {
Simon Hunte2575b62014-11-18 15:25:53 -08002162 x1: lnk.source.x,
2163 y1: lnk.source.y,
2164 x2: lnk.target.x,
2165 y2: lnk.target.y
2166 };
Thomas Vachuska4731f122014-11-20 04:56:19 -08002167 el.attr('transform', transformLabel(parms));
2168 }
Simon Hunte2575b62014-11-18 15:25:53 -08002169 });
Simon Huntc7ee0662014-11-05 16:44:37 -08002170 }
Simon Hunt934c3ce2014-11-05 11:45:07 -08002171
2172 // ==============================
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002173 // Web-Socket for live data
2174
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002175 function findGuiSuccessor() {
2176 var idx = -1;
2177 onosOrder.forEach(function (d, i) {
2178 if (d.uiAttached) {
2179 idx = i;
2180 }
2181 });
2182
2183 for (var i = 0; i < onosOrder.length - 1; i++) {
2184 var ni = (idx + 1 + i) % onosOrder.length;
2185 if (onosOrder[ni].online) {
2186 return onosOrder[ni].ip;
2187 }
2188 }
2189 return null;
2190 }
2191
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002192 function webSockUrl() {
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002193 var url = document.location.toString()
2194 .replace(/\#.*/, '')
2195 .replace('http://', 'ws://')
2196 .replace('https://', 'wss://')
2197 .replace('index.html', config.webSockUrl);
2198 if (guiSuccessor) {
2199 url = url.replace(location.hostname, guiSuccessor);
2200 }
2201 return url;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002202 }
2203
2204 webSock = {
2205 ws : null,
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002206 retries: 0,
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002207
2208 connect : function() {
2209 webSock.ws = new WebSocket(webSockUrl());
2210
2211 webSock.ws.onopen = function() {
Simon Hunt0c6d4192014-11-12 12:07:10 -08002212 noWebSock(false);
Thomas Vachuska47635c62014-11-22 01:21:36 -08002213 requestSummary();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002214 showInstances();
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002215 webSock.retries = 0;
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002216 };
2217
2218 webSock.ws.onmessage = function(m) {
2219 if (m.data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002220 wsTraceRx(m.data);
Thomas Vachuskad472c6e2014-11-07 19:11:05 -08002221 handleServerEvent(JSON.parse(m.data));
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002222 }
2223 };
2224
2225 webSock.ws.onclose = function(m) {
2226 webSock.ws = null;
Thomas Vachuska12dfdc32014-11-29 16:03:12 -08002227 guiSuccessor = findGuiSuccessor();
2228 if (guiSuccessor && webSock.retries < onosOrder.length) {
2229 webSock.retries++;
2230 webSock.connect();
2231 } else {
2232 noWebSock(true);
2233 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002234 };
2235 },
2236
2237 send : function(text) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002238 if (text != null) {
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002239 webSock._send(text);
2240 }
2241 },
2242
2243 _send : function(message) {
2244 if (webSock.ws) {
2245 webSock.ws.send(message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002246 } else if (config.useLiveData) {
Simon Hunt434cf142014-11-24 11:10:28 -08002247 console.warn('no web socket open', message);
Simon Hunta255a2c2014-11-13 22:29:35 -08002248 } else {
Simon Hunt434cf142014-11-24 11:10:28 -08002249 console.log('WS Send: ', message);
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002250 }
2251 }
2252
2253 };
2254
Simon Hunt0c6d4192014-11-12 12:07:10 -08002255 function noWebSock(b) {
2256 mask.style('display',b ? 'block' : 'none');
2257 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002258
2259 function sendMessage(evType, payload) {
Simon Huntc1cc81c2014-11-29 14:59:01 -08002260 var p = payload || {},
2261 toSend = {
Simon Huntbb282f52014-11-10 11:08:19 -08002262 event: evType,
2263 sid: ++sid,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002264 payload: p
Simon Huntbb282f52014-11-10 11:08:19 -08002265 },
2266 asText = JSON.stringify(toSend);
2267 wsTraceTx(asText);
2268 webSock.send(asText);
Simon Huntc76ae892014-11-18 17:31:51 -08002269
2270 // Temporary measure for debugging UI behavior ...
2271 if (!config.useLiveData) {
2272 handleTestSend(toSend);
2273 }
Simon Huntbb282f52014-11-10 11:08:19 -08002274 }
2275
2276 function wsTraceTx(msg) {
2277 wsTrace('tx', msg);
2278 }
2279 function wsTraceRx(msg) {
2280 wsTrace('rx', msg);
2281 }
2282 function wsTrace(rxtx, msg) {
Simon Huntbb282f52014-11-10 11:08:19 -08002283 console.log('[' + rxtx + '] ' + msg);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002284 }
2285
Simon Huntc76ae892014-11-18 17:31:51 -08002286 // NOTE: Temporary hardcoded example for showing detail pane
2287 // while we fine-
2288 // Probably should not merge this change...
2289 function handleTestSend(msg) {
2290 if (msg.event === 'requestDetails') {
2291 showDetails({
2292 event: 'showDetails',
2293 sid: 1001,
2294 payload: {
2295 "id": "of:0000ffffffffff09",
2296 "type": "roadm",
2297 "propOrder": [
2298 "Name",
2299 "Vendor",
2300 "H/W Version",
2301 "S/W Version",
2302 "-",
2303 "Latitude",
2304 "Longitude",
2305 "Ports"
2306 ],
2307 "props": {
2308 "Name": null,
2309 "Vendor": "Linc",
2310 "H/W Version": "OE",
2311 "S/W Version": "?",
2312 "-": "",
2313 "Latitude": "40.8",
2314 "Longitude": "73.1",
2315 "Ports": "2"
2316 }
2317 }
2318 });
2319 }
2320 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002321
2322 // ==============================
2323 // Selection stuff
2324
2325 function selectObject(obj, el) {
2326 var n,
Simon Hunt01095ff2014-11-13 16:37:29 -08002327 srcEv = d3.event.sourceEvent,
2328 meta = srcEv.metaKey,
2329 shift = srcEv.shiftKey;
2330
Simon Hunt3c5ca542014-11-29 14:11:43 -08002331 // if the meta key is pressed, we are panning/zooming, so ignore
2332 if (meta) {
Simon Hunt01095ff2014-11-13 16:37:29 -08002333 return;
2334 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002335
2336 if (el) {
2337 n = d3.select(el);
2338 } else {
2339 node.each(function(d) {
2340 if (d == obj) {
2341 n = d3.select(el = this);
2342 }
2343 });
2344 }
2345 if (!n) return;
2346
Simon Hunt01095ff2014-11-13 16:37:29 -08002347 if (shift && n.classed('selected')) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002348 deselectObject(obj.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002349 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002350 return;
2351 }
2352
Simon Hunt01095ff2014-11-13 16:37:29 -08002353 if (!shift) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002354 deselectAll();
2355 }
2356
Simon Huntc31d5692014-11-12 13:27:18 -08002357 selections[obj.id] = { obj: obj, el: el };
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002358 selectOrder.push(obj.id);
2359
2360 n.classed('selected', true);
Simon Huntb0ecfa52014-11-23 21:05:12 -08002361 updateDeviceColors(obj);
Simon Hunt61d04042014-11-11 17:27:16 -08002362 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002363 }
2364
2365 function deselectObject(id) {
Simon Huntc31d5692014-11-12 13:27:18 -08002366 var obj = selections[id],
2367 idx;
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002368 if (obj) {
2369 d3.select(obj.el).classed('selected', false);
Simon Hunt61d04042014-11-11 17:27:16 -08002370 delete selections[id];
Simon Huntc31d5692014-11-12 13:27:18 -08002371 idx = $.inArray(id, selectOrder);
2372 if (idx >= 0) {
2373 selectOrder.splice(idx, 1);
2374 }
Simon Huntb0ecfa52014-11-23 21:05:12 -08002375 updateDeviceColors(obj.obj);
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002376 }
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002377 }
2378
2379 function deselectAll() {
2380 // deselect all nodes in the network...
2381 node.classed('selected', false);
2382 selections = {};
2383 selectOrder = [];
Simon Huntb0ecfa52014-11-23 21:05:12 -08002384 updateDeviceColors();
Simon Hunt61d04042014-11-11 17:27:16 -08002385 updateDetailPane();
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002386 }
2387
Simon Huntb0ecfa52014-11-23 21:05:12 -08002388 function updateDeviceColors(d) {
2389 if (d) {
2390 setDeviceColor(d);
2391 } else {
2392 node.filter('.device').each(function (d) {
2393 setDeviceColor(d);
2394 });
2395 }
Thomas Vachuska47635c62014-11-22 01:21:36 -08002396 }
2397
Simon Hunt61d04042014-11-11 17:27:16 -08002398 // update the state of the detail pane, based on current selections
2399 function updateDetailPane() {
2400 var nSel = selectOrder.length;
2401 if (!nSel) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002402 emptySelect();
Simon Hunt61d04042014-11-11 17:27:16 -08002403 } else if (nSel === 1) {
2404 singleSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002405 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002406 } else {
2407 multiSelect();
2408 }
2409 }
2410
Thomas Vachuska9edca302014-11-22 17:06:42 -08002411 function emptySelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002412 haveDetails = false;
Simon Hunt06811b72014-11-25 18:54:48 -08002413 hideDetailPane();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002414 cancelTraffic();
2415 }
2416
Simon Hunt61d04042014-11-11 17:27:16 -08002417 function singleSelect() {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002418 // NOTE: detail is shown from showDetails event callback
Simon Hunt61d04042014-11-11 17:27:16 -08002419 requestDetails();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002420 requestTrafficForMode();
Simon Hunt61d04042014-11-11 17:27:16 -08002421 }
2422
2423 function multiSelect() {
Simon Hunt27d322d2014-11-28 10:45:43 -08002424 haveDetails = true;
Simon Huntb53e0682014-11-12 13:32:01 -08002425 populateMultiSelect();
Thomas Vachuska9edca302014-11-22 17:06:42 -08002426 requestTrafficForMode();
Simon Huntb53e0682014-11-12 13:32:01 -08002427 }
2428
2429 function addSep(tbody) {
2430 var tr = tbody.append('tr');
2431 $('<hr>').appendTo(tr.append('td').attr('colspan', 2));
2432 }
2433
2434 function addProp(tbody, label, value) {
2435 var tr = tbody.append('tr');
2436
2437 tr.append('td')
2438 .attr('class', 'label')
2439 .text(label + ' :');
2440
2441 tr.append('td')
2442 .attr('class', 'value')
2443 .text(value);
2444 }
2445
2446 function populateMultiSelect() {
2447 detailPane.empty();
2448
Simon Hunta3dd9572014-11-20 15:22:41 -08002449 var title = detailPane.append('h3'),
2450 table = detailPane.append('table'),
2451 tbody = table.append('tbody');
Simon Huntb53e0682014-11-12 13:32:01 -08002452
Thomas Vachuska4731f122014-11-20 04:56:19 -08002453 title.text('Selected Nodes');
Simon Huntb53e0682014-11-12 13:32:01 -08002454
2455 selectOrder.forEach(function (d, i) {
2456 addProp(tbody, i+1, d);
2457 });
Simon Huntd72bc702014-11-13 18:38:04 -08002458
2459 addMultiSelectActions();
Simon Hunt61d04042014-11-11 17:27:16 -08002460 }
2461
Thomas Vachuska47635c62014-11-22 01:21:36 -08002462 // TODO: refactor to consolidate with populateDetails
2463 function populateSummary(data) {
2464 summaryPane.empty();
2465
2466 var svg = summaryPane.append('svg'),
2467 iid = iconGlyphUrl(data);
2468
2469 var title = summaryPane.append('h2'),
2470 table = summaryPane.append('table'),
2471 tbody = table.append('tbody');
2472
2473 appendGlyph(svg, 0, 0, 40, iid);
2474
2475 svg.append('use')
2476 .attr({
2477 class: 'birdBadge',
2478 transform: translate(8,12),
2479 'xlink:href': '#bird',
2480 width: 24,
2481 height: 24,
2482 fill: '#fff'
2483 });
2484
2485 title.text('ONOS Summary');
2486
2487 data.propOrder.forEach(function(p) {
2488 if (p === '-') {
2489 addSep(tbody);
2490 } else {
2491 addProp(tbody, p, data.props[p]);
2492 }
2493 });
2494 }
2495
Simon Hunt61d04042014-11-11 17:27:16 -08002496 function populateDetails(data) {
2497 detailPane.empty();
2498
Simon Hunta6a9fe72014-11-20 11:17:12 -08002499 var svg = detailPane.append('svg'),
2500 iid = iconGlyphUrl(data);
2501
Simon Hunta3dd9572014-11-20 15:22:41 -08002502 var title = detailPane.append('h2'),
2503 table = detailPane.append('table'),
2504 tbody = table.append('tbody');
Simon Hunt61d04042014-11-11 17:27:16 -08002505
Simon Hunta6a9fe72014-11-20 11:17:12 -08002506 appendGlyph(svg, 0, 0, 40, iid);
2507 title.text(data.id);
Simon Hunt61d04042014-11-11 17:27:16 -08002508
2509 data.propOrder.forEach(function(p) {
2510 if (p === '-') {
2511 addSep(tbody);
2512 } else {
2513 addProp(tbody, p, data.props[p]);
2514 }
2515 });
Simon Huntd72bc702014-11-13 18:38:04 -08002516
Thomas Vachuska4731f122014-11-20 04:56:19 -08002517 addSingleSelectActions(data);
Simon Hunt61d04042014-11-11 17:27:16 -08002518 }
2519
Thomas Vachuska4731f122014-11-20 04:56:19 -08002520 function addSingleSelectActions(data) {
Simon Huntd72bc702014-11-13 18:38:04 -08002521 detailPane.append('hr');
2522 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002523 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
2524
2525 if (data.type === 'switch') {
2526 addAction(detailPane, 'Show Device Flows', showDeviceLinkFlowsAction);
2527 }
Simon Huntd72bc702014-11-13 18:38:04 -08002528 }
2529
2530 function addMultiSelectActions() {
2531 detailPane.append('hr');
2532 // always want to allow 'show traffic'
Thomas Vachuska4731f122014-11-20 04:56:19 -08002533 addAction(detailPane, 'Show Related Traffic', showTrafficAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002534 // if exactly two hosts are selected, also want 'add host intent'
2535 if (nSel() === 2 && allSelectionsClass('host')) {
Thomas Vachuska9edca302014-11-22 17:06:42 -08002536 addAction(detailPane, 'Create Host-to-Host Flow', addHostIntentAction);
2537 } else if (nSel() >= 2 && allSelectionsClass('host')) {
2538 addAction(detailPane, 'Create Multi-Source Flow', addMultiSourceIntentAction);
Simon Huntd72bc702014-11-13 18:38:04 -08002539 }
2540 }
2541
Simon Hunta5e89142014-11-14 07:00:33 -08002542 function addAction(panel, text, cb) {
2543 panel.append('div')
Simon Huntd72bc702014-11-13 18:38:04 -08002544 .classed('actionBtn', true)
2545 .text(text)
2546 .on('click', cb);
2547 }
2548
2549
Simon Hunt3c5ca542014-11-29 14:11:43 -08002550 // === Pan and Zoom behaviors...
2551
2552 function panZoom(translate, scale) {
2553 panZoomContainer.attr('transform',
2554 'translate(' + translate + ')scale(' + scale + ')');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002555 // keep the map lines constant width while zooming
Simon Hunt3c5ca542014-11-29 14:11:43 -08002556 bgImg.style('stroke-width', 2.0 / scale + 'px');
Paul Greysonfcba0e82014-11-13 10:21:16 -08002557 }
2558
Simon Hunt3c5ca542014-11-29 14:11:43 -08002559 function resetPanZoom() {
2560 panZoom([0,0], 1);
2561 zoom.translate([0,0]).scale(1);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002562 }
2563
Simon Hunt3c5ca542014-11-29 14:11:43 -08002564 function setupPanZoom() {
Paul Greysonfcba0e82014-11-13 10:21:16 -08002565 function zoomed() {
Simon Hunt3c5ca542014-11-29 14:11:43 -08002566 // pan zoom active when meta key is pressed...
2567 if (d3.event.sourceEvent.metaKey) {
2568 panZoom(d3.event.translate, d3.event.scale);
Paul Greysonfcba0e82014-11-13 10:21:16 -08002569 }
2570 }
2571
2572 zoom = d3.behavior.zoom()
2573 .translate([0, 0])
2574 .scale(1)
Simon Hunt1b18aa52014-11-29 17:57:55 -08002575 .scaleExtent([0.25, 10])
Paul Greysonfcba0e82014-11-13 10:21:16 -08002576 .on("zoom", zoomed);
2577
2578 svg.call(zoom);
2579 }
2580
Simon Hunt61d04042014-11-11 17:27:16 -08002581 // ==============================
2582 // Test harness code
Simon Hunt56d51852014-11-09 13:03:35 -08002583
2584 function prepareScenario(view, ctx, dbg) {
2585 var sc = scenario,
2586 urlSc = sc.evDir + ctx + sc.evScenario;
2587
2588 if (!ctx) {
2589 view.alert("No scenario specified (null ctx)");
2590 return;
2591 }
2592
2593 sc.view = view;
2594 sc.ctx = ctx;
2595 sc.debug = dbg;
2596 sc.evNumber = 0;
2597
2598 d3.json(urlSc, function(err, data) {
Simon Huntbb282f52014-11-10 11:08:19 -08002599 var p = data && data.params || {},
2600 desc = data && data.description || null,
Simon Huntfc274c92014-11-11 11:05:46 -08002601 intro = data && data.title;
Simon Huntbb282f52014-11-10 11:08:19 -08002602
Simon Hunt56d51852014-11-09 13:03:35 -08002603 if (err) {
2604 view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err);
2605 } else {
2606 sc.params = p;
Simon Huntbb282f52014-11-10 11:08:19 -08002607 if (desc) {
2608 intro += '\n\n ' + desc.join('\n ');
2609 }
2610 view.alert(intro);
Simon Hunt56d51852014-11-09 13:03:35 -08002611 }
2612 });
2613
2614 }
2615
Simon Hunt7fa116d2014-11-17 14:16:55 -08002616 function loadGlyphs(svg) {
2617 var defs = svg.append('defs');
2618 gly.defBird(defs);
Simon Huntc72967b2014-11-20 09:21:42 -08002619 gly.defGlyphs(defs);
Simon Huntb82f6902014-11-22 11:53:15 -08002620 gly.defBadges(defs);
Simon Hunt7fa116d2014-11-17 14:16:55 -08002621 }
Simon Hunt01095ff2014-11-13 16:37:29 -08002622
Simon Hunt395a70c2014-11-22 23:17:40 -08002623 function sendUpdateMeta(d, store) {
2624 var metaUi = {},
2625 ll;
2626
2627 if (store) {
Simon Hunt1b18aa52014-11-29 17:57:55 -08002628 ll = geoMapProj.invert([d.x, d.y]);
Simon Hunt62c47542014-11-22 22:16:32 -08002629 metaUi = {
2630 x: d.x,
2631 y: d.y,
2632 lng: ll[0],
2633 lat: ll[1]
2634 };
Simon Hunt395a70c2014-11-22 23:17:40 -08002635 }
Simon Hunt62c47542014-11-22 22:16:32 -08002636 d.metaUi = metaUi;
2637 sendMessage('updateMeta', {
2638 id: d.id,
2639 'class': d.class,
Simon Huntc1cc81c2014-11-29 14:59:01 -08002640 memento: metaUi
Simon Hunt62c47542014-11-22 22:16:32 -08002641 });
2642 }
2643
Thomas Vachuska7d638d32014-11-07 10:24:43 -08002644 // ==============================
Simon Hunt142d0032014-11-04 20:13:09 -08002645 // View life-cycle callbacks
Simon Hunt195cb382014-11-03 17:50:51 -08002646
Simon Huntf67722a2014-11-10 09:32:06 -08002647 function preload(view, ctx, flags) {
Simon Hunt142d0032014-11-04 20:13:09 -08002648 var w = view.width(),
2649 h = view.height(),
Simon Hunt1b18aa52014-11-29 17:57:55 -08002650 fcfg = config.force;
Simon Hunt195cb382014-11-03 17:50:51 -08002651
Simon Hunt142d0032014-11-04 20:13:09 -08002652 // NOTE: view.$div is a D3 selection of the view's div
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002653 var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize;
2654 svg = view.$div.append('svg').attr('viewBox', viewBox);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002655 setSize(svg, view);
2656
Simon Hunt6e18fe32014-11-29 13:35:41 -08002657 // load glyphs and filters...
Simon Hunt7fa116d2014-11-17 14:16:55 -08002658 loadGlyphs(svg);
Simon Hunt6e18fe32014-11-29 13:35:41 -08002659 d3u.appendGlow(svg);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002660
Simon Hunt3c5ca542014-11-29 14:11:43 -08002661 panZoomContainer = svg.append('g').attr('id', 'panZoomContainer');
2662 setupPanZoom();
Paul Greysonfcba0e82014-11-13 10:21:16 -08002663
Simon Huntc7ee0662014-11-05 16:44:37 -08002664 // group for the topology
Simon Hunt3c5ca542014-11-29 14:11:43 -08002665 topoG = panZoomContainer.append('g')
Simon Hunt1b18aa52014-11-29 17:57:55 -08002666 .attr('id', 'topo-G');
Simon Huntc7ee0662014-11-05 16:44:37 -08002667
Simon Hunte2575b62014-11-18 15:25:53 -08002668 // subgroups for links, link labels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002669 linkG = topoG.append('g').attr('id', 'links');
Simon Hunte2575b62014-11-18 15:25:53 -08002670 linkLabelG = topoG.append('g').attr('id', 'linkLabels');
Simon Huntc7ee0662014-11-05 16:44:37 -08002671 nodeG = topoG.append('g').attr('id', 'nodes');
2672
Simon Hunte2575b62014-11-18 15:25:53 -08002673 // selection of links, linkLabels, and nodes
Simon Huntc7ee0662014-11-05 16:44:37 -08002674 link = linkG.selectAll('.link');
Simon Hunte2575b62014-11-18 15:25:53 -08002675 linkLabel = linkLabelG.selectAll('.linkLabel');
Simon Huntc7ee0662014-11-05 16:44:37 -08002676 node = nodeG.selectAll('.node');
2677
Simon Hunt7cd48f32014-11-09 23:42:50 -08002678 function chrg(d) {
2679 return fcfg.charge[d.class] || -12000;
2680 }
Simon Hunt99c13842014-11-06 18:23:12 -08002681 function ldist(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002682 return fcfg.linkDistance[d.type] || 50;
Simon Hunt99c13842014-11-06 18:23:12 -08002683 }
2684 function lstrg(d) {
Simon Hunt7cd48f32014-11-09 23:42:50 -08002685 // 0.0 - 1.0
2686 return fcfg.linkStrength[d.type] || 1.0;
Simon Hunt99c13842014-11-06 18:23:12 -08002687 }
2688
Simon Hunt1a9eff92014-11-07 11:06:34 -08002689 function selectCb(d, self) {
Thomas Vachuskad1be50d2014-11-08 16:10:20 -08002690 selectObject(d, self);
Simon Hunt1a9eff92014-11-07 11:06:34 -08002691 }
2692
2693 function atDragEnd(d, self) {
Simon Hunt56d51852014-11-09 13:03:35 -08002694 // once we've finished moving, pin the node in position
2695 d.fixed = true;
2696 d3.select(self).classed('fixed', true);
2697 if (config.useLiveData) {
Simon Hunt395a70c2014-11-22 23:17:40 -08002698 sendUpdateMeta(d, true);
Simon Hunta255a2c2014-11-13 22:29:35 -08002699 } else {
2700 console.log('Moving node ' + d.id + ' to [' + d.x + ',' + d.y + ']');
Simon Hunt1a9eff92014-11-07 11:06:34 -08002701 }
2702 }
2703
Simon Hunt6e18fe32014-11-29 13:35:41 -08002704 // predicate that indicates when dragging is active
2705 function dragEnabled() {
2706 // meta key pressed means we are zooming/panning (so disable drag)
2707 return dragAllowed && !d3.event.sourceEvent.metaKey;
2708 // dragAllowed will be set false when we are in oblique view
2709 // or when we 'lock' node positions
2710 }
2711
Simon Huntc7ee0662014-11-05 16:44:37 -08002712 // set up the force layout
2713 network.force = d3.layout.force()
Simon Hunt1b18aa52014-11-29 17:57:55 -08002714 .size([w, h])
Simon Huntc7ee0662014-11-05 16:44:37 -08002715 .nodes(network.nodes)
2716 .links(network.links)
Simon Hunt7cd48f32014-11-09 23:42:50 -08002717 .gravity(0.4)
2718 .friction(0.7)
2719 .charge(chrg)
Simon Hunt99c13842014-11-06 18:23:12 -08002720 .linkDistance(ldist)
2721 .linkStrength(lstrg)
Simon Huntc7ee0662014-11-05 16:44:37 -08002722 .on('tick', tick);
Simon Hunt195cb382014-11-03 17:50:51 -08002723
Simon Hunt01095ff2014-11-13 16:37:29 -08002724 network.drag = d3u.createDragBehavior(network.force,
Simon Hunt6e18fe32014-11-29 13:35:41 -08002725 selectCb, atDragEnd, dragEnabled);
2726
Simon Hunt0c6d4192014-11-12 12:07:10 -08002727
2728 // create mask layer for when we lose connection to server.
Simon Hunta5e89142014-11-14 07:00:33 -08002729 // TODO: this should be part of the framework
Simon Hunt6e18fe32014-11-29 13:35:41 -08002730
2731 function para(sel, text) {
2732 sel.append('p').text(text);
2733 }
2734
Simon Hunt0c6d4192014-11-12 12:07:10 -08002735 mask = view.$div.append('div').attr('id','topo-mask');
2736 para(mask, 'Oops!');
2737 para(mask, 'Web-socket connection to server closed...');
2738 para(mask, 'Try refreshing the page.');
Simon Hunt12ce12e2014-11-15 21:13:19 -08002739
2740 mask.append('svg')
2741 .attr({
2742 id: 'mask-bird',
2743 width: w,
2744 height: h
2745 })
2746 .append('g')
2747 .attr('transform', birdTranslate(w, h))
2748 .style('opacity', 0.3)
2749 .append('use')
2750 .attr({
2751 'xlink:href': '#bird',
2752 width: config.birdDim,
2753 height: config.birdDim,
2754 fill: '#111'
Thomas Vachuska89543292014-11-19 11:28:33 -08002755 })
Simon Hunt1a9eff92014-11-07 11:06:34 -08002756 }
Simon Hunt195cb382014-11-03 17:50:51 -08002757
Simon Hunt01095ff2014-11-13 16:37:29 -08002758
Simon Hunt56d51852014-11-09 13:03:35 -08002759 function load(view, ctx, flags) {
Simon Huntf67722a2014-11-10 09:32:06 -08002760 // resize, in case the window was resized while we were not loaded
2761 resize(view, ctx, flags);
2762
Simon Hunt99c13842014-11-06 18:23:12 -08002763 // cache the view token, so network topo functions can access it
2764 network.view = view;
Simon Hunt56d51852014-11-09 13:03:35 -08002765 config.useLiveData = !flags.local;
2766
2767 if (!config.useLiveData) {
2768 prepareScenario(view, ctx, flags.debug);
2769 }
Simon Hunt99c13842014-11-06 18:23:12 -08002770
2771 // set our radio buttons and key bindings
Simon Hunt9462e8c2014-11-14 17:28:09 -08002772 layerBtnSet = view.setRadio(layerButtons);
Simon Hunt934c3ce2014-11-05 11:45:07 -08002773 view.setKeys(keyDispatch);
Simon Hunt87514342014-11-24 16:41:27 -08002774 view.setGestures(gestures);
Simon Hunt195cb382014-11-03 17:50:51 -08002775
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002776 // patch in our "button bar" for now
2777 // TODO: implement a more official frameworky way of doing this..
Simon Hunt87514342014-11-24 16:41:27 -08002778 //addButtonBar(view);
Simon Huntf8e5b4e02014-11-13 11:17:57 -08002779
Simon Huntd3b7d512014-11-12 15:48:41 -08002780 // Load map data asynchronously; complete startup after that..
2781 loadGeoJsonData();
Simon Hunta255a2c2014-11-13 22:29:35 -08002782 }
2783
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002784 function startAntTimer() {
Thomas Vachuska4731f122014-11-20 04:56:19 -08002785 if (!antTimer) {
2786 var pulses = [5, 3, 1.2, 3],
2787 pulse = 0;
2788 antTimer = setInterval(function () {
2789 pulse = pulse + 1;
2790 pulse = pulse === pulses.length ? 0 : pulse;
2791 d3.selectAll('.animated').style('stroke-width', pulses[pulse]);
2792 }, 200);
2793 }
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002794 }
2795
2796 function stopAntTimer() {
Simon Hunta255a2c2014-11-13 22:29:35 -08002797 if (antTimer) {
2798 clearInterval(antTimer);
2799 antTimer = null;
2800 }
Simon Huntd3b7d512014-11-12 15:48:41 -08002801 }
2802
Thomas Vachuskaa3148a72014-11-19 21:38:35 -08002803 function unload(view, ctx, flags) {
2804 stopAntTimer();
2805 }
2806
Simon Huntd3b7d512014-11-12 15:48:41 -08002807 // TODO: move these to config/state portion of script
Simon Hunta6a9fe72014-11-20 11:17:12 -08002808 var geoJsonUrl = 'json/map/continental_us.json',
Simon Huntd3b7d512014-11-12 15:48:41 -08002809 geoJson;
2810
2811 function loadGeoJsonData() {
2812 d3.json(geoJsonUrl, function (err, data) {
2813 if (err) {
2814 // fall back to USA map background
2815 loadStaticMap();
2816 } else {
2817 geoJson = data;
2818 loadGeoMap();
2819 }
2820
2821 // finally, connect to the server...
2822 if (config.useLiveData) {
2823 webSock.connect();
2824 }
2825 });
2826 }
2827
2828 function showBg() {
Simon Hunt434cf142014-11-24 11:10:28 -08002829 return visVal(config.options.showBackground);
Simon Huntd3b7d512014-11-12 15:48:41 -08002830 }
2831
2832 function loadStaticMap() {
2833 fnTrace('loadStaticMap', config.backgroundUrl);
2834 var w = network.view.width(),
2835 h = network.view.height();
2836
2837 // load the background image
2838 bgImg = svg.insert('svg:image', '#topo-G')
2839 .attr({
2840 id: 'topo-bg',
2841 width: w,
2842 height: h,
2843 'xlink:href': config.backgroundUrl
2844 })
2845 .style({
2846 visibility: showBg()
2847 });
2848 }
2849
Simon Hunt1b18aa52014-11-29 17:57:55 -08002850 function setProjForView(path, topoData) {
2851 var dim = config.logicalSize;
2852
2853 // start with unit scale, no translation..
2854 geoMapProj.scale(1).translate([0, 0]);
2855
2856 // figure out dimensions of map data..
2857 var b = path.bounds(topoData),
2858 x1 = b[0][0],
2859 y1 = b[0][1],
2860 x2 = b[1][0],
2861 y2 = b[1][1],
2862 dx = x2 - x1,
2863 dy = y2 - y1,
2864 x = (x1 + x2) / 2,
2865 y = (y1 + y2) / 2;
2866
2867 // size map to 95% of minimum dimension to fill space..
2868 var s = .95 / Math.min(dx / dim, dy / dim);
2869 var t = [dim / 2 - s * x, dim / 2 - s * y];
2870
2871 // set new scale, translation on the projection..
2872 geoMapProj.scale(s).translate(t);
2873 }
2874
Simon Huntd3b7d512014-11-12 15:48:41 -08002875 function loadGeoMap() {
2876 fnTrace('loadGeoMap', geoJsonUrl);
Simon Huntd3b7d512014-11-12 15:48:41 -08002877
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002878 // extracts the topojson data into geocoordinate-based geometry
2879 var topoData = topojson.feature(geoJson, geoJson.objects.states);
Simon Huntd3b7d512014-11-12 15:48:41 -08002880
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002881 // see: http://bl.ocks.org/mbostock/4707858
Simon Hunt1b18aa52014-11-29 17:57:55 -08002882 geoMapProj = d3.geo.mercator();
2883 var path = d3.geo.path().projection(geoMapProj);
Simon Huntd3b7d512014-11-12 15:48:41 -08002884
Simon Hunt1b18aa52014-11-29 17:57:55 -08002885 setProjForView(path, topoData);
Paul Greyson6cb8ca02014-11-12 18:09:02 -08002886
Simon Hunt3c5ca542014-11-29 14:11:43 -08002887 bgImg = panZoomContainer.insert("g", '#topo-G');
Thomas Vachuska89543292014-11-19 11:28:33 -08002888 bgImg.attr('id', 'map').selectAll('path')
2889 .data(topoData.features)
2890 .enter()
2891 .append('path')
2892 .attr('d', path);
Simon Hunt195cb382014-11-03 17:50:51 -08002893 }
2894
Simon Huntf67722a2014-11-10 09:32:06 -08002895 function resize(view, ctx, flags) {
Simon Hunt12ce12e2014-11-15 21:13:19 -08002896 var w = view.width(),
2897 h = view.height();
2898
Simon Hunt934c3ce2014-11-05 11:45:07 -08002899 setSize(svg, view);
Simon Hunt12ce12e2014-11-15 21:13:19 -08002900
2901 d3.select('#mask-bird').attr({ width: w, height: h})
2902 .select('g').attr('transform', birdTranslate(w, h));
Simon Hunt142d0032014-11-04 20:13:09 -08002903 }
2904
Simon Hunt8f40cce2014-11-23 15:57:30 -08002905 function theme(view, ctx, flags) {
2906 updateInstances();
Simon Huntb0ecfa52014-11-23 21:05:12 -08002907 updateDeviceColors();
Simon Hunt8f40cce2014-11-23 15:57:30 -08002908 }
2909
Simon Hunt12ce12e2014-11-15 21:13:19 -08002910 function birdTranslate(w, h) {
2911 var bdim = config.birdDim;
2912 return 'translate('+((w-bdim)*.4)+','+((h-bdim)*.1)+')';
2913 }
Simon Hunt142d0032014-11-04 20:13:09 -08002914
Simon Hunt06811b72014-11-25 18:54:48 -08002915 function isF(f) { return $.isFunction(f) ? f : null; }
2916 function noop() {}
2917
2918 function augmentDetailPane() {
2919 var dp = detailPane;
2920 dp.ypos = { up: 64, down: 320, current: 320};
2921
2922 dp._move = function (y, cb) {
2923 var endCb = isF(cb) || noop,
2924 yp = dp.ypos;
2925 if (yp.current !== y) {
2926 yp.current = y;
2927 dp.el.transition().duration(300)
2928 .each('end', endCb)
2929 .style('top', yp.current + 'px');
2930 } else {
2931 endCb();
2932 }
2933 };
2934
2935 dp.down = function (cb) { dp._move(dp.ypos.down, cb); };
2936 dp.up = function (cb) { dp._move(dp.ypos.up, cb); };
2937 }
2938
Simon Hunt142d0032014-11-04 20:13:09 -08002939 // ==============================
2940 // View registration
Simon Hunt195cb382014-11-03 17:50:51 -08002941
Simon Hunt25248912014-11-04 11:25:48 -08002942 onos.ui.addView('topo', {
Simon Hunt142d0032014-11-04 20:13:09 -08002943 preload: preload,
2944 load: load,
Simon Hunta255a2c2014-11-13 22:29:35 -08002945 unload: unload,
Simon Hunt8f40cce2014-11-23 15:57:30 -08002946 resize: resize,
2947 theme: theme
Simon Hunt195cb382014-11-03 17:50:51 -08002948 });
2949
Thomas Vachuska47635c62014-11-22 01:21:36 -08002950 summaryPane = onos.ui.addFloatingPanel('topo-summary');
Simon Hunt61d04042014-11-11 17:27:16 -08002951 detailPane = onos.ui.addFloatingPanel('topo-detail');
Simon Hunt06811b72014-11-25 18:54:48 -08002952 augmentDetailPane();
Simon Hunta5e89142014-11-14 07:00:33 -08002953 oiBox = onos.ui.addFloatingPanel('topo-oibox', 'TL');
Simon Huntb82f6902014-11-22 11:53:15 -08002954 oiBox.width(20);
Simon Hunt61d04042014-11-11 17:27:16 -08002955
Simon Hunt195cb382014-11-03 17:50:51 -08002956}(ONOS));