blob: f471e8585db44d30c0ba6a1fe46785e7f7e4a738 [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
123 deviceConnections[key].push(link);
124 });
125
126 _.each(deviceConnections, function (connection) {
127 if (connection.length > 1) {
128 _.orderBy(connection, ['portA']);
129 _.each(connection, function (link, index) {
130 link.multiline = {
131 deviceLinks: connection.length,
132 index: index
133 };
134 })
135 }
136 });
137 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000138 isRootRegion: function () {
139 return this.model.get('id') === ROOT;
140 },
141 findNodeById: function (link, id) {
142 if (link.get('type') !== 'UiEdgeLink') {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700143 id = this.removePort(id);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000144 }
145 return this.model.get('devices').get(id) ||
146 this.model.get('hosts').get(id) ||
147 this.model.get('subregions').get(id);
148 },
149 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000150 if (this.model) {
151 return [].concat(
152 this.model.get('devices').models,
153 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000154 this.model.get('subregions').models,
155 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000156 );
157 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000158 return [];
159 },
160 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000161 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000162 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000163 getLink: function (linkId) {
164 return this.model.get('links').get(linkId);
165 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000166 getDevice: function (deviceId) {
167 return this.model.get('devices').get(deviceId);
168 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000169 filterRegionNodes: function (predicate) {
170 var nodes = this.regionNodes();
171 return _.filter(nodes, predicate);
172 },
173 deselectAllNodes: function () {
174 var selected = this.filterRegionNodes(function (node) {
175 return node.get('selected', true);
176 });
177
178 if (selected.length) {
179
180 selected.forEach(function (node) {
181 node.deselect();
182 });
183
184 t2dps().el.hide();
185 return true;
186 }
187
188 return false;
189 },
190 deselectLink: function () {
191 var selected = _.filter(this.regionLinks(), function (link) {
192 return link.get('selected', true);
193 });
194
195 if (selected.length) {
196
197 selected.forEach(function (link) {
198 link.deselect();
199 });
200
201 t2dps().el.hide();
202 return true;
203 }
204
205 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000206 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700207 toggleHosts: function () {
208 var state = this.lookupPrefState('hosts');
209 this.updatePrefState('hosts', !state);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000210
Steven Burrowsaea509d2017-04-12 14:17:47 -0700211 _.each(this.model.get('hosts').models, function (host) {
212 host.setVisibility();
213 });
214
215 _.each(this.model.get('links').models, function (link) {
216 link.setVisibility();
217 });
Steven Burrows02e67f42017-04-13 13:59:43 -0700218
219 return !state;
220 },
221 toggleOfflineDevices: function () {
222 var state = this.lookupPrefState('offline_devices');
223 this.updatePrefState('offline_devices', !state);
224 _.each(this.regionNodes(), function (node) {
225 node.setOfflineVisibility();
226 });
227
228 return !state;
Steven Burrowsaea509d2017-04-12 14:17:47 -0700229 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000230 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000231
Steven Burrows6de27f42017-03-30 16:21:27 +0100232 if (!this.isLoadComplete()){
233 this.layout.createForceLayout();
234 }
235
Steven Burrowsb15a3942017-01-17 17:25:04 +0000236 if (this[event.type]) {
237 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000238 } else {
239 $log.error("Unhanded topology update", event);
240 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000241
242 this.layout.update()
Steven Burrows6501de92017-04-12 15:10:34 -0700243 this.displayNoDevs();
244 },
245 displayNoDevs: function () {
246 if (this.regionNodes().length > 0) {
247 t2ndcs.hide();
248 } else {
249 t2ndcs.show();
250 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000251 },
252
253 // Topology update event handlers
254 LINK_ADDED_OR_UPDATED: function (event) {
Steven Burrows6de27f42017-03-30 16:21:27 +0100255
256 var regionLinks = this.model.get('links'),
257 device;
258
259 if (!regionLinks) {
260 this.model.set({ links: t2ls.createLinkCollection([], this) })
261 }
262
Steven Burrowsb15a3942017-01-17 17:25:04 +0000263 if (event.memo === 'added') {
264 var link = this.model.get('links').add(event.data);
265 link.createLink();
Steven Burrows6de27f42017-03-30 16:21:27 +0100266 $log.debug('Added Link', link);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000267 }
268 },
269 LINK_REMOVED: function (event) {
270 var link = this.getLink(event.subject);
271 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000272 this.model.get('links').remove(link);
273 },
274 DEVICE_ADDED_OR_UPDATED: function (event) {
275
Steven Burrows6de27f42017-03-30 16:21:27 +0100276 var regionDevices = this.model.get('devices'),
277 device;
278
279 if (!regionDevices) {
280 this.model.set({ devices: t2ds.createDeviceCollection([], this) })
281 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000282
283 if (event.memo === 'added') {
284 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800285 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000286 } else if (event.memo === 'updated') {
287 device = this.getDevice(event.subject);
288 device.set(event.data);
289 }
290 },
291 DEVICE_REMOVED: function (event) {
292 device.remove();
Steven Burrows6de27f42017-03-30 16:21:27 +0100293 },
294 HOST_ADDED_OR_UPDATED: function (event) {
295 var regionHosts = this.model.get('hosts'),
296 host;
297
298 if (!regionHosts) {
299 this.model.set({ hosts: t2hs.createHostCollection([], this) })
300 }
301
302 if (event.memo === 'added') {
303 host = this.model.get('hosts').add(event.data);
304 $log.debug('Added host', host);
305 }
306 },
307 REGION_ADDED_OR_UPDATED: function (event) {
308 var regionSubRegions = this.model.get('subregions'),
309 region;
310
311 if (!regionSubRegions) {
312 this.model.set({ subregions: t2sr.createSubRegionCollection([], this) })
313 }
314
315 if (event.memo === 'added') {
316 region = this.model.get('subregions').add(event.data);
317 $log.debug('Added region', region);
318 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000319 }
320 });
321
322 function getInstance() {
323 return instance || new Region();
324 }
325
326 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000327
Steven Burrows57e24e92016-08-04 18:38:24 +0100328 }]);
329
330})();