blob: 473bd3a4122930360c90427e39dae859560392a0 [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 Burrows68d6f952017-03-10 13:53:35 +0000139 var regionLinks = t2rs.regionLinks(),
Steven Burrowsaf96a212016-12-28 12:57:02 +0000140 regionNodes = t2rs.regionNodes();
141
142 this.force = d3.layout.force()
143 .size(t2vs.getDimensions())
144 .gravity(this.settings.gravity)
145 .friction(this.settings.friction)
146 .charge(this.settingOrDefault.bind(this, 'charge'))
147 .linkDistance(this.settingOrDefault.bind(this, 'linkDistance'))
148 .linkStrength(this.settingOrDefault.bind(this, 'linkStrength'))
149 .nodes(regionNodes)
150 .links(regionLinks)
151 .on("tick", this.tick.bind(this))
Steven Burrowsaf96a212016-12-28 12:57:02 +0000152 .start();
153
154 this.link = this.elements.linkG.selectAll('.link')
155 .data(regionLinks, function (d) { return d.get('key'); });
156
157 this.node = this.elements.nodeG.selectAll('.node')
158 .data(regionNodes, function (d) { return d.get('id'); });
159
160 this.drag = sus.createDragBehavior(this.force,
Steven Burrows5fa057e2017-03-15 17:07:56 +0000161 function () {}, // click event is no longer handled in the drag service
Steven Burrowsaf96a212016-12-28 12:57:02 +0000162 this.atDragEnd,
163 this.dragEnabled.bind(this),
164 clickEnabled
165 );
166
167 this.update();
168 },
169 centerLayout: function () {
Simon Hunt95f4b422017-03-03 13:49:05 -0800170 d3.select('#topo2-zoomlayer').attr('data-layout', t2rs.model.get('id'));
Steven Burrowsaf96a212016-12-28 12:57:02 +0000171
Simon Hunt95f4b422017-03-03 13:49:05 -0800172 var zoomer = d3.select('#topo2-zoomlayer').node().getBBox(),
Steven Burrowsaf96a212016-12-28 12:57:02 +0000173 layoutBBox = this.forceG.node().getBBox(),
174 scale = (zoomer.height - 150) / layoutBBox.height,
175 x = (zoomer.width / 2) - ((layoutBBox.x + layoutBBox.width / 2) * scale),
176 y = (zoomer.height / 2) - ((layoutBBox.y + layoutBBox.height / 2) * scale);
177
178 t2zs.panAndZoom([x, y], scale, 1000);
179 },
180 tick: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000181
Steven Burrowsaf96a212016-12-28 12:57:02 +0000182 this.link
Steven Burrows42eb9e22017-02-06 14:20:24 +0000183 .attr("x1", function (d) { return d.get('source').x; })
184 .attr("y1", function (d) { return d.get('source').y; })
185 .attr("x2", function (d) { return d.get('target').x; })
186 .attr("y2", function (d) { return d.get('target').y; });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000187
188 this.node
189 .attr({
190 transform: function (d) {
191 var dx = isNaN(d.x) ? 0 : d.x,
192 dy = isNaN(d.y) ? 0 : d.y;
193 return sus.translate(dx, dy);
194 }
195 });
196 },
197
198 start: function () {
199 this.force.start();
200 },
201 update: function () {
Steven Burrows42eb9e22017-02-06 14:20:24 +0000202
203 if (updateTimer) {
204 $timeout.cancel(updateTimer);
205 }
206 updateTimer = $timeout(this._update.bind(this), 150);
207 },
208 _update: function () {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000209 this.updateNodes();
210 this.updateLinks();
211 },
212 updateNodes: function () {
213 var regionNodes = t2rs.regionNodes();
214
215 // select all the nodes in the layout:
216 this.node = this.elements.nodeG.selectAll('.node')
217 .data(regionNodes, function (d) { return d.get('id'); });
218
219 var entering = this.node.enter()
220 .append('g')
221 .attr({
222 id: function (d) { return sus.safeId(d.get('id')); },
223 class: function (d) { return d.svgClassName(); },
224 transform: function (d) {
225 // Need to guard against NaN here ??
226 return sus.translate(d.node.x, d.node.y);
227 },
228 opacity: 0
229 })
230 .call(this.drag)
231 .transition()
232 .attr('opacity', 1);
233
234 entering.filter('.device').each(t2d3.nodeEnter);
235 entering.filter('.sub-region').each(t2d3.nodeEnter);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000236 entering.filter('.host').each(t2d3.nodeEnter);
237 entering.filter('.peer-region').each(t2d3.nodeEnter);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000238 },
239 updateLinks: function () {
240
241 var regionLinks = t2rs.regionLinks();
242
243 this.link = this.elements.linkG.selectAll('.link')
Steven Burrows42eb9e22017-02-06 14:20:24 +0000244 .data(regionLinks, function (d) { return d.get('key'); });
Steven Burrowsaf96a212016-12-28 12:57:02 +0000245
246 // operate on entering links:
247 var entering = this.link.enter()
248 .append('line')
249 .call(this.calcPosition)
250 .attr({
251 x1: function (d) { return d.get('position').x1; },
252 y1: function (d) { return d.get('position').y1; },
253 x2: function (d) { return d.get('position').x2; },
254 y2: function (d) { return d.get('position').y2; },
255 stroke: linkConfig.light.inColor,
256 'stroke-width': linkConfig.inWidth
257 });
258
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000259 entering.each(t2d3.nodeEnter);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000260
261 // operate on exiting links:
262 this.link.exit()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000263 .attr('stroke-dasharray', '3 3')
264 .style('opacity', 0.5)
Steven Burrowsaf96a212016-12-28 12:57:02 +0000265 .transition()
Steven Burrows42eb9e22017-02-06 14:20:24 +0000266 .duration(1500)
267 .attr({
268 'stroke-dasharray': '3 12',
269 })
Steven Burrowsaf96a212016-12-28 12:57:02 +0000270 .style('opacity', 0.0)
271 .remove();
272 },
273 calcPosition: function () {
274 var lines = this;
275
276 lines.each(function (d) {
277 if (d.get('type') === 'hostLink') {
278 d.set('position', getDefaultPosition(d));
279 }
280 });
281
282 lines.each(function (d) {
283 d.set('position', getDefaultPosition(d));
284 });
285 },
286 sendUpdateMeta: function (d, clearPos) {
287 var metaUi = {},
288 ll;
289
290 // if we are not clearing the position data (unpinning),
291 // attach the x, y, (and equivalent longitude, latitude)...
292 if (!clearPos) {
293 ll = d.lngLatFromCoord([d.x, d.y]);
294 metaUi = {
295 x: d.x,
296 y: d.y,
297 equivLoc: {
298 lng: ll[0],
299 lat: ll[1]
300 }
301 };
302 }
303 d.metaUi = metaUi;
304 wss.sendEvent('updateMeta2', {
305 id: d.get('id'),
306 class: d.get('class'),
307 memento: metaUi
308 });
309 },
310 setDimensions: function () {
311 if (this.force) {
312 this.force.size(t2vs.getDimensions());
313 }
314 },
315 dragEnabled: function () {
316 var ev = d3.event.sourceEvent;
317 // nodeLock means we aren't allowing nodes to be dragged...
318 return !nodeLock && !this.zoomingOrPanning(ev);
319 },
320 zoomingOrPanning: function (ev) {
321 return ev.metaKey || ev.altKey;
322 },
323 atDragEnd: function (d) {
324 // once we've finished moving, pin the node in position
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000325 d.fix(true);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000326 instance.sendUpdateMeta(d);
Steven Burrowsaf96a212016-12-28 12:57:02 +0000327 t2ss.clickConsumed(true);
328 },
329 transitionDownRegion: function () {
330
331 this.prevForce.transition()
332 .duration(1500)
333 .style('opacity', 0)
334 .remove();
335
336 this.forceG
337 .style('opacity', 0)
338 .transition()
339 .delay(500)
340 .duration(500)
341 .style('opacity', 1);
342 },
343 transitionUpRegion: function () {
344 this.prevForce.transition()
345 .duration(1000)
346 .style('opacity', 0)
347 .remove();
348
349 this.forceG
350 .style('opacity', 0)
351 .transition()
352 .delay(500)
353 .duration(500)
354 .style('opacity', 1);
355 }
356 });
357
358 function getInstance(svg, forceG, uplink, dim, zoomer, opts) {
359 return instance || new Layout(svg, forceG, uplink, dim, zoomer, opts);
360 }
361
Steven Burrowsbd402842017-03-08 21:30:38 +0000362 return getInstance();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100363 }
364 ]
365 );
366})();