blob: f19f2df2772304a25c83b1a216f09c0cf16a4642 [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 Burrowsc515e602017-04-13 11:17:40 -070025 var $log, Collection, Model, ts, sus, t2zs, t2vs, t2lps, fn, ps, t2mss;
Steven Burrows9edc7e02016-08-29 11:52:07 +010026
27 var linkLabelOffset = '0.35em';
Steven Burrows57e24e92016-08-04 18:38:24 +010028
Steven Burrowsec1f45c2016-08-08 16:14:41 +010029 var widthRatio = 1.4,
30 linkScale = d3.scale.linear()
31 .domain([1, 12])
32 .range([widthRatio, 12 * widthRatio])
33 .clamp(true),
Steven Burrows583f4be2016-11-04 14:06:50 +010034 allLinkTypes = 'direct optical tunnel UiDeviceLink',
35 allLinkSubTypes = 'not-permitted',
Steven Burrowsa3fca812016-10-14 15:11:04 -050036 labelDim = 30;
Steven Burrowsec1f45c2016-08-08 16:14:41 +010037
38 // configuration
39 var linkConfig = {
40 light: {
41 baseColor: '#939598',
42 inColor: '#66f',
43 outColor: '#f00'
44 },
45 dark: {
46 // TODO : theme
47 baseColor: '#939598',
48 inColor: '#66f',
49 outColor: '#f00'
50 },
51 inWidth: 12,
52 outWidth: 10
53 };
54
Steven Burrowsec1f45c2016-08-08 16:14:41 +010055 function createLink() {
56
57 var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB'));
Steven Burrowsec1f45c2016-08-08 16:14:41 +010058
59 var attrs = angular.extend({}, linkPoints, {
60 key: this.get('id'),
61 class: 'link',
Steven Burrowsec1f45c2016-08-08 16:14:41 +010062 position: {
63 x1: 0,
64 y1: 0,
65 x2: 0,
66 y2: 0
67 }
68 // functions to aggregate dual link state
Steven Burrows9edc7e02016-08-29 11:52:07 +010069 // extra: link.extra
Steven Burrowsec1f45c2016-08-08 16:14:41 +010070 });
71
72 this.set(attrs);
73 }
74
Steven Burrows9edc7e02016-08-29 11:52:07 +010075 function rectAroundText(el) {
76 var text = el.select('text'),
77 box = text.node().getBBox();
78
79 // translate the bbox so that it is centered on [x,y]
80 box.x = -box.width / 2;
81 box.y = -box.height / 2;
82
83 // add padding
84 box.x -= 4;
85 box.width += 8;
86 return box;
87 }
88
Steven Burrowsdfa52b02016-09-02 13:50:43 +010089 function isLinkOnline(node) {
90 return (node.get('nodeType') === 'region') ? true : node.get('online');
91 }
92
Steven Burrowsec1f45c2016-08-08 16:14:41 +010093 function linkEndPoints(srcId, dstId) {
94
Steven Burrowsaf96a212016-12-28 12:57:02 +000095 var findNodeById = this.region.model.findNodeById.bind(this.region),
96 allNodes = this.region.model.nodes(),
97 sourceNode = findNodeById(this, srcId),
98 targetNode = findNodeById(this, dstId);
Steven Burrowsec1f45c2016-08-08 16:14:41 +010099
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100100 if (!sourceNode || !targetNode) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500101 $log.error('Node(s) not on map for link:' + srcId + '~' + dstId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100102 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100103 return null;
104 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100105
Steven Burrowsa3fca812016-10-14 15:11:04 -0500106 this.source = allNodes.indexOf(sourceNode);
107 this.target = allNodes.indexOf(targetNode);
108 this.sourceNode = sourceNode;
109 this.targetNode = targetNode;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100110
111 return {
112 source: sourceNode,
113 target: targetNode
114 };
115 }
116
117 function createLinkCollection(data, _region) {
118
119 var LinkModel = Model.extend({
Steven Burrows68d6f952017-03-10 13:53:35 +0000120 initialize: function () {
121 this.super = this.constructor.__super__;
122 this.super.initialize.apply(this, arguments);
123 this.createLink();
124 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100125 region: _region,
126 createLink: createLink,
127 linkEndPoints: linkEndPoints,
128 type: function () {
129 return this.get('type');
130 },
Steven Burrows86af4352016-11-16 18:19:12 -0600131 svgClassName: function () {
132 return fn.classNames('link',
133 this.nodeType,
134 this.get('type'),
135 {
136 enhanced: this.get('enhanced'),
Steven Burrowsc515e602017-04-13 11:17:40 -0700137 selected: this.get('selected'),
138 suppressedmax: this.get('mastership')
Steven Burrows86af4352016-11-16 18:19:12 -0600139 }
140 );
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());
159 }
160 },
Steven Burrows9edc7e02016-08-29 11:52:07 +0100161 enhance: function () {
162 var data = [],
163 point;
164
Steven Burrows1c5c8612016-10-05 13:45:13 -0500165 if (showPort()) {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000166 this.set('enhanced', true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500167 point = this.locatePortLabel();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100168 angular.extend(point, {
Simon Hunt95f4b422017-03-03 13:49:05 -0800169 id: 'topo2-port-tgt',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500170 num: this.get('portB')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100171 });
172 data.push(point);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500173
174 if (this.get('portA')) {
175 point = this.locatePortLabel(1);
176 angular.extend(point, {
Simon Hunt95f4b422017-03-03 13:49:05 -0800177 id: 'topo2-port-src',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500178 num: this.get('portA')
179 });
180 data.push(point);
181 }
182
Steven Burrowsbd402842017-03-08 21:30:38 +0000183 var entering = d3.select('.topo2-portLabels')
Steven Burrows1c5c8612016-10-05 13:45:13 -0500184 .selectAll('.portLabel')
Steven Burrowsa3fca812016-10-14 15:11:04 -0500185 .data(data)
186 .enter().append('g')
Steven Burrows1c5c8612016-10-05 13:45:13 -0500187 .classed('portLabel', true)
Steven Burrowsaf96a212016-12-28 12:57:02 +0000188 .attr('id', function (d) { return d.id; });
Steven Burrows1c5c8612016-10-05 13:45:13 -0500189
190 entering.each(function (d) {
191 var el = d3.select(this),
192 rect = el.append('rect'),
193 text = el.append('text').text(d.num);
194
195 var rectSize = rectAroundText(el);
196
197 rect.attr(rectSize)
198 .attr('rx', 2)
199 .attr('ry', 2);
200
201 text.attr('dy', linkLabelOffset)
202 .attr('text-anchor', 'middle');
203
204 el.attr('transform', sus.translate(d.x, d.y));
Steven Burrowsa3fca812016-10-14 15:11:04 -0500205
Steven Burrows1c5c8612016-10-05 13:45:13 -0500206 });
Steven Burrowsa3fca812016-10-14 15:11:04 -0500207
208 this.setScale();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100209 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100210 },
211 unenhance: function () {
Steven Burrows86af4352016-11-16 18:19:12 -0600212 this.set('enhanced', false);
Steven Burrowsbd402842017-03-08 21:30:38 +0000213 d3.select('.topo2-portLabels').selectAll('.portLabel').remove();
Steven Burrows9edc7e02016-08-29 11:52:07 +0100214 },
Steven Burrowsa31c5b02017-04-12 10:45:08 -0700215 setPosition: function () {
216 this.set({
217 position: {
218 x1: this.get('source').x,
219 y1: this.get('source').y,
220 x2: this.get('target').x,
221 y2: this.get('target').y
222 }
223 });
224
225 if (this.get('enhanced')) {
226 this.updatePortPosition();
227 }
228 },
229 updatePortPosition: function () {
230 var sourcePos = this.locatePortLabel(1),
231 targetPos = this.locatePortLabel();
232 d3.select('#topo2-port-src')
233 .attr('transform', sus.translate(sourcePos.x, sourcePos.y));
234 d3.select('#topo2-port-tgt')
235 .attr('transform', sus.translate(targetPos.x, targetPos.y));
236 },
Steven Burrows1aa4f582016-12-13 15:05:41 -0500237 getSelected: function () {
238 return this.collection.filter(function (m) {
239 return m.get('selected');
240 });
241 },
Steven Burrows86af4352016-11-16 18:19:12 -0600242 select: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +0000243 this.set({ 'selected': true });
Steven Burrows1aa4f582016-12-13 15:05:41 -0500244 return this.getSelected();
245 },
246 deselect: function () {
Steven Burrows5fa057e2017-03-15 17:07:56 +0000247 this.set({
248 'selected': false,
249 'enhanced': false
250 });
Steven Burrows86af4352016-11-16 18:19:12 -0600251 },
252 showDetails: function () {
Steven Burrows1aa4f582016-12-13 15:05:41 -0500253 var selected = this.getSelected();
Steven Burrows86af4352016-11-16 18:19:12 -0600254
255 if (selected) {
256 t2lps.displayLink(this);
257 } else {
258 t2lps.hide();
259 }
260 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100261 locatePortLabel: function (src) {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500262
263 var offset = 32 / (labelDim * t2zs.scale()),
264 sourceX = this.get('position').x1,
265 sourceY = this.get('position').y1,
266 targetX = this.get('position').x2,
267 targetY = this.get('position').y2,
268 nearX = src ? sourceX : targetX,
269 nearY = src ? sourceY : targetY,
270 farX = src ? targetX : sourceX,
271 farY = src ? targetY : sourceY;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100272
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100273 function dist(x, y) {
274 return Math.sqrt(x * x + y * y);
275 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100276
277 var dx = farX - nearX,
278 dy = farY - nearY,
Steven Burrowsa3fca812016-10-14 15:11:04 -0500279 k = (32 * offset) / dist(dx, dy);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100280
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100281 return { x: k * dx + nearX, y: k * dy + nearY };
Steven Burrows9edc7e02016-08-29 11:52:07 +0100282 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100283 restyleLinkElement: function (immediate) {
284 // this fn's job is to look at raw links and decide what svg classes
285 // need to be applied to the line element in the DOM
286 var th = ts.theme(),
287 el = this.el,
288 type = this.get('type'),
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100289 online = this.online(),
Steven Burrows583f4be2016-11-04 14:06:50 +0100290 modeCls = this.expected() ? '-inactive' : 'not-permitted',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100291 delay = immediate ? 0 : 1000;
292
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100293 // NOTE: understand why el is sometimes undefined on addLink events...
294 // Investigated:
295 // el is undefined when it's a reverse link that is being added.
296 // updateLinks (which sets ldata.el) isn't called before this is called.
297 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
298 // a more efficient way to fix it.
299 if (el && !el.empty()) {
300 el.classed('link', true);
301 el.classed(allLinkSubTypes, false);
302 el.classed(modeCls, !online);
303 el.classed(allLinkTypes, false);
304 if (type) {
305 el.classed(type, true);
306 }
307 el.transition()
308 .duration(delay)
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100309 .attr('stroke', linkConfig[th].baseColor);
Steven Burrowsa3fca812016-10-14 15:11:04 -0500310
311 this.setScale();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100312 }
313 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100314 onEnter: function (el) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100315 var link = d3.select(el);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100316
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100317 this.el = link;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100318 this.restyleLinkElement();
Steven Burrowsaea509d2017-04-12 14:17:47 -0700319 this.setVisibility();
Steven Burrows0616e802016-10-06 21:45:07 -0500320 },
321 setScale: function () {
Steven Burrowse8a455a2017-03-16 16:58:59 +0000322
323 if (!this.el) return;
324
Steven Burrowsa3fca812016-10-14 15:11:04 -0500325 var width = linkScale(widthRatio) / t2zs.scale();
Steven Burrows0616e802016-10-06 21:45:07 -0500326 this.el.style('stroke-width', width + 'px');
Steven Burrowsa3fca812016-10-14 15:11:04 -0500327
328 var labelScale = labelDim / (labelDim * t2zs.scale());
329
Steven Burrows8909c5c2017-04-10 09:56:52 -0700330 d3.select('.topo2-portLabels')
Steven Burrowsa3fca812016-10-14 15:11:04 -0500331 .selectAll('.portLabel')
332 .selectAll('*')
333 .style('transform', 'scale(' + labelScale + ')');
334
Steven Burrows1c5c8612016-10-05 13:45:13 -0500335 },
336 update: function () {
Steven Burrows86af4352016-11-16 18:19:12 -0600337 if (this.get('enhanced')) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500338 this.enhance();
339 }
Steven Burrowsb15a3942017-01-17 17:25:04 +0000340 },
Steven Burrowsaea509d2017-04-12 14:17:47 -0700341 setVisibility: function () {
342
343 if (this.get('type') !== 'UiEdgeLink') {
344 return;
345 }
346
347 var visible = ps.getPrefs('topo2_prefs')['hosts'];
348 this.el.style('visibility', visible ? 'visible' : 'hidden');
349 },
Steven Burrowsc515e602017-04-13 11:17:40 -0700350 displayMastership: function () {
351 this.set({ mastership: t2mss.mastership() !== null});
352 },
Steven Burrowsb15a3942017-01-17 17:25:04 +0000353 remove: function () {
354
355 var width = linkScale(widthRatio) / t2zs.scale();
356
357 this.el.transition()
358 .duration(300)
359 .attr('stroke', '#ff0000')
360 .style('stroke-width', width * 4)
361 .transition()
362 .delay(1000)
363 .style('opacity', 0);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100364 }
365 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100366
367 var LinkCollection = Collection.extend({
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100368 model: LinkModel
Steven Burrows57e24e92016-08-04 18:38:24 +0100369 });
370
371 return new LinkCollection(data);
372 }
373
Steven Burrows1c5c8612016-10-05 13:45:13 -0500374 function showPort() {
375 return t2vs.getPortHighlighting();
376 }
377
Steven Burrows57e24e92016-08-04 18:38:24 +0100378 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +0000379 .factory('Topo2LinkService', [
380 '$log', 'Topo2Collection', 'Topo2Model',
Steven Burrows0616e802016-10-06 21:45:07 -0500381 'ThemeService', 'SvgUtilService', 'Topo2ZoomService',
Steven Burrowsaea509d2017-04-12 14:17:47 -0700382 'Topo2ViewService', 'Topo2LinkPanelService', 'FnService', 'PrefsService',
Steven Burrowsc515e602017-04-13 11:17:40 -0700383 'Topo2MastershipService',
Steven Burrowsaf96a212016-12-28 12:57:02 +0000384 function (_$log_, _c_, _Model_, _ts_, _sus_,
Steven Burrowsc515e602017-04-13 11:17:40 -0700385 _t2zs_, _t2vs_, _t2lps_, _fn_, _ps_, _t2mss_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100386
Steven Burrowsaf96a212016-12-28 12:57:02 +0000387 $log = _$log_;
388 ts = _ts_;
389 sus = _sus_;
390 t2zs = _t2zs_;
391 t2vs = _t2vs_;
392 Collection = _c_;
393 Model = _Model_;
394 t2lps = _t2lps_;
395 fn = _fn_;
Steven Burrowsaea509d2017-04-12 14:17:47 -0700396 ps = _ps_;
Steven Burrowsc515e602017-04-13 11:17:40 -0700397 t2mss = _t2mss_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100398
Steven Burrowsaf96a212016-12-28 12:57:02 +0000399 return {
400 createLinkCollection: createLinkCollection
401 };
402 }
403 ]);
Steven Burrows57e24e92016-08-04 18:38:24 +0100404})();