blob: a835d6be0aa87a82bc455c5f62e8ed8a0eeb02f2 [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 Huntac4c6f72015-02-03 19:50:53 -080031 var $log, fs, ks, zs, gs, ms, sus, tfs, tis;
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({
45 //O: [toggleSummary, 'Toggle ONOS summary pane'],
46 I: [toggleInstances, 'Toggle ONOS instances pane'],
47 //D: [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'],
51 //B: [toggleBg, 'Toggle background image'],
52 //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 Hunt5724fb42015-02-05 16:59:40 -080060 //V: [showRelatedIntentsAction, 'Show all related intents'],
61 //rightArrow: [showNextIntentAction, 'Show next related intent'],
62 //leftArrow: [showPrevIntentAction, 'Show previous related intent'],
63 //W: [showSelectedIntentTrafficAction, 'Monitor traffic of selected intent'],
64 //A: [showAllTrafficAction, 'Monitor all traffic'],
65 //F: [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 Hunt5724fb42015-02-05 16:59:40 -080078 // TODO: // mouse gestures
79 var gestures = [
80 ['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']
85 ];
86 }
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
90 function toggleInstances() {
Simon Hunt5724fb42015-02-05 16:59:40 -080091 tis.toggle();
Simon Huntac4c6f72015-02-03 19:50:53 -080092 tfs.updateDeviceColors();
Simon Hunt6cc53692015-01-07 11:33:45 -080093 }
Simon Huntac4c6f72015-02-03 19:50:53 -080094
Simon Hunt08f841d02015-02-10 14:39:20 -080095 function resetZoom() {
96 zoomer.reset();
97 }
98
99 function handleEscape() {
100 $log.debug("TODO: handle-ESCAPE...");
101 // if showingAffinity: cancelAffinity
102
103 // else if showingDetails: deselectAll
104
105 // else if oiBox visible: hide oiBox
106
107 // else if summary panel visible: cancel Summary
108
109 // else: hoverMode = hoverModeNone
110 }
Simon Huntcacce342015-01-07 16:13:05 -0800111
112 // --- Glyphs, Icons, and the like -----------------------------------
113
Simon Hunt6cc53692015-01-07 11:33:45 -0800114 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -0800115 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -0800116 gs.loadDefs(defs);
117 }
118
119
Simon Huntcacce342015-01-07 16:13:05 -0800120 // --- Pan and Zoom --------------------------------------------------
121
122 // zoom enabled predicate. ev is a D3 source event.
123 function zoomEnabled(ev) {
124 return (ev.metaKey || ev.altKey);
125 }
126
127 function zoomCallback() {
Simon Hunt5724fb42015-02-05 16:59:40 -0800128 var sc = zoomer.scale();
Simon Huntcacce342015-01-07 16:13:05 -0800129
Simon Hunt0541fb82015-01-14 18:59:57 -0800130 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -0800131 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -0800132 }
133
134 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800135 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -0800136 zoomer = zs.createZoomer({
137 svg: svg,
138 zoomLayer: zoomLayer,
139 zoomEnabled: zoomEnabled,
140 zoomCallback: zoomCallback
141 });
142 }
143
144
Simon Hunt0541fb82015-01-14 18:59:57 -0800145 // callback invoked when the SVG view has been resized..
Simon Hunt3a6eec02015-02-09 21:16:43 -0800146 function svgResized(s) {
147 tfs.newDim([s.width, s.height]);
Simon Hunt0541fb82015-01-14 18:59:57 -0800148 }
149
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800150 // --- Background Map ------------------------------------------------
151
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800152 function setUpNoDevs() {
153 var g, box;
154 noDevsLayer = svg.append('g').attr({
155 id: 'topo-noDevsLayer',
156 transform: sus.translate(500,500)
157 });
158 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
159 // We are translating this layer to have its origin at the center
160
161 g = noDevsLayer.append('g');
162 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
163 g.append('text').text('No devices are connected')
164 .attr({ x: 120, y: 80});
165
166 box = g.node().getBBox();
167 box.x -= box.width/2;
168 box.y -= box.height/2;
169 g.attr('transform', sus.translate(box.x, box.y));
170
171 showNoDevs(true);
172 }
173
174 function showNoDevs(b) {
175 sus.makeVisible(noDevsLayer, b);
176 }
177
Simon Hunt0541fb82015-01-14 18:59:57 -0800178 function showCallibrationPoints() {
Simon Hunt426bd862015-01-14 16:48:41 -0800179 // temp code for calibration
180 var points = [
181 [0, 0], [0, 1000], [1000, 0], [1000, 1000]
182 ];
Simon Hunt737c89f2015-01-28 12:23:19 -0800183 mapG.selectAll('circle')
Simon Hunt426bd862015-01-14 16:48:41 -0800184 .data(points)
185 .enter()
186 .append('circle')
187 .attr('cx', function (d) { return d[0]; })
188 .attr('cy', function (d) { return d[1]; })
189 .attr('r', 5)
190 .style('fill', 'red');
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800191 }
192
Simon Hunt0541fb82015-01-14 18:59:57 -0800193 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800194 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Huntac4c6f72015-02-03 19:50:53 -0800195
Simon Hunt0541fb82015-01-14 18:59:57 -0800196 //showCallibrationPoints();
Simon Huntac4c6f72015-02-03 19:50:53 -0800197 //return ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5});
198
199 // returns a promise for the projection...
200 return ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800201 }
202
Simon Hunt737c89f2015-01-28 12:23:19 -0800203
Simon Huntcacce342015-01-07 16:13:05 -0800204 // --- Controller Definition -----------------------------------------
205
Simon Hunt6cc53692015-01-07 11:33:45 -0800206 angular.module('ovTopo', moduleDependencies)
207
208 .controller('OvTopoCtrl', [
Simon Hunt54442fa2015-01-26 14:17:38 -0800209 '$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800210 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800211 'GlyphService', 'MapService', 'SvgUtilService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800212 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt4b668592015-01-29 17:33:53 -0800213 'TopoInstService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800214
Simon Hunta11b4eb2015-01-28 16:20:50 -0800215 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Huntac4c6f72015-02-03 19:50:53 -0800216 _ks_, _zs_, _gs_, _ms_, _sus_, tes, _tfs_, tps, _tis_) {
217 var self = this,
Simon Hunt1894d792015-02-04 17:09:20 -0800218 projection,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800219 dim,
Simon Hunt1894d792015-02-04 17:09:20 -0800220 uplink = {
221 // provides function calls back into this space
222 showNoDevs: showNoDevs,
223 projection: function () { return projection; },
224 sendEvent: tes.sendEvent
Simon Huntac4c6f72015-02-03 19:50:53 -0800225 };
226
Simon Hunt6cc53692015-01-07 11:33:45 -0800227 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800228 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800229 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800230 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800231 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800232 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800233 sus = _sus_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800234 tfs = _tfs_;
Simon Huntac4c6f72015-02-03 19:50:53 -0800235 tis = _tis_;
Simon Huntef31fb22014-12-19 13:16:44 -0800236
Simon Hunt426bd862015-01-14 16:48:41 -0800237 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800238 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800239 };
Simon Huntef31fb22014-12-19 13:16:44 -0800240
Simon Hunt54442fa2015-01-26 14:17:38 -0800241 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800242 $scope.$on('$destroy', function () {
243 $log.log('OvTopoCtrl is saying Buh-Bye!');
Simon Huntfd1231a2015-01-26 22:14:51 -0800244 tes.closeSock();
Simon Hunt626d2102015-01-29 11:54:50 -0800245 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800246 tis.destroyInst();
Simon Hunt3a6eec02015-02-09 21:16:43 -0800247 tfs.destroyForce();
Simon Hunt584122a2015-01-21 15:32:40 -0800248 });
249
Simon Huntcacce342015-01-07 16:13:05 -0800250 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800251 ovtopo = d3.select('#ov-topo');
252 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800253 // set the svg size to match that of the window, less the masthead
254 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt3a6eec02015-02-09 21:16:43 -0800255 dim = [svg.attr('width'), svg.attr('height')];
Simon Hunt426bd862015-01-14 16:48:41 -0800256
Simon Hunt6cc53692015-01-07 11:33:45 -0800257 setUpKeys();
258 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800259 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800260 setUpNoDevs();
Simon Hunt1894d792015-02-04 17:09:20 -0800261 setUpMap().then(
262 function (proj) {
263 projection = proj;
264 $log.debug('** We installed the projection: ', proj);
265 }
266 );
Simon Huntfd1231a2015-01-26 22:14:51 -0800267
Simon Hunt1894d792015-02-04 17:09:20 -0800268 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Hunt3a6eec02015-02-09 21:16:43 -0800269 tfs.initForce(forceG, uplink, dim);
Simon Hunt4b668592015-01-29 17:33:53 -0800270 tis.initInst();
Simon Hunt626d2102015-01-29 11:54:50 -0800271 tps.initPanels();
Simon Huntfd1231a2015-01-26 22:14:51 -0800272 tes.openSock();
Simon Hunt6cc53692015-01-07 11:33:45 -0800273
274 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800275 }]);
276}());