blob: 2a6d037d41d3d27058227eeff1a4fceffc00e406 [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,
32 tes, tfs, tps, tis, tss, tts, tos;
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
Simon Huntedf5c0e2015-01-29 15:00:53 -080038 var zoomer;
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...
45 ks.keyBindings({
Simon Hunt6036b192015-02-11 11:20:26 -080046 O: [tps.toggleSummary, 'Toggle ONOS summary pane'],
Simon Hunt5724fb42015-02-05 16:59:40 -080047 I: [toggleInstances, 'Toggle ONOS instances 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 Hunt5724fb42015-02-05 16:59:40 -080072 _helpFormat: [
Simon Hunt9e2104c2015-02-26 10:48:59 -080073 ['O', 'I', 'D', '-', 'H', 'M', 'P', 'B' ],
Simon Hunt5724fb42015-02-05 16:59:40 -080074 ['X', 'Z', 'L', 'U', 'R' ],
75 ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ]
76 ]
77 });
Simon Huntac4c6f72015-02-03 19:50:53 -080078
Simon Hunt639dc662015-02-18 14:19:20 -080079 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080080 ['click', 'Select the item and show details'],
81 ['shift-click', 'Toggle selection state'],
82 ['drag', 'Reposition (and pin) device / host'],
83 ['cmd-scroll', 'Zoom in / out'],
84 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -080085 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -080086 }
Simon Hunt6cc53692015-01-07 11:33:45 -080087
Simon Hunt08f841d02015-02-10 14:39:20 -080088 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -080089
Simon Hunt6036b192015-02-11 11:20:26 -080090 // NOTE: this really belongs in the TopoPanelService -- but how to
91 // cleanly link in the updateDeviceColors() call? To be fixed later.
Simon Huntac4c6f72015-02-03 19:50:53 -080092 function toggleInstances() {
Simon Hunt5724fb42015-02-05 16:59:40 -080093 tis.toggle();
Simon Huntac4c6f72015-02-03 19:50:53 -080094 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -080095 }
Simon Huntac4c6f72015-02-03 19:50:53 -080096
Simon Hunt18bf9822015-02-12 17:35:45 -080097 function toggleMap() {
98 sus.visible(mapG, !sus.visible(mapG));
99 }
100
Simon Hunt08f841d02015-02-10 14:39:20 -0800101 function resetZoom() {
102 zoomer.reset();
103 }
104
Simon Hunt0e48c292015-02-19 16:11:37 -0800105 function equalizeMasters() {
Simon Hunt237676b52015-03-10 19:04:26 -0700106 wss.sendEvent('equalizeMasters');
Simon Hunt0e48c292015-02-19 16:11:37 -0800107 flash.flash('Equalizing master roles');
108 }
109
Simon Hunt08f841d02015-02-10 14:39:20 -0800110 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800111 if (tis.showMaster()) {
112 // if an instance is selected, cancel the affinity mapping
113 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800114
Simon Hunta0eb0a82015-02-11 12:30:06 -0800115 } else if (tss.haveDetails()) {
116 // else if we have node selections, deselect them all
117 tss.deselectAll();
Simon Hunt08f841d02015-02-10 14:39:20 -0800118
Simon Hunta0eb0a82015-02-11 12:30:06 -0800119 } else if (tis.isVisible()) {
120 // else if the Instance Panel is visible, hide it
121 tis.hide();
122 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800123
Simon Hunta0eb0a82015-02-11 12:30:06 -0800124 } else if (tps.summaryVisible()) {
125 // else if the Summary Panel is visible, hide it
126 tps.hideSummaryPanel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800127
Simon Hunta0eb0a82015-02-11 12:30:06 -0800128 } else {
129 // TODO: set hover mode to hoverModeNone
Simon Hunt62c2f8f2015-02-19 14:19:10 -0800130 // talk to Thomas about this: shouldn't it be done
131 // when we deselect the node (if tss.haveDetails()...)
Simon Hunta0eb0a82015-02-11 12:30:06 -0800132 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800133 }
Simon Huntcacce342015-01-07 16:13:05 -0800134
135 // --- Glyphs, Icons, and the like -----------------------------------
136
Simon Hunt6cc53692015-01-07 11:33:45 -0800137 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800138 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800139 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800140 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800141 }
142
143
Simon Huntcacce342015-01-07 16:13:05 -0800144 // --- Pan and Zoom --------------------------------------------------
145
146 // zoom enabled predicate. ev is a D3 source event.
147 function zoomEnabled(ev) {
148 return (ev.metaKey || ev.altKey);
149 }
150
151 function zoomCallback() {
Simon Hunt5724fb42015-02-05 16:59:40 -0800152 var sc = zoomer.scale();
Simon Huntcacce342015-01-07 16:13:05 -0800153
Simon Hunt0541fb82015-01-14 18:59:57 -0800154 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800155 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800156 }
157
158 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800159 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800160 zoomer = zs.createZoomer({
161 svg: svg,
162 zoomLayer: zoomLayer,
163 zoomEnabled: zoomEnabled,
164 zoomCallback: zoomCallback
165 });
166 }
167
168
Simon Hunt0541fb82015-01-14 18:59:57 -0800169 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800170 function svgResized(s) {
171 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800172 }
173
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800174 // --- Background Map ------------------------------------------------
175
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800176 function setUpNoDevs() {
177 var g, box;
178 noDevsLayer = svg.append('g').attr({
179 id: 'topo-noDevsLayer',
180 transform: sus.translate(500,500)
181 });
182 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
183 // We are translating this layer to have its origin at the center
184
185 g = noDevsLayer.append('g');
186 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
187 g.append('text').text('No devices are connected')
188 .attr({ x: 120, y: 80});
189
190 box = g.node().getBBox();
191 box.x -= box.width/2;
192 box.y -= box.height/2;
193 g.attr('transform', sus.translate(box.x, box.y));
194
195 showNoDevs(true);
196 }
197
198 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800199 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800200 }
201
Simon Hunt0541fb82015-01-14 18:59:57 -0800202 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800203 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Huntac4c6f72015-02-03 19:50:53 -0800204 // returns a promise for the projection...
205 return ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800206 }
207
Simon Huntc3c5b672015-02-20 11:32:13 -0800208 function opacifyMap(b) {
209 mapG.transition()
210 .duration(1000)
211 .attr('opacity', b ? 1 : 0);
212 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800213
Simon Huntcacce342015-01-07 16:13:05 -0800214 // --- Controller Definition -----------------------------------------
215
Simon Hunt6cc53692015-01-07 11:33:45 -0800216 angular.module('ovTopo', moduleDependencies)
Simon Hunt237676b52015-03-10 19:04:26 -0700217 .controller('OvTopoCtrl', ['$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800218 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt0e48c292015-02-19 16:11:37 -0800219 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService',
Simon Hunt237676b52015-03-10 19:04:26 -0700220 'WebSocketService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800221 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Huntf542d842015-02-11 16:20:33 -0800222 'TopoInstService', 'TopoSelectService', 'TopoTrafficService',
Simon Hunt96f88c62015-02-19 17:57:25 -0800223 'TopoObliqueService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800224
Simon Hunta11b4eb2015-01-28 16:20:50 -0800225 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Hunt237676b52015-03-10 19:04:26 -0700226 _ks_, _zs_, _gs_, _ms_, _sus_, _flash_, _wss_,
Simon Hunt96f88c62015-02-19 17:57:25 -0800227 _tes_, _tfs_, _tps_, _tis_, _tss_, _tts_, _tos_) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800228 var self = this,
Simon Hunt1894d792015-02-04 17:09:20 -0800229 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800230 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800231 uplink = {
232 // provides function calls back into this space
233 showNoDevs: showNoDevs,
234 projection: function () { return projection; },
Simon Huntc3c5b672015-02-20 11:32:13 -0800235 zoomLayer: function () { return zoomLayer; },
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800236 zoomer: function () { return zoomer; },
Simon Hunt237676b52015-03-10 19:04:26 -0700237 opacifyMap: opacifyMap
Simon Huntac4c6f72015-02-03 19:50:53 -0800238 };
239
Simon Hunt6cc53692015-01-07 11:33:45 -0800240 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800241 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800242 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800243 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800244 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800245 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800246 sus = _sus_;
Simon Hunt0e48c292015-02-19 16:11:37 -0800247 flash = _flash_;
Simon Hunt237676b52015-03-10 19:04:26 -0700248 wss = _wss_;
Simon Huntc252aa62015-02-10 16:45:39 -0800249 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800250 tfs = _tfs_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800251 // TODO: consider funnelling actions through TopoForceService...
252 // rather than injecting references to these 'sub-modules',
253 // just so we can invoke functions on them.
Simon Huntc252aa62015-02-10 16:45:39 -0800254 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800255 tis = _tis_;
Simon Hunt6036b192015-02-11 11:20:26 -0800256 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800257 tts = _tts_;
Simon Hunt96f88c62015-02-19 17:57:25 -0800258 tos = _tos_;
Simon Huntef31fb22014-12-19 13:16:44 -0800259
Simon Hunt426bd862015-01-14 16:48:41 -0800260 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800261 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800262 };
Simon Huntef31fb22014-12-19 13:16:44 -0800263
Simon Hunt54442fa2015-01-26 14:17:38 -0800264 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800265 $scope.$on('$destroy', function () {
266 $log.log('OvTopoCtrl is saying Buh-Bye!');
Thomas Vachuska329af532015-03-10 02:08:33 -0700267 tes.stop();
Simon Hunt626d2102015-01-29 11:54:50 -0800268 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800269 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800270 tfs.destroyForce();
Simon Hunt584122a2015-01-21 15:32:40 -0800271 });
272
Simon Huntcacce342015-01-07 16:13:05 -0800273 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800274 ovtopo = d3.select('#ov-topo');
275 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800276 // set the svg size to match that of the window, less the masthead
277 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800278 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800279
Simon Hunt6cc53692015-01-07 11:33:45 -0800280 setUpKeys();
281 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800282 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800283 setUpNoDevs();
Simon Hunt1894d792015-02-04 17:09:20 -0800284 setUpMap().then(
285 function (proj) {
286 projection = proj;
287 $log.debug('** We installed the projection: ', proj);
288 }
289 );
Simon Huntfd1231a2015-01-26 22:14:51 -0800290
Simon Hunt1894d792015-02-04 17:09:20 -0800291 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800292 tfs.initForce(svg, forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800293 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt237676b52015-03-10 19:04:26 -0700294 tps.initPanels();
Thomas Vachuska329af532015-03-10 02:08:33 -0700295 tes.start();
Simon Hunt6cc53692015-01-07 11:33:45 -0800296
297 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800298 }]);
299}());