Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Topology View Module |
| 19 | |
| 20 | NOTE: currently under development to support Regions. |
| 21 | */ |
| 22 | |
| 23 | (function () { |
| 24 | 'use strict'; |
| 25 | |
| 26 | // references to injected services |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 27 | var $scope, $log, fs, mast, ks, zs, |
| 28 | gs, sus, ps, t2es, t2fs, t2is, t2bcs; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 29 | |
| 30 | // DOM elements |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 31 | var ovtopo2, svg, defs, zoomLayer, forceG; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 32 | |
| 33 | // Internal state |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 34 | var zoomer; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 35 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 36 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 37 | |
| 38 | function setUpDefs() { |
| 39 | defs = svg.append('defs'); |
| 40 | gs.loadDefs(defs); |
| 41 | sus.loadGlowDefs(defs); |
| 42 | } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 43 | |
| 44 | // callback invoked when the SVG view has been resized.. |
| 45 | function svgResized(s) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 46 | $log.debug('topo2 view resized', s); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 47 | t2fs.newDim([s.width, s.height]); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | function setUpKeys(overlayKeys) { |
| 51 | $log.debug('topo2: set up keys....'); |
| 52 | } |
| 53 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 54 | // --- Pan and Zoom -------------------------------------------------- |
| 55 | |
| 56 | // zoom enabled predicate. ev is a D3 source event. |
| 57 | function zoomEnabled(ev) { |
| 58 | return fs.isMobile() || (ev.metaKey || ev.altKey); |
| 59 | } |
| 60 | |
| 61 | function zoomCallback() { |
| 62 | var sc = zoomer.scale(), |
| 63 | tr = zoomer.translate(); |
| 64 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 65 | ps.setPrefs('topo_zoom', { tx: tr[0], ty: tr[1], sc: sc }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 66 | |
| 67 | // keep the map lines constant width while zooming |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 68 | // mapG.style('stroke-width', (2.0 / sc) + 'px'); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function setUpZoom() { |
| 72 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
| 73 | zoomer = zs.createZoomer({ |
| 74 | svg: svg, |
| 75 | zoomLayer: zoomLayer, |
| 76 | zoomEnabled: zoomEnabled, |
| 77 | zoomCallback: zoomCallback |
| 78 | }); |
| 79 | } |
| 80 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 81 | // === Controller Definition ----------------------------------------- |
| 82 | |
| 83 | angular.module('ovTopo2', ['onosUtil', 'onosSvg', 'onosRemote']) |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 84 | .controller('OvTopo2Ctrl', |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 85 | ['$scope', '$log', '$location', |
| 86 | 'FnService', 'MastService', 'KeyService', 'ZoomService', |
| 87 | 'GlyphService', 'MapService', 'SvgUtilService', 'FlashService', |
| 88 | 'WebSocketService', 'PrefsService', 'ThemeService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 89 | 'Topo2EventService', 'Topo2ForceService', 'Topo2InstanceService', |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 90 | 'Topo2BreadcrumbService', |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 91 | |
| 92 | function (_$scope_, _$log_, _$loc_, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 93 | _fs_, _mast_, _ks_, _zs_, |
| 94 | _gs_, _ms_, _sus_, _flash_, |
| 95 | _wss_, _ps_, _th_, |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 96 | _t2es_, _t2fs_, _t2is_, _t2bcs_) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 97 | |
| 98 | var params = _$loc_.search(), |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 99 | dim, |
| 100 | wh, |
| 101 | uplink = { |
| 102 | // provides function calls back into this space |
| 103 | // showNoDevs: showNoDevs, |
| 104 | // projection: function () { return projection; }, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 105 | zoomLayer: function () { return zoomLayer; }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 106 | zoomer: function () { return zoomer; } |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 107 | // opacifyMap: opacifyMap, |
| 108 | // topoStartDone: topoStartDone |
| 109 | }; |
| 110 | |
| 111 | $scope = _$scope_; |
| 112 | $log = _$log_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 113 | |
| 114 | fs = _fs_; |
| 115 | mast = _mast_; |
| 116 | ks = _ks_; |
| 117 | zs = _zs_; |
| 118 | |
| 119 | gs = _gs_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 120 | sus = _sus_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 121 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 122 | ps = _ps_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 123 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 124 | t2es = _t2es_; |
| 125 | t2fs = _t2fs_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 126 | t2is = _t2is_; |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 127 | t2bcs = _t2bcs_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 128 | |
| 129 | // capture selected intent parameters (if they are set in the |
| 130 | // query string) so that the traffic overlay can highlight |
| 131 | // the path for that intent |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 132 | if (params.intentKey && |
| 133 | params.intentAppId && |
| 134 | params.intentAppName) { |
| 135 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 136 | $scope.intentData = { |
| 137 | key: params.intentKey, |
| 138 | appId: params.intentAppId, |
| 139 | appName: params.intentAppName |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | $scope.notifyResize = function () { |
| 144 | svgResized(fs.windowSize(mast.mastHeight())); |
| 145 | }; |
| 146 | |
| 147 | // Cleanup on destroyed scope.. |
| 148 | $scope.$on('$destroy', function () { |
| 149 | $log.log('OvTopo2Ctrl is saying Buh-Bye!'); |
| 150 | t2es.stop(); |
| 151 | ks.unbindKeys(); |
| 152 | t2fs.destroy(); |
| 153 | }); |
| 154 | |
| 155 | // svg layer and initialization of components |
| 156 | ovtopo2 = d3.select('#ov-topo2'); |
| 157 | svg = ovtopo2.select('svg'); |
| 158 | // set the svg size to match that of the window, less the masthead |
| 159 | wh = fs.windowSize(mast.mastHeight()); |
| 160 | $log.debug('setting topo SVG size to', wh); |
| 161 | svg.attr(wh); |
| 162 | dim = [wh.width, wh.height]; |
| 163 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 164 | // set up our keyboard shortcut bindings |
| 165 | setUpKeys(); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 166 | setUpZoom(); |
| 167 | setUpDefs(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 168 | |
| 169 | // make sure we can respond to topology events from the server |
| 170 | t2es.bindHandlers(); |
| 171 | |
| 172 | // initialize the force layout, ready to render the topology |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 173 | forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
| 174 | t2fs.init(svg, forceG, uplink, dim); |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 175 | t2bcs.init(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 176 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 177 | // =-=-=-=-=-=-=-=- |
| 178 | // TODO: in future, we will load background map data |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 179 | // asynchronously (hence the promise) and then chain off |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 180 | // there to send the topo2start event to the server. |
| 181 | // For now, we'll send the event inline... |
| 182 | t2es.start(); |
| 183 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 184 | t2is.initInst({ showMastership: t2fs.showMastership }); |
| 185 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 186 | // === ORIGINAL CODE === |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 187 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 188 | // setUpKeys(); |
| 189 | // setUpToolbar(); |
| 190 | // setUpDefs(); |
| 191 | // setUpZoom(); |
| 192 | // setUpNoDevs(); |
| 193 | /* |
| 194 | setUpMap().then( |
| 195 | function (proj) { |
| 196 | var z = ps.getPrefs('topo_zoom', { tx:0, ty:0, sc:1 }); |
| 197 | zoomer.panZoom([z.tx, z.ty], z.sc); |
| 198 | $log.debug('** Zoom restored:', z); |
| 199 | |
| 200 | projection = proj; |
| 201 | $log.debug('** We installed the projection:', proj); |
| 202 | flash.enable(false); |
| 203 | toggleMap(prefsState.bg); |
| 204 | flash.enable(true); |
| 205 | mapShader(true); |
| 206 | |
| 207 | // now we have the map projection, we are ready for |
| 208 | // the server to send us device/host data... |
| 209 | tes.start(); |
| 210 | // need to do the following so we immediately get |
| 211 | // the summary panel data back from the server |
| 212 | restoreSummaryFromPrefs(); |
| 213 | } |
| 214 | ); |
| 215 | */ |
| 216 | // tes.bindHandlers(); |
| 217 | // setUpSprites(); |
| 218 | |
| 219 | // forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
| 220 | // tfs.initForce(svg, forceG, uplink, dim); |
| 221 | // tis.initInst({ showMastership: tfs.showMastership }); |
| 222 | // tps.initPanels(); |
| 223 | |
| 224 | // restoreConfigFromPrefs(); |
| 225 | // ttbs.setDefaultOverlay(prefsState.ovidx); |
| 226 | |
| 227 | // $log.debug('registered overlays...', tov.list()); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 228 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 229 | $log.log('OvTopo2Ctrl has been created'); |
| 230 | }]); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 231 | })(); |