blob: 15c5463e326ee2c4150f1844d09ec8703ed284bc [file] [log] [blame]
Steven Burrowsec1f45c2016-08-08 16:14:41 +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 Layout Module.
19 Module that contains the d3.force.layout logic
20 */
21
22(function () {
23 'use strict';
24
Steven Burrows42eb9e22017-02-06 14:20:24 +000025 var instance,
26 updateTimer;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010027
Steven Burrowsec1f45c2016-08-08 16:14:41 +010028 // default settings for force layout
29 var defaultSettings = {
30 gravity: 0.4,
31 friction: 0.7,
32 charge: {
33 // note: key is node.class
34 device: -8000,
Steven Burrows583f4be2016-11-04 14:06:50 +010035 host: -20000,
Steven Burrowsb11a8b82017-03-10 16:00:31 +000036 region: -8000,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010037 _def_: -12000
38 },
39 linkDistance: {
40 // note: key is link.type
41 direct: 100,
42 optical: 120,
Steven Burrowsb11a8b82017-03-10 16:00:31 +000043 UiEdgeLink: 100,
Steven Burrowsec1f45c2016-08-08 16:14:41 +010044 _def_: 50
45 },
46 linkStrength: {
47 // note: key is link.type
48 // range: {0.0 ... 1.0}
Steven Burrowsec1f45c2016-08-08 16:14:41 +010049 _def_: 1.0
50 }
51 };
52
53 // configuration
54 var linkConfig = {
55 light: {
56 baseColor: '#939598',
57 inColor: '#66f',
58 outColor: '#f00'
59 },
60 dark: {
61 // TODO : theme
62 baseColor: '#939598',
63 inColor: '#66f',
64 outColor: '#f00'
65 },
66 inWidth: 12,
67 outWidth: 10
68 };
69
70 // internal state
Steven Burrowsaf96a212016-12-28 12:57:02 +000071 var nodeLock = false; // whether nodes can be dragged or not (locked)
Steven Burrows9edc7e02016-08-29 11:52:07 +010072
Steven Burrowsa3fca812016-10-14 15:11:04 -050073 // predicate that indicates when clicking is active
74 function clickEnabled() {
75 return true;
76 }
77
Steven Burrowsaf96a212016-12-28 12:57:02 +000078 function getDefaultPosition(link) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010079 return {
80 x1: link.get('source').x,
81 y1: link.get('source').y,
82 x2: link.get('target').x,
83 y2: link.get('target').y
84 };
85 }
86
Steven Burrowsec1f45c2016-08-08 16:14:41 +010087 angular.module('ovTopo2')
88 .factory('Topo2LayoutService',
89 [
Steven Burrows42eb9e22017-02-06 14:20:24 +000090 '$log', '$timeout', 'WebSocketService', 'SvgUtilService', 'Topo2RegionService',
Steven Burrowsaf96a212016-12-28 12:57:02 +000091 'Topo2D3Service', 'Topo2ViewService', 'Topo2SelectService', 'Topo2ZoomService',
92 'Topo2ViewController',
Steven Burrows42eb9e22017-02-06 14:20:24 +000093 function ($log, $timeout, wss, sus, t2rs, t2d3, t2vs, t2ss, t2zs,
Steven Burrowsaf96a212016-12-28 12:57:02 +000094 ViewController) {
Steven Burrowsec1f45c2016-08-08 16:14:41 +010095
Steven Burrowsaf96a212016-12-28 12:57:02 +000096 var Layout = ViewController.extend({
Steven Burrowsbd402842017-03-08 21:30:38 +000097 init: function (svg, forceG, uplink, dim, zoomer, opts) {
Steven Burrowsaf96a212016-12-28 12:57:02 +000098 $log.debug('initialize Layout');
99 instance = this;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100100
Steven Burrowsaf96a212016-12-28 12:57:02 +0000101 this.svg = svg;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100102
Steven Burrowsaf96a212016-12-28 12:57:02 +0000103 // Append all the SVG Group elements to the forceG object
104 this.createForceElements();
105
106 this.uplink = uplink;
107 this.dim = dim;
108 this.zoomer = zoomer;
109
110 this.settings = angular.extend({}, defaultSettings, opts);
111
112 this.link = this.elements.linkG.selectAll('.link');
113 this.elements.linkLabelG.selectAll('.linkLabel');
114 this.node = this.elements.nodeG.selectAll('.node');
115 },
116 createForceElements: function () {
117
118 this.prevForce = this.forceG;
119
Simon Hunt95f4b422017-03-03 13:49:05 -0800120 this.forceG = d3.select('#topo2-zoomlayer')
121 .append('g').attr('class', 'topo2-force');
Steven Burrowsaf96a212016-12-28 12:57:02 +0000122
123 this.elements = {
Simon Hunt95f4b422017-03-03 13:49:05 -0800124 linkG: this.addElement(this.forceG, 'topo2-links'),
125 linkLabelG: this.addElement(this.forceG, 'topo2-linkLabels'),
126 numLinksLabels: this.addElement(this.forceG, 'topo2-numLinkLabels'),
127 nodeG: this.addElement(this.forceG, 'topo2-nodes'),
128 portLabels: this.addElement(this.forceG, 'topo2-portLabels')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000129 };
130 },
131 addElement: function (parent, className) {
132 return parent.append('g').attr('class', className);
133 },
134 settingOrDefault: function (settingName, node) {
135 var nodeType = node.get('nodeType');
136 return this.settings[settingName][nodeType] || this.settings[settingName]._def_;
137 },
138 createForceLayout: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000139
140 this.force = d3.layout.force()
141 .size(t2vs.getDimensions())
142 .gravity(this.settings.gravity)
143 .friction(this.settings.friction)
144 .charge(this.settingOrDefault.bind(this, 'charge'))
145 .linkDistance(this.settingOrDefault.bind(this, 'linkDistance'))
146 .linkStrength(this.settingOrDefault.bind(this, 'linkStrength'))
Steven Burrows6de27f42017-03-30 16:21:27 +0100147 .nodes([])
148 .links([])
Steven Burrowsaf96a212016-12-28 12:57:02 +0000149 .on("tick", this.tick.bind(this))
Steven Burrowsaf96a212016-12-28 12:57:02 +0000150 .start();
151
Steven Burrowsaf96a212016-12-28 12:57:02 +0000152 this.drag = sus.createDragBehavior(this.force,
Steven Burrows5fa057e2017-03-15 17:07:56 +0000153 function () {}, // click event is no longer handled in the drag service
Steven Burrowsaf96a212016-12-28 12:57:02 +0000154 this.atDragEnd,
155 this.dragEnabled.bind(this),
156 clickEnabled
157 );
158
159 this.update();
160 },
161 centerLayout: function () {
Simon Hunt95f4b422017-03-03 13:49:05 -0800162 d3.select('#topo2-zoomlayer').attr('data-layout', t2rs.model.get('id'));
Steven Burrowsaf96a212016-12-28 12:57:02 +0000163
Simon Hunt95f4b422017-03-03 13:49:05 -0800164 var zoomer = d3.select('#topo2-zoomlayer').node().getBBox(),
Steven Burrowsaf96a212016-12-28 12:57:02 +0000165 layoutBBox = this.forceG.node().getBBox(),
166 scale = (zoomer.height - 150) / layoutBBox.height,
167 x = (zoomer.width / 2) - ((layoutBBox.x + layoutBBox.width / 2) * scale),
168 y = (zoomer.height / 2) - ((layoutBBox.y + layoutBBox.height / 2) * scale);
169
170 t2zs.panAndZoom([x, y], scale, 1000);
171 },
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700172 setLinkPosition: function (link) {
173 link.setPosition.bind(link)();
174 },
Steven Burrowsaf96a212016-12-28 12:57:02 +0000175 tick: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000176
Steven Burrowsaf96a212016-12-28 12:57:02 +0000177 this.node
178 .attr({
179 transform: function (d) {
180 var dx = isNaN(d.x) ? 0 : d.x,
181 dy = isNaN(d.y) ? 0 : d.y;
182 return sus.translate(dx, dy);
183 }
184 });
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700185
186 this.link
187 .each(this.setLinkPosition)
188 .attr("x1", function (d) { return d.get('position').x1; })
189 .attr("y1", function (d) { return d.get('position').y1; })
190 .attr("x2", function (d) { return d.get('position').x2; })
191 .attr("y2", function (d) { return d.get('position').y2; });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000192 },
193
194 start: function () {
195 this.force.start();
196 },
197 update: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000198
199 if (updateTimer) {
200 $timeout.cancel(updateTimer);
201 }
202 updateTimer = $timeout(this._update.bind(this), 150);
203 },
204 _update: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000205 this.updateNodes();
206 this.updateLinks();
Steven Burrows3d8d9332017-03-30 15:05:52 +0100207 this.force.start();
Steven Burrowsaf96a212016-12-28 12:57:02 +0000208 },
209 updateNodes: function () {
210 var regionNodes = t2rs.regionNodes();
211
212 // select all the nodes in the layout:
213 this.node = this.elements.nodeG.selectAll('.node')
214 .data(regionNodes, function (d) { return d.get('id'); });
215
216 var entering = this.node.enter()
217 .append('g')
218 .attr({
219 id: function (d) { return sus.safeId(d.get('id')); },
220 class: function (d) { return d.svgClassName(); },
221 transform: function (d) {
222 // Need to guard against NaN here ??
223 return sus.translate(d.node.x, d.node.y);
224 },
225 opacity: 0
226 })
227 .call(this.drag)
228 .transition()
229 .attr('opacity', 1);
230
231 entering.filter('.device').each(t2d3.nodeEnter);
232 entering.filter('.sub-region').each(t2d3.nodeEnter);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000233 entering.filter('.host').each(t2d3.nodeEnter);
234 entering.filter('.peer-region').each(t2d3.nodeEnter);
Steven Burrows3d8d9332017-03-30 15:05:52 +0100235
236 this.force.nodes(regionNodes);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000237 },
238 updateLinks: function () {
239
240 var regionLinks = t2rs.regionLinks();
241
242 this.link = this.elements.linkG.selectAll('.link')
Steven Burrows42eb9e22017-02-06 14:20:24 +0000243 .data(regionLinks, function (d) { return d.get('key'); });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000244
245 // operate on entering links:
246 var entering = this.link.enter()
247 .append('line')
248 .call(this.calcPosition)
249 .attr({
250 x1: function (d) { return d.get('position').x1; },
251 y1: function (d) { return d.get('position').y1; },
252 x2: function (d) { return d.get('position').x2; },
253 y2: function (d) { return d.get('position').y2; },
254 stroke: linkConfig.light.inColor,
255 'stroke-width': linkConfig.inWidth
256 });
257
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000258 entering.each(t2d3.nodeEnter);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000259
260 // operate on exiting links:
261 this.link.exit()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000262 .attr('stroke-dasharray', '3 3')
263 .style('opacity', 0.5)
Steven Burrowsaf96a212016-12-28 12:57:02 +0000264 .transition()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000265 .duration(1500)
266 .attr({
267 'stroke-dasharray': '3 12',
268 })
Steven Burrowsaf96a212016-12-28 12:57:02 +0000269 .style('opacity', 0.0)
270 .remove();
Steven Burrows3d8d9332017-03-30 15:05:52 +0100271
272 this.force.links(regionLinks);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000273 },
274 calcPosition: function () {
275 var lines = this;
276
277 lines.each(function (d) {
278 if (d.get('type') === 'hostLink') {
279 d.set('position', getDefaultPosition(d));
280 }
281 });
282
283 lines.each(function (d) {
284 d.set('position', getDefaultPosition(d));
285 });
286 },
287 sendUpdateMeta: function (d, clearPos) {
288 var metaUi = {},
289 ll;
290
291 // if we are not clearing the position data (unpinning),
292 // attach the x, y, (and equivalent longitude, latitude)...
293 if (!clearPos) {
294 ll = d.lngLatFromCoord([d.x, d.y]);
295 metaUi = {
296 x: d.x,
297 y: d.y,
298 equivLoc: {
299 lng: ll[0],
300 lat: ll[1]
301 }
302 };
303 }
304 d.metaUi = metaUi;
305 wss.sendEvent('updateMeta2', {
306 id: d.get('id'),
307 class: d.get('class'),
308 memento: metaUi
309 });
310 },
311 setDimensions: function () {
312 if (this.force) {
313 this.force.size(t2vs.getDimensions());
314 }
315 },
316 dragEnabled: function () {
317 var ev = d3.event.sourceEvent;
318 // nodeLock means we aren't allowing nodes to be dragged...
319 return !nodeLock && !this.zoomingOrPanning(ev);
320 },
321 zoomingOrPanning: function (ev) {
322 return ev.metaKey || ev.altKey;
323 },
324 atDragEnd: function (d) {
325 // once we've finished moving, pin the node in position
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000326 d.fix(true);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000327 instance.sendUpdateMeta(d);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000328 t2ss.clickConsumed(true);
329 },
330 transitionDownRegion: function () {
331
332 this.prevForce.transition()
333 .duration(1500)
334 .style('opacity', 0)
335 .remove();
336
337 this.forceG
338 .style('opacity', 0)
339 .transition()
340 .delay(500)
341 .duration(500)
342 .style('opacity', 1);
343 },
344 transitionUpRegion: function () {
345 this.prevForce.transition()
346 .duration(1000)
347 .style('opacity', 0)
348 .remove();
349
350 this.forceG
351 .style('opacity', 0)
352 .transition()
353 .delay(500)
354 .duration(500)
355 .style('opacity', 1);
356 }
357 });
358
359 function getInstance(svg, forceG, uplink, dim, zoomer, opts) {
360 return instance || new Layout(svg, forceG, uplink, dim, zoomer, opts);
361 }
362
Steven Burrowsbd402842017-03-08 21:30:38 +0000363 return getInstance();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100364 }
365 ]
366 );
367})();