blob: eb0da8f5d4fb82c44793bb05c7e0b092f310e160 [file] [log] [blame]
Steven Burrows57e24e92016-08-04 18:38:24 +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 Links Module.
19 Module that holds the links for a region
20 */
21
22(function () {
23 'use strict';
24
Steven Burrowsca1a39c2017-05-08 17:31:08 -040025 var $log, Collection, Model, ts, sus, t2zs, t2vs, t2lps,
26 fn, ps, t2mss, t2ts;
Steven Burrows9edc7e02016-08-29 11:52:07 +010027
28 var linkLabelOffset = '0.35em';
Steven Burrows57e24e92016-08-04 18:38:24 +010029
Steven Burrowsec1f45c2016-08-08 16:14:41 +010030 var widthRatio = 1.4,
31 linkScale = d3.scale.linear()
32 .domain([1, 12])
33 .range([widthRatio, 12 * widthRatio])
34 .clamp(true),
Steven Burrowsa3fca812016-10-14 15:11:04 -050035 labelDim = 30;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010036
37 // configuration
38 var linkConfig = {
39 light: {
40 baseColor: '#939598',
41 inColor: '#66f',
42 outColor: '#f00'
43 },
44 dark: {
45 // TODO : theme
46 baseColor: '#939598',
47 inColor: '#66f',
48 outColor: '#f00'
49 },
50 inWidth: 12,
51 outWidth: 10
52 };
53
Steven Burrowsec1f45c2016-08-08 16:14:41 +010054 function createLink() {
55
56 var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB'));
Steven Burrowsec1f45c2016-08-08 16:14:41 +010057
58 var attrs = angular.extend({}, linkPoints, {
59 key: this.get('id'),
60 class: 'link',
Steven Burrowsec1f45c2016-08-08 16:14:41 +010061 position: {
62 x1: 0,
63 y1: 0,
64 x2: 0,
65 y2: 0
66 }
67 // functions to aggregate dual link state
Steven Burrows9edc7e02016-08-29 11:52:07 +010068 // extra: link.extra
Steven Burrowsec1f45c2016-08-08 16:14:41 +010069 });
70
71 this.set(attrs);
72 }
73
Steven Burrows9edc7e02016-08-29 11:52:07 +010074 function rectAroundText(el) {
75 var text = el.select('text'),
76 box = text.node().getBBox();
77
78 // translate the bbox so that it is centered on [x,y]
79 box.x = -box.width / 2;
80 box.y = -box.height / 2;
81
82 // add padding
83 box.x -= 4;
84 box.width += 8;
85 return box;
86 }
87
Steven Burrowsdfa52b02016-09-02 13:50:43 +010088 function isLinkOnline(node) {
89 return (node.get('nodeType') === 'region') ? true : node.get('online');
90 }
91
Steven Burrowsec1f45c2016-08-08 16:14:41 +010092 function linkEndPoints(srcId, dstId) {
93
Steven Burrowsaf96a212016-12-28 12:57:02 +000094 var findNodeById = this.region.model.findNodeById.bind(this.region),
95 allNodes = this.region.model.nodes(),
96 sourceNode = findNodeById(this, srcId),
97 targetNode = findNodeById(this, dstId);
Steven Burrowsec1f45c2016-08-08 16:14:41 +010098
Steven Burrows6deb4ce2016-08-26 16:06:23 +010099 if (!sourceNode || !targetNode) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500100 $log.error('Node(s) not on map for link:' + srcId + '~' + dstId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100101 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100102 return null;
103 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100104
Steven Burrowsa3fca812016-10-14 15:11:04 -0500105 this.source = allNodes.indexOf(sourceNode);
106 this.target = allNodes.indexOf(targetNode);
107 this.sourceNode = sourceNode;
108 this.targetNode = targetNode;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100109
110 return {
111 source: sourceNode,
112 target: targetNode
113 };
114 }
115
116 function createLinkCollection(data, _region) {
117
118 var LinkModel = Model.extend({
Steven Burrows68d6f952017-03-10 13:53:35 +0000119 initialize: function () {
120 this.super = this.constructor.__super__;
121 this.super.initialize.apply(this, arguments);
122 this.createLink();
123 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100124 region: _region,
125 createLink: createLink,
126 linkEndPoints: linkEndPoints,
127 type: function () {
128 return this.get('type');
129 },
Steven Burrows86af4352016-11-16 18:19:12 -0600130 svgClassName: function () {
131 return fn.classNames('link',
132 this.nodeType,
133 this.get('type'),
134 {
135 enhanced: this.get('enhanced'),
Steven Burrowsc515e602017-04-13 11:17:40 -0700136 selected: this.get('selected'),
137 suppressedmax: this.get('mastership')
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400138 },
139 (this.linkLabel) ? this.linkLabel.linkLabelCSSClass() : null
Steven Burrows86af4352016-11-16 18:19:12 -0600140 );
141 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100142 expected: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100143 // TODO: original code is: (s && s.expected) && (t && t.expected);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100144 return true;
145 },
146 online: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100147
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100148 var source = this.get('source'),
149 target = this.get('target'),
150 sourceOnline = isLinkOnline(source),
151 targetOnline = isLinkOnline(target);
152
153 return (sourceOnline) && (targetOnline);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100154 },
Steven Burrows86af4352016-11-16 18:19:12 -0600155 onChange: function () {
156 // Update class names when the model changes
157 if (this.el) {
158 this.el.attr('class', this.svgClassName());
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400159 this.setScale();
Steven Burrows86af4352016-11-16 18:19:12 -0600160 }
161 },
Steven Burrows9edc7e02016-08-29 11:52:07 +0100162 enhance: function () {
163 var data = [],
164 point;
165
Steven Burrows1c5c8612016-10-05 13:45:13 -0500166 if (showPort()) {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000167 this.set('enhanced', true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500168 point = this.locatePortLabel();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100169 angular.extend(point, {
Simon Hunt95f4b422017-03-03 13:49:05 -0800170 id: 'topo2-port-tgt',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500171 num: this.get('portB')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100172 });
173 data.push(point);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500174
175 if (this.get('portA')) {
176 point = this.locatePortLabel(1);
177 angular.extend(point, {
Simon Hunt95f4b422017-03-03 13:49:05 -0800178 id: 'topo2-port-src',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500179 num: this.get('portA')
180 });
181 data.push(point);
182 }
183
Steven Burrowsbd402842017-03-08 21:30:38 +0000184 var entering = d3.select('.topo2-portLabels')
Steven Burrows1c5c8612016-10-05 13:45:13 -0500185 .selectAll('.portLabel')
Steven Burrowsa3fca812016-10-14 15:11:04 -0500186 .data(data)
187 .enter().append('g')
Steven Burrows1c5c8612016-10-05 13:45:13 -0500188 .classed('portLabel', true)
Steven Burrowsaf96a212016-12-28 12:57:02 +0000189 .attr('id', function (d) { return d.id; });
Steven Burrows1c5c8612016-10-05 13:45:13 -0500190
191 entering.each(function (d) {
192 var el = d3.select(this),
193 rect = el.append('rect'),
194 text = el.append('text').text(d.num);
195
196 var rectSize = rectAroundText(el);
197
198 rect.attr(rectSize)
199 .attr('rx', 2)
200 .attr('ry', 2);
201
202 text.attr('dy', linkLabelOffset)
203 .attr('text-anchor', 'middle');
204
205 el.attr('transform', sus.translate(d.x, d.y));
Steven Burrowsa3fca812016-10-14 15:11:04 -0500206
Steven Burrows1c5c8612016-10-05 13:45:13 -0500207 });
Steven Burrowsa3fca812016-10-14 15:11:04 -0500208
209 this.setScale();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100210 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100211 },
212 unenhance: function () {
Steven Burrows86af4352016-11-16 18:19:12 -0600213 this.set('enhanced', false);
Steven Burrowsbd402842017-03-08 21:30:38 +0000214 d3.select('.topo2-portLabels').selectAll('.portLabel').remove();
Steven Burrows0bc66652017-04-20 13:04:10 -0400215 this.setScale();
Steven Burrows9edc7e02016-08-29 11:52:07 +0100216 },
Steven Burrows32ce1d92017-04-13 13:18:44 -0700217 amt: function (numLinks, index) {
Steven Burrows9ce0e402017-04-20 16:39:17 -0400218 var bbox = this.get('source').el.node().getBBox(),
219 gap = bbox.width / 4;
Steven Burrows32ce1d92017-04-13 13:18:44 -0700220 return (index - ((numLinks - 1) / 2)) * gap;
221 },
222 defaultPosition: function () {
223 return {
224 x1: this.get('source').x,
225 y1: this.get('source').y,
226 x2: this.get('target').x,
227 y2: this.get('target').y
228 }
229 },
230 calcMovement: function (amt, flipped) {
231 var pos = this.defaultPosition(),
232 mult = flipped ? -amt : amt,
233 dx = pos.x2 - pos.x1,
234 dy = pos.y2 - pos.y1,
235 length = Math.sqrt((dx * dx) + (dy * dy));
236
237 return {
238 x1: pos.x1 + (mult * dy / length),
239 y1: pos.y1 + (mult * -dx / length),
240 x2: pos.x2 + (mult * dy / length),
241 y2: pos.y2 + (mult * -dx / length)
242 };
243 },
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700244 setPosition: function () {
Steven Burrows32ce1d92017-04-13 13:18:44 -0700245 var multiline = this.get('multiline');
246 if (multiline) {
247 var offsetAmt = this.amt(multiline.deviceLinks, multiline.index);
248 this.set('position', this.calcMovement(offsetAmt));
249 } else {
250 this.set('position', this.defaultPosition());
251 }
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700252
253 if (this.get('enhanced')) {
254 this.updatePortPosition();
255 }
Steven Burrows9ce0e402017-04-20 16:39:17 -0400256
257 if (this.el) {
258 this.el.attr(this.get('position'));
259 }
260
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400261 if (this.linkLabel) {
262 this.linkLabel.setPosition();
263 }
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700264 },
265 updatePortPosition: function () {
266 var sourcePos = this.locatePortLabel(1),
267 targetPos = this.locatePortLabel();
268 d3.select('#topo2-port-src')
269 .attr('transform', sus.translate(sourcePos.x, sourcePos.y));
270 d3.select('#topo2-port-tgt')
271 .attr('transform', sus.translate(targetPos.x, targetPos.y));
272 },
Steven Burrows1aa4f582016-12-13 15:05:41 -0500273 getSelected: function () {
274 return this.collection.filter(function (m) {
275 return m.get('selected');
276 });
277 },
Steven Burrows86af4352016-11-16 18:19:12 -0600278 select: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +0000279 this.set({ 'selected': true });
Steven Burrows1aa4f582016-12-13 15:05:41 -0500280 return this.getSelected();
281 },
282 deselect: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +0000283 this.set({
284 'selected': false,
285 'enhanced': false
286 });
Steven Burrows86af4352016-11-16 18:19:12 -0600287 },
288 showDetails: function () {
Steven Burrows1aa4f582016-12-13 15:05:41 -0500289 var selected = this.getSelected();
Steven Burrows86af4352016-11-16 18:19:12 -0600290
291 if (selected) {
292 t2lps.displayLink(this);
293 } else {
294 t2lps.hide();
295 }
296 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100297 locatePortLabel: function (src) {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500298 var offset = 32 / (labelDim * t2zs.scale()),
299 sourceX = this.get('position').x1,
300 sourceY = this.get('position').y1,
301 targetX = this.get('position').x2,
302 targetY = this.get('position').y2,
303 nearX = src ? sourceX : targetX,
304 nearY = src ? sourceY : targetY,
305 farX = src ? targetX : sourceX,
306 farY = src ? targetY : sourceY;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100307
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100308 function dist(x, y) {
309 return Math.sqrt(x * x + y * y);
310 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100311
312 var dx = farX - nearX,
313 dy = farY - nearY,
Steven Burrows0bc66652017-04-20 13:04:10 -0400314 k = (32 * offset) / dist(dx, dy);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100315
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100316 return { x: k * dx + nearX, y: k * dy + nearY };
Steven Burrows9edc7e02016-08-29 11:52:07 +0100317 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100318 onEnter: function (el) {
Steven Burrows448468c2017-04-13 16:09:30 -0700319 var link = d3.select(el),
320 th = ts.theme(),
321 delay = 1000;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100322
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100323 this.el = link;
Steven Burrows448468c2017-04-13 16:09:30 -0700324
325 link.transition()
326 .duration(delay)
327 .attr('stroke', linkConfig[th].baseColor);
328
Steven Burrowsaea509d2017-04-12 14:17:47 -0700329 this.setVisibility();
Steven Burrows448468c2017-04-13 16:09:30 -0700330 this.setScale();
Steven Burrows0616e802016-10-06 21:45:07 -0500331 },
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400332 linkWidth: function () {
333 var width = widthRatio;
334 if (this.get('enhanced')) { width = 3.5; }
335 if (this.linkLabel) {
336 var scale = d3.scale.ordinal()
337 .rangeRoundPoints([4, 8]),
338 label = this.linkLabel.get('label').split(' ');
339
340 switch (t2ts.selectedTrafficOverlay()) {
341 case 'flowStatsBytes':
342 scale.domain(['KB', 'MB', 'GB']);
343 width = scale(label[1]);
344 break;
345 case 'portStatsBitSec':
346 scale.domain(['Kbps', 'Mbps', 'Gbps'])
347 width = scale(label[1]);
348 break;
349 case 'portStatsPktSec':
350 scale = d3.scale.linear()
351 .domain([1, 10, 100, 1000, 10000])
352 .range(d3.range(3.5, 9))
353 .clamp(true);
354 width = scale(parseInt(label[0]));
355 }
356 }
357
358 return width;
359 },
Steven Burrows0616e802016-10-06 21:45:07 -0500360 setScale: function () {
Steven Burrowse8a455a2017-03-16 16:58:59 +0000361
362 if (!this.el) return;
363
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400364 var linkWidthRatio = this.linkWidth();
Steven Burrows0bc66652017-04-20 13:04:10 -0400365
366 var width = linkScale(linkWidthRatio) / t2zs.scale();
Steven Burrows448468c2017-04-13 16:09:30 -0700367 this.el.attr('stroke-width', width + 'px');
Steven Burrowsa3fca812016-10-14 15:11:04 -0500368
369 var labelScale = labelDim / (labelDim * t2zs.scale());
370
Steven Burrows8909c5c2017-04-10 09:56:52 -0700371 d3.select('.topo2-portLabels')
Steven Burrowsa3fca812016-10-14 15:11:04 -0500372 .selectAll('.portLabel')
373 .selectAll('*')
374 .style('transform', 'scale(' + labelScale + ')');
375
Steven Burrows9ce0e402017-04-20 16:39:17 -0400376 this.setPosition();
377
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400378 if (this.linkLabel) {
379 this.linkLabel.setScale();
380 }
Steven Burrows1c5c8612016-10-05 13:45:13 -0500381 },
382 update: function () {
Steven Burrows86af4352016-11-16 18:19:12 -0600383 if (this.get('enhanced')) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500384 this.enhance();
385 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000386 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700387 setVisibility: function () {
388
389 if (this.get('type') !== 'UiEdgeLink') {
390 return;
391 }
392
393 var visible = ps.getPrefs('topo2_prefs')['hosts'];
394 this.el.style('visibility', visible ? 'visible' : 'hidden');
395 },
Steven Burrowsc515e602017-04-13 11:17:40 -0700396 displayMastership: function () {
397 this.set({ mastership: t2mss.mastership() !== null});
398 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000399 remove: function () {
400
401 var width = linkScale(widthRatio) / t2zs.scale();
402
403 this.el.transition()
404 .duration(300)
405 .attr('stroke', '#ff0000')
406 .style('stroke-width', width * 4)
407 .transition()
408 .delay(1000)
409 .style('opacity', 0);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100410 }
411 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100412
413 var LinkCollection = Collection.extend({
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100414 model: LinkModel
Steven Burrows57e24e92016-08-04 18:38:24 +0100415 });
416
417 return new LinkCollection(data);
418 }
419
Steven Burrows1c5c8612016-10-05 13:45:13 -0500420 function showPort() {
421 return t2vs.getPortHighlighting();
422 }
423
Steven Burrows57e24e92016-08-04 18:38:24 +0100424 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000425 .factory('Topo2LinkService', [
426 '$log', 'Topo2Collection', 'Topo2Model',
Steven Burrows0616e802016-10-06 21:45:07 -0500427 'ThemeService', 'SvgUtilService', 'Topo2ZoomService',
Steven Burrowsaea509d2017-04-12 14:17:47 -0700428 'Topo2ViewService', 'Topo2LinkPanelService', 'FnService', 'PrefsService',
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400429 'Topo2MastershipService', 'Topo2TrafficService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000430 function (_$log_, _c_, _Model_, _ts_, _sus_,
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400431 _t2zs_, _t2vs_, _t2lps_, _fn_, _ps_, _t2mss_, _t2ts_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100432
Steven Burrowsaf96a212016-12-28 12:57:02 +0000433 $log = _$log_;
434 ts = _ts_;
435 sus = _sus_;
436 t2zs = _t2zs_;
437 t2vs = _t2vs_;
438 Collection = _c_;
439 Model = _Model_;
440 t2lps = _t2lps_;
441 fn = _fn_;
Steven Burrowsaea509d2017-04-12 14:17:47 -0700442 ps = _ps_;
Steven Burrowsc515e602017-04-13 11:17:40 -0700443 t2mss = _t2mss_;
Steven Burrowsca1a39c2017-05-08 17:31:08 -0400444 t2ts = _t2ts_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100445
Steven Burrowsaf96a212016-12-28 12:57:02 +0000446 return {
447 createLinkCollection: createLinkCollection
448 };
449 }
450 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100451})();