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 | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 25 | var $log; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | var Collection, Model, region, ts; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 27 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 28 | var widthRatio = 1.4, |
| 29 | linkScale = d3.scale.linear() |
| 30 | .domain([1, 12]) |
| 31 | .range([widthRatio, 12 * widthRatio]) |
| 32 | .clamp(true), |
| 33 | allLinkTypes = 'direct indirect optical tunnel UiDeviceLink', |
| 34 | allLinkSubTypes = 'inactive not-permitted'; |
| 35 | |
| 36 | // configuration |
| 37 | var linkConfig = { |
| 38 | light: { |
| 39 | baseColor: '#939598', |
| 40 | inColor: '#66f', |
| 41 | outColor: '#f00' |
| 42 | }, |
| 43 | dark: { |
| 44 | // TODO : theme |
| 45 | baseColor: '#939598', |
| 46 | inColor: '#66f', |
| 47 | outColor: '#f00' |
| 48 | }, |
| 49 | inWidth: 12, |
| 50 | outWidth: 10 |
| 51 | }; |
| 52 | |
| 53 | var defaultLinkType = 'direct', |
| 54 | nearDist = 15; |
| 55 | |
| 56 | function createLink() { |
| 57 | |
| 58 | var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB')); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 59 | |
| 60 | var attrs = angular.extend({}, linkPoints, { |
| 61 | key: this.get('id'), |
| 62 | class: 'link', |
| 63 | weight: 1, |
| 64 | srcPort: this.get('srcPort'), |
| 65 | tgtPort: this.get('dstPort'), |
| 66 | position: { |
| 67 | x1: 0, |
| 68 | y1: 0, |
| 69 | x2: 0, |
| 70 | y2: 0 |
| 71 | } |
| 72 | // functions to aggregate dual link state |
| 73 | // extra: link.extra |
| 74 | }); |
| 75 | |
| 76 | this.set(attrs); |
| 77 | } |
| 78 | |
| 79 | function linkEndPoints(srcId, dstId) { |
| 80 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 81 | var sourceNode = this.region.findNodeById(srcId) |
| 82 | var targetNode = this.region.findNodeById(dstId) |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 83 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 84 | if (!sourceNode || !targetNode) { |
| 85 | $log.error('Node(s) not on map for link:' + srcId + ':' + dstId); |
| 86 | //logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 87 | return null; |
| 88 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 89 | |
| 90 | this.source = sourceNode.toJSON(); |
| 91 | this.target = targetNode.toJSON(); |
| 92 | |
| 93 | return { |
| 94 | source: sourceNode, |
| 95 | target: targetNode |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | function createLinkCollection(data, _region) { |
| 100 | |
| 101 | var LinkModel = Model.extend({ |
| 102 | region: _region, |
| 103 | createLink: createLink, |
| 104 | linkEndPoints: linkEndPoints, |
| 105 | type: function () { |
| 106 | return this.get('type'); |
| 107 | }, |
| 108 | expected: function () { |
| 109 | //TODO: original code is: (s && s.expected) && (t && t.expected); |
| 110 | return true; |
| 111 | }, |
| 112 | online: function () { |
| 113 | return true; |
| 114 | return both && (s && s.online) && (t && t.online); |
| 115 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 116 | restyleLinkElement: function (immediate) { |
| 117 | // this fn's job is to look at raw links and decide what svg classes |
| 118 | // need to be applied to the line element in the DOM |
| 119 | var th = ts.theme(), |
| 120 | el = this.el, |
| 121 | type = this.get('type'), |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 122 | online = this.online(), |
| 123 | modeCls = this.expected() ? 'inactive' : 'not-permitted', |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 124 | lw = 1.2, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 125 | delay = immediate ? 0 : 1000; |
| 126 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 127 | // NOTE: understand why el is sometimes undefined on addLink events... |
| 128 | // Investigated: |
| 129 | // el is undefined when it's a reverse link that is being added. |
| 130 | // updateLinks (which sets ldata.el) isn't called before this is called. |
| 131 | // Calling _updateLinks in addLinkUpdate fixes it, but there might be |
| 132 | // a more efficient way to fix it. |
| 133 | if (el && !el.empty()) { |
| 134 | el.classed('link', true); |
| 135 | el.classed(allLinkSubTypes, false); |
| 136 | el.classed(modeCls, !online); |
| 137 | el.classed(allLinkTypes, false); |
| 138 | if (type) { |
| 139 | el.classed(type, true); |
| 140 | } |
| 141 | el.transition() |
| 142 | .duration(delay) |
| 143 | .attr('stroke-width', linkScale(lw)) |
| 144 | .attr('stroke', linkConfig[th].baseColor); |
| 145 | } |
| 146 | }, |
| 147 | |
| 148 | onEnter: function (el) { |
| 149 | var link = d3.select(el); |
| 150 | this.el = link; |
| 151 | |
| 152 | this.restyleLinkElement(); |
| 153 | |
| 154 | if (this.get('type') === 'hostLink') { |
| 155 | sus.visible(link, api.showHosts()); |
| 156 | } |
| 157 | } |
| 158 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 159 | |
| 160 | var LinkCollection = Collection.extend({ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 161 | model: LinkModel, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 162 | }); |
| 163 | |
| 164 | return new LinkCollection(data); |
| 165 | } |
| 166 | |
| 167 | angular.module('ovTopo2') |
| 168 | .factory('Topo2LinkService', |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 169 | ['$log', 'Topo2Collection', 'Topo2Model', 'ThemeService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 170 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 171 | function (_$log_, _Collection_, _Model_, _ts_) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 172 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 173 | $log = _$log_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 174 | ts = _ts_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 175 | Collection = _Collection_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 176 | Model = _Model_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 177 | |
| 178 | return { |
| 179 | createLinkCollection: createLinkCollection |
| 180 | }; |
| 181 | } |
| 182 | ]); |
| 183 | |
| 184 | })(); |