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 |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 26 | var $log, |
| 27 | wss; |
| 28 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 29 | var t2is, t2rs, t2ls, t2vs, t2bcs, t2ss; |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 30 | var svg, forceG, uplink, dim, opts, zoomer; |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 31 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 32 | // D3 Selections |
| 33 | var node; |
| 34 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 35 | // ========================== Helper Functions |
| 36 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 37 | function init(_svg_, _forceG_, _uplink_, _dim_, zoomer, _opts_) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 38 | svg = _svg_; |
| 39 | forceG = _forceG_; |
| 40 | uplink = _uplink_; |
| 41 | dim = _dim_; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 42 | opts = _opts_; |
| 43 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 44 | t2ls = t2ls(svg, forceG, uplink, dim, zoomer, opts); |
| 45 | t2bcs.addLayout(t2ls); |
| 46 | t2rs.layout = t2ls; |
| 47 | t2ss.init(svg, zoomer); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | function destroy() { |
| 51 | $log.debug('Destroy topo force layout'); |
| 52 | } |
| 53 | |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 54 | // ========================== Temporary Code (to be deleted later) |
| 55 | |
| 56 | function request(dir, rid) { |
| 57 | wss.sendEvent('topo2navRegion', { |
| 58 | dir: dir, |
| 59 | rid: rid |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | function doTmpCurrentLayout(data) { |
| 64 | var topdiv = d3.select('#topo2tmp'); |
| 65 | var parentRegion = data.parent; |
| 66 | var span = topdiv.select('.parentRegion').select('span'); |
| 67 | span.text(parentRegion || '[no parent]'); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 68 | span.classed('nav-me', Boolean(parentRegion)); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function doTmpCurrentRegion(data) { |
| 72 | var topdiv = d3.select('#topo2tmp'); |
| 73 | var span = topdiv.select('.thisRegion').select('span'); |
| 74 | var div; |
| 75 | |
| 76 | span.text(data.id); |
| 77 | |
| 78 | div = topdiv.select('.subRegions').select('div'); |
| 79 | data.subregions.forEach(function (r) { |
| 80 | |
| 81 | function nav() { |
| 82 | request('down', r.id); |
| 83 | } |
| 84 | |
| 85 | div.append('p') |
| 86 | .classed('nav-me', true) |
| 87 | .text(r.id) |
| 88 | .on('click', nav); |
| 89 | }); |
| 90 | |
| 91 | div = topdiv.select('.devices').select('div'); |
| 92 | data.layerOrder.forEach(function (tag, idx) { |
| 93 | var devs = data.devices[idx]; |
| 94 | devs.forEach(function (d) { |
| 95 | div.append('p') |
| 96 | .text('[' + tag + '] ' + d.id); |
| 97 | }); |
| 98 | |
| 99 | }); |
| 100 | |
| 101 | div = topdiv.select('.hosts').select('div'); |
| 102 | data.layerOrder.forEach(function (tag, idx) { |
| 103 | var hosts = data.hosts[idx]; |
| 104 | hosts.forEach(function (h) { |
| 105 | div.append('p') |
| 106 | .text('[' + tag + '] ' + h.id); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | div = topdiv.select('.links').select('div'); |
| 111 | var links = data.links; |
| 112 | links.forEach(function (lnk) { |
| 113 | div.append('p') |
| 114 | .text(lnk.id); |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | function doTmpPeerRegions(data) { |
| 119 | |
| 120 | } |
| 121 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 122 | // ========================== Event Handlers |
| 123 | |
| 124 | function allInstances(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 125 | $log.debug('>> topo2AllInstances event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 126 | doTmpCurrentLayout(data); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 127 | t2is.allInstances(data); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | function currentLayout(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 131 | $log.debug('>> topo2CurrentLayout event:', data); |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 132 | t2bcs.addBreadcrumb(data.crumbs); |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 133 | t2rs.addLayout(data); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | function currentRegion(data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 137 | $log.debug('>> topo2CurrentRegion event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 138 | doTmpCurrentRegion(data); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 139 | t2rs.addRegion(data); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 140 | t2ls.createForceLayout(); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | function topo2PeerRegions(data) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 144 | $log.debug('>> topo2PeerRegions event:', data); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 145 | doTmpPeerRegions(data); |
| 146 | } |
| 147 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 148 | function modelEvent(data) { |
| 149 | $log.debug('>> topo2UiModelEvent event:', data); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 150 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 151 | // TODO: Interpret the event and update our topo model state (if needed) |
| 152 | // To Decide: Can we assume that the server will only send events |
| 153 | // related to objects that we are currently showing? |
| 154 | // (e.g. filtered by subregion contents?) |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 155 | t2rs.update(data); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 158 | function showMastership(masterId) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 159 | if (masterId) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 160 | showMastershipFor(masterId); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 161 | } else { |
| 162 | restoreLayerState(); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | function restoreLayerState() { |
| 167 | // NOTE: this level of indirection required, for when we have |
| 168 | // the layer filter functionality re-implemented |
| 169 | suppressLayers(false); |
| 170 | } |
| 171 | |
| 172 | // ========================== Main Service Definition |
| 173 | |
| 174 | function showMastershipFor(id) { |
| 175 | suppressLayers(true); |
| 176 | node.each(function (n) { |
| 177 | if (n.master === id) { |
| 178 | n.el.classed('suppressedmax', false); |
| 179 | } |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | function supAmt(less) { |
| 184 | return less ? 'suppressed' : 'suppressedmax'; |
| 185 | } |
| 186 | |
| 187 | function suppressLayers(b, less) { |
| 188 | var cls = supAmt(less); |
| 189 | node.classed(cls, b); |
| 190 | // link.classed(cls, b); |
| 191 | } |
| 192 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 193 | function newDim(_dim_) { |
| 194 | dim = _dim_; |
| 195 | t2vs.newDim(dim); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 198 | // ========================== Main Service Definition |
| 199 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 200 | function update(elements) { |
| 201 | angular.forEach(elements, function (el) { |
| 202 | el.update(); |
| 203 | }); |
| 204 | } |
| 205 | |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 206 | function updateNodes() { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 207 | update(t2rs.regionNodes()); |
| 208 | } |
| 209 | |
| 210 | function updateLinks() { |
| 211 | update(t2rs.regionLinks()); |
| 212 | } |
| 213 | |
| 214 | function resetAllLocations() { |
| 215 | var nodes = t2rs.regionNodes(); |
| 216 | |
| 217 | angular.forEach(nodes, function (node) { |
| 218 | node.resetPosition(); |
| 219 | }); |
| 220 | |
| 221 | t2ls.update(); |
| 222 | t2ls.tick(); |
| 223 | } |
| 224 | |
| 225 | function unpin() { |
| 226 | var hovered = t2rs.filterRegionNodes(function (model) { |
| 227 | return model.get('hovered'); |
| 228 | }); |
| 229 | |
| 230 | angular.forEach(hovered, function (model) { |
| 231 | model.fixed = false; |
| 232 | model.el.classed('fixed', false); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 233 | }); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 236 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 237 | .factory('Topo2ForceService', [ |
| 238 | '$log', 'WebSocketService', 'Topo2InstanceService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 239 | 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService', |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 240 | 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 241 | function (_$log_, _wss_, _t2is_, _t2rs_, _t2ls_, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 242 | _t2vs_, _t2bcs_, zoomService, _t2ss_) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 243 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 244 | $log = _$log_; |
| 245 | wss = _wss_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 246 | t2is = _t2is_; |
| 247 | t2rs = _t2rs_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 248 | t2ls = _t2ls_; |
| 249 | t2vs = _t2vs_; |
Steven Burrows | af3159d | 2016-08-25 14:54:30 +0100 | [diff] [blame] | 250 | t2bcs = _t2bcs_; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 251 | t2ss = _t2ss_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 252 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 253 | var onZoom = function () { |
| 254 | var nodes = [].concat( |
| 255 | t2rs.regionNodes(), |
| 256 | t2rs.regionLinks() |
| 257 | ); |
| 258 | |
| 259 | angular.forEach(nodes, function (node) { |
| 260 | node.setScale(); |
| 261 | }); |
| 262 | }; |
| 263 | |
| 264 | zoomService.addZoomEventListener(onZoom); |
| 265 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 266 | return { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 267 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 268 | init: init, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 269 | newDim: newDim, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 270 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 271 | destroy: destroy, |
| 272 | topo2AllInstances: allInstances, |
| 273 | topo2CurrentLayout: currentLayout, |
| 274 | topo2CurrentRegion: currentRegion, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 275 | |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 276 | topo2UiModelEvent: modelEvent, |
| 277 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 278 | showMastership: showMastership, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 279 | topo2PeerRegions: topo2PeerRegions, |
| 280 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 281 | updateNodes: updateNodes, |
| 282 | updateLinks: updateLinks, |
| 283 | resetAllLocations: resetAllLocations, |
| 284 | unpin: unpin |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 285 | }; |
| 286 | }]); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 287 | })(); |