blob: a03c8399591be66fbda83043b80ddf9694ee634c [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 Burrows68d6f952017-03-10 13:53:35 +000039 'Topo2MapConfigService', 'Topo2PeerRegionService',
Steven Burrowsb11a8b82017-03-10 16:00:31 +000040 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController,
Steven Burrows68d6f952017-03-10 13:53:35 +000041 t2sls, t2ms, t2mcs, t2pr) {
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 Burrowsaf96a212016-12-28 12:57:02 +000060 },
Steven Burrows247ab152017-03-29 13:55:54 +010061 isLoadComplete: function() {
62 return this.bgRendered && this.regionData && this.peers;
63 },
Steven Burrows68d6f952017-03-10 13:53:35 +000064 loaded: function (key, value) {
65 this[key] = value;
Steven Burrows247ab152017-03-29 13:55:54 +010066 if (this.isLoadComplete()) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000067 this.startRegion();
68 }
69 },
70 startRegion: function () {
Steven Burrows57e24e92016-08-04 18:38:24 +010071
Steven Burrows68d6f952017-03-10 13:53:35 +000072 var _this = this;
73
Steven Burrowsaf96a212016-12-28 12:57:02 +000074 this.model.set({
Steven Burrowsb11a8b82017-03-10 16:00:31 +000075 id: this.regionData.id,
Steven Burrows68d6f952017-03-10 13:53:35 +000076 layerOrder: this.regionData.layerOrder
Steven Burrowsaf96a212016-12-28 12:57:02 +000077 });
78
Steven Burrows68d6f952017-03-10 13:53:35 +000079 this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) });
80 this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) });
81 this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) });
82 this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) });
83 this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) });
Steven Burrowsaf96a212016-12-28 12:57:02 +000084
85 // Hide Breadcrumbs if there are no subregions configured in the root region
86 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
87 t2bcs.hide();
88 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000089
Steven Burrowsb11a8b82017-03-10 16:00:31 +000090 this.layout.createForceLayout();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000091 },
92 clear: function () {
93
94 this.regionData = null;
95
96 if (!this.model)
97 return;
Steven Burrowsaf96a212016-12-28 12:57:02 +000098 },
99 isRootRegion: function () {
100 return this.model.get('id') === ROOT;
101 },
102 findNodeById: function (link, id) {
103 if (link.get('type') !== 'UiEdgeLink') {
104 // Remove /{port} from id if needed
105 var regex = new RegExp('^[^/]*');
106 id = regex.exec(id)[0];
107 }
108 return this.model.get('devices').get(id) ||
109 this.model.get('hosts').get(id) ||
110 this.model.get('subregions').get(id);
111 },
112 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000113 if (this.model) {
114 return [].concat(
115 this.model.get('devices').models,
116 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000117 this.model.get('subregions').models,
118 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000119 );
120 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000121 return [];
122 },
123 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000124 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000125 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000126 getLink: function (linkId) {
127 return this.model.get('links').get(linkId);
128 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000129 getDevice: function (deviceId) {
130 return this.model.get('devices').get(deviceId);
131 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000132 filterRegionNodes: function (predicate) {
133 var nodes = this.regionNodes();
134 return _.filter(nodes, predicate);
135 },
136 deselectAllNodes: function () {
137 var selected = this.filterRegionNodes(function (node) {
138 return node.get('selected', true);
139 });
140
141 if (selected.length) {
142
143 selected.forEach(function (node) {
144 node.deselect();
145 });
146
147 t2dps().el.hide();
148 return true;
149 }
150
151 return false;
152 },
153 deselectLink: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +0000154 console.log('remove link')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000155 var selected = _.filter(this.regionLinks(), function (link) {
156 return link.get('selected', true);
157 });
158
159 if (selected.length) {
160
161 selected.forEach(function (link) {
162 link.deselect();
163 });
164
165 t2dps().el.hide();
166 return true;
167 }
168
169 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000170 },
171
172 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000173
Steven Burrowsb15a3942017-01-17 17:25:04 +0000174 if (this[event.type]) {
175 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000176 } else {
177 $log.error("Unhanded topology update", event);
178 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000179
180 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000181 },
182
183 // Topology update event handlers
184 LINK_ADDED_OR_UPDATED: function (event) {
185 if (event.memo === 'added') {
186 var link = this.model.get('links').add(event.data);
187 link.createLink();
188 }
189 },
190 LINK_REMOVED: function (event) {
191 var link = this.getLink(event.subject);
192 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000193 this.model.get('links').remove(link);
194 },
195 DEVICE_ADDED_OR_UPDATED: function (event) {
196
197 var device;
198
199 if (event.memo === 'added') {
200 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800201 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000202 } else if (event.memo === 'updated') {
203 device = this.getDevice(event.subject);
204 device.set(event.data);
205 }
206 },
207 DEVICE_REMOVED: function (event) {
208 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000209 }
210 });
211
212 function getInstance() {
213 return instance || new Region();
214 }
215
216 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000217
Steven Burrows57e24e92016-08-04 18:38:24 +0100218 }]);
219
220})();