blob: b67df6f3ab521dc5aa973c3785b1390cfaa80cd5 [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntd5b96732016-07-08 13:22:27 -07003 *
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
Simon Hunt9edd1722017-03-10 17:43:26 -080026 var $log, $loc, wss;
Steven Burrows57e24e92016-08-04 18:38:24 +010027
Steven Burrowsc515e602017-04-13 11:17:40 -070028 var t2is, t2rs, t2ls, t2vs, t2bcs, t2ss, t2bgs, t2tbs, t2mss;
Steven Burrows1c2a9682017-07-14 16:52:46 +010029 var svg, uplink, dim, opts, zoomer;
Steven Burrowsdfa52b02016-09-02 13:50:43 +010030
Simon Huntd5b96732016-07-08 13:22:27 -070031 // ========================== Helper Functions
32
Steven Burrowsbd402842017-03-08 21:30:38 +000033 function init(_svg_, _forceG_, _uplink_, _dim_, _zoomer_, _opts_) {
34
Steven Burrowsec1f45c2016-08-08 16:14:41 +010035 svg = _svg_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010036 uplink = _uplink_;
37 dim = _dim_;
Steven Burrowsbd402842017-03-08 21:30:38 +000038 zoomer = _zoomer_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +010039 opts = _opts_;
40
Steven Burrowsbd402842017-03-08 21:30:38 +000041
Steven Burrowsb43c1a92017-03-07 17:13:28 +000042 t2bgs.init();
Steven Burrows68d6f952017-03-10 13:53:35 +000043 t2bgs.region = t2rs;
Steven Burrowsbd402842017-03-08 21:30:38 +000044 t2ls.init(svg, uplink, dim, zoomer, opts);
45 t2bcs.addLayout(t2ls);
Steven Burrowsaf96a212016-12-28 12:57:02 +000046 t2ss.init(svg, zoomer);
Steven Burrows5fa057e2017-03-15 17:07:56 +000047 t2ss.region = t2rs;
48 t2rs.layout = t2ls;
Steven Burrowsc515e602017-04-13 11:17:40 -070049 t2mss.region = t2rs;
Steven Burrowsc8468932017-03-17 16:13:41 +000050 t2tbs.init();
Simon Hunt9edd1722017-03-10 17:43:26 -080051 navToBookmarkedRegion($loc.search().regionId);
Simon Huntd5b96732016-07-08 13:22:27 -070052 }
53
54 function destroy() {
Steven Burrowsc8468932017-03-17 16:13:41 +000055 t2tbs.destroy();
Simon Huntd5b96732016-07-08 13:22:27 -070056 $log.debug('Destroy topo force layout');
57 }
58
Simon Hunt9edd1722017-03-10 17:43:26 -080059 function navToBookmarkedRegion(regionId) {
60 $log.debug('navToBookmarkedRegion:', regionId);
61 if (regionId) {
62 wss.sendEvent('topo2navRegion', {
Steven Burrows1c2a9682017-07-14 16:52:46 +010063 rid: regionId,
Simon Hunt98189192016-07-29 19:02:27 -070064 });
65
Simon Hunt9edd1722017-03-10 17:43:26 -080066 t2ls.createForceElements();
67 t2ls.transitionDownRegion();
68 }
Simon Hunt98189192016-07-29 19:02:27 -070069 }
70
Simon Huntd5b96732016-07-08 13:22:27 -070071 // ========================== Event Handlers
72
73 function allInstances(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010074 $log.debug('>> topo2AllInstances event:', data);
Steven Burrows57e24e92016-08-04 18:38:24 +010075 t2is.allInstances(data);
Simon Huntd5b96732016-07-08 13:22:27 -070076 }
77
78 function currentLayout(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010079 $log.debug('>> topo2CurrentLayout event:', data);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000080 t2rs.clear();
Steven Burrowsaf3159d2016-08-25 14:54:30 +010081 t2bcs.addBreadcrumb(data.crumbs);
Steven Burrowsb43c1a92017-03-07 17:13:28 +000082 t2bgs.addLayout(data);
Simon Huntd5b96732016-07-08 13:22:27 -070083 }
84
85 function currentRegion(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010086 $log.debug('>> topo2CurrentRegion event:', data);
Steven Burrows68d6f952017-03-10 13:53:35 +000087 t2rs.loaded('regionData', data);
Simon Hunt98189192016-07-29 19:02:27 -070088 }
89
90 function topo2PeerRegions(data) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010091 $log.debug('>> topo2PeerRegions event:', data);
Steven Burrows68d6f952017-03-10 13:53:35 +000092 t2rs.loaded('peers', data.peers);
Simon Hunt98189192016-07-29 19:02:27 -070093 }
94
Simon Hunt537bc762016-12-20 12:15:13 -080095 function modelEvent(data) {
Steven Burrows6de27f42017-03-30 16:21:27 +010096 // $log.debug('>> topo2UiModelEvent event:', data);
Steven Burrows42eb9e22017-02-06 14:20:24 +000097
Simon Hunt537bc762016-12-20 12:15:13 -080098 // TODO: Interpret the event and update our topo model state (if needed)
99 // To Decide: Can we assume that the server will only send events
100 // related to objects that we are currently showing?
101 // (e.g. filtered by subregion contents?)
Steven Burrows42eb9e22017-02-06 14:20:24 +0000102 t2rs.update(data);
Simon Hunt537bc762016-12-20 12:15:13 -0800103 }
104
Steven Burrows57e24e92016-08-04 18:38:24 +0100105 function showMastership(masterId) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100106 if (masterId) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100107 showMastershipFor(masterId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100108 } else {
109 restoreLayerState();
Steven Burrows57e24e92016-08-04 18:38:24 +0100110 }
111 }
112
113 function restoreLayerState() {
114 // NOTE: this level of indirection required, for when we have
115 // the layer filter functionality re-implemented
116 suppressLayers(false);
117 }
118
119 // ========================== Main Service Definition
120
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100121 function newDim(_dim_) {
122 dim = _dim_;
123 t2vs.newDim(dim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100124 }
125
Simon Huntd5b96732016-07-08 13:22:27 -0700126 // ========================== Main Service Definition
127
Steven Burrows1c5c8612016-10-05 13:45:13 -0500128 function update(elements) {
129 angular.forEach(elements, function (el) {
130 el.update();
131 });
132 }
133
Steven Burrows37549ee2016-09-21 14:41:39 +0100134 function updateNodes() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500135 update(t2rs.regionNodes());
136 }
137
138 function updateLinks() {
139 update(t2rs.regionLinks());
140 }
141
Steven Burrows892cba62017-03-10 16:31:48 +0000142 function resetNodeLocation() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500143
Steven Burrows892cba62017-03-10 16:31:48 +0000144 var hovered = t2rs.filterRegionNodes(function (model) {
145 return model.get('hovered');
Steven Burrows1c5c8612016-10-05 13:45:13 -0500146 });
147
Steven Burrows892cba62017-03-10 16:31:48 +0000148 angular.forEach(hovered, function (model) {
149 model.resetPosition();
150 });
Steven Burrows1c5c8612016-10-05 13:45:13 -0500151 }
152
153 function unpin() {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000154
Steven Burrows1c5c8612016-10-05 13:45:13 -0500155 var hovered = t2rs.filterRegionNodes(function (model) {
156 return model.get('hovered');
157 });
158
159 angular.forEach(hovered, function (model) {
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000160 model.fix(false);
Steven Burrows0616e802016-10-06 21:45:07 -0500161 });
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000162
163 t2ls.start();
Steven Burrows37549ee2016-09-21 14:41:39 +0100164 }
165
Simon Huntd5b96732016-07-08 13:22:27 -0700166 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000167 .factory('Topo2ForceService', [
Steven Burrowsf303c1e2017-03-13 12:26:22 +0000168 '$log', '$location', 'WebSocketService', 'Topo2InstanceService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500169 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000170 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService',
Steven Burrowsc515e602017-04-13 11:17:40 -0700171 'Topo2BackgroundService', 'Topo2ToolbarService', 'Topo2MastershipService',
Steven Burrowsf303c1e2017-03-13 12:26:22 +0000172 function (_$log_, _$loc_, _wss_, _t2is_, _t2rs_, _t2ls_,
Steven Burrowsc515e602017-04-13 11:17:40 -0700173 _t2vs_, _t2bcs_, zoomService, _t2ss_, _t2bgs_, _t2tbs_, _t2mss_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100174
Simon Huntd5b96732016-07-08 13:22:27 -0700175 $log = _$log_;
Steven Burrowsf303c1e2017-03-13 12:26:22 +0000176 $loc = _$loc_;
Simon Huntd5b96732016-07-08 13:22:27 -0700177 wss = _wss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100178 t2is = _t2is_;
179 t2rs = _t2rs_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100180 t2ls = _t2ls_;
181 t2vs = _t2vs_;
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100182 t2bcs = _t2bcs_;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000183 t2ss = _t2ss_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000184 t2bgs = _t2bgs_;
Steven Burrowsc8468932017-03-17 16:13:41 +0000185 t2tbs = _t2tbs_;
Steven Burrowsc515e602017-04-13 11:17:40 -0700186 t2mss = _t2mss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100187
Steven Burrows0616e802016-10-06 21:45:07 -0500188 var onZoom = function () {
Steven Burrows247ab152017-03-29 13:55:54 +0100189 if (!t2rs.isLoadComplete()) {
190 return;
191 }
192
Steven Burrows0616e802016-10-06 21:45:07 -0500193 var nodes = [].concat(
194 t2rs.regionNodes(),
195 t2rs.regionLinks()
196 );
197
198 angular.forEach(nodes, function (node) {
199 node.setScale();
200 });
201 };
202
203 zoomService.addZoomEventListener(onZoom);
204
Simon Huntd5b96732016-07-08 13:22:27 -0700205 return {
Steven Burrows57e24e92016-08-04 18:38:24 +0100206
Simon Huntd5b96732016-07-08 13:22:27 -0700207 init: init,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100208 newDim: newDim,
Steven Burrows57e24e92016-08-04 18:38:24 +0100209
Simon Huntd5b96732016-07-08 13:22:27 -0700210 destroy: destroy,
211 topo2AllInstances: allInstances,
212 topo2CurrentLayout: currentLayout,
213 topo2CurrentRegion: currentRegion,
Steven Burrows57e24e92016-08-04 18:38:24 +0100214
Simon Hunt537bc762016-12-20 12:15:13 -0800215 topo2UiModelEvent: modelEvent,
216
Steven Burrows57e24e92016-08-04 18:38:24 +0100217 showMastership: showMastership,
Steven Burrows37549ee2016-09-21 14:41:39 +0100218 topo2PeerRegions: topo2PeerRegions,
219
Steven Burrows1c5c8612016-10-05 13:45:13 -0500220 updateNodes: updateNodes,
221 updateLinks: updateLinks,
Steven Burrows892cba62017-03-10 16:31:48 +0000222 resetNodeLocation: resetNodeLocation,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100223 unpin: unpin,
Simon Huntd5b96732016-07-08 13:22:27 -0700224 };
225 }]);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100226})();