blob: cd4a75b2f8d15114a4deb9d5975dae215a41ff18 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +01001/*
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 Burrows6deb4ce2016-08-26 16:06:23 +010025 // Injected Services
Steven Burrowsdfa52b02016-09-02 13:50:43 +010026 var Model;
Steven Burrows57e24e92016-08-04 18:38:24 +010027
Steven Burrowsdfa52b02016-09-02 13:50:43 +010028 // Internal
Steven Burrowsb15a3942017-01-17 17:25:04 +000029 var instance;
Steven Burrows57e24e92016-08-04 18:38:24 +010030
Steven Burrows8ea5dea2016-12-27 13:28:41 +000031 // 'static' vars
32 var ROOT = '(root)';
33
Steven Burrows57e24e92016-08-04 18:38:24 +010034 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +000035 .factory('Topo2RegionService', [
36 '$log', 'Topo2Model', 'Topo2SubRegionService', 'Topo2DeviceService',
Steven Burrows1aa4f582016-12-13 15:05:41 -050037 'Topo2HostService', 'Topo2LinkService', 'Topo2ZoomService', 'Topo2DetailsPanelService',
Steven Burrows86b74fc2017-02-22 00:15:16 +000038 'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService',
Steven Burrows6501de92017-04-12 15:10:34 -070039 'Topo2MapConfigService', 'Topo2PeerRegionService', 'Topo2NoDevicesConnectedService',
Steven Burrowsb11a8b82017-03-10 16:00:31 +000040 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController,
Steven Burrows6501de92017-04-12 15:10:34 -070041 t2sls, t2ms, t2mcs, t2pr, t2ndcs) {
Steven Burrows57e24e92016-08-04 18:38:24 +010042
Steven Burrowsdfa52b02016-09-02 13:50:43 +010043 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +010044
Steven Burrowsaf96a212016-12-28 12:57:02 +000045 var Region = ViewController.extend({
46 initialize: function () {
47 instance = this;
48 this.model = null;
Steven Burrows68d6f952017-03-10 13:53:35 +000049
Steven Burrowsb43c1a92017-03-07 17:13:28 +000050 this.bgRendered = false;
Steven Burrows68d6f952017-03-10 13:53:35 +000051 this.regionData = null;
52 this.peers = null;
Steven Burrowsb11a8b82017-03-10 16:00:31 +000053
54 var RegionModel = Model.extend({
55 findNodeById: this.findNodeById,
56 nodes: this.regionNodes.bind(this)
57 });
58
59 this.model = new RegionModel();
Steven Burrows6de27f42017-03-30 16:21:27 +010060 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 Burrowsaf96a212016-12-28 12:57:02 +000069 },
Steven Burrows247ab152017-03-29 13:55:54 +010070 isLoadComplete: function() {
71 return this.bgRendered && this.regionData && this.peers;
72 },
Steven Burrows68d6f952017-03-10 13:53:35 +000073 loaded: function (key, value) {
74 this[key] = value;
Steven Burrows247ab152017-03-29 13:55:54 +010075 if (this.isLoadComplete()) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000076 this.startRegion();
77 }
78 },
79 startRegion: function () {
Steven Burrows57e24e92016-08-04 18:38:24 +010080
Steven Burrowsaf96a212016-12-28 12:57:02 +000081 this.model.set({
Steven Burrowsb11a8b82017-03-10 16:00:31 +000082 id: this.regionData.id,
Steven Burrows68d6f952017-03-10 13:53:35 +000083 layerOrder: this.regionData.layerOrder
Steven Burrowsaf96a212016-12-28 12:57:02 +000084 });
85
Steven Burrows32ce1d92017-04-13 13:18:44 -070086 this.sortMultiLinks();
Steven Burrows4a1b5532017-04-28 11:08:07 -040087 this.assignPeerLocations();
Steven Burrows32ce1d92017-04-13 13:18:44 -070088
Steven Burrows2b29ca42017-05-12 10:58:05 -040089 // TODO: RegionLinks are dublicated in JSON Payload
90
Steven Burrows68d6f952017-03-10 13:53:35 +000091 this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) });
92 this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) });
93 this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) });
94 this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) });
95 this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) });
Steven Burrowsaf96a212016-12-28 12:57:02 +000096
97 // Hide Breadcrumbs if there are no subregions configured in the root region
98 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
99 t2bcs.hide();
100 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000101
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000102 this.layout.createForceLayout();
Steven Burrows6501de92017-04-12 15:10:34 -0700103 this.displayNoDevs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000104 },
105 clear: function () {
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000106 this.regionData = null;
Steven Burrows6de27f42017-03-30 16:21:27 +0100107 this.createEmptyModel();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000108 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700109 removePort: function (key) {
110 var regex = new RegExp('^[^/]*');
111 return regex.exec(key)[0];
112 },
Steven Burrows4a1b5532017-04-28 11:08:07 -0400113 assignPeerLocations: function () {
114 var _this = this;
115 _.each(this.regionData.peerLocations, function (location, id) {
116 _.each(_this.peers, function (peer) {
117 if (peer.id === id) {
118 peer.location = location;
119 }
120 })
121 });
122 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700123 sortMultiLinks: function () {
124 var _this = this,
125 deviceConnections = {};
126
127 _.each(this.regionData.links, function (link) {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700128
Steven Burrows9ce0e402017-04-20 16:39:17 -0400129 var epA = _this.removePort(link.epA),
130 epB = _this.removePort(link.epB),
131 key = epA + '~' + epB,
132 collection = deviceConnections[key] || [],
133 dup = _.find(collection, link);
Steven Burrows32ce1d92017-04-13 13:18:44 -0700134
Steven Burrows0bc66652017-04-20 13:04:10 -0400135 // TODO: Investigate why region contains dup links?!?!
136 // FIXME: This shouldn't be needed - The backend is sending dups
137 // and this is preventing the client thinking its a multilink
Steven Burrows9ce0e402017-04-20 16:39:17 -0400138 if (!dup) {
139 collection.push(link);
Steven Burrows0bc66652017-04-20 13:04:10 -0400140 }
Steven Burrows9ce0e402017-04-20 16:39:17 -0400141
142 deviceConnections[key] = collection;
Steven Burrows32ce1d92017-04-13 13:18:44 -0700143 });
144
Steven Burrows9ce0e402017-04-20 16:39:17 -0400145 _.forIn(deviceConnections, function (collection) {
146 if (collection.length > 1) {
147 _.each(collection, function (link, index) {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700148 link.multiline = {
Steven Burrows9ce0e402017-04-20 16:39:17 -0400149 deviceLinks: collection.length,
Steven Burrows32ce1d92017-04-13 13:18:44 -0700150 index: index
Steven Burrows9ce0e402017-04-20 16:39:17 -0400151 }
152 });
Steven Burrows32ce1d92017-04-13 13:18:44 -0700153 }
Steven Burrows9ce0e402017-04-20 16:39:17 -0400154 })
155
Steven Burrows32ce1d92017-04-13 13:18:44 -0700156 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000157 isRootRegion: function () {
158 return this.model.get('id') === ROOT;
159 },
160 findNodeById: function (link, id) {
161 if (link.get('type') !== 'UiEdgeLink') {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700162 id = this.removePort(id);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000163 }
164 return this.model.get('devices').get(id) ||
165 this.model.get('hosts').get(id) ||
166 this.model.get('subregions').get(id);
167 },
168 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000169 if (this.model) {
170 return [].concat(
171 this.model.get('devices').models,
172 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000173 this.model.get('subregions').models,
174 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000175 );
176 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000177 return [];
178 },
179 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000180 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000181 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000182 getLink: function (linkId) {
183 return this.model.get('links').get(linkId);
184 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000185 getDevice: function (deviceId) {
186 return this.model.get('devices').get(deviceId);
187 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000188 filterRegionNodes: function (predicate) {
189 var nodes = this.regionNodes();
190 return _.filter(nodes, predicate);
191 },
192 deselectAllNodes: function () {
193 var selected = this.filterRegionNodes(function (node) {
194 return node.get('selected', true);
195 });
196
197 if (selected.length) {
198
199 selected.forEach(function (node) {
200 node.deselect();
201 });
202
203 t2dps().el.hide();
204 return true;
205 }
206
207 return false;
208 },
209 deselectLink: function () {
210 var selected = _.filter(this.regionLinks(), function (link) {
211 return link.get('selected', true);
212 });
213
214 if (selected.length) {
215
216 selected.forEach(function (link) {
217 link.deselect();
218 });
219
220 t2dps().el.hide();
221 return true;
222 }
223
224 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000225 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700226 toggleHosts: function () {
227 var state = this.lookupPrefState('hosts');
228 this.updatePrefState('hosts', !state);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000229
Steven Burrowsaea509d2017-04-12 14:17:47 -0700230 _.each(this.model.get('hosts').models, function (host) {
231 host.setVisibility();
232 });
233
234 _.each(this.model.get('links').models, function (link) {
235 link.setVisibility();
236 });
Steven Burrows02e67f42017-04-13 13:59:43 -0700237
238 return !state;
239 },
240 toggleOfflineDevices: function () {
241 var state = this.lookupPrefState('offline_devices');
242 this.updatePrefState('offline_devices', !state);
243 _.each(this.regionNodes(), function (node) {
244 node.setOfflineVisibility();
245 });
246
247 return !state;
Steven Burrowsaea509d2017-04-12 14:17:47 -0700248 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000249 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000250
Steven Burrows6de27f42017-03-30 16:21:27 +0100251 if (!this.isLoadComplete()){
252 this.layout.createForceLayout();
253 }
254
Steven Burrowsb15a3942017-01-17 17:25:04 +0000255 if (this[event.type]) {
256 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000257 } else {
258 $log.error("Unhanded topology update", event);
259 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000260
261 this.layout.update()
Steven Burrows6501de92017-04-12 15:10:34 -0700262 this.displayNoDevs();
263 },
264 displayNoDevs: function () {
265 if (this.regionNodes().length > 0) {
266 t2ndcs.hide();
267 } else {
268 t2ndcs.show();
269 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000270 },
271
272 // Topology update event handlers
273 LINK_ADDED_OR_UPDATED: function (event) {
Steven Burrows6de27f42017-03-30 16:21:27 +0100274
275 var regionLinks = this.model.get('links'),
276 device;
277
278 if (!regionLinks) {
279 this.model.set({ links: t2ls.createLinkCollection([], this) })
280 }
281
Steven Burrowsb15a3942017-01-17 17:25:04 +0000282 if (event.memo === 'added') {
283 var link = this.model.get('links').add(event.data);
284 link.createLink();
Steven Burrows6de27f42017-03-30 16:21:27 +0100285 $log.debug('Added Link', link);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000286 }
287 },
288 LINK_REMOVED: function (event) {
289 var link = this.getLink(event.subject);
290 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000291 this.model.get('links').remove(link);
292 },
293 DEVICE_ADDED_OR_UPDATED: function (event) {
294
Steven Burrows6de27f42017-03-30 16:21:27 +0100295 var regionDevices = this.model.get('devices'),
296 device;
297
298 if (!regionDevices) {
299 this.model.set({ devices: t2ds.createDeviceCollection([], this) })
300 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000301
302 if (event.memo === 'added') {
303 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800304 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000305 } else if (event.memo === 'updated') {
306 device = this.getDevice(event.subject);
307 device.set(event.data);
308 }
309 },
310 DEVICE_REMOVED: function (event) {
311 device.remove();
Steven Burrows6de27f42017-03-30 16:21:27 +0100312 },
313 HOST_ADDED_OR_UPDATED: function (event) {
314 var regionHosts = this.model.get('hosts'),
315 host;
316
317 if (!regionHosts) {
318 this.model.set({ hosts: t2hs.createHostCollection([], this) })
319 }
320
321 if (event.memo === 'added') {
322 host = this.model.get('hosts').add(event.data);
323 $log.debug('Added host', host);
324 }
325 },
326 REGION_ADDED_OR_UPDATED: function (event) {
327 var regionSubRegions = this.model.get('subregions'),
328 region;
329
330 if (!regionSubRegions) {
331 this.model.set({ subregions: t2sr.createSubRegionCollection([], this) })
332 }
333
334 if (event.memo === 'added') {
335 region = this.model.get('subregions').add(event.data);
336 $log.debug('Added region', region);
337 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000338 }
339 });
340
341 function getInstance() {
342 return instance || new Region();
343 }
344
345 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000346
Steven Burrows57e24e92016-08-04 18:38:24 +0100347 }]);
348
349})();