blob: 67b2a7ca8d620a0a0ce11f453f76d615dc6d6158 [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 Burrows68d6f952017-03-10 13:53:35 +000061 loaded: function (key, value) {
62 this[key] = value;
63 if (this.bgRendered && this.regionData && this.peers) {
Steven Burrowsb43c1a92017-03-07 17:13:28 +000064 this.startRegion();
65 }
66 },
67 startRegion: function () {
Steven Burrows57e24e92016-08-04 18:38:24 +010068
Steven Burrows68d6f952017-03-10 13:53:35 +000069 var _this = this;
70
Steven Burrowsaf96a212016-12-28 12:57:02 +000071 this.model.set({
Steven Burrowsb11a8b82017-03-10 16:00:31 +000072 id: this.regionData.id,
Steven Burrows68d6f952017-03-10 13:53:35 +000073 layerOrder: this.regionData.layerOrder
Steven Burrowsaf96a212016-12-28 12:57:02 +000074 });
75
Steven Burrows68d6f952017-03-10 13:53:35 +000076 this.model.set({ subregions: t2sr.createSubRegionCollection(this.regionData.subregions, this) });
77 this.model.set({ devices: t2ds.createDeviceCollection(this.regionData.devices, this) });
78 this.model.set({ hosts: t2hs.createHostCollection(this.regionData.hosts, this) });
79 this.model.set({ peerRegions: t2pr.createCollection(this.peers, this) });
80 this.model.set({ links: t2ls.createLinkCollection(this.regionData.links, this) });
Steven Burrowsaf96a212016-12-28 12:57:02 +000081
82 // Hide Breadcrumbs if there are no subregions configured in the root region
83 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
84 t2bcs.hide();
85 }
Steven Burrows3cc0c372017-02-10 15:15:24 +000086
Steven Burrowsb11a8b82017-03-10 16:00:31 +000087 this.layout.createForceLayout();
Steven Burrowsb43c1a92017-03-07 17:13:28 +000088 },
89 clear: function () {
90
91 this.regionData = null;
92
93 if (!this.model)
94 return;
Steven Burrowsaf96a212016-12-28 12:57:02 +000095 },
96 isRootRegion: function () {
97 return this.model.get('id') === ROOT;
98 },
99 findNodeById: function (link, id) {
100 if (link.get('type') !== 'UiEdgeLink') {
101 // Remove /{port} from id if needed
102 var regex = new RegExp('^[^/]*');
103 id = regex.exec(id)[0];
104 }
105 return this.model.get('devices').get(id) ||
106 this.model.get('hosts').get(id) ||
107 this.model.get('subregions').get(id);
108 },
109 regionNodes: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000110 if (this.model) {
111 return [].concat(
112 this.model.get('devices').models,
113 this.model.get('hosts').models,
Steven Burrows68d6f952017-03-10 13:53:35 +0000114 this.model.get('subregions').models,
115 this.model.get('peerRegions').models
Steven Burrowsaf96a212016-12-28 12:57:02 +0000116 );
117 }
Steven Burrowsaf96a212016-12-28 12:57:02 +0000118 return [];
119 },
120 regionLinks: function () {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000121 return this.model.get('links').models;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000122 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000123 getLink: function (linkId) {
124 return this.model.get('links').get(linkId);
125 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000126 getDevice: function (deviceId) {
127 return this.model.get('devices').get(deviceId);
128 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000129 filterRegionNodes: function (predicate) {
130 var nodes = this.regionNodes();
131 return _.filter(nodes, predicate);
132 },
133 deselectAllNodes: function () {
134 var selected = this.filterRegionNodes(function (node) {
135 return node.get('selected', true);
136 });
137
138 if (selected.length) {
139
140 selected.forEach(function (node) {
141 node.deselect();
142 });
143
144 t2dps().el.hide();
145 return true;
146 }
147
148 return false;
149 },
150 deselectLink: function () {
151 var selected = _.filter(this.regionLinks(), function (link) {
152 return link.get('selected', true);
153 });
154
155 if (selected.length) {
156
157 selected.forEach(function (link) {
158 link.deselect();
159 });
160
161 t2dps().el.hide();
162 return true;
163 }
164
165 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000166 },
167
168 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000169
Steven Burrowsb15a3942017-01-17 17:25:04 +0000170 if (this[event.type]) {
171 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000172 } else {
173 $log.error("Unhanded topology update", event);
174 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000175
176 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000177 },
178
179 // Topology update event handlers
180 LINK_ADDED_OR_UPDATED: function (event) {
181 if (event.memo === 'added') {
182 var link = this.model.get('links').add(event.data);
183 link.createLink();
184 }
185 },
186 LINK_REMOVED: function (event) {
187 var link = this.getLink(event.subject);
188 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000189 this.model.get('links').remove(link);
190 },
191 DEVICE_ADDED_OR_UPDATED: function (event) {
192
193 var device;
194
195 if (event.memo === 'added') {
196 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800197 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000198 } else if (event.memo === 'updated') {
199 device = this.getDevice(event.subject);
200 device.set(event.data);
201 }
202 },
203 DEVICE_REMOVED: function (event) {
204 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000205 }
206 });
207
208 function getInstance() {
209 return instance || new Region();
210 }
211
212 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000213
Steven Burrows57e24e92016-08-04 18:38:24 +0100214 }]);
215
216})();