blob: 9e5b9e938781054e403c210e311ae17438a5ad22 [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 Burrows68d6f952017-03-10 13:53:35 +000086 this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) });
87 this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) });
88 this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) });
89 this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) });
90 this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) });
Steven Burrowsaf96a212016-12-28 12:57:02 +000091
92 // Hide Breadcrumbs if there are no subregions configured in the root region
93 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
94 t2bcs.hide();
95 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000096
Steven Burrowsb11a8b82017-03-10 16:00:31 +000097 this.layout.createForceLayout();
Steven Burrows6501de92017-04-12 15:10:34 -070098 this.displayNoDevs();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000099 },
100 clear: function () {
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000101 this.regionData = null;
Steven Burrows6de27f42017-03-30 16:21:27 +0100102 this.createEmptyModel();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000103 },
104 isRootRegion: function () {
105 return this.model.get('id') === ROOT;
106 },
107 findNodeById: function (link, id) {
108 if (link.get('type') !== 'UiEdgeLink') {
109 // Remove /{port} from id if needed
110 var regex = new RegExp('^[^/]*');
111 id = regex.exec(id)[0];
112 }
113 return this.model.get('devices').get(id) ||
114 this.model.get('hosts').get(id) ||
115 this.model.get('subregions').get(id);
116 },
117 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000118 if (this.model) {
119 return [].concat(
120 this.model.get('devices').models,
121 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000122 this.model.get('subregions').models,
123 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000124 );
125 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000126 return [];
127 },
128 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000129 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000130 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000131 getLink: function (linkId) {
132 return this.model.get('links').get(linkId);
133 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000134 getDevice: function (deviceId) {
135 return this.model.get('devices').get(deviceId);
136 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000137 filterRegionNodes: function (predicate) {
138 var nodes = this.regionNodes();
139 return _.filter(nodes, predicate);
140 },
141 deselectAllNodes: function () {
142 var selected = this.filterRegionNodes(function (node) {
143 return node.get('selected', true);
144 });
145
146 if (selected.length) {
147
148 selected.forEach(function (node) {
149 node.deselect();
150 });
151
152 t2dps().el.hide();
153 return true;
154 }
155
156 return false;
157 },
158 deselectLink: function () {
159 var selected = _.filter(this.regionLinks(), function (link) {
160 return link.get('selected', true);
161 });
162
163 if (selected.length) {
164
165 selected.forEach(function (link) {
166 link.deselect();
167 });
168
169 t2dps().el.hide();
170 return true;
171 }
172
173 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000174 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700175 toggleHosts: function () {
176 var state = this.lookupPrefState('hosts');
177 this.updatePrefState('hosts', !state);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000178
Steven Burrowsaea509d2017-04-12 14:17:47 -0700179 _.each(this.model.get('hosts').models, function (host) {
180 host.setVisibility();
181 });
182
183 _.each(this.model.get('links').models, function (link) {
184 link.setVisibility();
185 });
186 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000187 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000188
Steven Burrows6de27f42017-03-30 16:21:27 +0100189 if (!this.isLoadComplete()){
190 this.layout.createForceLayout();
191 }
192
Steven Burrowsb15a3942017-01-17 17:25:04 +0000193 if (this[event.type]) {
194 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000195 } else {
196 $log.error("Unhanded topology update", event);
197 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000198
199 this.layout.update()
Steven Burrows6501de92017-04-12 15:10:34 -0700200 this.displayNoDevs();
201 },
202 displayNoDevs: function () {
203 if (this.regionNodes().length > 0) {
204 t2ndcs.hide();
205 } else {
206 t2ndcs.show();
207 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000208 },
209
210 // Topology update event handlers
211 LINK_ADDED_OR_UPDATED: function (event) {
Steven Burrows6de27f42017-03-30 16:21:27 +0100212
213 var regionLinks = this.model.get('links'),
214 device;
215
216 if (!regionLinks) {
217 this.model.set({ links: t2ls.createLinkCollection([], this) })
218 }
219
Steven Burrowsb15a3942017-01-17 17:25:04 +0000220 if (event.memo === 'added') {
221 var link = this.model.get('links').add(event.data);
222 link.createLink();
Steven Burrows6de27f42017-03-30 16:21:27 +0100223 $log.debug('Added Link', link);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000224 }
225 },
226 LINK_REMOVED: function (event) {
227 var link = this.getLink(event.subject);
228 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000229 this.model.get('links').remove(link);
230 },
231 DEVICE_ADDED_OR_UPDATED: function (event) {
232
Steven Burrows6de27f42017-03-30 16:21:27 +0100233 var regionDevices = this.model.get('devices'),
234 device;
235
236 if (!regionDevices) {
237 this.model.set({ devices: t2ds.createDeviceCollection([], this) })
238 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000239
240 if (event.memo === 'added') {
241 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800242 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000243 } else if (event.memo === 'updated') {
244 device = this.getDevice(event.subject);
245 device.set(event.data);
246 }
247 },
248 DEVICE_REMOVED: function (event) {
249 device.remove();
Steven Burrows6de27f42017-03-30 16:21:27 +0100250 },
251 HOST_ADDED_OR_UPDATED: function (event) {
252 var regionHosts = this.model.get('hosts'),
253 host;
254
255 if (!regionHosts) {
256 this.model.set({ hosts: t2hs.createHostCollection([], this) })
257 }
258
259 if (event.memo === 'added') {
260 host = this.model.get('hosts').add(event.data);
261 $log.debug('Added host', host);
262 }
263 },
264 REGION_ADDED_OR_UPDATED: function (event) {
265 var regionSubRegions = this.model.get('subregions'),
266 region;
267
268 if (!regionSubRegions) {
269 this.model.set({ subregions: t2sr.createSubRegionCollection([], this) })
270 }
271
272 if (event.memo === 'added') {
273 region = this.model.get('subregions').add(event.data);
274 $log.debug('Added region', region);
275 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000276 }
277 });
278
279 function getInstance() {
280 return instance || new Region();
281 }
282
283 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000284
Steven Burrows57e24e92016-08-04 18:38:24 +0100285 }]);
286
287})();