Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Topology Force Module. |
| 19 | Visualization of the topology in an SVG layer, using a D3 Force Layout. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | 9edd172 | 2017-03-10 17:43:26 -0800 | [diff] [blame] | 26 | var $log, $loc, wss; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 28 | var t2is, t2rs, t2ls, t2vs, t2bcs, t2ss, t2bgs, t2tbs, t2mss; |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 29 | var svg, forceG, uplink, dim, opts, zoomer; |
| 30 | |
| 31 | // D3 Selections |
| 32 | var node; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 33 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 34 | // ========================== Helper Functions |
| 35 | |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 36 | function init(_svg_, _forceG_, _uplink_, _dim_, _zoomer_, _opts_) { |
| 37 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 38 | svg = _svg_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 39 | uplink = _uplink_; |
| 40 | dim = _dim_; |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 41 | zoomer = _zoomer_; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 42 | opts = _opts_; |
| 43 | |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 44 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 45 | t2bgs.init(); |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 46 | t2bgs.region = t2rs; |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 47 | t2ls.init(svg, uplink, dim, zoomer, opts); |
| 48 | t2bcs.addLayout(t2ls); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 49 | t2ss.init(svg, zoomer); |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 50 | t2ss.region = t2rs; |
| 51 | t2rs.layout = t2ls; |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 52 | t2mss.region = t2rs; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 53 | t2tbs.init(); |
Simon Hunt | 9edd172 | 2017-03-10 17:43:26 -0800 | [diff] [blame] | 54 | navToBookmarkedRegion($loc.search().regionId); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | function destroy() { |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 58 | t2tbs.destroy(); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 59 | $log.debug('Destroy topo force layout'); |
| 60 | } |
| 61 | |
Simon Hunt | 9edd172 | 2017-03-10 17:43:26 -0800 | [diff] [blame] | 62 | function navToBookmarkedRegion(regionId) { |
| 63 | $log.debug('navToBookmarkedRegion:', regionId); |
| 64 | if (regionId) { |
| 65 | wss.sendEvent('topo2navRegion', { |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 66 | rid: regionId |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 67 | }); |
| 68 | |
Simon Hunt | 9edd172 | 2017-03-10 17:43:26 -0800 | [diff] [blame] | 69 | t2ls.createForceElements(); |
| 70 | t2ls.transitionDownRegion(); |
| 71 | } |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 74 | // ========================== Event Handlers |
| 75 | |
| 76 | function allInstances(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 77 | $log.debug('>> topo2AllInstances event:', data); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 78 | t2is.allInstances(data); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | function currentLayout(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 82 | $log.debug('>> topo2CurrentLayout event:', data); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 83 | t2rs.clear(); |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 84 | t2bcs.addBreadcrumb(data.crumbs); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 85 | t2bgs.addLayout(data); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | function currentRegion(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 89 | $log.debug('>> topo2CurrentRegion event:', data); |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 90 | t2rs.loaded('regionData', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | function topo2PeerRegions(data) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 94 | $log.debug('>> topo2PeerRegions event:', data); |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 95 | t2rs.loaded('peers', data.peers); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 98 | function modelEvent(data) { |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 99 | // $log.debug('>> topo2UiModelEvent event:', data); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 100 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 101 | // TODO: Interpret the event and update our topo model state (if needed) |
| 102 | // To Decide: Can we assume that the server will only send events |
| 103 | // related to objects that we are currently showing? |
| 104 | // (e.g. filtered by subregion contents?) |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 105 | t2rs.update(data); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 108 | function showMastership(masterId) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 109 | if (masterId) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 110 | showMastershipFor(masterId); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 111 | } else { |
| 112 | restoreLayerState(); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | function restoreLayerState() { |
| 117 | // NOTE: this level of indirection required, for when we have |
| 118 | // the layer filter functionality re-implemented |
| 119 | suppressLayers(false); |
| 120 | } |
| 121 | |
| 122 | // ========================== Main Service Definition |
| 123 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 124 | function newDim(_dim_) { |
| 125 | dim = _dim_; |
| 126 | t2vs.newDim(dim); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 129 | // ========================== Main Service Definition |
| 130 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 131 | function update(elements) { |
| 132 | angular.forEach(elements, function (el) { |
| 133 | el.update(); |
| 134 | }); |
| 135 | } |
| 136 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 137 | function updateNodes() { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 138 | update(t2rs.regionNodes()); |
| 139 | } |
| 140 | |
| 141 | function updateLinks() { |
| 142 | update(t2rs.regionLinks()); |
| 143 | } |
| 144 | |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 145 | function resetNodeLocation() { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 146 | |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 147 | var hovered = t2rs.filterRegionNodes(function (model) { |
| 148 | return model.get('hovered'); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 149 | }); |
| 150 | |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 151 | angular.forEach(hovered, function (model) { |
| 152 | model.resetPosition(); |
| 153 | }); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | function unpin() { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 157 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 158 | var hovered = t2rs.filterRegionNodes(function (model) { |
| 159 | return model.get('hovered'); |
| 160 | }); |
| 161 | |
| 162 | angular.forEach(hovered, function (model) { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 163 | model.fix(false); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 164 | }); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 165 | |
| 166 | t2ls.start(); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 169 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 170 | .factory('Topo2ForceService', [ |
Steven Burrows | f303c1e | 2017-03-13 12:26:22 +0000 | [diff] [blame] | 171 | '$log', '$location', 'WebSocketService', 'Topo2InstanceService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 172 | 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService', |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 173 | 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService', |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 174 | 'Topo2BackgroundService', 'Topo2ToolbarService', 'Topo2MastershipService', |
Steven Burrows | f303c1e | 2017-03-13 12:26:22 +0000 | [diff] [blame] | 175 | function (_$log_, _$loc_, _wss_, _t2is_, _t2rs_, _t2ls_, |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 176 | _t2vs_, _t2bcs_, zoomService, _t2ss_, _t2bgs_, _t2tbs_, _t2mss_) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 177 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 178 | $log = _$log_; |
Steven Burrows | f303c1e | 2017-03-13 12:26:22 +0000 | [diff] [blame] | 179 | $loc = _$loc_; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 180 | wss = _wss_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 181 | t2is = _t2is_; |
| 182 | t2rs = _t2rs_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 183 | t2ls = _t2ls_; |
| 184 | t2vs = _t2vs_; |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 185 | t2bcs = _t2bcs_; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 186 | t2ss = _t2ss_; |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 187 | t2bgs = _t2bgs_; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 188 | t2tbs = _t2tbs_; |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 189 | t2mss = _t2mss_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 190 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 191 | var onZoom = function () { |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 192 | if (!t2rs.isLoadComplete()) { |
| 193 | return; |
| 194 | } |
| 195 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 196 | var nodes = [].concat( |
| 197 | t2rs.regionNodes(), |
| 198 | t2rs.regionLinks() |
| 199 | ); |
| 200 | |
| 201 | angular.forEach(nodes, function (node) { |
| 202 | node.setScale(); |
| 203 | }); |
| 204 | }; |
| 205 | |
| 206 | zoomService.addZoomEventListener(onZoom); |
| 207 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 208 | return { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 209 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 210 | init: init, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 211 | newDim: newDim, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 212 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 213 | destroy: destroy, |
| 214 | topo2AllInstances: allInstances, |
| 215 | topo2CurrentLayout: currentLayout, |
| 216 | topo2CurrentRegion: currentRegion, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 217 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 218 | topo2UiModelEvent: modelEvent, |
| 219 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 220 | showMastership: showMastership, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 221 | topo2PeerRegions: topo2PeerRegions, |
| 222 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 223 | updateNodes: updateNodes, |
| 224 | updateLinks: updateLinks, |
Steven Burrows | 892cba6 | 2017-03-10 16:31:48 +0000 | [diff] [blame] | 225 | resetNodeLocation: resetNodeLocation, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 226 | unpin: unpin |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 227 | }; |
| 228 | }]); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 229 | })(); |