blob: 1795df4d86a52a49c24f550c4549a3be16991ad7 [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 Huntd5b96732016-07-08 13:22:27 -0700148 function startDone(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100149 $log.debug('>> topo2StartDone event:', data);
Simon Huntd5b96732016-07-08 13:22:27 -0700150 }
Steven Burrows57e24e92016-08-04 18:38:24 +0100151
Simon Hunt537bc762016-12-20 12:15:13 -0800152 function modelEvent(data) {
153 $log.debug('>> topo2UiModelEvent event:', data);
Steven Burrows42eb9e22017-02-06 14:20:24 +0000154
Simon Hunt537bc762016-12-20 12:15:13 -0800155 // TODO: Interpret the event and update our topo model state (if needed)
156 // To Decide: Can we assume that the server will only send events
157 // related to objects that we are currently showing?
158 // (e.g. filtered by subregion contents?)
Steven Burrows42eb9e22017-02-06 14:20:24 +0000159 t2rs.update(data);
Simon Hunt537bc762016-12-20 12:15:13 -0800160 }
161
Steven Burrows57e24e92016-08-04 18:38:24 +0100162 function showMastership(masterId) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100163 if (masterId) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100164 showMastershipFor(masterId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100165 } else {
166 restoreLayerState();
Steven Burrows57e24e92016-08-04 18:38:24 +0100167 }
168 }
169
170 function restoreLayerState() {
171 // NOTE: this level of indirection required, for when we have
172 // the layer filter functionality re-implemented
173 suppressLayers(false);
174 }
175
176 // ========================== Main Service Definition
177
178 function showMastershipFor(id) {
179 suppressLayers(true);
180 node.each(function (n) {
181 if (n.master === id) {
182 n.el.classed('suppressedmax', false);
183 }
184 });
185 }
186
187 function supAmt(less) {
188 return less ? 'suppressed' : 'suppressedmax';
189 }
190
191 function suppressLayers(b, less) {
192 var cls = supAmt(less);
193 node.classed(cls, b);
194 // link.classed(cls, b);
195 }
196
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100197 function newDim(_dim_) {
198 dim = _dim_;
199 t2vs.newDim(dim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100200 }
201
Simon Huntd5b96732016-07-08 13:22:27 -0700202 // ========================== Main Service Definition
203
Steven Burrows1c5c8612016-10-05 13:45:13 -0500204 function update(elements) {
205 angular.forEach(elements, function (el) {
206 el.update();
207 });
208 }
209
Steven Burrows37549ee2016-09-21 14:41:39 +0100210 function updateNodes() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500211 update(t2rs.regionNodes());
212 }
213
214 function updateLinks() {
215 update(t2rs.regionLinks());
216 }
217
218 function resetAllLocations() {
219 var nodes = t2rs.regionNodes();
220
221 angular.forEach(nodes, function (node) {
222 node.resetPosition();
223 });
224
225 t2ls.update();
226 t2ls.tick();
227 }
228
229 function unpin() {
230 var hovered = t2rs.filterRegionNodes(function (model) {
231 return model.get('hovered');
232 });
233
234 angular.forEach(hovered, function (model) {
235 model.fixed = false;
236 model.el.classed('fixed', false);
Steven Burrows0616e802016-10-06 21:45:07 -0500237 });
Steven Burrows37549ee2016-09-21 14:41:39 +0100238 }
239
Simon Huntd5b96732016-07-08 13:22:27 -0700240 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000241 .factory('Topo2ForceService', [
242 '$log', 'WebSocketService', 'Topo2InstanceService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500243 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000244 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500245 function (_$log_, _wss_, _t2is_, _t2rs_, _t2ls_,
Steven Burrowsaf96a212016-12-28 12:57:02 +0000246 _t2vs_, _t2bcs_, zoomService, _t2ss_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100247
Simon Huntd5b96732016-07-08 13:22:27 -0700248 $log = _$log_;
249 wss = _wss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100250 t2is = _t2is_;
251 t2rs = _t2rs_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100252 t2ls = _t2ls_;
253 t2vs = _t2vs_;
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100254 t2bcs = _t2bcs_;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000255 t2ss = _t2ss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100256
Steven Burrows0616e802016-10-06 21:45:07 -0500257 var onZoom = function () {
258 var nodes = [].concat(
259 t2rs.regionNodes(),
260 t2rs.regionLinks()
261 );
262
263 angular.forEach(nodes, function (node) {
264 node.setScale();
265 });
266 };
267
268 zoomService.addZoomEventListener(onZoom);
269
Simon Huntd5b96732016-07-08 13:22:27 -0700270 return {
Steven Burrows57e24e92016-08-04 18:38:24 +0100271
Simon Huntd5b96732016-07-08 13:22:27 -0700272 init: init,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100273 newDim: newDim,
Steven Burrows57e24e92016-08-04 18:38:24 +0100274
Simon Huntd5b96732016-07-08 13:22:27 -0700275 destroy: destroy,
276 topo2AllInstances: allInstances,
277 topo2CurrentLayout: currentLayout,
278 topo2CurrentRegion: currentRegion,
Steven Burrows57e24e92016-08-04 18:38:24 +0100279 topo2StartDone: startDone,
280
Simon Hunt537bc762016-12-20 12:15:13 -0800281 topo2UiModelEvent: modelEvent,
282
Steven Burrows57e24e92016-08-04 18:38:24 +0100283 showMastership: showMastership,
Steven Burrows37549ee2016-09-21 14:41:39 +0100284 topo2PeerRegions: topo2PeerRegions,
285
Steven Burrows1c5c8612016-10-05 13:45:13 -0500286 updateNodes: updateNodes,
287 updateLinks: updateLinks,
288 resetAllLocations: resetAllLocations,
289 unpin: unpin
Simon Huntd5b96732016-07-08 13:22:27 -0700290 };
291 }]);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100292})();