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', |
| 39 | 'Topo2MapConfigService', |
| 40 | function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls, t2ms, t2mcs) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 41 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 42 | Model = _Model_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 43 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 44 | var Region = ViewController.extend({ |
| 45 | initialize: function () { |
| 46 | instance = this; |
| 47 | this.model = null; |
| 48 | }, |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 49 | addLayout: function (data) { |
| 50 | |
Steven Burrows | 86b74fc | 2017-02-22 00:15:16 +0000 | [diff] [blame] | 51 | var _this = this; |
| 52 | |
| 53 | if (data.bgType === 'geo') { |
| 54 | t2ms.setMap({ |
| 55 | mapid: data.bgId, |
| 56 | |
| 57 | // TODO: The following value will be specified in the Topo2CurrentLayout Payload |
| 58 | mapfilepath: "*bayarea" |
| 59 | }).then(function () { |
| 60 | _.each(_this.regionNodes(), function (node) { |
| 61 | node.resetPosition(); |
| 62 | }); |
| 63 | }); |
| 64 | t2ms.show(); |
| 65 | } else { |
| 66 | t2ms.hide(); |
| 67 | } |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 68 | |
| 69 | if (data.bgType === 'grid') { |
| 70 | t2sls.loadLayout(data.bgId); |
| 71 | t2sls.show(); |
| 72 | } else { |
| 73 | t2sls.hide(); |
| 74 | } |
| 75 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 76 | addRegion: function (data) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 77 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 78 | var RegionModel = Model.extend({ |
| 79 | findNodeById: this.findNodeById, |
| 80 | nodes: this.regionNodes.bind(this) |
| 81 | }); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 82 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 83 | this.model = new RegionModel({ |
| 84 | id: data.id, |
| 85 | layerOrder: data.layerOrder |
| 86 | }); |
| 87 | |
| 88 | this.model.set({ |
| 89 | subregions: t2sr.createSubRegionCollection(data.subregions, this), |
| 90 | devices: t2ds.createDeviceCollection(data.devices, this), |
| 91 | hosts: t2hs.createHostCollection(data.hosts, this), |
| 92 | links: t2ls.createLinkCollection(data.links, this) |
| 93 | }); |
| 94 | |
| 95 | angular.forEach(this.model.get('links').models, function (link) { |
| 96 | link.createLink(); |
| 97 | }); |
| 98 | |
| 99 | // Hide Breadcrumbs if there are no subregions configured in the root region |
| 100 | if (this.isRootRegion() && !this.model.get('subregions').models.length) { |
| 101 | t2bcs.hide(); |
| 102 | } |
Steven Burrows | 3cc0c37 | 2017-02-10 15:15:24 +0000 | [diff] [blame] | 103 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 104 | }, |
| 105 | isRootRegion: function () { |
| 106 | return this.model.get('id') === ROOT; |
| 107 | }, |
| 108 | findNodeById: function (link, id) { |
| 109 | if (link.get('type') !== 'UiEdgeLink') { |
| 110 | // Remove /{port} from id if needed |
| 111 | var regex = new RegExp('^[^/]*'); |
| 112 | id = regex.exec(id)[0]; |
| 113 | } |
| 114 | return this.model.get('devices').get(id) || |
| 115 | this.model.get('hosts').get(id) || |
| 116 | this.model.get('subregions').get(id); |
| 117 | }, |
| 118 | regionNodes: function () { |
| 119 | |
| 120 | if (this.model) { |
| 121 | return [].concat( |
| 122 | this.model.get('devices').models, |
| 123 | this.model.get('hosts').models, |
| 124 | this.model.get('subregions').models |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | return []; |
| 129 | }, |
| 130 | regionLinks: function () { |
| 131 | return (this.model) ? this.model.get('links').models : []; |
| 132 | }, |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 133 | getLink: function (linkId) { |
| 134 | return this.model.get('links').get(linkId); |
| 135 | }, |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 136 | getDevice: function (deviceId) { |
| 137 | return this.model.get('devices').get(deviceId); |
| 138 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 139 | filterRegionNodes: function (predicate) { |
| 140 | var nodes = this.regionNodes(); |
| 141 | return _.filter(nodes, predicate); |
| 142 | }, |
| 143 | deselectAllNodes: function () { |
| 144 | var selected = this.filterRegionNodes(function (node) { |
| 145 | return node.get('selected', true); |
| 146 | }); |
| 147 | |
| 148 | if (selected.length) { |
| 149 | |
| 150 | selected.forEach(function (node) { |
| 151 | node.deselect(); |
| 152 | }); |
| 153 | |
| 154 | t2dps().el.hide(); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | return false; |
| 159 | }, |
| 160 | deselectLink: function () { |
| 161 | var selected = _.filter(this.regionLinks(), function (link) { |
| 162 | return link.get('selected', true); |
| 163 | }); |
| 164 | |
| 165 | if (selected.length) { |
| 166 | |
| 167 | selected.forEach(function (link) { |
| 168 | link.deselect(); |
| 169 | }); |
| 170 | |
| 171 | t2dps().el.hide(); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | return false; |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 176 | }, |
| 177 | |
| 178 | update: function (event) { |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 179 | |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 180 | if (this[event.type]) { |
| 181 | this[event.type](event); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 182 | } else { |
| 183 | $log.error("Unhanded topology update", event); |
| 184 | } |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 185 | |
| 186 | this.layout.update() |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 187 | }, |
| 188 | |
| 189 | // Topology update event handlers |
| 190 | LINK_ADDED_OR_UPDATED: function (event) { |
| 191 | if (event.memo === 'added') { |
| 192 | var link = this.model.get('links').add(event.data); |
| 193 | link.createLink(); |
| 194 | } |
| 195 | }, |
| 196 | LINK_REMOVED: function (event) { |
| 197 | var link = this.getLink(event.subject); |
| 198 | link.remove(); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 199 | this.model.get('links').remove(link); |
| 200 | }, |
| 201 | DEVICE_ADDED_OR_UPDATED: function (event) { |
| 202 | |
| 203 | var device; |
| 204 | |
| 205 | if (event.memo === 'added') { |
| 206 | device = this.model.get('devices').add(event.data); |
Simon Hunt | eb3cf54 | 2017-02-10 13:18:41 -0800 | [diff] [blame] | 207 | $log.debug('Added device', device); |
Steven Burrows | 42eb9e2 | 2017-02-06 14:20:24 +0000 | [diff] [blame] | 208 | } else if (event.memo === 'updated') { |
| 209 | device = this.getDevice(event.subject); |
| 210 | device.set(event.data); |
| 211 | } |
| 212 | }, |
| 213 | DEVICE_REMOVED: function (event) { |
| 214 | device.remove(); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 215 | } |
| 216 | }); |
| 217 | |
| 218 | function getInstance() { |
| 219 | return instance || new Region(); |
| 220 | } |
| 221 | |
| 222 | return getInstance(); |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 223 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 224 | }]); |
| 225 | |
| 226 | })(); |