blob: 98c8484ae1c94b10c9285878c40ba45decf73024 [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
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 Force Module.
19 Visualization of the topology in an SVG layer, using a D3 Force Layout.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Steven Burrows57e24e92016-08-04 18:38:24 +010026 var $log,
27 wss;
28
Steven Burrowsaf96a212016-12-28 12:57:02 +000029 var t2is, t2rs, t2ls, t2vs, t2bcs, t2ss;
Steven Burrows1aa4f582016-12-13 15:05:41 -050030 var svg, forceG, uplink, dim, opts, zoomer;
Simon Huntd5b96732016-07-08 13:22:27 -070031
Steven Burrowsdfa52b02016-09-02 13:50:43 +010032 // D3 Selections
33 var node;
34
Simon Huntd5b96732016-07-08 13:22:27 -070035 // ========================== Helper Functions
36
Steven Burrowsaf96a212016-12-28 12:57:02 +000037 function init(_svg_, _forceG_, _uplink_, _dim_, zoomer, _opts_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010038 svg = _svg_;
39 forceG = _forceG_;
40 uplink = _uplink_;
41 dim = _dim_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +010042 opts = _opts_;
43
Steven Burrowsaf96a212016-12-28 12:57:02 +000044 t2ls = t2ls(svg, forceG, uplink, dim, zoomer, opts);
45 t2bcs.addLayout(t2ls);
46 t2rs.layout = t2ls;
47 t2ss.init(svg, zoomer);
Simon Huntd5b96732016-07-08 13:22:27 -070048 }
49
50 function destroy() {
51 $log.debug('Destroy topo force layout');
52 }
53
Simon Hunt98189192016-07-29 19:02:27 -070054 // ========================== Temporary Code (to be deleted later)
55
56 function request(dir, rid) {
57 wss.sendEvent('topo2navRegion', {
58 dir: dir,
59 rid: rid
60 });
61 }
62
63 function doTmpCurrentLayout(data) {
64 var topdiv = d3.select('#topo2tmp');
65 var parentRegion = data.parent;
66 var span = topdiv.select('.parentRegion').select('span');
67 span.text(parentRegion || '[no parent]');
Steven Burrowsdfa52b02016-09-02 13:50:43 +010068 span.classed('nav-me', Boolean(parentRegion));
Simon Hunt98189192016-07-29 19:02:27 -070069 }
70
71 function doTmpCurrentRegion(data) {
72 var topdiv = d3.select('#topo2tmp');
73 var span = topdiv.select('.thisRegion').select('span');
74 var div;
75
76 span.text(data.id);
77
78 div = topdiv.select('.subRegions').select('div');
79 data.subregions.forEach(function (r) {
80
81 function nav() {
82 request('down', r.id);
83 }
84
85 div.append('p')
86 .classed('nav-me', true)
87 .text(r.id)
88 .on('click', nav);
89 });
90
91 div = topdiv.select('.devices').select('div');
92 data.layerOrder.forEach(function (tag, idx) {
93 var devs = data.devices[idx];
94 devs.forEach(function (d) {
95 div.append('p')
96 .text('[' + tag + '] ' + d.id);
97 });
98
99 });
100
101 div = topdiv.select('.hosts').select('div');
102 data.layerOrder.forEach(function (tag, idx) {
103 var hosts = data.hosts[idx];
104 hosts.forEach(function (h) {
105 div.append('p')
106 .text('[' + tag + '] ' + h.id);
107 });
108 });
109
110 div = topdiv.select('.links').select('div');
111 var links = data.links;
112 links.forEach(function (lnk) {
113 div.append('p')
114 .text(lnk.id);
115 });
116 }
117
118 function doTmpPeerRegions(data) {
119
120 }
121
Simon Huntd5b96732016-07-08 13:22:27 -0700122 // ========================== Event Handlers
123
124 function allInstances(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100125 $log.debug('>> topo2AllInstances event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700126 doTmpCurrentLayout(data);
Steven Burrows57e24e92016-08-04 18:38:24 +0100127 t2is.allInstances(data);
Simon Huntd5b96732016-07-08 13:22:27 -0700128 }
129
130 function currentLayout(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100131 $log.debug('>> topo2CurrentLayout event:', data);
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100132 t2bcs.addBreadcrumb(data.crumbs);
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000133 t2rs.addLayout(data);
Simon Huntd5b96732016-07-08 13:22:27 -0700134 }
135
136 function currentRegion(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100137 $log.debug('>> topo2CurrentRegion event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700138 doTmpCurrentRegion(data);
Steven Burrows57e24e92016-08-04 18:38:24 +0100139 t2rs.addRegion(data);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100140 t2ls.createForceLayout();
Simon Hunt98189192016-07-29 19:02:27 -0700141 }
142
143 function topo2PeerRegions(data) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100144 $log.debug('>> topo2PeerRegions event:', data);
Simon Hunt98189192016-07-29 19:02:27 -0700145 doTmpPeerRegions(data);
146 }
147
Simon Hunt537bc762016-12-20 12:15:13 -0800148 function modelEvent(data) {
149 $log.debug('>> topo2UiModelEvent event:', data);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000150
Simon Hunt537bc762016-12-20 12:15:13 -0800151 // TODO: Interpret the event and update our topo model state (if needed)
152 // To Decide: Can we assume that the server will only send events
153 // related to objects that we are currently showing?
154 // (e.g. filtered by subregion contents?)
Steven Burrows42eb9e22017-02-06 14:20:24 +0000155 t2rs.update(data);
Simon Hunt537bc762016-12-20 12:15:13 -0800156 }
157
Steven Burrows57e24e92016-08-04 18:38:24 +0100158 function showMastership(masterId) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100159 if (masterId) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100160 showMastershipFor(masterId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100161 } else {
162 restoreLayerState();
Steven Burrows57e24e92016-08-04 18:38:24 +0100163 }
164 }
165
166 function restoreLayerState() {
167 // NOTE: this level of indirection required, for when we have
168 // the layer filter functionality re-implemented
169 suppressLayers(false);
170 }
171
172 // ========================== Main Service Definition
173
174 function showMastershipFor(id) {
175 suppressLayers(true);
176 node.each(function (n) {
177 if (n.master === id) {
178 n.el.classed('suppressedmax', false);
179 }
180 });
181 }
182
183 function supAmt(less) {
184 return less ? 'suppressed' : 'suppressedmax';
185 }
186
187 function suppressLayers(b, less) {
188 var cls = supAmt(less);
189 node.classed(cls, b);
190 // link.classed(cls, b);
191 }
192
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100193 function newDim(_dim_) {
194 dim = _dim_;
195 t2vs.newDim(dim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100196 }
197
Simon Huntd5b96732016-07-08 13:22:27 -0700198 // ========================== Main Service Definition
199
Steven Burrows1c5c8612016-10-05 13:45:13 -0500200 function update(elements) {
201 angular.forEach(elements, function (el) {
202 el.update();
203 });
204 }
205
Steven Burrows37549ee2016-09-21 14:41:39 +0100206 function updateNodes() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500207 update(t2rs.regionNodes());
208 }
209
210 function updateLinks() {
211 update(t2rs.regionLinks());
212 }
213
214 function resetAllLocations() {
215 var nodes = t2rs.regionNodes();
216
217 angular.forEach(nodes, function (node) {
218 node.resetPosition();
219 });
220
221 t2ls.update();
222 t2ls.tick();
223 }
224
225 function unpin() {
226 var hovered = t2rs.filterRegionNodes(function (model) {
227 return model.get('hovered');
228 });
229
230 angular.forEach(hovered, function (model) {
231 model.fixed = false;
232 model.el.classed('fixed', false);
Steven Burrows0616e802016-10-06 21:45:07 -0500233 });
Steven Burrows37549ee2016-09-21 14:41:39 +0100234 }
235
Simon Huntd5b96732016-07-08 13:22:27 -0700236 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000237 .factory('Topo2ForceService', [
238 '$log', 'WebSocketService', 'Topo2InstanceService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500239 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000240 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500241 function (_$log_, _wss_, _t2is_, _t2rs_, _t2ls_,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000242 _t2vs_, _t2bcs_, zoomService, _t2ss_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100243
Simon Huntd5b96732016-07-08 13:22:27 -0700244 $log = _$log_;
245 wss = _wss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100246 t2is = _t2is_;
247 t2rs = _t2rs_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100248 t2ls = _t2ls_;
249 t2vs = _t2vs_;
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100250 t2bcs = _t2bcs_;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000251 t2ss = _t2ss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100252
Steven Burrows0616e802016-10-06 21:45:07 -0500253 var onZoom = function () {
254 var nodes = [].concat(
255 t2rs.regionNodes(),
256 t2rs.regionLinks()
257 );
258
259 angular.forEach(nodes, function (node) {
260 node.setScale();
261 });
262 };
263
264 zoomService.addZoomEventListener(onZoom);
265
Simon Huntd5b96732016-07-08 13:22:27 -0700266 return {
Steven Burrows57e24e92016-08-04 18:38:24 +0100267
Simon Huntd5b96732016-07-08 13:22:27 -0700268 init: init,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100269 newDim: newDim,
Steven Burrows57e24e92016-08-04 18:38:24 +0100270
Simon Huntd5b96732016-07-08 13:22:27 -0700271 destroy: destroy,
272 topo2AllInstances: allInstances,
273 topo2CurrentLayout: currentLayout,
274 topo2CurrentRegion: currentRegion,
Steven Burrows57e24e92016-08-04 18:38:24 +0100275
Simon Hunt537bc762016-12-20 12:15:13 -0800276 topo2UiModelEvent: modelEvent,
277
Steven Burrows57e24e92016-08-04 18:38:24 +0100278 showMastership: showMastership,
Steven Burrows37549ee2016-09-21 14:41:39 +0100279 topo2PeerRegions: topo2PeerRegions,
280
Steven Burrows1c5c8612016-10-05 13:45:13 -0500281 updateNodes: updateNodes,
282 updateLinks: updateLinks,
283 resetAllLocations: resetAllLocations,
284 unpin: unpin
Simon Huntd5b96732016-07-08 13:22:27 -0700285 };
286 }]);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100287})();