blob: 777e8ede11dce34f9673f3879a404604a9354990 [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 Huntf542d842015-02-11 16:20:33 -080031 var $log, fs, ks, zs, gs, ms, sus, tes, tfs, tps, tis, tss, tts;
Simon Hunt6cc53692015-01-07 11:33:45 -080032
33 // DOM elements
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080034 var ovtopo, svg, defs, zoomLayer, mapG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080035
36 // Internal state
Simon Huntedf5c0e2015-01-29 15:00:53 -080037 var zoomer;
Simon Hunt6cc53692015-01-07 11:33:45 -080038
Simon Huntcacce342015-01-07 16:13:05 -080039 // --- Short Cut Keys ------------------------------------------------
40
Simon Hunt5724fb42015-02-05 16:59:40 -080041 function setUpKeys() {
42 // key bindings need to be made after the services have been injected
43 // thus, deferred to here...
44 ks.keyBindings({
Simon Hunt6036b192015-02-11 11:20:26 -080045 O: [tps.toggleSummary, 'Toggle ONOS summary pane'],
Simon Hunt5724fb42015-02-05 16:59:40 -080046 I: [toggleInstances, 'Toggle ONOS instances pane'],
Simon Hunt6036b192015-02-11 11:20:26 -080047 D: [tss.toggleDetails, 'Disable / enable details pane'],
Simon Huntac4c6f72015-02-03 19:50:53 -080048
Simon Hunt5724fb42015-02-05 16:59:40 -080049 H: [tfs.toggleHosts, 'Toggle host visibility'],
50 M: [tfs.toggleOffline, 'Toggle offline visibility'],
Simon Hunt18bf9822015-02-12 17:35:45 -080051 B: [toggleMap, 'Toggle background map'],
Simon Hunt5724fb42015-02-05 16:59:40 -080052 //P: togglePorts,
Simon Huntac4c6f72015-02-03 19:50:53 -080053
Simon Hunt5724fb42015-02-05 16:59:40 -080054 //X: [toggleNodeLock, 'Lock / unlock node positions'],
55 //Z: [toggleOblique, 'Toggle oblique view (Experimental)'],
56 L: [tfs.cycleDeviceLabels, 'Cycle device labels'],
Simon Hunt445e8152015-02-06 13:00:12 -080057 U: [tfs.unpin, 'Unpin node (hover mouse over)'],
Simon Hunt5724fb42015-02-05 16:59:40 -080058 R: [resetZoom, 'Reset pan / zoom'],
Simon Huntac4c6f72015-02-03 19:50:53 -080059
Simon Huntf542d842015-02-11 16:20:33 -080060 V: [tts.showRelatedIntentsAction, 'Show all related intents'],
61 rightArrow: [tts.showNextIntentAction, 'Show next related intent'],
62 leftArrow: [tts.showPrevIntentAction, 'Show previous related intent'],
63 W: [tts.showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
64 A: [tts.showAllTrafficAction, 'Monitor all traffic'],
65 F: [tts.showDeviceLinkFlowsAction, 'Show device link flows'],
Simon Huntac4c6f72015-02-03 19:50:53 -080066
Simon Hunt5724fb42015-02-05 16:59:40 -080067 //E: [equalizeMasters, 'Equalize mastership roles'],
Simon Huntac4c6f72015-02-03 19:50:53 -080068
Simon Hunt08f841d02015-02-10 14:39:20 -080069 esc: handleEscape,
Simon Huntac4c6f72015-02-03 19:50:53 -080070
Simon Hunt5724fb42015-02-05 16:59:40 -080071 _helpFormat: [
72 ['O', 'I', 'D', '-', 'H', 'M', 'B', 'P' ],
73 ['X', 'Z', 'L', 'U', 'R' ],
74 ['V', 'rightArrow', 'leftArrow', 'W', 'A', 'F', '-', 'E' ]
75 ]
76 });
Simon Huntac4c6f72015-02-03 19:50:53 -080077
Simon Hunt639dc662015-02-18 14:19:20 -080078 ks.gestureNotes([
Simon Hunt5724fb42015-02-05 16:59:40 -080079 ['click', 'Select the item and show details'],
80 ['shift-click', 'Toggle selection state'],
81 ['drag', 'Reposition (and pin) device / host'],
82 ['cmd-scroll', 'Zoom in / out'],
83 ['cmd-drag', 'Pan']
Simon Hunt639dc662015-02-18 14:19:20 -080084 ]);
Simon Hunt5724fb42015-02-05 16:59:40 -080085 }
Simon Hunt6cc53692015-01-07 11:33:45 -080086
Simon Hunt08f841d02015-02-10 14:39:20 -080087 // --- Keystroke functions -------------------------------------------
Simon Huntac4c6f72015-02-03 19:50:53 -080088
Simon Hunt6036b192015-02-11 11:20:26 -080089 // NOTE: this really belongs in the TopoPanelService -- but how to
90 // cleanly link in the updateDeviceColors() call? To be fixed later.
Simon Huntac4c6f72015-02-03 19:50:53 -080091 function toggleInstances() {
Simon Hunt5724fb42015-02-05 16:59:40 -080092 tis.toggle();
Simon Huntac4c6f72015-02-03 19:50:53 -080093 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -080094 }
Simon Huntac4c6f72015-02-03 19:50:53 -080095
Simon Hunt18bf9822015-02-12 17:35:45 -080096 function toggleMap() {
97 sus.visible(mapG, !sus.visible(mapG));
98 }
99
Simon Hunt08f841d02015-02-10 14:39:20 -0800100 function resetZoom() {
101 zoomer.reset();
102 }
103
104 function handleEscape() {
Simon Hunta142dd22015-02-12 22:07:51 -0800105 if (tis.showMaster()) {
106 // if an instance is selected, cancel the affinity mapping
107 tis.cancelAffinity()
Simon Hunt08f841d02015-02-10 14:39:20 -0800108
Simon Hunta0eb0a82015-02-11 12:30:06 -0800109 } else if (tss.haveDetails()) {
110 // else if we have node selections, deselect them all
111 tss.deselectAll();
Simon Hunt08f841d02015-02-10 14:39:20 -0800112
Simon Hunta0eb0a82015-02-11 12:30:06 -0800113 } else if (tis.isVisible()) {
114 // else if the Instance Panel is visible, hide it
115 tis.hide();
116 tfs.updateDeviceColors();
Simon Hunt08f841d02015-02-10 14:39:20 -0800117
Simon Hunta0eb0a82015-02-11 12:30:06 -0800118 } else if (tps.summaryVisible()) {
119 // else if the Summary Panel is visible, hide it
120 tps.hideSummaryPanel();
Simon Hunt08f841d02015-02-10 14:39:20 -0800121
Simon Hunta0eb0a82015-02-11 12:30:06 -0800122 } else {
123 // TODO: set hover mode to hoverModeNone
Simon Hunt62c2f8f2015-02-19 14:19:10 -0800124 // talk to Thomas about this: shouldn't it be done
125 // when we deselect the node (if tss.haveDetails()...)
Simon Hunta0eb0a82015-02-11 12:30:06 -0800126 }
Simon Hunt08f841d02015-02-10 14:39:20 -0800127 }
Simon Huntcacce342015-01-07 16:13:05 -0800128
129 // --- Glyphs, Icons, and the like -----------------------------------
130
Simon Hunt6cc53692015-01-07 11:33:45 -0800131 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800132 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800133 gs.loadDefs(defs);
Simon Hunt0ee28682015-02-12 20:48:11 -0800134 sus.loadGlowDefs(defs);
Simon Hunt6cc53692015-01-07 11:33:45 -0800135 }
136
137
Simon Huntcacce342015-01-07 16:13:05 -0800138 // --- Pan and Zoom --------------------------------------------------
139
140 // zoom enabled predicate. ev is a D3 source event.
141 function zoomEnabled(ev) {
142 return (ev.metaKey || ev.altKey);
143 }
144
145 function zoomCallback() {
Simon Hunt5724fb42015-02-05 16:59:40 -0800146 var sc = zoomer.scale();
Simon Huntcacce342015-01-07 16:13:05 -0800147
Simon Hunt0541fb82015-01-14 18:59:57 -0800148 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800149 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800150 }
151
152 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800153 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800154 zoomer = zs.createZoomer({
155 svg: svg,
156 zoomLayer: zoomLayer,
157 zoomEnabled: zoomEnabled,
158 zoomCallback: zoomCallback
159 });
160 }
161
162
Simon Hunt0541fb82015-01-14 18:59:57 -0800163 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800164 function svgResized(s) {
165 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800166 }
167
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800168 // --- Background Map ------------------------------------------------
169
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800170 function setUpNoDevs() {
171 var g, box;
172 noDevsLayer = svg.append('g').attr({
173 id: 'topo-noDevsLayer',
174 transform: sus.translate(500,500)
175 });
176 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
177 // We are translating this layer to have its origin at the center
178
179 g = noDevsLayer.append('g');
180 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
181 g.append('text').text('No devices are connected')
182 .attr({ x: 120, y: 80});
183
184 box = g.node().getBBox();
185 box.x -= box.width/2;
186 box.y -= box.height/2;
187 g.attr('transform', sus.translate(box.x, box.y));
188
189 showNoDevs(true);
190 }
191
192 function showNoDevs(b) {
Simon Hunt18bf9822015-02-12 17:35:45 -0800193 sus.visible(noDevsLayer, b);
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800194 }
195
Simon Hunt0541fb82015-01-14 18:59:57 -0800196 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800197 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Huntac4c6f72015-02-03 19:50:53 -0800198 // returns a promise for the projection...
199 return ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800200 }
201
Simon Hunt737c89f2015-01-28 12:23:19 -0800202
Simon Huntcacce342015-01-07 16:13:05 -0800203 // --- Controller Definition -----------------------------------------
204
Simon Hunt6cc53692015-01-07 11:33:45 -0800205 angular.module('ovTopo', moduleDependencies)
Simon Hunt6cc53692015-01-07 11:33:45 -0800206 .controller('OvTopoCtrl', [
Simon Hunt54442fa2015-01-26 14:17:38 -0800207 '$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800208 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800209 'GlyphService', 'MapService', 'SvgUtilService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800210 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Huntf542d842015-02-11 16:20:33 -0800211 'TopoInstService', 'TopoSelectService', 'TopoTrafficService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800212
Simon Hunta11b4eb2015-01-28 16:20:50 -0800213 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Hunt6036b192015-02-11 11:20:26 -0800214 _ks_, _zs_, _gs_, _ms_, _sus_,
Simon Huntf542d842015-02-11 16:20:33 -0800215 _tes_, _tfs_, _tps_, _tis_, _tss_, _tts_) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800216 var self = this,
Simon Hunt1894d792015-02-04 17:09:20 -0800217 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800218 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800219 uplink = {
220 // provides function calls back into this space
221 showNoDevs: showNoDevs,
222 projection: function () { return projection; },
Simon Huntc252aa62015-02-10 16:45:39 -0800223 sendEvent: _tes_.sendEvent
Simon Huntac4c6f72015-02-03 19:50:53 -0800224 };
225
Simon Hunt6cc53692015-01-07 11:33:45 -0800226 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800227 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800228 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800229 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800230 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800231 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800232 sus = _sus_;
Simon Huntc252aa62015-02-10 16:45:39 -0800233 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800234 tfs = _tfs_;
Simon Huntc252aa62015-02-10 16:45:39 -0800235 tps = _tps_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800236 tis = _tis_;
Simon Hunt6036b192015-02-11 11:20:26 -0800237 tss = _tss_;
Simon Huntf542d842015-02-11 16:20:33 -0800238 tts = _tts_;
Simon Huntef31fb22014-12-19 13:16:44 -0800239
Simon Hunt426bd862015-01-14 16:48:41 -0800240 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800241 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800242 };
Simon Huntef31fb22014-12-19 13:16:44 -0800243
Simon Hunt54442fa2015-01-26 14:17:38 -0800244 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800245 $scope.$on('$destroy', function () {
246 $log.log('OvTopoCtrl is saying Buh-Bye!');
Simon Huntfd1231a2015-01-26 22:14:51 -0800247 tes.closeSock();
Simon Hunt626d2102015-01-29 11:54:50 -0800248 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800249 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800250 tfs.destroyForce();
Simon Hunt584122a2015-01-21 15:32:40 -0800251 });
252
Simon Huntcacce342015-01-07 16:13:05 -0800253 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800254 ovtopo = d3.select('#ov-topo');
255 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800256 // set the svg size to match that of the window, less the masthead
257 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800258 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800259
Simon Hunt6cc53692015-01-07 11:33:45 -0800260 setUpKeys();
261 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800262 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800263 setUpNoDevs();
Simon Hunt1894d792015-02-04 17:09:20 -0800264 setUpMap().then(
265 function (proj) {
266 projection = proj;
267 $log.debug('** We installed the projection: ', proj);
268 }
269 );
Simon Huntfd1231a2015-01-26 22:14:51 -0800270
Simon Hunt1894d792015-02-04 17:09:20 -0800271 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Hunt3a6eec02015-02-09 21:16:43 -0800272 tfs.initForce(forceG, uplink, dim);
Simon Hunta142dd22015-02-12 22:07:51 -0800273 tis.initInst({ showMastership: tfs.showMastership });
Simon Hunt6036b192015-02-11 11:20:26 -0800274 tps.initPanels({ sendEvent: tes.sendEvent });
Simon Huntfd1231a2015-01-26 22:14:51 -0800275 tes.openSock();
Simon Hunt6cc53692015-01-07 11:33:45 -0800276
277 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800278 }]);
279}());