Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 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 | /* |
| 18 | ONOS GUI -- Topology View Module |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 23 | |
| 24 | var moduleDependencies = [ |
| 25 | 'onosUtil', |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 26 | 'onosSvg', |
| 27 | 'onosRemote' |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 28 | ]; |
| 29 | |
| 30 | // references to injected services etc. |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 31 | var $log, fs, ks, zs, gs, ms, sus, tfs, tis; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 32 | |
| 33 | // DOM elements |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 34 | var ovtopo, svg, defs, zoomLayer, mapG, forceG, noDevsLayer; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 35 | |
| 36 | // Internal state |
Simon Hunt | edf5c0e | 2015-01-29 15:00:53 -0800 | [diff] [blame] | 37 | var zoomer; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 38 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 39 | // --- Short Cut Keys ------------------------------------------------ |
| 40 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 41 | 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 Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 48 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 49 | H: [tfs.toggleHosts, 'Toggle host visibility'], |
| 50 | M: [tfs.toggleOffline, 'Toggle offline visibility'], |
| 51 | //B: [toggleBg, 'Toggle background image'], |
| 52 | //P: togglePorts, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 53 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 54 | //X: [toggleNodeLock, 'Lock / unlock node positions'], |
| 55 | //Z: [toggleOblique, 'Toggle oblique view (Experimental)'], |
| 56 | L: [tfs.cycleDeviceLabels, 'Cycle device labels'], |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 57 | U: [tfs.unpin, 'Unpin node (hover mouse over)'], |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 58 | R: [resetZoom, 'Reset pan / zoom'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 59 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 60 | //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 Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 66 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 67 | //E: [equalizeMasters, 'Equalize mastership roles'], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 68 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 69 | //esc: handleEscape, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 70 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 71 | _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 Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 77 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 78 | // 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 Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 87 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 88 | |
| 89 | function toggleInstances() { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 90 | tis.toggle(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 91 | tfs.updateDeviceColors(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 92 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 93 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 94 | |
| 95 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 96 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 97 | function setUpDefs() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 98 | defs = svg.append('defs'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 99 | gs.loadDefs(defs); |
| 100 | } |
| 101 | |
| 102 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 103 | // --- Pan and Zoom -------------------------------------------------- |
| 104 | |
| 105 | // zoom enabled predicate. ev is a D3 source event. |
| 106 | function zoomEnabled(ev) { |
| 107 | return (ev.metaKey || ev.altKey); |
| 108 | } |
| 109 | |
| 110 | function zoomCallback() { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 111 | var sc = zoomer.scale(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 112 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 113 | // keep the map lines constant width while zooming |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 114 | mapG.style('stroke-width', (2.0 / sc) + 'px'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | function setUpZoom() { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 118 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 119 | zoomer = zs.createZoomer({ |
| 120 | svg: svg, |
| 121 | zoomLayer: zoomLayer, |
| 122 | zoomEnabled: zoomEnabled, |
| 123 | zoomCallback: zoomCallback |
| 124 | }); |
| 125 | } |
| 126 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 127 | function resetZoom() { |
| 128 | zoomer.reset(); |
| 129 | } |
| 130 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 131 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 132 | // callback invoked when the SVG view has been resized.. |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame^] | 133 | function svgResized(s) { |
| 134 | tfs.newDim([s.width, s.height]); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 137 | // --- Background Map ------------------------------------------------ |
| 138 | |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 139 | function setUpNoDevs() { |
| 140 | var g, box; |
| 141 | noDevsLayer = svg.append('g').attr({ |
| 142 | id: 'topo-noDevsLayer', |
| 143 | transform: sus.translate(500,500) |
| 144 | }); |
| 145 | // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html. |
| 146 | // We are translating this layer to have its origin at the center |
| 147 | |
| 148 | g = noDevsLayer.append('g'); |
| 149 | gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird'); |
| 150 | g.append('text').text('No devices are connected') |
| 151 | .attr({ x: 120, y: 80}); |
| 152 | |
| 153 | box = g.node().getBBox(); |
| 154 | box.x -= box.width/2; |
| 155 | box.y -= box.height/2; |
| 156 | g.attr('transform', sus.translate(box.x, box.y)); |
| 157 | |
| 158 | showNoDevs(true); |
| 159 | } |
| 160 | |
| 161 | function showNoDevs(b) { |
| 162 | sus.makeVisible(noDevsLayer, b); |
| 163 | } |
| 164 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 165 | function showCallibrationPoints() { |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 166 | // temp code for calibration |
| 167 | var points = [ |
| 168 | [0, 0], [0, 1000], [1000, 0], [1000, 1000] |
| 169 | ]; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 170 | mapG.selectAll('circle') |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 171 | .data(points) |
| 172 | .enter() |
| 173 | .append('circle') |
| 174 | .attr('cx', function (d) { return d[0]; }) |
| 175 | .attr('cy', function (d) { return d[1]; }) |
| 176 | .attr('r', 5) |
| 177 | .style('fill', 'red'); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 180 | function setUpMap() { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 181 | mapG = zoomLayer.append('g').attr('id', 'topo-map'); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 182 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 183 | //showCallibrationPoints(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 184 | //return ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5}); |
| 185 | |
| 186 | // returns a promise for the projection... |
| 187 | return ms.loadMapInto(mapG, '*continental_us'); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 190 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 191 | // --- Controller Definition ----------------------------------------- |
| 192 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 193 | angular.module('ovTopo', moduleDependencies) |
| 194 | |
| 195 | .controller('OvTopoCtrl', [ |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 196 | '$scope', '$log', '$location', '$timeout', |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 197 | 'FnService', 'MastService', 'KeyService', 'ZoomService', |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 198 | 'GlyphService', 'MapService', 'SvgUtilService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 199 | 'TopoEventService', 'TopoForceService', 'TopoPanelService', |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 200 | 'TopoInstService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 201 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 202 | function ($scope, _$log_, $loc, $timeout, _fs_, mast, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 203 | _ks_, _zs_, _gs_, _ms_, _sus_, tes, _tfs_, tps, _tis_) { |
| 204 | var self = this, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 205 | projection, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame^] | 206 | dim, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 207 | uplink = { |
| 208 | // provides function calls back into this space |
| 209 | showNoDevs: showNoDevs, |
| 210 | projection: function () { return projection; }, |
| 211 | sendEvent: tes.sendEvent |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 214 | $log = _$log_; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 215 | fs = _fs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 216 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 217 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 218 | gs = _gs_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame] | 219 | ms = _ms_; |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 220 | sus = _sus_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 221 | tfs = _tfs_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 222 | tis = _tis_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 223 | |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 224 | self.notifyResize = function () { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 225 | svgResized(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 226 | }; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 227 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 228 | // Cleanup on destroyed scope.. |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 229 | $scope.$on('$destroy', function () { |
| 230 | $log.log('OvTopoCtrl is saying Buh-Bye!'); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 231 | tes.closeSock(); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 232 | tps.destroyPanels(); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 233 | tis.destroyInst(); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame^] | 234 | tfs.destroyForce(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 235 | }); |
| 236 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 237 | // svg layer and initialization of components |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 238 | ovtopo = d3.select('#ov-topo'); |
| 239 | svg = ovtopo.select('svg'); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 240 | // set the svg size to match that of the window, less the masthead |
| 241 | svg.attr(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame^] | 242 | dim = [svg.attr('width'), svg.attr('height')]; |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 243 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 244 | setUpKeys(); |
| 245 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 246 | setUpZoom(); |
Simon Hunt | 7c8ab8d | 2015-02-03 15:05:15 -0800 | [diff] [blame] | 247 | setUpNoDevs(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 248 | setUpMap().then( |
| 249 | function (proj) { |
| 250 | projection = proj; |
| 251 | $log.debug('** We installed the projection: ', proj); |
| 252 | } |
| 253 | ); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 254 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 255 | forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame^] | 256 | tfs.initForce(forceG, uplink, dim); |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 257 | tis.initInst(); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 258 | tps.initPanels(); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 259 | tes.openSock(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 260 | |
| 261 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 262 | }]); |
| 263 | }()); |