blob: 5895871d9fa6f6a3d684aa5bee8c1b40f98ed48a [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',
39 'Topo2MapConfigService',
Steven Burrowsb11a8b82017-03-10 16:00:31 +000040 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController,
41 t2sls, t2ms, t2mcs) {
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 Burrowsb43c1a92017-03-07 17:13:28 +000049 this.bgRendered = false;
Steven Burrowsb11a8b82017-03-10 16:00:31 +000050
51 var RegionModel = Model.extend({
52 findNodeById: this.findNodeById,
53 nodes: this.regionNodes.bind(this)
54 });
55
56 this.model = new RegionModel();
Steven Burrowsaf96a212016-12-28 12:57:02 +000057 },
Steven Burrowsb43c1a92017-03-07 17:13:28 +000058 backgroundRendered: function () {
59 this.bgRendered = true;
Steven Burrows3db5ddb2017-02-17 15:21:20 +000060
Steven Burrowsb43c1a92017-03-07 17:13:28 +000061 if (this.regionData) {
62 this.startRegion();
Steven Burrows3db5ddb2017-02-17 15:21:20 +000063 }
64 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000065 addRegion: function (data) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000066 this.clear();
67 this.regionData = data;
68
69 if (this.bgRendered) {
70 this.startRegion();
71 }
72 },
73 startRegion: function () {
Steven Burrows57e24e92016-08-04 18:38:24 +010074
Steven Burrowsaf96a212016-12-28 12:57:02 +000075 this.model.set({
Steven Burrowsb11a8b82017-03-10 16:00:31 +000076 id: this.regionData.id,
77 layerOrder: this.regionData.layerOrder,
Steven Burrowsb43c1a92017-03-07 17:13:28 +000078 subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this),
79 devices: t2ds.createDeviceCollection(this.regionData.devices, this),
80 hosts: t2hs.createHostCollection(this.regionData.hosts, this),
81 links: t2ls.createLinkCollection(this.regionData.links, this)
Steven Burrowsaf96a212016-12-28 12:57:02 +000082 });
83
84 angular.forEach(this.model.get('links').models, function (link) {
85 link.createLink();
86 });
87
88 // Hide Breadcrumbs if there are no subregions configured in the root region
89 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
90 t2bcs.hide();
91 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000092
Steven Burrowsb11a8b82017-03-10 16:00:31 +000093 this.layout.createForceLayout();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000094 },
95 clear: function () {
96
97 this.regionData = null;
98
99 if (!this.model)
100 return;
101
102 this.model.set({
103 id: null,
104 layerOrder: null,
105 subregions: t2sr.createSubRegionCollection([], this),
106 devices: t2ds.createDeviceCollection([], this),
107 hosts: t2hs.createHostCollection([], this),
108 links: t2ls.createLinkCollection([], this)
109 });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000110 },
111 isRootRegion: function () {
112 return this.model.get('id') === ROOT;
113 },
114 findNodeById: function (link, id) {
115 if (link.get('type') !== 'UiEdgeLink') {
116 // Remove /{port} from id if needed
117 var regex = new RegExp('^[^/]*');
118 id = regex.exec(id)[0];
119 }
120 return this.model.get('devices').get(id) ||
121 this.model.get('hosts').get(id) ||
122 this.model.get('subregions').get(id);
123 },
124 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000125 if (this.model) {
126 return [].concat(
127 this.model.get('devices').models,
128 this.model.get('hosts').models,
129 this.model.get('subregions').models
130 );
131 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000132 return [];
133 },
134 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000135 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000136 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000137 getLink: function (linkId) {
138 return this.model.get('links').get(linkId);
139 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000140 getDevice: function (deviceId) {
141 return this.model.get('devices').get(deviceId);
142 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000143 filterRegionNodes: function (predicate) {
144 var nodes = this.regionNodes();
145 return _.filter(nodes, predicate);
146 },
147 deselectAllNodes: function () {
148 var selected = this.filterRegionNodes(function (node) {
149 return node.get('selected', true);
150 });
151
152 if (selected.length) {
153
154 selected.forEach(function (node) {
155 node.deselect();
156 });
157
158 t2dps().el.hide();
159 return true;
160 }
161
162 return false;
163 },
164 deselectLink: function () {
165 var selected = _.filter(this.regionLinks(), function (link) {
166 return link.get('selected', true);
167 });
168
169 if (selected.length) {
170
171 selected.forEach(function (link) {
172 link.deselect();
173 });
174
175 t2dps().el.hide();
176 return true;
177 }
178
179 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000180 },
181
182 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000183
Steven Burrowsb15a3942017-01-17 17:25:04 +0000184 if (this[event.type]) {
185 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000186 } else {
187 $log.error("Unhanded topology update", event);
188 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000189
190 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000191 },
192
193 // Topology update event handlers
194 LINK_ADDED_OR_UPDATED: function (event) {
195 if (event.memo === 'added') {
196 var link = this.model.get('links').add(event.data);
197 link.createLink();
198 }
199 },
200 LINK_REMOVED: function (event) {
201 var link = this.getLink(event.subject);
202 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000203 this.model.get('links').remove(link);
204 },
205 DEVICE_ADDED_OR_UPDATED: function (event) {
206
207 var device;
208
209 if (event.memo === 'added') {
210 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800211 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000212 } else if (event.memo === 'updated') {
213 device = this.getDevice(event.subject);
214 device.set(event.data);
215 }
216 },
217 DEVICE_REMOVED: function (event) {
218 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000219 }
220 });
221
222 function getInstance() {
223 return instance || new Region();
224 }
225
226 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000227
Steven Burrows57e24e92016-08-04 18:38:24 +0100228 }]);
229
230})();