Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [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 Region Module. |
| 19 | Module that holds the current region in memory |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 25 | // Injected Services |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 26 | var Model; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 28 | // Internal |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 29 | var instance; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 30 | |
Steven Burrows | 8ea5dea | 2016-12-27 13:28:41 +0000 | [diff] [blame] | 31 | // 'static' vars |
| 32 | var ROOT = '(root)'; |
| 33 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 34 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 35 | .factory('Topo2RegionService', [ |
| 36 | '$log', 'Topo2Model', 'Topo2SubRegionService', 'Topo2DeviceService', |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 37 | 'Topo2HostService', 'Topo2LinkService', 'Topo2ZoomService', 'Topo2DetailsPanelService', |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 38 | 'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService', |
Steven Burrows | 6501de9 | 2017-04-12 15:10:34 -0700 | [diff] [blame] | 39 | 'Topo2MapConfigService', 'Topo2PeerRegionService', 'Topo2NoDevicesConnectedService', |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 40 | function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, |
Steven Burrows | 6501de9 | 2017-04-12 15:10:34 -0700 | [diff] [blame] | 41 | t2sls, t2ms, t2mcs, t2pr, t2ndcs) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 42 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 43 | Model = _Model_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 44 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 45 | var Region = ViewController.extend({ |
| 46 | initialize: function () { |
| 47 | instance = this; |
| 48 | this.model = null; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 49 | |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 50 | this.bgRendered = false; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 51 | this.regionData = null; |
| 52 | this.peers = null; |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 53 | |
| 54 | var RegionModel = Model.extend({ |
| 55 | findNodeById: this.findNodeById, |
| 56 | nodes: this.regionNodes.bind(this) |
| 57 | }); |
| 58 | |
| 59 | this.model = new RegionModel(); |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 60 | this.createEmptyModel(); |
| 61 | |
| 62 | }, |
| 63 | createEmptyModel: function () { |
| 64 | this.model.set({ subregions: t2sr.createSubRegionCollection([], this) }); |
| 65 | this.model.set({ devices: t2ds.createDeviceCollection([], this) }); |
| 66 | this.model.set({ hosts: t2hs.createHostCollection([], this) }); |
| 67 | this.model.set({ peerRegions: t2pr.createCollection([], this) }); |
| 68 | this.model.set({ links: t2ls.createLinkCollection([], this) }); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 69 | }, |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 70 | isLoadComplete: function() { |
| 71 | return this.bgRendered && this.regionData && this.peers; |
| 72 | }, |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 73 | loaded: function (key, value) { |
| 74 | this[key] = value; |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 75 | if (this.isLoadComplete()) { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 76 | this.startRegion(); |
| 77 | } |
| 78 | }, |
| 79 | startRegion: function () { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 80 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 81 | this.model.set({ |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 82 | id: this.regionData.id, |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 83 | layerOrder: this.regionData.layerOrder |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 84 | }); |
| 85 | |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 86 | this.sortMultiLinks(); |
| 87 | |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 88 | this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) }); |
| 89 | this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) }); |
| 90 | this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) }); |
| 91 | this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) }); |
| 92 | this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) }); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 93 | |
| 94 | // Hide Breadcrumbs if there are no subregions configured in the root region |
| 95 | if (this.isRootRegion() && !this.model.get('subregions').models.length) { |
| 96 | t2bcs.hide(); |
| 97 | } |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 98 | |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 99 | this.layout.createForceLayout(); |
Steven Burrows | 6501de9 | 2017-04-12 15:10:34 -0700 | [diff] [blame] | 100 | this.displayNoDevs(); |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 101 | }, |
| 102 | clear: function () { |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 103 | this.regionData = null; |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 104 | this.createEmptyModel(); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 105 | }, |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 106 | removePort: function (key) { |
| 107 | var regex = new RegExp('^[^/]*'); |
| 108 | return regex.exec(key)[0]; |
| 109 | }, |
| 110 | sortMultiLinks: function () { |
| 111 | var _this = this, |
| 112 | deviceConnections = {}; |
| 113 | |
| 114 | _.each(this.regionData.links, function (link) { |
| 115 | var devA = _this.removePort(link.epA), |
| 116 | devB = _this.removePort(link.epB), |
| 117 | key = devA + '~' + devB; |
| 118 | |
| 119 | if (!deviceConnections[key]) { |
| 120 | deviceConnections[key] = []; |
| 121 | } |
| 122 | |
Steven Burrows | 0bc6665 | 2017-04-20 13:04:10 -0400 | [diff] [blame^] | 123 | // TODO: Investigate why region contains dup links?!?! |
| 124 | // FIXME: This shouldn't be needed - The backend is sending dups |
| 125 | // and this is preventing the client thinking its a multilink |
| 126 | if (deviceConnections[key].indexOf(link) > -1) { |
| 127 | deviceConnections[key].push(link); |
| 128 | } |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 129 | }); |
| 130 | |
| 131 | _.each(deviceConnections, function (connection) { |
| 132 | if (connection.length > 1) { |
| 133 | _.orderBy(connection, ['portA']); |
| 134 | _.each(connection, function (link, index) { |
| 135 | link.multiline = { |
| 136 | deviceLinks: connection.length, |
| 137 | index: index |
| 138 | }; |
| 139 | }) |
| 140 | } |
| 141 | }); |
| 142 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 143 | isRootRegion: function () { |
| 144 | return this.model.get('id') === ROOT; |
| 145 | }, |
| 146 | findNodeById: function (link, id) { |
| 147 | if (link.get('type') !== 'UiEdgeLink') { |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 148 | id = this.removePort(id); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 149 | } |
| 150 | return this.model.get('devices').get(id) || |
| 151 | this.model.get('hosts').get(id) || |
| 152 | this.model.get('subregions').get(id); |
| 153 | }, |
| 154 | regionNodes: function () { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 155 | if (this.model) { |
| 156 | return [].concat( |
| 157 | this.model.get('devices').models, |
| 158 | this.model.get('hosts').models, |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 159 | this.model.get('subregions').models, |
| 160 | this.model.get('peerRegions').models |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 161 | ); |
| 162 | } |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 163 | return []; |
| 164 | }, |
| 165 | regionLinks: function () { |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 166 | return this.model.get('links').models; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 167 | }, |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 168 | getLink: function (linkId) { |
| 169 | return this.model.get('links').get(linkId); |
| 170 | }, |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 171 | getDevice: function (deviceId) { |
| 172 | return this.model.get('devices').get(deviceId); |
| 173 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 174 | filterRegionNodes: function (predicate) { |
| 175 | var nodes = this.regionNodes(); |
| 176 | return _.filter(nodes, predicate); |
| 177 | }, |
| 178 | deselectAllNodes: function () { |
| 179 | var selected = this.filterRegionNodes(function (node) { |
| 180 | return node.get('selected', true); |
| 181 | }); |
| 182 | |
| 183 | if (selected.length) { |
| 184 | |
| 185 | selected.forEach(function (node) { |
| 186 | node.deselect(); |
| 187 | }); |
| 188 | |
| 189 | t2dps().el.hide(); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | }, |
| 195 | deselectLink: function () { |
| 196 | var selected = _.filter(this.regionLinks(), function (link) { |
| 197 | return link.get('selected', true); |
| 198 | }); |
| 199 | |
| 200 | if (selected.length) { |
| 201 | |
| 202 | selected.forEach(function (link) { |
| 203 | link.deselect(); |
| 204 | }); |
| 205 | |
| 206 | t2dps().el.hide(); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | return false; |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 211 | }, |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 212 | toggleHosts: function () { |
| 213 | var state = this.lookupPrefState('hosts'); |
| 214 | this.updatePrefState('hosts', !state); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 215 | |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 216 | _.each(this.model.get('hosts').models, function (host) { |
| 217 | host.setVisibility(); |
| 218 | }); |
| 219 | |
| 220 | _.each(this.model.get('links').models, function (link) { |
| 221 | link.setVisibility(); |
| 222 | }); |
Steven Burrows | 02e67f4 | 2017-04-13 13:59:43 -0700 | [diff] [blame] | 223 | |
| 224 | return !state; |
| 225 | }, |
| 226 | toggleOfflineDevices: function () { |
| 227 | var state = this.lookupPrefState('offline_devices'); |
| 228 | this.updatePrefState('offline_devices', !state); |
| 229 | _.each(this.regionNodes(), function (node) { |
| 230 | node.setOfflineVisibility(); |
| 231 | }); |
| 232 | |
| 233 | return !state; |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 234 | }, |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 235 | update: function (event) { |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 236 | |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 237 | if (!this.isLoadComplete()){ |
| 238 | this.layout.createForceLayout(); |
| 239 | } |
| 240 | |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 241 | if (this[event.type]) { |
| 242 | this[event.type](event); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 243 | } else { |
| 244 | $log.error("Unhanded topology update", event); |
| 245 | } |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 246 | |
| 247 | this.layout.update() |
Steven Burrows | 6501de9 | 2017-04-12 15:10:34 -0700 | [diff] [blame] | 248 | this.displayNoDevs(); |
| 249 | }, |
| 250 | displayNoDevs: function () { |
| 251 | if (this.regionNodes().length > 0) { |
| 252 | t2ndcs.hide(); |
| 253 | } else { |
| 254 | t2ndcs.show(); |
| 255 | } |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 256 | }, |
| 257 | |
| 258 | // Topology update event handlers |
| 259 | LINK_ADDED_OR_UPDATED: function (event) { |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 260 | |
| 261 | var regionLinks = this.model.get('links'), |
| 262 | device; |
| 263 | |
| 264 | if (!regionLinks) { |
| 265 | this.model.set({ links: t2ls.createLinkCollection([], this) }) |
| 266 | } |
| 267 | |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 268 | if (event.memo === 'added') { |
| 269 | var link = this.model.get('links').add(event.data); |
| 270 | link.createLink(); |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 271 | $log.debug('Added Link', link); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 272 | } |
| 273 | }, |
| 274 | LINK_REMOVED: function (event) { |
| 275 | var link = this.getLink(event.subject); |
| 276 | link.remove(); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 277 | this.model.get('links').remove(link); |
| 278 | }, |
| 279 | DEVICE_ADDED_OR_UPDATED: function (event) { |
| 280 | |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 281 | var regionDevices = this.model.get('devices'), |
| 282 | device; |
| 283 | |
| 284 | if (!regionDevices) { |
| 285 | this.model.set({ devices: t2ds.createDeviceCollection([], this) }) |
| 286 | } |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 287 | |
| 288 | if (event.memo === 'added') { |
| 289 | device = this.model.get('devices').add(event.data); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 290 | $log.debug('Added device', device); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 291 | } else if (event.memo === 'updated') { |
| 292 | device = this.getDevice(event.subject); |
| 293 | device.set(event.data); |
| 294 | } |
| 295 | }, |
| 296 | DEVICE_REMOVED: function (event) { |
| 297 | device.remove(); |
Steven Burrows | 6de27f4 | 2017-03-30 16:21:27 +0100 | [diff] [blame] | 298 | }, |
| 299 | HOST_ADDED_OR_UPDATED: function (event) { |
| 300 | var regionHosts = this.model.get('hosts'), |
| 301 | host; |
| 302 | |
| 303 | if (!regionHosts) { |
| 304 | this.model.set({ hosts: t2hs.createHostCollection([], this) }) |
| 305 | } |
| 306 | |
| 307 | if (event.memo === 'added') { |
| 308 | host = this.model.get('hosts').add(event.data); |
| 309 | $log.debug('Added host', host); |
| 310 | } |
| 311 | }, |
| 312 | REGION_ADDED_OR_UPDATED: function (event) { |
| 313 | var regionSubRegions = this.model.get('subregions'), |
| 314 | region; |
| 315 | |
| 316 | if (!regionSubRegions) { |
| 317 | this.model.set({ subregions: t2sr.createSubRegionCollection([], this) }) |
| 318 | } |
| 319 | |
| 320 | if (event.memo === 'added') { |
| 321 | region = this.model.get('subregions').add(event.data); |
| 322 | $log.debug('Added region', region); |
| 323 | } |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 324 | } |
| 325 | }); |
| 326 | |
| 327 | function getInstance() { |
| 328 | return instance || new Region(); |
| 329 | } |
| 330 | |
| 331 | return getInstance(); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 332 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 333 | }]); |
| 334 | |
| 335 | })(); |