Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
| 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 25 | var $log, Collection, Model, ts, sus, t2zs, t2vs; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 26 | |
| 27 | var linkLabelOffset = '0.35em'; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 28 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 29 | 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 Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 35 | allLinkSubTypes = 'inactive not-permitted', |
| 36 | labelDim = 30; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 37 | |
| 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 55 | function createLink() { |
| 56 | |
| 57 | var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB')); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 58 | |
| 59 | var attrs = angular.extend({}, linkPoints, { |
| 60 | key: this.get('id'), |
| 61 | class: 'link', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 62 | 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 Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 71 | // extra: link.extra |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | this.set(attrs); |
| 75 | } |
| 76 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 77 | 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 Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 91 | function isLinkOnline(node) { |
| 92 | return (node.get('nodeType') === 'region') ? true : node.get('online'); |
| 93 | } |
| 94 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 95 | function linkEndPoints(srcId, dstId) { |
| 96 | |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 97 | var allNodes = this.region.nodes(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 98 | var sourceNode = this.region.findNodeById(srcId); |
| 99 | var targetNode = this.region.findNodeById(dstId); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 100 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 101 | if (!sourceNode || !targetNode) { |
| 102 | $log.error('Node(s) not on map for link:' + srcId + ':' + dstId); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 103 | // logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 104 | return null; |
| 105 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 106 | |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 107 | this.source = allNodes.indexOf(sourceNode); |
| 108 | this.target = allNodes.indexOf(targetNode); |
| 109 | this.sourceNode = sourceNode; |
| 110 | this.targetNode = targetNode; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 111 | |
| 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 Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 128 | // TODO: original code is: (s && s.expected) && (t && t.expected); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 129 | return true; |
| 130 | }, |
| 131 | online: function () { |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 132 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 133 | var source = this.get('source'), |
| 134 | target = this.get('target'), |
| 135 | sourceOnline = isLinkOnline(source), |
| 136 | targetOnline = isLinkOnline(target); |
| 137 | |
| 138 | return (sourceOnline) && (targetOnline); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 139 | }, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 140 | 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 Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 149 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 150 | if (showPort()) { |
| 151 | point = this.locatePortLabel(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 152 | angular.extend(point, { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 153 | id: 'topo-port-tgt', |
| 154 | num: this.get('portB') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 155 | }); |
| 156 | data.push(point); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 157 | |
| 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 Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 169 | .data(data) |
| 170 | .enter().append('g') |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 171 | .classed('portLabel', true) |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 172 | .attr('id', function (d) { return d.id; }) |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 173 | |
| 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 Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 189 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 190 | }); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 191 | |
| 192 | this.setScale(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 193 | } |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 194 | }, |
| 195 | unenhance: function () { |
| 196 | this.el.classed('enhanced', false); |
| 197 | d3.select('#topo-portLabels').selectAll('.portLabel').remove(); |
| 198 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 199 | locatePortLabel: function (src) { |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 200 | |
| 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 Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 210 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 211 | function dist(x, y) { |
| 212 | return Math.sqrt(x * x + y * y); |
| 213 | } |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 214 | |
| 215 | var dx = farX - nearX, |
| 216 | dy = farY - nearY, |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 217 | k = (32 * offset) / dist(dx, dy); |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 218 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 219 | return { x: k * dx + nearX, y: k * dy + nearY }; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 220 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 221 | 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 227 | online = this.online(), |
| 228 | modeCls = this.expected() ? 'inactive' : 'not-permitted', |
| 229 | delay = immediate ? 0 : 1000; |
| 230 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 231 | // 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 247 | .attr('stroke', linkConfig[th].baseColor); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 248 | |
| 249 | this.setScale(); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 250 | } |
| 251 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 252 | onEnter: function (el) { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 253 | var link = d3.select(el); |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 254 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 255 | this.el = link; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 256 | this.restyleLinkElement(); |
| 257 | |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 258 | // 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 Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 263 | if (this.get('type') === 'hostLink') { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 264 | // sus.visible(link, api.showHosts()); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 265 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 266 | }, |
| 267 | setScale: function () { |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 268 | var width = linkScale(widthRatio) / t2zs.scale(); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 269 | this.el.style('stroke-width', width + 'px'); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 270 | |
| 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 Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 278 | }, |
| 279 | update: function () { |
| 280 | if (this.el.classed('enhanced')) { |
| 281 | this.enhance(); |
| 282 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 283 | } |
| 284 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 285 | |
| 286 | var LinkCollection = Collection.extend({ |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 287 | model: LinkModel |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 288 | }); |
| 289 | |
| 290 | return new LinkCollection(data); |
| 291 | } |
| 292 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 293 | function showPort() { |
| 294 | return t2vs.getPortHighlighting(); |
| 295 | } |
| 296 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 297 | angular.module('ovTopo2') |
| 298 | .factory('Topo2LinkService', |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 299 | ['$log', 'Topo2Collection', 'Topo2Model', |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 300 | 'ThemeService', 'SvgUtilService', 'Topo2ZoomService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 301 | 'Topo2ViewService', |
| 302 | function (_$log_, _Collection_, _Model_, _ts_, _sus_, |
| 303 | _t2zs_, _t2vs_) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 304 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 305 | $log = _$log_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 306 | ts = _ts_; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 307 | sus = _sus_; |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 308 | t2zs = _t2zs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 309 | t2vs = _t2vs_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 310 | Collection = _Collection_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 311 | Model = _Model_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 312 | |
| 313 | return { |
| 314 | createLinkCollection: createLinkCollection |
| 315 | }; |
| 316 | } |
| 317 | ]); |
| 318 | |
| 319 | })(); |