blob: 54c88c755d58d4bfe175355b3467581192e53f49 [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;
Steven Burrowsb43c1a92017-03-07 17:13:28 +000048 this.bgRendered = false;
Steven Burrowsaf96a212016-12-28 12:57:02 +000049 },
Steven Burrowsb43c1a92017-03-07 17:13:28 +000050 backgroundRendered: function () {
51 this.bgRendered = true;
Steven Burrows3db5ddb2017-02-17 15:21:20 +000052
Steven Burrowsb43c1a92017-03-07 17:13:28 +000053 if (this.regionData) {
54 this.startRegion();
Steven Burrows3db5ddb2017-02-17 15:21:20 +000055 }
56 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000057 addRegion: function (data) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000058 this.clear();
59 this.regionData = data;
60
61 if (this.bgRendered) {
62 this.startRegion();
63 }
64 },
65 startRegion: function () {
Steven Burrows57e24e92016-08-04 18:38:24 +010066
Steven Burrowsaf96a212016-12-28 12:57:02 +000067 var RegionModel = Model.extend({
68 findNodeById: this.findNodeById,
69 nodes: this.regionNodes.bind(this)
70 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010071
Steven Burrowsaf96a212016-12-28 12:57:02 +000072 this.model = new RegionModel({
Steven Burrowsb43c1a92017-03-07 17:13:28 +000073 id: this.regionData.id,
74 layerOrder: this.regionData.layerOrder
Steven Burrowsaf96a212016-12-28 12:57:02 +000075 });
76
77 this.model.set({
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 Burrowsb43c1a92017-03-07 17:13:28 +000093 // this.layout.update();
94 },
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 () {
125
126 if (this.model) {
127 return [].concat(
128 this.model.get('devices').models,
129 this.model.get('hosts').models,
130 this.model.get('subregions').models
131 );
132 }
133
134 return [];
135 },
136 regionLinks: function () {
137 return (this.model) ? this.model.get('links').models : [];
138 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000139 getLink: function (linkId) {
140 return this.model.get('links').get(linkId);
141 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000142 getDevice: function (deviceId) {
143 return this.model.get('devices').get(deviceId);
144 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000145 filterRegionNodes: function (predicate) {
146 var nodes = this.regionNodes();
147 return _.filter(nodes, predicate);
148 },
149 deselectAllNodes: function () {
150 var selected = this.filterRegionNodes(function (node) {
151 return node.get('selected', true);
152 });
153
154 if (selected.length) {
155
156 selected.forEach(function (node) {
157 node.deselect();
158 });
159
160 t2dps().el.hide();
161 return true;
162 }
163
164 return false;
165 },
166 deselectLink: function () {
167 var selected = _.filter(this.regionLinks(), function (link) {
168 return link.get('selected', true);
169 });
170
171 if (selected.length) {
172
173 selected.forEach(function (link) {
174 link.deselect();
175 });
176
177 t2dps().el.hide();
178 return true;
179 }
180
181 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000182 },
183
184 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000185
Steven Burrowsb15a3942017-01-17 17:25:04 +0000186 if (this[event.type]) {
187 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000188 } else {
189 $log.error("Unhanded topology update", event);
190 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000191
192 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000193 },
194
195 // Topology update event handlers
196 LINK_ADDED_OR_UPDATED: function (event) {
197 if (event.memo === 'added') {
198 var link = this.model.get('links').add(event.data);
199 link.createLink();
200 }
201 },
202 LINK_REMOVED: function (event) {
203 var link = this.getLink(event.subject);
204 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000205 this.model.get('links').remove(link);
206 },
207 DEVICE_ADDED_OR_UPDATED: function (event) {
208
209 var device;
210
211 if (event.memo === 'added') {
212 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800213 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000214 } else if (event.memo === 'updated') {
215 device = this.getDevice(event.subject);
216 device.set(event.data);
217 }
218 },
219 DEVICE_REMOVED: function (event) {
220 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000221 }
222 });
223
224 function getInstance() {
225 return instance || new Region();
226 }
227
228 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000229
Steven Burrows57e24e92016-08-04 18:38:24 +0100230 }]);
231
232})();