blob: d76963140cb8324f41b8bd1a288630e80adedea7 [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 Burrows86af4352016-11-16 18:19:12 -060025 var $log, Collection, Model, ts, sus, t2zs, t2vs, t2lps, fn;
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 srcPort: this.get('srcPort'),
63 tgtPort: this.get('dstPort'),
64 position: {
65 x1: 0,
66 y1: 0,
67 x2: 0,
68 y2: 0
69 }
70 // functions to aggregate dual link state
Steven Burrows9edc7e02016-08-29 11:52:07 +010071 // extra: link.extra
Steven Burrowsec1f45c2016-08-08 16:14:41 +010072 });
73
74 this.set(attrs);
75 }
76
Steven Burrows9edc7e02016-08-29 11:52:07 +010077 function rectAroundText(el) {
78 var text = el.select('text'),
79 box = text.node().getBBox();
80
81 // translate the bbox so that it is centered on [x,y]
82 box.x = -box.width / 2;
83 box.y = -box.height / 2;
84
85 // add padding
86 box.x -= 4;
87 box.width += 8;
88 return box;
89 }
90
Steven Burrowsdfa52b02016-09-02 13:50:43 +010091 function isLinkOnline(node) {
92 return (node.get('nodeType') === 'region') ? true : node.get('online');
93 }
94
Steven Burrowsec1f45c2016-08-08 16:14:41 +010095 function linkEndPoints(srcId, dstId) {
96
Steven Burrowsa3fca812016-10-14 15:11:04 -050097 var allNodes = this.region.nodes();
Steven Burrows8f45ce22016-10-27 20:04:14 -050098 var sourceNode = this.region.findNodeById(this, srcId);
99 var targetNode = this.region.findNodeById(this, dstId);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100100
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100101 if (!sourceNode || !targetNode) {
Steven Burrows8f45ce22016-10-27 20:04:14 -0500102 $log.error('Node(s) not on map for link:' + srcId + '~' + dstId);
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100103 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100104 return null;
105 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100106
Steven Burrowsa3fca812016-10-14 15:11:04 -0500107 this.source = allNodes.indexOf(sourceNode);
108 this.target = allNodes.indexOf(targetNode);
109 this.sourceNode = sourceNode;
110 this.targetNode = targetNode;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100111
112 return {
113 source: sourceNode,
114 target: targetNode
115 };
116 }
117
118 function createLinkCollection(data, _region) {
119
120 var LinkModel = Model.extend({
121 region: _region,
122 createLink: createLink,
123 linkEndPoints: linkEndPoints,
124 type: function () {
125 return this.get('type');
126 },
Steven Burrows86af4352016-11-16 18:19:12 -0600127 svgClassName: function () {
128 return fn.classNames('link',
129 this.nodeType,
130 this.get('type'),
131 {
132 enhanced: this.get('enhanced'),
133 selected: this.get('selected')
134 }
135 );
136 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100137 expected: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100138 // TODO: original code is: (s && s.expected) && (t && t.expected);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100139 return true;
140 },
141 online: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100142
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100143 var source = this.get('source'),
144 target = this.get('target'),
145 sourceOnline = isLinkOnline(source),
146 targetOnline = isLinkOnline(target);
147
148 return (sourceOnline) && (targetOnline);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100149 },
Steven Burrows86af4352016-11-16 18:19:12 -0600150 onChange: function () {
151 // Update class names when the model changes
152 if (this.el) {
153 this.el.attr('class', this.svgClassName());
154 }
155 },
Steven Burrows9edc7e02016-08-29 11:52:07 +0100156 enhance: function () {
157 var data = [],
158 point;
159
Steven Burrows1aa4f582016-12-13 15:05:41 -0500160 // angular.forEach(this.collection.models, function (link) {
161 // link.unenhance();
162 // });
Steven Burrows9edc7e02016-08-29 11:52:07 +0100163
Steven Burrows86af4352016-11-16 18:19:12 -0600164 this.set('enhanced', true);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100165
Steven Burrows1c5c8612016-10-05 13:45:13 -0500166 if (showPort()) {
167 point = this.locatePortLabel();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100168 angular.extend(point, {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500169 id: 'topo-port-tgt',
170 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, {
177 id: 'topo-port-src',
178 num: this.get('portA')
179 });
180 data.push(point);
181 }
182
183 var entering = d3.select('#topo-portLabels')
184 .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 Burrowsa3fca812016-10-14 15:11:04 -0500188 .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 Burrows9edc7e02016-08-29 11:52:07 +0100213 d3.select('#topo-portLabels').selectAll('.portLabel').remove();
214 },
Steven Burrows1aa4f582016-12-13 15:05:41 -0500215 getSelected: function () {
216 return this.collection.filter(function (m) {
217 return m.get('selected');
218 });
219 },
Steven Burrows86af4352016-11-16 18:19:12 -0600220 select: function () {
Steven Burrows1aa4f582016-12-13 15:05:41 -0500221
Steven Burrows86af4352016-11-16 18:19:12 -0600222 var ev = d3.event;
223
224 // TODO: if single selection clear selected devices, hosts, sub-regions
225 var s = Boolean(this.get('selected'));
226 // Clear all selected Items
227 _.each(this.collection.models, function (m) {
228 m.set('selected', false);
229 });
230
231 this.set('selected', !s);
Steven Burrows1aa4f582016-12-13 15:05:41 -0500232 this.showDetails();
Steven Burrows86af4352016-11-16 18:19:12 -0600233
Steven Burrows1aa4f582016-12-13 15:05:41 -0500234 return this.getSelected();
235 },
236 deselect: function () {
237 this.set('selected', false);
238 this.set('enhanced', false);
Steven Burrows86af4352016-11-16 18:19:12 -0600239 },
240 showDetails: function () {
Steven Burrows1aa4f582016-12-13 15:05:41 -0500241 var selected = this.getSelected();
Steven Burrows86af4352016-11-16 18:19:12 -0600242
243 if (selected) {
244 t2lps.displayLink(this);
245 } else {
246 t2lps.hide();
247 }
248 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100249 locatePortLabel: function (src) {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500250
251 var offset = 32 / (labelDim * t2zs.scale()),
252 sourceX = this.get('position').x1,
253 sourceY = this.get('position').y1,
254 targetX = this.get('position').x2,
255 targetY = this.get('position').y2,
256 nearX = src ? sourceX : targetX,
257 nearY = src ? sourceY : targetY,
258 farX = src ? targetX : sourceX,
259 farY = src ? targetY : sourceY;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100260
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100261 function dist(x, y) {
262 return Math.sqrt(x * x + y * y);
263 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100264
265 var dx = farX - nearX,
266 dy = farY - nearY,
Steven Burrowsa3fca812016-10-14 15:11:04 -0500267 k = (32 * offset) / dist(dx, dy);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100268
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100269 return { x: k * dx + nearX, y: k * dy + nearY };
Steven Burrows9edc7e02016-08-29 11:52:07 +0100270 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100271 restyleLinkElement: function (immediate) {
272 // this fn's job is to look at raw links and decide what svg classes
273 // need to be applied to the line element in the DOM
274 var th = ts.theme(),
275 el = this.el,
276 type = this.get('type'),
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100277 online = this.online(),
Steven Burrows583f4be2016-11-04 14:06:50 +0100278 modeCls = this.expected() ? '-inactive' : 'not-permitted',
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100279 delay = immediate ? 0 : 1000;
280
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100281 // NOTE: understand why el is sometimes undefined on addLink events...
282 // Investigated:
283 // el is undefined when it's a reverse link that is being added.
284 // updateLinks (which sets ldata.el) isn't called before this is called.
285 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
286 // a more efficient way to fix it.
287 if (el && !el.empty()) {
288 el.classed('link', true);
289 el.classed(allLinkSubTypes, false);
290 el.classed(modeCls, !online);
291 el.classed(allLinkTypes, false);
292 if (type) {
293 el.classed(type, true);
294 }
295 el.transition()
296 .duration(delay)
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100297 .attr('stroke', linkConfig[th].baseColor);
Steven Burrowsa3fca812016-10-14 15:11:04 -0500298
299 this.setScale();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100300 }
301 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100302 onEnter: function (el) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100303 var link = d3.select(el);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100304
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100305 this.el = link;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100306 this.restyleLinkElement();
307
308 if (this.get('type') === 'hostLink') {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100309 // sus.visible(link, api.showHosts());
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100310 }
Steven Burrows0616e802016-10-06 21:45:07 -0500311 },
312 setScale: function () {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500313 var width = linkScale(widthRatio) / t2zs.scale();
Steven Burrows0616e802016-10-06 21:45:07 -0500314 this.el.style('stroke-width', width + 'px');
Steven Burrowsa3fca812016-10-14 15:11:04 -0500315
316 var labelScale = labelDim / (labelDim * t2zs.scale());
317
318 d3.select('#topo-portLabels')
319 .selectAll('.portLabel')
320 .selectAll('*')
321 .style('transform', 'scale(' + labelScale + ')');
322
Steven Burrows1c5c8612016-10-05 13:45:13 -0500323 },
324 update: function () {
Steven Burrows86af4352016-11-16 18:19:12 -0600325 if (this.get('enhanced')) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500326 this.enhance();
327 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100328 }
329 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100330
331 var LinkCollection = Collection.extend({
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100332 model: LinkModel
Steven Burrows57e24e92016-08-04 18:38:24 +0100333 });
334
335 return new LinkCollection(data);
336 }
337
Steven Burrows1c5c8612016-10-05 13:45:13 -0500338 function showPort() {
339 return t2vs.getPortHighlighting();
340 }
341
Steven Burrows57e24e92016-08-04 18:38:24 +0100342 angular.module('ovTopo2')
343 .factory('Topo2LinkService',
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100344 ['$log', 'Topo2Collection', 'Topo2Model',
Steven Burrows0616e802016-10-06 21:45:07 -0500345 'ThemeService', 'SvgUtilService', 'Topo2ZoomService',
Steven Burrows86af4352016-11-16 18:19:12 -0600346 'Topo2ViewService', 'Topo2LinkPanelService', 'FnService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500347 function (_$log_, _Collection_, _Model_, _ts_, _sus_,
Steven Burrows86af4352016-11-16 18:19:12 -0600348 _t2zs_, _t2vs_, _t2lps_, _fn_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100349
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100350 $log = _$log_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100351 ts = _ts_;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100352 sus = _sus_;
Steven Burrows0616e802016-10-06 21:45:07 -0500353 t2zs = _t2zs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500354 t2vs = _t2vs_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100355 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100356 Model = _Model_;
Steven Burrows86af4352016-11-16 18:19:12 -0600357 t2lps = _t2lps_;
358 fn = _fn_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100359
360 return {
361 createLinkCollection: createLinkCollection
362 };
363 }
364 ]);
365
366})();