blob: f8f331c6fb5bbc18232602436c4069a7be94573a [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 Burrows1c5c8612016-10-05 13:45:13 -050025 var $log, Collection, Model, ts, sus, t2zs, t2vs;
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),
34 allLinkTypes = 'direct indirect optical tunnel UiDeviceLink',
Steven Burrowsa3fca812016-10-14 15:11:04 -050035 allLinkSubTypes = 'inactive not-permitted',
36 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 },
127 expected: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100128 // TODO: original code is: (s && s.expected) && (t && t.expected);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100129 return true;
130 },
131 online: function () {
Steven Burrows9edc7e02016-08-29 11:52:07 +0100132
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100133 var source = this.get('source'),
134 target = this.get('target'),
135 sourceOnline = isLinkOnline(source),
136 targetOnline = isLinkOnline(target);
137
138 return (sourceOnline) && (targetOnline);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100139 },
Steven Burrows9edc7e02016-08-29 11:52:07 +0100140 enhance: function () {
141 var data = [],
142 point;
143
144 angular.forEach(this.collection.models, function (link) {
145 link.unenhance();
146 });
147
148 this.el.classed('enhanced', true);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100149
Steven Burrows1c5c8612016-10-05 13:45:13 -0500150 if (showPort()) {
151 point = this.locatePortLabel();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100152 angular.extend(point, {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500153 id: 'topo-port-tgt',
154 num: this.get('portB')
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100155 });
156 data.push(point);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500157
158 if (this.get('portA')) {
159 point = this.locatePortLabel(1);
160 angular.extend(point, {
161 id: 'topo-port-src',
162 num: this.get('portA')
163 });
164 data.push(point);
165 }
166
167 var entering = d3.select('#topo-portLabels')
168 .selectAll('.portLabel')
Steven Burrowsa3fca812016-10-14 15:11:04 -0500169 .data(data)
170 .enter().append('g')
Steven Burrows1c5c8612016-10-05 13:45:13 -0500171 .classed('portLabel', true)
Steven Burrowsa3fca812016-10-14 15:11:04 -0500172 .attr('id', function (d) { return d.id; })
Steven Burrows1c5c8612016-10-05 13:45:13 -0500173
174 entering.each(function (d) {
175 var el = d3.select(this),
176 rect = el.append('rect'),
177 text = el.append('text').text(d.num);
178
179 var rectSize = rectAroundText(el);
180
181 rect.attr(rectSize)
182 .attr('rx', 2)
183 .attr('ry', 2);
184
185 text.attr('dy', linkLabelOffset)
186 .attr('text-anchor', 'middle');
187
188 el.attr('transform', sus.translate(d.x, d.y));
Steven Burrowsa3fca812016-10-14 15:11:04 -0500189
Steven Burrows1c5c8612016-10-05 13:45:13 -0500190 });
Steven Burrowsa3fca812016-10-14 15:11:04 -0500191
192 this.setScale();
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100193 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100194 },
195 unenhance: function () {
196 this.el.classed('enhanced', false);
197 d3.select('#topo-portLabels').selectAll('.portLabel').remove();
198 },
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100199 locatePortLabel: function (src) {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500200
201 var offset = 32 / (labelDim * t2zs.scale()),
202 sourceX = this.get('position').x1,
203 sourceY = this.get('position').y1,
204 targetX = this.get('position').x2,
205 targetY = this.get('position').y2,
206 nearX = src ? sourceX : targetX,
207 nearY = src ? sourceY : targetY,
208 farX = src ? targetX : sourceX,
209 farY = src ? targetY : sourceY;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100210
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100211 function dist(x, y) {
212 return Math.sqrt(x * x + y * y);
213 }
Steven Burrows9edc7e02016-08-29 11:52:07 +0100214
215 var dx = farX - nearX,
216 dy = farY - nearY,
Steven Burrowsa3fca812016-10-14 15:11:04 -0500217 k = (32 * offset) / dist(dx, dy);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100218
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100219 return { x: k * dx + nearX, y: k * dy + nearY };
Steven Burrows9edc7e02016-08-29 11:52:07 +0100220 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100221 restyleLinkElement: function (immediate) {
222 // this fn's job is to look at raw links and decide what svg classes
223 // need to be applied to the line element in the DOM
224 var th = ts.theme(),
225 el = this.el,
226 type = this.get('type'),
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100227 online = this.online(),
228 modeCls = this.expected() ? 'inactive' : 'not-permitted',
229 delay = immediate ? 0 : 1000;
230
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100231 // NOTE: understand why el is sometimes undefined on addLink events...
232 // Investigated:
233 // el is undefined when it's a reverse link that is being added.
234 // updateLinks (which sets ldata.el) isn't called before this is called.
235 // Calling _updateLinks in addLinkUpdate fixes it, but there might be
236 // a more efficient way to fix it.
237 if (el && !el.empty()) {
238 el.classed('link', true);
239 el.classed(allLinkSubTypes, false);
240 el.classed(modeCls, !online);
241 el.classed(allLinkTypes, false);
242 if (type) {
243 el.classed(type, true);
244 }
245 el.transition()
246 .duration(delay)
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100247 .attr('stroke', linkConfig[th].baseColor);
Steven Burrowsa3fca812016-10-14 15:11:04 -0500248
249 this.setScale();
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100250 }
251 },
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100252 onEnter: function (el) {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100253 var link = d3.select(el);
Steven Burrows9edc7e02016-08-29 11:52:07 +0100254
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100255 this.el = link;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100256 this.restyleLinkElement();
257
Steven Burrowsa3fca812016-10-14 15:11:04 -0500258 // TODO: Needs improving - originally this was calculated
259 // from mouse position.
260 this.el.on('mouseover', this.enhance.bind(this));
261 this.el.on('mouseout', this.unenhance.bind(this));
262
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100263 if (this.get('type') === 'hostLink') {
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100264 // sus.visible(link, api.showHosts());
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100265 }
Steven Burrows0616e802016-10-06 21:45:07 -0500266 },
267 setScale: function () {
Steven Burrowsa3fca812016-10-14 15:11:04 -0500268 var width = linkScale(widthRatio) / t2zs.scale();
Steven Burrows0616e802016-10-06 21:45:07 -0500269 this.el.style('stroke-width', width + 'px');
Steven Burrowsa3fca812016-10-14 15:11:04 -0500270
271 var labelScale = labelDim / (labelDim * t2zs.scale());
272
273 d3.select('#topo-portLabels')
274 .selectAll('.portLabel')
275 .selectAll('*')
276 .style('transform', 'scale(' + labelScale + ')');
277
Steven Burrows1c5c8612016-10-05 13:45:13 -0500278 },
279 update: function () {
280 if (this.el.classed('enhanced')) {
281 this.enhance();
282 }
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100283 }
284 });
Steven Burrows57e24e92016-08-04 18:38:24 +0100285
286 var LinkCollection = Collection.extend({
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100287 model: LinkModel
Steven Burrows57e24e92016-08-04 18:38:24 +0100288 });
289
290 return new LinkCollection(data);
291 }
292
Steven Burrows1c5c8612016-10-05 13:45:13 -0500293 function showPort() {
294 return t2vs.getPortHighlighting();
295 }
296
Steven Burrows57e24e92016-08-04 18:38:24 +0100297 angular.module('ovTopo2')
298 .factory('Topo2LinkService',
Steven Burrowsdfa52b02016-09-02 13:50:43 +0100299 ['$log', 'Topo2Collection', 'Topo2Model',
Steven Burrows0616e802016-10-06 21:45:07 -0500300 'ThemeService', 'SvgUtilService', 'Topo2ZoomService',
Steven Burrows1c5c8612016-10-05 13:45:13 -0500301 'Topo2ViewService',
302 function (_$log_, _Collection_, _Model_, _ts_, _sus_,
303 _t2zs_, _t2vs_) {
Steven Burrows57e24e92016-08-04 18:38:24 +0100304
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100305 $log = _$log_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100306 ts = _ts_;
Steven Burrows9edc7e02016-08-29 11:52:07 +0100307 sus = _sus_;
Steven Burrows0616e802016-10-06 21:45:07 -0500308 t2zs = _t2zs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500309 t2vs = _t2vs_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100310 Collection = _Collection_;
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100311 Model = _Model_;
Steven Burrows57e24e92016-08-04 18:38:24 +0100312
313 return {
314 createLinkCollection: createLinkCollection
315 };
316 }
317 ]);
318
319})();