blob: 0ed2a7c977e9c4f9773bf3ca17a84c83bc61399e [file] [log] [blame]
Simon Huntef31fb22014-12-19 13:16:44 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Huntef31fb22014-12-19 13:16:44 -08003 *
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/*
18 ONOS GUI -- Topology View Module
Simon Huntef31fb22014-12-19 13:16:44 -080019 */
20
21(function () {
22 'use strict';
Simon Hunt6cc53692015-01-07 11:33:45 -080023
24 var moduleDependencies = [
25 'onosUtil',
Simon Hunt1e4a0012015-01-21 11:36:08 -080026 'onosSvg',
27 'onosRemote'
Simon Hunt6cc53692015-01-07 11:33:45 -080028 ];
29
30 // references to injected services etc.
Simon Hunt237676b52015-03-10 19:04:26 -070031 var $log, fs, ks, zs, gs, ms, sus, flash, wss,
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070032 tes, tfs, tps, tis, tss, tts, tos, ttbs;
Simon Hunt6cc53692015-01-07 11:33:45 -080033
34 // DOM elements
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080035 var ovtopo, svg, defs, zoomLayer, mapG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080036
37 // Internal state
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070038 var zoomer, actionMap;
Simon Hunt6cc53692015-01-07 11:33:45 -080039
Simon Huntcacce342015-01-07 16:13:05 -080040 // --- Short Cut Keys ------------------------------------------------
41
Simon Hunt5724fb42015-02-05 16:59:40 -080042 function setUpKeys() {
43 // key bindings need to be made after the services have been injected
44 // thus, deferred to here...
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070045 actionMap = {
Simon Hunt5724fb42015-02-05 16:59:40 -080046 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Huntda2f3cc2015-03-19 15:11:57 -070047 O: [tps.toggleSummary, 'Toggle ONOS summary pane'],
Simon Hunt6036b192015-02-11 11:20:26 -080048 D: [tss.toggleDetails, 'Disable / enable details pane'],
Simon Huntac4c6f72015-02-03 19:50:53 -080049
Simon Hunt5724fb42015-02-05 16:59:40 -080050 H: [tfs.toggleHosts, 'Toggle host visibility'],
51 M: [tfs.toggleOffline, 'Toggle offline visibility'],
Simon Hunt18bf9822015-02-12 17:35:45 -080052 B: [toggleMap, 'Toggle background map'],
Simon Hunt9e2104c2015-02-26 10:48:59 -080053 P: [tfs.togglePorts, 'Toggle Port Highlighting'],
Simon Huntac4c6f72015-02-03 19:50:53 -080054
Simon Hunt5724fb42015-02-05 16:59:40 -080055 //X: [toggleNodeLock, 'Lock / unlock node positions'],
Simon Hunt96f88c62015-02-19 17:57:25 -080056 Z: [tos.toggleOblique, 'Toggle oblique view (Experimental)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080057 L: [tfs.cycleDeviceLabels, 'Cycle device labels'],
Simon Hunt445e8152015-02-06 13:00:12 -080058 U: [tfs.unpin, 'Unpin node (hover mouse over)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080059 R: [resetZoom, 'Reset pan / zoom'],
Simon Huntac4c6f72015-02-03 19:50:53 -080060
Simon Huntf542d842015-02-11 16:20:33 -080061 V: [tts.showRelatedIntentsAction, 'Show all related intents'],
62 rightArrow: [tts.showNextIntentAction, 'Show next related intent'],
63 leftArrow: [tts.showPrevIntentAction, 'Show previous related intent'],
64 W: [tts.showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
65 A: [tts.showAllTrafficAction, 'Monitor all traffic'],
66 F: [tts.showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntac4c6f72015-02-03 19:50:53 -080067
Simon Hunt0e48c292015-02-19 16:11:37 -080068 E: [equalizeMasters, 'Equalize mastership roles'],
Simon Huntac4c6f72015-02-03 19:50:53 -080069
Simon Hunt08f841d02015-02-10 14:39:20 -080070 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -080071
Simon Hunt09060142015-03-18 20:23:32 -070072 _keyListener: ttbs.keyListener,
73
Simon Hunt5724fb42015-02-05 16:59:40 -080074 _helpFormat: [
Simon Huntda2f3cc2015-03-19 15:11:57 -070075 ['I', 'O', 'D', '-', 'H', 'M', 'P', 'B' ],
Simon Hunt5724fb42015-02-05 16:59:40 -080076 ['X', 'Z', 'L', 'U', 'R' ],
77 ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ]
78 ]
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -070079 };
80
81 ks.keyBindings(actionMap);
Simon Huntac4c6f72015-02-03 19:50:53 -080082
Simon Hunt639dc662015-02-18 14:19:20 -080083 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080084 ['click', 'Select the item and show details'],
85 ['shift-click', 'Toggle selection state'],
86 ['drag', 'Reposition (and pin) device / host'],
87 ['cmd-scroll', 'Zoom in / out'],
88 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -080089 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -080090 }
Simon Hunt6cc53692015-01-07 11:33:45 -080091
Simon Hunt08f841d02015-02-10 14:39:20 -080092 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -080093
Simon Hunt6036b192015-02-11 11:20:26 -080094 // NOTE: this really belongs in the TopoPanelService -- but how to
95 // cleanly link in the updateDeviceColors() call? To be fixed later.
Simon Huntac4c6f72015-02-03 19:50:53 -080096 function toggleInstances() {
Simon Hunt5724fb42015-02-05 16:59:40 -080097 tis.toggle();
Simon Huntac4c6f72015-02-03 19:50:53 -080098 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -080099 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800100
Simon Hunt18bf9822015-02-12 17:35:45 -0800101 function toggleMap() {
102 sus.visible(mapG, !sus.visible(mapG));
103 }
104
Simon Hunt08f841d02015-02-10 14:39:20 -0800105 function resetZoom() {
106 zoomer.reset();
107 }
108
Simon Hunt0e48c292015-02-19 16:11:37 -0800109 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700110 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800111 flash.flash('Equalizing master roles');
112 }
113
Simon Hunt08f841d02015-02-10 14:39:20 -0800114 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800115 if (tis.showMaster()) {
116 // if an instance is selected, cancel the affinity mapping
117 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800118
Simon Hunta0eb0a82015-02-11 12:30:06 -0800119 } else if (tss.haveDetails()) {
120 // else if we have node selections, deselect them all
121 tss.deselectAll();
Simon Hunt08f841d02015-02-10 14:39:20 -0800122
Simon Hunta0eb0a82015-02-11 12:30:06 -0800123 } else if (tis.isVisible()) {
124 // else if the Instance Panel is visible, hide it
125 tis.hide();
126 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800127
Simon Hunta0eb0a82015-02-11 12:30:06 -0800128 } else if (tps.summaryVisible()) {
129 // else if the Summary Panel is visible, hide it
130 tps.hideSummaryPanel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800131
Simon Hunta0eb0a82015-02-11 12:30:06 -0800132 } else {
133 // TODO: set hover mode to hoverModeNone
Simon Hunt62c2f8f2015-02-19 14:19:10 -0800134 // talk to Thomas about this: shouldn't it be done
135 // when we deselect the node (if tss.haveDetails()...)
Simon Hunta0eb0a82015-02-11 12:30:06 -0800136 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800137 }
Simon Huntcacce342015-01-07 16:13:05 -0800138
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700139 // --- Toolbar Functions ---------------------------------------------
140
141 function getActionEntry(key) {
142 var entry = actionMap[key];
143 return fs.isA(entry) || [entry, ''];
144 }
145
146 function setUpToolbar() {
147 ttbs.init({
148 getActionEntry: getActionEntry
149 });
150 ttbs.createToolbar();
151 }
152
Simon Huntcacce342015-01-07 16:13:05 -0800153 // --- Glyphs, Icons, and the like -----------------------------------
154
Simon Hunt6cc53692015-01-07 11:33:45 -0800155 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800156 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800157 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800158 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800159 }
160
161
Simon Huntcacce342015-01-07 16:13:05 -0800162 // --- Pan and Zoom --------------------------------------------------
163
164 // zoom enabled predicate. ev is a D3 source event.
165 function zoomEnabled(ev) {
166 return (ev.metaKey || ev.altKey);
167 }
168
169 function zoomCallback() {
Simon Hunt5724fb42015-02-05 16:59:40 -0800170 var sc = zoomer.scale();
Simon Huntcacce342015-01-07 16:13:05 -0800171
Simon Hunt0541fb82015-01-14 18:59:57 -0800172 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800173 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800174 }
175
176 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800177 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800178 zoomer = zs.createZoomer({
179 svg: svg,
180 zoomLayer: zoomLayer,
181 zoomEnabled: zoomEnabled,
182 zoomCallback: zoomCallback
183 });
184 }
185
186
Simon Hunt0541fb82015-01-14 18:59:57 -0800187 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800188 function svgResized(s) {
189 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800190 }
191
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800192 // --- Background Map ------------------------------------------------
193
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800194 function setUpNoDevs() {
195 var g, box;
196 noDevsLayer = svg.append('g').attr({
197 id: 'topo-noDevsLayer',
198 transform: sus.translate(500,500)
199 });
200 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
201 // We are translating this layer to have its origin at the center
202
203 g = noDevsLayer.append('g');
204 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
205 g.append('text').text('No devices are connected')
206 .attr({ x: 120, y: 80});
207
208 box = g.node().getBBox();
209 box.x -= box.width/2;
210 box.y -= box.height/2;
211 g.attr('transform', sus.translate(box.x, box.y));
212
213 showNoDevs(true);
214 }
215
216 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800217 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800218 }
219
Simon Hunt0541fb82015-01-14 18:59:57 -0800220 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800221 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Huntac4c6f72015-02-03 19:50:53 -0800222 // returns a promise for the projection...
223 return ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800224 }
225
Simon Huntc3c5b672015-02-20 11:32:13 -0800226 function opacifyMap(b) {
227 mapG.transition()
228 .duration(1000)
229 .attr('opacity', b ? 1 : 0);
230 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800231
Simon Huntcacce342015-01-07 16:13:05 -0800232 // --- Controller Definition -----------------------------------------
233
Simon Hunt6cc53692015-01-07 11:33:45 -0800234 angular.module('ovTopo', moduleDependencies)
Simon Hunt237676b52015-03-10 19:04:26 -0700235 .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800236 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800237 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunt237676b52015-03-10 19:04:26 -0700238 'WebSocketService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800239 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Huntf542d842015-02-11 16:20:33 -0800240 'TopoInstService', 'TopoSelectService', 'TopoTrafficService',
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700241 'TopoObliqueService', 'TopoToolbarService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800242
Simon Hunta11b4eb2015-01-28 16:20:50 -0800243 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Hunt237676b52015-03-10 19:04:26 -0700244 _ks_, _zs_, _gs_, _ms_, _sus_, _flash_, _wss_,
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700245 _tes_, _tfs_, _tps_, _tis_, _tss_, _tts_, _tos_, _ttbs_) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800246 var self = this,
Simon Hunt1894d792015-02-04 17:09:20 -0800247 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800248 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800249 uplink = {
250 // provides function calls back into this space
251 showNoDevs: showNoDevs,
252 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800253 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800254 zoomer: function () { return zoomer; },
Simon Hunt237676b52015-03-10 19:04:26 -0700255 opacifyMap: opacifyMap
Simon Huntac4c6f72015-02-03 19:50:53 -0800256 };
257
Simon Hunt6cc53692015-01-07 11:33:45 -0800258 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800259 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800260 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800261 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800262 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800263 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800264 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800265 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700266 wss = _wss_;
Simon Huntc252aa62015-02-10 16:45:39 -0800267 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800268 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800269 // TODO: consider funnelling actions through TopoForceService...
270 // rather than injecting references to these 'sub-modules',
271 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800272 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800273 tis = _tis_;
Simon Hunt6036b192015-02-11 11:20:26 -0800274 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800275 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800276 tos = _tos_;
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700277 ttbs = _ttbs_;
Simon Huntef31fb22014-12-19 13:16:44 -0800278
Simon Hunt426bd862015-01-14 16:48:41 -0800279 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800280 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800281 };
Simon Huntef31fb22014-12-19 13:16:44 -0800282
Simon Hunt54442fa2015-01-26 14:17:38 -0800283 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800284 $scope.$on('$destroy', function () {
285 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700286 tes.stop();
Simon Hunt626d2102015-01-29 11:54:50 -0800287 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800288 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800289 tfs.destroyForce();
Bri Prebilic Coled6219052015-03-19 14:34:02 -0700290 ttbs.destroyToolbar();
Simon Hunt584122a2015-01-21 15:32:40 -0800291 });
292
Simon Huntcacce342015-01-07 16:13:05 -0800293 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800294 ovtopo = d3.select('#ov-topo');
295 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800296 // set the svg size to match that of the window, less the masthead
297 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800298 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800299
Simon Hunt6cc53692015-01-07 11:33:45 -0800300 setUpKeys();
Bri Prebilic Cole4db8dce2015-03-18 13:57:24 -0700301 setUpToolbar();
Simon Hunt6cc53692015-01-07 11:33:45 -0800302 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800303 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800304 setUpNoDevs();
Simon Hunt1894d792015-02-04 17:09:20 -0800305 setUpMap().then(
306 function (proj) {
307 projection = proj;
308 $log.debug('** We installed the projection: ', proj);
309 }
310 );
Simon Huntfd1231a2015-01-26 22:14:51 -0800311
Simon Hunt1894d792015-02-04 17:09:20 -0800312 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800313 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800314 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700315 tps.initPanels();
Thomas Vachuska329af532015-03-10 02:08:33 -0700316 tes.start();
Simon Hunt6cc53692015-01-07 11:33:45 -0800317
318 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800319 }]);
320}());