blob: 0202ca91e92be250478a4118e0c39db6d72a355b [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',
40 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls, t2ms, t2mcs) {
Steven Burrows57e24e92016-08-04 18:38:24 +010041
Steven Burrowsdfa52b02016-09-02 13:50:43 +010042 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +010043
Steven Burrowsaf96a212016-12-28 12:57:02 +000044 var Region = ViewController.extend({
45 initialize: function () {
46 instance = this;
47 this.model = null;
48 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000049 addLayout: function (data) {
50
Steven Burrows86b74fc2017-02-22 00:15:16 +000051 var _this = this;
52
53 if (data.bgType === 'geo') {
54 t2ms.setMap({
55 mapid: data.bgId,
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000056 mapfilepath: data.bgFilePath
Steven Burrows86b74fc2017-02-22 00:15:16 +000057 }).then(function () {
58 _.each(_this.regionNodes(), function (node) {
59 node.resetPosition();
60 });
61 });
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000062
63 if (t2ms.enabled()) {
64 t2ms.show();
65 }
Steven Burrows86b74fc2017-02-22 00:15:16 +000066 } else {
67 t2ms.hide();
68 }
Steven Burrows3db5ddb2017-02-17 15:21:20 +000069
70 if (data.bgType === 'grid') {
71 t2sls.loadLayout(data.bgId);
Steven Burrowsea1d1ec2017-02-23 15:39:25 +000072
73 if (t2sls.enabled()) {
74 t2sls.show();
75 }
Steven Burrows3db5ddb2017-02-17 15:21:20 +000076 } else {
77 t2sls.hide();
78 }
79 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000080 addRegion: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010081
Steven Burrowsaf96a212016-12-28 12:57:02 +000082 var RegionModel = Model.extend({
83 findNodeById: this.findNodeById,
84 nodes: this.regionNodes.bind(this)
85 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010086
Steven Burrowsaf96a212016-12-28 12:57:02 +000087 this.model = new RegionModel({
88 id: data.id,
89 layerOrder: data.layerOrder
90 });
91
92 this.model.set({
93 subregions: t2sr.createSubRegionCollection(data.subregions, this),
94 devices: t2ds.createDeviceCollection(data.devices, this),
95 hosts: t2hs.createHostCollection(data.hosts, this),
96 links: t2ls.createLinkCollection(data.links, this)
97 });
98
99 angular.forEach(this.model.get('links').models, function (link) {
100 link.createLink();
101 });
102
103 // Hide Breadcrumbs if there are no subregions configured in the root region
104 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
105 t2bcs.hide();
106 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000107
Steven Burrowsaf96a212016-12-28 12:57:02 +0000108 },
109 isRootRegion: function () {
110 return this.model.get('id') === ROOT;
111 },
112 findNodeById: function (link, id) {
113 if (link.get('type') !== 'UiEdgeLink') {
114 // Remove /{port} from id if needed
115 var regex = new RegExp('^[^/]*');
116 id = regex.exec(id)[0];
117 }
118 return this.model.get('devices').get(id) ||
119 this.model.get('hosts').get(id) ||
120 this.model.get('subregions').get(id);
121 },
122 regionNodes: function () {
123
124 if (this.model) {
125 return [].concat(
126 this.model.get('devices').models,
127 this.model.get('hosts').models,
128 this.model.get('subregions').models
129 );
130 }
131
132 return [];
133 },
134 regionLinks: function () {
135 return (this.model) ? this.model.get('links').models : [];
136 },
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})();