blob: 68b1ad1cca72db204c37fd1e4310fc3c876e26fd [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 Burrows68d6f952017-03-10 13:53:35 +000089 this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) });
90 this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) });
91 this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) });
92 this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) });
93 this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) });
Steven Burrowsaf96a212016-12-28 12:57:02 +000094
95 // Hide Breadcrumbs if there are no subregions configured in the root region
96 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
97 t2bcs.hide();
98 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000099
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000100 this.layout.createForceLayout();
Steven Burrows6501de92017-04-12 15:10:34 -0700101 this.displayNoDevs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000102 },
103 clear: function () {
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000104 this.regionData = null;
Steven Burrows6de27f42017-03-30 16:21:27 +0100105 this.createEmptyModel();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000106 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700107 removePort: function (key) {
108 var regex = new RegExp('^[^/]*');
109 return regex.exec(key)[0];
110 },
Steven Burrows4a1b5532017-04-28 11:08:07 -0400111 assignPeerLocations: function () {
112 var _this = this;
113 _.each(this.regionData.peerLocations, function (location, id) {
114 _.each(_this.peers, function (peer) {
115 if (peer.id === id) {
116 peer.location = location;
117 }
118 })
119 });
120 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700121 sortMultiLinks: function () {
122 var _this = this,
123 deviceConnections = {};
124
125 _.each(this.regionData.links, function (link) {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700126
Steven Burrows9ce0e402017-04-20 16:39:17 -0400127 var epA = _this.removePort(link.epA),
128 epB = _this.removePort(link.epB),
129 key = epA + '~' + epB,
130 collection = deviceConnections[key] || [],
131 dup = _.find(collection, link);
Steven Burrows32ce1d92017-04-13 13:18:44 -0700132
Steven Burrows0bc66652017-04-20 13:04:10 -0400133 // TODO: Investigate why region contains dup links?!?!
134 // FIXME: This shouldn't be needed - The backend is sending dups
135 // and this is preventing the client thinking its a multilink
Steven Burrows9ce0e402017-04-20 16:39:17 -0400136 if (!dup) {
137 collection.push(link);
Steven Burrows0bc66652017-04-20 13:04:10 -0400138 }
Steven Burrows9ce0e402017-04-20 16:39:17 -0400139
140 deviceConnections[key] = collection;
Steven Burrows32ce1d92017-04-13 13:18:44 -0700141 });
142
Steven Burrows9ce0e402017-04-20 16:39:17 -0400143 _.forIn(deviceConnections, function (collection) {
144 if (collection.length > 1) {
145 _.each(collection, function (link, index) {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700146 link.multiline = {
Steven Burrows9ce0e402017-04-20 16:39:17 -0400147 deviceLinks: collection.length,
Steven Burrows32ce1d92017-04-13 13:18:44 -0700148 index: index
Steven Burrows9ce0e402017-04-20 16:39:17 -0400149 }
150 });
Steven Burrows32ce1d92017-04-13 13:18:44 -0700151 }
Steven Burrows9ce0e402017-04-20 16:39:17 -0400152 })
153
Steven Burrows32ce1d92017-04-13 13:18:44 -0700154 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000155 isRootRegion: function () {
156 return this.model.get('id') === ROOT;
157 },
158 findNodeById: function (link, id) {
159 if (link.get('type') !== 'UiEdgeLink') {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700160 id = this.removePort(id);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000161 }
162 return this.model.get('devices').get(id) ||
163 this.model.get('hosts').get(id) ||
164 this.model.get('subregions').get(id);
165 },
166 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000167 if (this.model) {
168 return [].concat(
169 this.model.get('devices').models,
170 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000171 this.model.get('subregions').models,
172 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000173 );
174 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000175 return [];
176 },
177 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000178 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000179 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000180 getLink: function (linkId) {
181 return this.model.get('links').get(linkId);
182 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000183 getDevice: function (deviceId) {
184 return this.model.get('devices').get(deviceId);
185 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000186 filterRegionNodes: function (predicate) {
187 var nodes = this.regionNodes();
188 return _.filter(nodes, predicate);
189 },
190 deselectAllNodes: function () {
191 var selected = this.filterRegionNodes(function (node) {
192 return node.get('selected', true);
193 });
194
195 if (selected.length) {
196
197 selected.forEach(function (node) {
198 node.deselect();
199 });
200
201 t2dps().el.hide();
202 return true;
203 }
204
205 return false;
206 },
207 deselectLink: function () {
208 var selected = _.filter(this.regionLinks(), function (link) {
209 return link.get('selected', true);
210 });
211
212 if (selected.length) {
213
214 selected.forEach(function (link) {
215 link.deselect();
216 });
217
218 t2dps().el.hide();
219 return true;
220 }
221
222 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000223 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700224 toggleHosts: function () {
225 var state = this.lookupPrefState('hosts');
226 this.updatePrefState('hosts', !state);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000227
Steven Burrowsaea509d2017-04-12 14:17:47 -0700228 _.each(this.model.get('hosts').models, function (host) {
229 host.setVisibility();
230 });
231
232 _.each(this.model.get('links').models, function (link) {
233 link.setVisibility();
234 });
Steven Burrows02e67f42017-04-13 13:59:43 -0700235
236 return !state;
237 },
238 toggleOfflineDevices: function () {
239 var state = this.lookupPrefState('offline_devices');
240 this.updatePrefState('offline_devices', !state);
241 _.each(this.regionNodes(), function (node) {
242 node.setOfflineVisibility();
243 });
244
245 return !state;
Steven Burrowsaea509d2017-04-12 14:17:47 -0700246 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000247 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000248
Steven Burrows6de27f42017-03-30 16:21:27 +0100249 if (!this.isLoadComplete()){
250 this.layout.createForceLayout();
251 }
252
Steven Burrowsb15a3942017-01-17 17:25:04 +0000253 if (this[event.type]) {
254 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000255 } else {
256 $log.error("Unhanded topology update", event);
257 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000258
259 this.layout.update()
Steven Burrows6501de92017-04-12 15:10:34 -0700260 this.displayNoDevs();
261 },
262 displayNoDevs: function () {
263 if (this.regionNodes().length > 0) {
264 t2ndcs.hide();
265 } else {
266 t2ndcs.show();
267 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000268 },
269
270 // Topology update event handlers
271 LINK_ADDED_OR_UPDATED: function (event) {
Steven Burrows6de27f42017-03-30 16:21:27 +0100272
273 var regionLinks = this.model.get('links'),
274 device;
275
276 if (!regionLinks) {
277 this.model.set({ links: t2ls.createLinkCollection([], this) })
278 }
279
Steven Burrowsb15a3942017-01-17 17:25:04 +0000280 if (event.memo === 'added') {
281 var link = this.model.get('links').add(event.data);
282 link.createLink();
Steven Burrows6de27f42017-03-30 16:21:27 +0100283 $log.debug('Added Link', link);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000284 }
285 },
286 LINK_REMOVED: function (event) {
287 var link = this.getLink(event.subject);
288 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000289 this.model.get('links').remove(link);
290 },
291 DEVICE_ADDED_OR_UPDATED: function (event) {
292
Steven Burrows6de27f42017-03-30 16:21:27 +0100293 var regionDevices = this.model.get('devices'),
294 device;
295
296 if (!regionDevices) {
297 this.model.set({ devices: t2ds.createDeviceCollection([], this) })
298 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000299
300 if (event.memo === 'added') {
301 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800302 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000303 } else if (event.memo === 'updated') {
304 device = this.getDevice(event.subject);
305 device.set(event.data);
306 }
307 },
308 DEVICE_REMOVED: function (event) {
309 device.remove();
Steven Burrows6de27f42017-03-30 16:21:27 +0100310 },
311 HOST_ADDED_OR_UPDATED: function (event) {
312 var regionHosts = this.model.get('hosts'),
313 host;
314
315 if (!regionHosts) {
316 this.model.set({ hosts: t2hs.createHostCollection([], this) })
317 }
318
319 if (event.memo === 'added') {
320 host = this.model.get('hosts').add(event.data);
321 $log.debug('Added host', host);
322 }
323 },
324 REGION_ADDED_OR_UPDATED: function (event) {
325 var regionSubRegions = this.model.get('subregions'),
326 region;
327
328 if (!regionSubRegions) {
329 this.model.set({ subregions: t2sr.createSubRegionCollection([], this) })
330 }
331
332 if (event.memo === 'added') {
333 region = this.model.get('subregions').add(event.data);
334 $log.debug('Added region', region);
335 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000336 }
337 });
338
339 function getInstance() {
340 return instance || new Region();
341 }
342
343 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000344
Steven Burrows57e24e92016-08-04 18:38:24 +0100345 }]);
346
347})();