blob: 56ba42fb73932ab389feb86e9441770cb6aa7379 [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
Simon Hunt9edd1722017-03-10 17:43:26 -080026 var $log, $loc, wss;
Steven Burrows57e24e92016-08-04 18:38:24 +010027
Steven Burrowsb43c1a92017-03-07 17:13:28 +000028 var t2is, t2rs, t2ls, t2vs, t2bcs, t2ss, t2bgs;
Steven Burrows1aa4f582016-12-13 15:05:41 -050029 var svg, forceG, uplink, dim, opts, zoomer;
Simon Huntd5b96732016-07-08 13:22:27 -070030
Steven Burrowsdfa52b02016-09-02 13:50:43 +010031 // D3 Selections
32 var node;
33
Simon Huntd5b96732016-07-08 13:22:27 -070034 // ========================== Helper Functions
35
Steven Burrowsbd402842017-03-08 21:30:38 +000036 function init(_svg_, _forceG_, _uplink_, _dim_, _zoomer_, _opts_) {
37
Steven Burrowsec1f45c2016-08-08 16:14:41 +010038 svg = _svg_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010039 uplink = _uplink_;
40 dim = _dim_;
Steven Burrowsbd402842017-03-08 21:30:38 +000041 zoomer = _zoomer_;
Steven Burrowsdfa52b02016-09-02 13:50:43 +010042 opts = _opts_;
43
Steven Burrowsbd402842017-03-08 21:30:38 +000044
Steven Burrowsb43c1a92017-03-07 17:13:28 +000045 t2bgs.init();
Steven Burrowsbd402842017-03-08 21:30:38 +000046 t2ls.init(svg, uplink, dim, zoomer, opts);
47 t2bcs.addLayout(t2ls);
Steven Burrowsaf96a212016-12-28 12:57:02 +000048 t2rs.layout = t2ls;
49 t2ss.init(svg, zoomer);
Simon Hunt9edd1722017-03-10 17:43:26 -080050
51 navToBookmarkedRegion($loc.search().regionId);
Simon Huntd5b96732016-07-08 13:22:27 -070052 }
53
54 function destroy() {
55 $log.debug('Destroy topo force layout');
56 }
57
Simon Hunt9edd1722017-03-10 17:43:26 -080058 function navToBookmarkedRegion(regionId) {
59 $log.debug('navToBookmarkedRegion:', regionId);
60 if (regionId) {
61 wss.sendEvent('topo2navRegion', {
62 rid: regionId
Simon Hunt98189192016-07-29 19:02:27 -070063 });
64
Simon Hunt9edd1722017-03-10 17:43:26 -080065 t2ls.createForceElements();
66 t2ls.transitionDownRegion();
67 }
Simon Hunt98189192016-07-29 19:02:27 -070068 }
69
Simon Huntd5b96732016-07-08 13:22:27 -070070 // ========================== Event Handlers
71
72 function allInstances(data) {
Steven Burrows57e24e92016-08-04 18:38:24 +010073 $log.debug('>> topo2AllInstances event:', data);
Simon Hunt98189192016-07-29 19:02:27 -070074 doTmpCurrentLayout(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);
Simon Hunt98189192016-07-29 19:02:27 -070087 doTmpCurrentRegion(data);
Steven Burrows57e24e92016-08-04 18:38:24 +010088 t2rs.addRegion(data);
Steven Burrowsdfa52b02016-09-02 13:50:43 +010089 t2ls.createForceLayout();
Simon Hunt98189192016-07-29 19:02:27 -070090 }
91
92 function topo2PeerRegions(data) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +010093 $log.debug('>> topo2PeerRegions event:', data);
Simon Hunt98189192016-07-29 19:02:27 -070094 doTmpPeerRegions(data);
95 }
96
Simon Hunt537bc762016-12-20 12:15:13 -080097 function modelEvent(data) {
98 $log.debug('>> topo2UiModelEvent event:', data);
Steven Burrows42eb9e22017-02-06 14:20:24 +000099
Simon Hunt537bc762016-12-20 12:15:13 -0800100 // TODO: Interpret the event and update our topo model state (if needed)
101 // To Decide: Can we assume that the server will only send events
102 // related to objects that we are currently showing?
103 // (e.g. filtered by subregion contents?)
Steven Burrows42eb9e22017-02-06 14:20:24 +0000104 t2rs.update(data);
Simon Hunt537bc762016-12-20 12:15:13 -0800105 }
106
Steven Burrows57e24e92016-08-04 18:38:24 +0100107 function showMastership(masterId) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100108 if (masterId) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100109 showMastershipFor(masterId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100110 } else {
111 restoreLayerState();
Steven Burrows57e24e92016-08-04 18:38:24 +0100112 }
113 }
114
115 function restoreLayerState() {
116 // NOTE: this level of indirection required, for when we have
117 // the layer filter functionality re-implemented
118 suppressLayers(false);
119 }
120
121 // ========================== Main Service Definition
122
123 function showMastershipFor(id) {
124 suppressLayers(true);
125 node.each(function (n) {
126 if (n.master === id) {
127 n.el.classed('suppressedmax', false);
128 }
129 });
130 }
131
132 function supAmt(less) {
133 return less ? 'suppressed' : 'suppressedmax';
134 }
135
136 function suppressLayers(b, less) {
137 var cls = supAmt(less);
138 node.classed(cls, b);
139 // link.classed(cls, b);
140 }
141
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100142 function newDim(_dim_) {
143 dim = _dim_;
144 t2vs.newDim(dim);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100145 }
146
Simon Huntd5b96732016-07-08 13:22:27 -0700147 // ========================== Main Service Definition
148
Steven Burrows1c5c8612016-10-05 13:45:13 -0500149 function update(elements) {
150 angular.forEach(elements, function (el) {
151 el.update();
152 });
153 }
154
Steven Burrows37549ee2016-09-21 14:41:39 +0100155 function updateNodes() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500156 update(t2rs.regionNodes());
157 }
158
159 function updateLinks() {
160 update(t2rs.regionLinks());
161 }
162
Steven Burrows892cba62017-03-10 16:31:48 +0000163 function resetNodeLocation() {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500164
Steven Burrows892cba62017-03-10 16:31:48 +0000165 var hovered = t2rs.filterRegionNodes(function (model) {
166 return model.get('hovered');
Steven Burrows1c5c8612016-10-05 13:45:13 -0500167 });
168
Steven Burrows892cba62017-03-10 16:31:48 +0000169 angular.forEach(hovered, function (model) {
170 model.resetPosition();
171 });
Steven Burrows1c5c8612016-10-05 13:45:13 -0500172 }
173
174 function unpin() {
175 var hovered = t2rs.filterRegionNodes(function (model) {
176 return model.get('hovered');
177 });
178
179 angular.forEach(hovered, function (model) {
180 model.fixed = false;
181 model.el.classed('fixed', false);
Steven Burrows0616e802016-10-06 21:45:07 -0500182 });
Steven Burrows37549ee2016-09-21 14:41:39 +0100183 }
184
Simon Huntd5b96732016-07-08 13:22:27 -0700185 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000186 .factory('Topo2ForceService', [
187 '$log', 'WebSocketService', 'Topo2InstanceService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500188 'Topo2RegionService', 'Topo2LayoutService', 'Topo2ViewService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000189 'Topo2BreadcrumbService', 'Topo2ZoomService', 'Topo2SelectService',
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000190 'Topo2BackgroundService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500191 function (_$log_, _wss_, _t2is_, _t2rs_, _t2ls_,
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000192 _t2vs_, _t2bcs_, zoomService, _t2ss_, _t2bgs_) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100193
Simon Huntd5b96732016-07-08 13:22:27 -0700194 $log = _$log_;
195 wss = _wss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100196 t2is = _t2is_;
197 t2rs = _t2rs_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100198 t2ls = _t2ls_;
199 t2vs = _t2vs_;
Steven Burrowsaf3159d2016-08-25 14:54:30 +0100200 t2bcs = _t2bcs_;
Steven Burrowsaf96a212016-12-28 12:57:02 +0000201 t2ss = _t2ss_;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000202 t2bgs = _t2bgs_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100203
Steven Burrows0616e802016-10-06 21:45:07 -0500204 var onZoom = function () {
205 var nodes = [].concat(
206 t2rs.regionNodes(),
207 t2rs.regionLinks()
208 );
209
210 angular.forEach(nodes, function (node) {
211 node.setScale();
212 });
213 };
214
215 zoomService.addZoomEventListener(onZoom);
216
Simon Huntd5b96732016-07-08 13:22:27 -0700217 return {
Steven Burrows57e24e92016-08-04 18:38:24 +0100218
Simon Huntd5b96732016-07-08 13:22:27 -0700219 init: init,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100220 newDim: newDim,
Steven Burrows57e24e92016-08-04 18:38:24 +0100221
Simon Huntd5b96732016-07-08 13:22:27 -0700222 destroy: destroy,
223 topo2AllInstances: allInstances,
224 topo2CurrentLayout: currentLayout,
225 topo2CurrentRegion: currentRegion,
Steven Burrows57e24e92016-08-04 18:38:24 +0100226
Simon Hunt537bc762016-12-20 12:15:13 -0800227 topo2UiModelEvent: modelEvent,
228
Steven Burrows57e24e92016-08-04 18:38:24 +0100229 showMastership: showMastership,
Steven Burrows37549ee2016-09-21 14:41:39 +0100230 topo2PeerRegions: topo2PeerRegions,
231
Steven Burrows1c5c8612016-10-05 13:45:13 -0500232 updateNodes: updateNodes,
233 updateLinks: updateLinks,
Steven Burrows892cba62017-03-10 16:31:48 +0000234 resetNodeLocation: resetNodeLocation,
Steven Burrows1c5c8612016-10-05 13:45:13 -0500235 unpin: unpin
Simon Huntd5b96732016-07-08 13:22:27 -0700236 };
237 }]);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100238})();