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 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 | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 30 | remappedDeviceTypes = {}; |
| 31 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 32 | function createHostCollection(data, region) { |
| 33 | |
| 34 | var HostCollection = Collection.extend({ |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 35 | model: Model |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 36 | }); |
| 37 | |
| 38 | var hosts = []; |
| 39 | data.forEach(function (hostsLayer) { |
| 40 | hostsLayer.forEach(function (host) { |
| 41 | hosts.push(host); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | return new HostCollection(hosts); |
| 46 | } |
| 47 | |
| 48 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 49 | .factory('Topo2HostService', [ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 50 | 'Topo2Collection', 'Topo2NodeModel', 'Topo2ViewService', |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 51 | 'IconService', 'Topo2ZoomService', 'Topo2HostsPanelService', 'PrefsService', |
| 52 | function (_c_, NodeModel, _t2vs_, is, zs, t2hds, ps) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 53 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 54 | Collection = _c_; |
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 | Model = NodeModel.extend({ |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 57 | |
| 58 | nodeType: 'host', |
| 59 | events: { |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 60 | 'click': 'onClick' |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 61 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 62 | initialize: function () { |
| 63 | this.super = this.constructor.__super__; |
| 64 | this.super.initialize.apply(this, arguments); |
| 65 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 66 | onChange: function () { |
| 67 | // Update class names when the model changes |
| 68 | if (this.el) { |
| 69 | this.el.attr('class', this.svgClassName()); |
| 70 | } |
| 71 | }, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 72 | showDetails: function() { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 73 | t2hds.displayPanel(this); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 74 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 75 | icon: function () { |
| 76 | var type = this.get('type'); |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 77 | return remappedDeviceTypes[type] || type || 'm_endstation'; |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 78 | }, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 79 | label: function () { |
Steven Burrows | 474a0fd | 2017-04-12 09:35:55 -0700 | [diff] [blame] | 80 | return this.get('ips')[0] || 'unknown'; |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 81 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 82 | setScale: function () { |
| 83 | |
Steven Burrows | 8909c5c | 2017-04-10 09:56:52 -0700 | [diff] [blame] | 84 | if (!this.el) return; |
| 85 | |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 86 | var dim = hostIconDim, |
| 87 | multipler = 1; |
| 88 | |
| 89 | if (dim * zs.scale() < hostIconDimMin) { |
| 90 | multipler = hostIconDimMin / (dim * zs.scale()); |
| 91 | } else if (dim * zs.scale() > hostIconDimMax) { |
| 92 | multipler = hostIconDimMax / (dim * zs.scale()); |
| 93 | } |
| 94 | |
| 95 | this.el.select('g').selectAll('*') |
| 96 | .style('transform', 'scale(' + multipler + ')'); |
| 97 | }, |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 98 | setVisibility: function () { |
| 99 | var visible = ps.getPrefs('topo2_prefs')['hosts']; |
| 100 | this.el.style('visibility', visible ? 'visible' : 'hidden'); |
| 101 | }, |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 102 | onEnter: function (el) { |
| 103 | var node = d3.select(el), |
| 104 | icon = this.icon(), |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 105 | textDy = 5, |
| 106 | textDx = (hostIconDim * 2) + 20; |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 107 | |
| 108 | this.el = node; |
| 109 | |
| 110 | var g = node.append('g') |
| 111 | .attr('class', 'svgIcon hostIcon'); |
| 112 | |
| 113 | g.append('circle').attr('r', hostIconDim); |
Steven Burrows | e1ea27b | 2017-04-12 09:53:41 -0700 | [diff] [blame] | 114 | |
| 115 | var glyphSize = hostIconDim * 1.5; |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 116 | g.append('use').attr({ |
| 117 | 'xlink:href': '#' + icon, |
Steven Burrows | e1ea27b | 2017-04-12 09:53:41 -0700 | [diff] [blame] | 118 | width: glyphSize, |
| 119 | height: glyphSize, |
| 120 | x: -glyphSize / 2, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 121 | y: -glyphSize / 2 |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 122 | }); |
| 123 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 124 | var labelText = this.label(); |
| 125 | |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 126 | g.append('text') |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 127 | .text(labelText) |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 128 | .attr('dy', textDy) |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame] | 129 | .attr('dx', textDx) |
Steven Burrows | beef935 | 2016-10-21 14:09:50 -0500 | [diff] [blame] | 130 | .attr('text-anchor', 'middle'); |
| 131 | |
| 132 | this.setScale(); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 133 | this.setUpEvents(); |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 134 | this.setVisibility(); |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 135 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 136 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 137 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 138 | return { |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 139 | createHostCollection: createHostCollection |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 140 | }; |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 141 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 142 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 143 | |
| 144 | })(); |