blob: 643aaa5f31dad9eec8a1d827da30130c0bf3b1de [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();
87
Steven Burrows68d6f952017-03-10 13:53:35 +000088 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 Burrowsaf96a212016-12-28 12:57:02 +000093
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 Burrows3cc0c372017-02-10 15:15:24 +000098
Steven Burrowsb11a8b82017-03-10 16:00:31 +000099 this.layout.createForceLayout();
Steven Burrows6501de92017-04-12 15:10:34 -0700100 this.displayNoDevs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000101 },
102 clear: function () {
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000103 this.regionData = null;
Steven Burrows6de27f42017-03-30 16:21:27 +0100104 this.createEmptyModel();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000105 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700106 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 Burrows0bc66652017-04-20 13:04:10 -0400123 // 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 Burrows32ce1d92017-04-13 13:18:44 -0700129 });
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 Burrowsaf96a212016-12-28 12:57:02 +0000143 isRootRegion: function () {
144 return this.model.get('id') === ROOT;
145 },
146 findNodeById: function (link, id) {
147 if (link.get('type') !== 'UiEdgeLink') {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700148 id = this.removePort(id);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000149 }
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 Burrowsaf96a212016-12-28 12:57:02 +0000155 if (this.model) {
156 return [].concat(
157 this.model.get('devices').models,
158 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000159 this.model.get('subregions').models,
160 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000161 );
162 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000163 return [];
164 },
165 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000166 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000167 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000168 getLink: function (linkId) {
169 return this.model.get('links').get(linkId);
170 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000171 getDevice: function (deviceId) {
172 return this.model.get('devices').get(deviceId);
173 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000174 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 Burrowsb15a3942017-01-17 17:25:04 +0000211 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700212 toggleHosts: function () {
213 var state = this.lookupPrefState('hosts');
214 this.updatePrefState('hosts', !state);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000215
Steven Burrowsaea509d2017-04-12 14:17:47 -0700216 _.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 Burrows02e67f42017-04-13 13:59:43 -0700223
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 Burrowsaea509d2017-04-12 14:17:47 -0700234 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000235 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000236
Steven Burrows6de27f42017-03-30 16:21:27 +0100237 if (!this.isLoadComplete()){
238 this.layout.createForceLayout();
239 }
240
Steven Burrowsb15a3942017-01-17 17:25:04 +0000241 if (this[event.type]) {
242 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000243 } else {
244 $log.error("Unhanded topology update", event);
245 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000246
247 this.layout.update()
Steven Burrows6501de92017-04-12 15:10:34 -0700248 this.displayNoDevs();
249 },
250 displayNoDevs: function () {
251 if (this.regionNodes().length > 0) {
252 t2ndcs.hide();
253 } else {
254 t2ndcs.show();
255 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000256 },
257
258 // Topology update event handlers
259 LINK_ADDED_OR_UPDATED: function (event) {
Steven Burrows6de27f42017-03-30 16:21:27 +0100260
261 var regionLinks = this.model.get('links'),
262 device;
263
264 if (!regionLinks) {
265 this.model.set({ links: t2ls.createLinkCollection([], this) })
266 }
267
Steven Burrowsb15a3942017-01-17 17:25:04 +0000268 if (event.memo === 'added') {
269 var link = this.model.get('links').add(event.data);
270 link.createLink();
Steven Burrows6de27f42017-03-30 16:21:27 +0100271 $log.debug('Added Link', link);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000272 }
273 },
274 LINK_REMOVED: function (event) {
275 var link = this.getLink(event.subject);
276 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000277 this.model.get('links').remove(link);
278 },
279 DEVICE_ADDED_OR_UPDATED: function (event) {
280
Steven Burrows6de27f42017-03-30 16:21:27 +0100281 var regionDevices = this.model.get('devices'),
282 device;
283
284 if (!regionDevices) {
285 this.model.set({ devices: t2ds.createDeviceCollection([], this) })
286 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000287
288 if (event.memo === 'added') {
289 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800290 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000291 } 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 Burrows6de27f42017-03-30 16:21:27 +0100298 },
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 Burrowsaf96a212016-12-28 12:57:02 +0000324 }
325 });
326
327 function getInstance() {
328 return instance || new Region();
329 }
330
331 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000332
Steven Burrows57e24e92016-08-04 18:38:24 +0100333 }]);
334
335})();