blob: ba1a8c451aaa8225635d9c0dc4a00fd7ecf4d457 [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 Burrows3cc0c372017-02-10 15:15:24 +000038 'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService',
39 function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls) {
Steven Burrows57e24e92016-08-04 18:38:24 +010040
Steven Burrowsdfa52b02016-09-02 13:50:43 +010041 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +010042
Steven Burrowsaf96a212016-12-28 12:57:02 +000043 var Region = ViewController.extend({
44 initialize: function () {
45 instance = this;
46 this.model = null;
47 },
Steven Burrows3db5ddb2017-02-17 15:21:20 +000048 addLayout: function (data) {
49
50 // TODO: Dynamically change the Geo Map from the layout data
51
52 if (data.bgType === 'grid') {
53 t2sls.loadLayout(data.bgId);
54 t2sls.show();
55 } else {
56 t2sls.hide();
57 }
58 },
Steven Burrowsaf96a212016-12-28 12:57:02 +000059 addRegion: function (data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010060
Steven Burrowsaf96a212016-12-28 12:57:02 +000061 var RegionModel = Model.extend({
62 findNodeById: this.findNodeById,
63 nodes: this.regionNodes.bind(this)
64 });
Steven Burrowsec1f45c2016-08-08 16:14:41 +010065
Steven Burrowsaf96a212016-12-28 12:57:02 +000066 this.model = new RegionModel({
67 id: data.id,
68 layerOrder: data.layerOrder
69 });
70
71 this.model.set({
72 subregions: t2sr.createSubRegionCollection(data.subregions, this),
73 devices: t2ds.createDeviceCollection(data.devices, this),
74 hosts: t2hs.createHostCollection(data.hosts, this),
75 links: t2ls.createLinkCollection(data.links, this)
76 });
77
78 angular.forEach(this.model.get('links').models, function (link) {
79 link.createLink();
80 });
81
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 Burrowsaf96a212016-12-28 12:57:02 +000087 },
88 isRootRegion: function () {
89 return this.model.get('id') === ROOT;
90 },
91 findNodeById: function (link, id) {
92 if (link.get('type') !== 'UiEdgeLink') {
93 // Remove /{port} from id if needed
94 var regex = new RegExp('^[^/]*');
95 id = regex.exec(id)[0];
96 }
97 return this.model.get('devices').get(id) ||
98 this.model.get('hosts').get(id) ||
99 this.model.get('subregions').get(id);
100 },
101 regionNodes: function () {
102
103 if (this.model) {
104 return [].concat(
105 this.model.get('devices').models,
106 this.model.get('hosts').models,
107 this.model.get('subregions').models
108 );
109 }
110
111 return [];
112 },
113 regionLinks: function () {
114 return (this.model) ? this.model.get('links').models : [];
115 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000116 getLink: function (linkId) {
117 return this.model.get('links').get(linkId);
118 },
Steven Burrows42eb9e22017-02-06 14:20:24 +0000119 getDevice: function (deviceId) {
120 return this.model.get('devices').get(deviceId);
121 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000122 filterRegionNodes: function (predicate) {
123 var nodes = this.regionNodes();
124 return _.filter(nodes, predicate);
125 },
126 deselectAllNodes: function () {
127 var selected = this.filterRegionNodes(function (node) {
128 return node.get('selected', true);
129 });
130
131 if (selected.length) {
132
133 selected.forEach(function (node) {
134 node.deselect();
135 });
136
137 t2dps().el.hide();
138 return true;
139 }
140
141 return false;
142 },
143 deselectLink: function () {
144 var selected = _.filter(this.regionLinks(), function (link) {
145 return link.get('selected', true);
146 });
147
148 if (selected.length) {
149
150 selected.forEach(function (link) {
151 link.deselect();
152 });
153
154 t2dps().el.hide();
155 return true;
156 }
157
158 return false;
Steven Burrowsb15a3942017-01-17 17:25:04 +0000159 },
160
161 update: function (event) {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000162
Steven Burrowsb15a3942017-01-17 17:25:04 +0000163 if (this[event.type]) {
164 this[event.type](event);
Steven Burrowsb15a3942017-01-17 17:25:04 +0000165 } else {
166 $log.error("Unhanded topology update", event);
167 }
Steven Burrows42eb9e22017-02-06 14:20:24 +0000168
169 this.layout.update()
Steven Burrowsb15a3942017-01-17 17:25:04 +0000170 },
171
172 // Topology update event handlers
173 LINK_ADDED_OR_UPDATED: function (event) {
174 if (event.memo === 'added') {
175 var link = this.model.get('links').add(event.data);
176 link.createLink();
177 }
178 },
179 LINK_REMOVED: function (event) {
180 var link = this.getLink(event.subject);
181 link.remove();
Steven Burrows42eb9e22017-02-06 14:20:24 +0000182 this.model.get('links').remove(link);
183 },
184 DEVICE_ADDED_OR_UPDATED: function (event) {
185
186 var device;
187
188 if (event.memo === 'added') {
189 device = this.model.get('devices').add(event.data);
Simon Hunteb3cf542017-02-10 13:18:41 -0800190 $log.debug('Added device', device);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000191 } else if (event.memo === 'updated') {
192 device = this.getDevice(event.subject);
193 device.set(event.data);
194 }
195 },
196 DEVICE_REMOVED: function (event) {
197 device.remove();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000198 }
199 });
200
201 function getInstance() {
202 return instance || new Region();
203 }
204
205 return getInstance();
Steven Burrowsb15a3942017-01-17 17:25:04 +0000206
Steven Burrows57e24e92016-08-04 18:38:24 +0100207 }]);
208
209})();