Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 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 Hosts Module. |
| 19 | Module that holds the hosts for a region |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 25 | var Collection, Model; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 26 | |
Steven Burrows | e1ea27b | 2017-04-12 09:53:41 -0700 | [diff] [blame] | 27 | var hostIconDim = 15, |
| 28 | hostIconDimMin = 8, |
| 29 | hostIconDimMax = 15, |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 30 | labelPadding = 20, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 31 | remappedDeviceTypes = {}; |
| 32 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 33 | function createHostCollection(data, region) { |
| 34 | |
| 35 | var HostCollection = Collection.extend({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 36 | model: Model, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 37 | }); |
| 38 | |
| 39 | var hosts = []; |
| 40 | data.forEach(function (hostsLayer) { |
| 41 | hostsLayer.forEach(function (host) { |
| 42 | hosts.push(host); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | return new HostCollection(hosts); |
| 47 | } |
| 48 | |
| 49 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 50 | .factory('Topo2HostService', [ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 51 | 'Topo2Collection', 'Topo2NodeModel', 'Topo2ViewService', |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 52 | 'IconService', 'Topo2ZoomService', 'Topo2HostsPanelService', 'PrefsService', |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 53 | 'Topo2PrefsService', |
| 54 | function (_c_, NodeModel, _t2vs_, is, zs, t2hds, ps, t2ps) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 55 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 56 | Collection = _c_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 57 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 58 | Model = NodeModel.extend({ |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 59 | |
| 60 | nodeType: 'host', |
| 61 | events: { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 62 | 'click': 'onClick', |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 63 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 64 | initialize: function () { |
| 65 | this.super = this.constructor.__super__; |
| 66 | this.super.initialize.apply(this, arguments); |
| 67 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 68 | onChange: function () { |
| 69 | // Update class names when the model changes |
| 70 | if (this.el) { |
| 71 | this.el.attr('class', this.svgClassName()); |
| 72 | } |
| 73 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 74 | showDetails: function () { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 75 | t2hds.displayPanel(this); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 76 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 77 | icon: function () { |
| 78 | var type = this.get('type'); |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 79 | return remappedDeviceTypes[type] || type || 'm_endstation'; |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 80 | }, |
Steven Burrows | 22cb550 | 2018-02-06 11:28:33 +0000 | [diff] [blame] | 81 | title: function () { |
| 82 | var props = this.get('props'); |
| 83 | return props.name || this.get('ips')[0] || 'unknown'; |
| 84 | }, |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 85 | labelIndex: function () { |
| 86 | return t2ps.get('hlbls'); |
| 87 | }, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 88 | label: function () { |
Steven Burrows | cc6f6de | 2017-08-30 12:50:38 +0100 | [diff] [blame] | 89 | var props = this.get('props'), |
| 90 | id = this.get('ips')[0] || 'unknown', |
| 91 | friendlyName = props && props.name ? props.name : id, |
| 92 | labels = ['', friendlyName || id, id, this.get('id')], |
| 93 | nli = this.labelIndex(), |
| 94 | idx = (nli < labels.length) ? nli : 0; |
| 95 | |
| 96 | return labels[idx]; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 97 | }, |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 98 | updateLabel: function () { |
| 99 | var node = this.el, |
| 100 | label = this.trimLabel(this.label()), |
| 101 | labelWidth; |
| 102 | |
| 103 | node.select('text').text(label); |
| 104 | labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0; |
| 105 | |
| 106 | node.select('rect') |
| 107 | .transition() |
| 108 | .attr({ |
| 109 | width: labelWidth, |
| 110 | }); |
| 111 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 112 | setScale: function () { |
| 113 | |
Steven Burrows | 8909c5c | 2017-04-10 09:56:52 -0700 | [diff] [blame] | 114 | if (!this.el) return; |
| 115 | |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 116 | var dim = hostIconDim, |
| 117 | multipler = 1; |
| 118 | |
| 119 | if (dim * zs.scale() < hostIconDimMin) { |
| 120 | multipler = hostIconDimMin / (dim * zs.scale()); |
| 121 | } else if (dim * zs.scale() > hostIconDimMax) { |
| 122 | multipler = hostIconDimMax / (dim * zs.scale()); |
| 123 | } |
| 124 | |
| 125 | this.el.select('g').selectAll('*') |
| 126 | .style('transform', 'scale(' + multipler + ')'); |
| 127 | }, |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 128 | setVisibility: function () { |
| 129 | var visible = ps.getPrefs('topo2_prefs')['hosts']; |
| 130 | this.el.style('visibility', visible ? 'visible' : 'hidden'); |
| 131 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 132 | onEnter: function (el) { |
| 133 | var node = d3.select(el), |
| 134 | icon = this.icon(), |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 135 | textDy = 5, |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 136 | textDx = (hostIconDim * 2); |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 137 | |
| 138 | this.el = node; |
| 139 | |
| 140 | var g = node.append('g') |
| 141 | .attr('class', 'svgIcon hostIcon'); |
| 142 | |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 143 | // Add Label background to host |
| 144 | |
| 145 | var rect = g.append('rect').attr({ |
| 146 | width: 0, |
| 147 | height: hostIconDim * 2, |
| 148 | y: - hostIconDim, |
| 149 | }); |
| 150 | |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 151 | g.append('circle').attr('r', hostIconDim); |
Steven Burrows | e1ea27b | 2017-04-12 09:53:41 -0700 | [diff] [blame] | 152 | |
| 153 | var glyphSize = hostIconDim * 1.5; |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 154 | g.append('use').attr({ |
| 155 | 'xlink:href': '#' + icon, |
Steven Burrows | e1ea27b | 2017-04-12 09:53:41 -0700 | [diff] [blame] | 156 | width: glyphSize, |
| 157 | height: glyphSize, |
| 158 | x: -glyphSize / 2, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 159 | y: -glyphSize / 2, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 160 | }); |
| 161 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 162 | var labelText = this.label(); |
| 163 | |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 164 | g.append('text') |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 165 | .text(labelText) |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 166 | .attr('dy', textDy) |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 167 | .attr('dx', textDx) |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 168 | .attr('text-anchor', 'left'); |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 169 | |
| 170 | this.setScale(); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 171 | this.setUpEvents(); |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 172 | this.setVisibility(); |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 173 | |
| 174 | var labelWidth = !_.isEmpty(this.label()) ? this.computeLabelWidth(node) + (labelPadding * 2) : 0; |
| 175 | rect.attr({ width: labelWidth }); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 176 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 177 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 178 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 179 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 180 | createHostCollection: createHostCollection, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 181 | }; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 182 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 183 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 184 | |
| 185 | })(); |