blob: bb8e0db8e15e824429c931966258d50ccaa98534 [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,
56
57 // TODO: The following value will be specified in the Topo2CurrentLayout Payload
58 mapfilepath: "*bayarea"
59 }).then(function () {
60 _.each(_this.regionNodes(), function (node) {
61 node.resetPosition();
62 });
63 });
64 t2ms.show();
65 } else {
66 t2ms.hide();
67 }
Steven Burrows3db5ddb2017-02-17 15:21:20 +000068
69 if (data.bgType === 'grid') {
70 t2sls.loadLayout(data.bgId);
71 t2sls.show();
72 } else {
73 t2sls.hide();
74 }
75 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000076 addRegion: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010077
Steven Burrowsaf96a212016-12-28 12:57:02 +000078 var RegionModel = Model.extend({
79 findNodeById: this.findNodeById,
80 nodes: this.regionNodes.bind(this)
81 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010082
Steven Burrowsaf96a212016-12-28 12:57:02 +000083 this.model = new RegionModel({
84 id: data.id,
85 layerOrder: data.layerOrder
86 });
87
88 this.model.set({
89 subregions: t2sr.createSubRegionCollection(data.subregions, this),
90 devices: t2ds.createDeviceCollection(data.devices, this),
91 hosts: t2hs.createHostCollection(data.hosts, this),
92 links: t2ls.createLinkCollection(data.links, this)
93 });
94
95 angular.forEach(this.model.get('links').models, function (link) {
96 link.createLink();
97 });
98
99 // Hide Breadcrumbs if there are no subregions configured in the root region
100 if (this.isRootRegion() && !this.model.get('subregions').models.length) {
101 t2bcs.hide();
102 }
Steven Burrows3cc0c372017-02-10 15:15:24 +0000103
Steven Burrowsaf96a212016-12-28 12:57:02 +0000104 },
105 isRootRegion: function () {
106 return this.model.get('id') === ROOT;
107 },
108 findNodeById: function (link, id) {
109 if (link.get('type') !== 'UiEdgeLink') {
110 // Remove /{port} from id if needed
111 var regex = new RegExp('^[^/]*');
112 id = regex.exec(id)[0];
113 }
114 return this.model.get('devices').get(id) ||
115 this.model.get('hosts').get(id) ||
116 this.model.get('subregions').get(id);
117 },
118 regionNodes: function () {
119
120 if (this.model) {
121 return [].concat(
122 this.model.get('devices').models,
123 this.model.get('hosts').models,
124 this.model.get('subregions').models
125 );
126 }
127
128 return [];
129 },
130 regionLinks: function () {
131 return (this.model) ? this.model.get('links').models : [];
132 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000133 getLink: function (linkId) {
134 return this.model.get('links').get(linkId);
135 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000136 getDevice: function (deviceId) {
137 return this.model.get('devices').get(deviceId);
138 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000139 filterRegionNodes: function (predicate) {
140 var nodes = this.regionNodes();
141 return _.filter(nodes, predicate);
142 },
143 deselectAllNodes: function () {
144 var selected = this.filterRegionNodes(function (node) {
145 return node.get('selected', true);
146 });
147
148 if (selected.length) {
149
150 selected.forEach(function (node) {
151 node.deselect();
152 });
153
154 t2dps().el.hide();
155 return true;
156 }
157
158 return false;
159 },
160 deselectLink: function () {
161 var selected = _.filter(this.regionLinks(), function (link) {
162 return link.get('selected', true);
163 });
164
165 if (selected.length) {
166
167 selected.forEach(function (link) {
168 link.deselect();
169 });
170
171 t2dps().el.hide();
172 return true;
173 }
174
175 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000176 },
177
178 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000179
Steven Burrowsb15a3942017-01-17 17:25:04 +0000180 if (this[event.type]) {
181 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000182 } else {
183 $log.error("Unhanded topology update", event);
184 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000185
186 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000187 },
188
189 // Topology update event handlers
190 LINK_ADDED_OR_UPDATED: function (event) {
191 if (event.memo === 'added') {
192 var link = this.model.get('links').add(event.data);
193 link.createLink();
194 }
195 },
196 LINK_REMOVED: function (event) {
197 var link = this.getLink(event.subject);
198 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000199 this.model.get('links').remove(link);
200 },
201 DEVICE_ADDED_OR_UPDATED: function (event) {
202
203 var device;
204
205 if (event.memo === 'added') {
206 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800207 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000208 } else if (event.memo === 'updated') {
209 device = this.getDevice(event.subject);
210 device.set(event.data);
211 }
212 },
213 DEVICE_REMOVED: function (event) {
214 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000215 }
216 });
217
218 function getInstance() {
219 return instance || new Region();
220 }
221
222 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000223
Steven Burrows57e24e92016-08-04 18:38:24 +0100224 }]);
225
226})();