Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [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 Node Position Module. |
| 19 | Module that helps position nodes in the topology |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // Injected vars |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 26 | var rs, t2mcs, t2sls, t2bgs; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 27 | |
| 28 | // Internal state; |
| 29 | var nearDist = 15; |
| 30 | |
| 31 | function positionNode(node, forUpdate) { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 32 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 33 | var meta = node.get('metaUi'), |
| 34 | x = meta && meta.x, |
| 35 | y = meta && meta.y, |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 36 | hasMeta = x !== undefined && y !== undefined, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 37 | dim = [800, 600], |
| 38 | xy; |
| 39 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 40 | // If the node has metaUI data attached, it indicates that the user |
| 41 | // has dragged the node to a new position on the view; so we should |
| 42 | // respect that above any script-configured position... |
| 43 | // (NOTE: This is a slightly different to the original topology code) |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 44 | |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 45 | if (hasMeta) { |
| 46 | node.fix(true); |
| 47 | node.px = node.x = x; |
| 48 | node.py = node.y = y; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // Otherwise, use a precomputed location for peer regions, or |
| 53 | // LONG/LAT (or GRID) locations for regions/devices/hosts |
| 54 | |
| 55 | if (node.nodeType === 'peer-region') { |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 56 | var coord = [0, 0], |
| 57 | loc = {}; |
| 58 | |
| 59 | if (t2bgs.getBackgroundType() === 'geo') { |
Steven Burrows | 4a1b553 | 2017-04-28 11:08:07 -0400 | [diff] [blame^] | 60 | |
| 61 | var loc = node.get('location'), |
| 62 | type = loc.locType || loc.type; |
| 63 | |
| 64 | node.set({ location: { |
| 65 | type: type.toLowerCase(), |
| 66 | lat: loc.latOrY || loc.lat, |
| 67 | lng: loc.longOrX || loc.lng |
| 68 | }}); |
| 69 | |
Steven Burrows | a85a5d3 | 2017-04-10 09:46:27 -0700 | [diff] [blame] | 70 | setLongLat(node); |
Steven Burrows | 4a1b553 | 2017-04-28 11:08:07 -0400 | [diff] [blame^] | 71 | |
Steven Burrows | a85a5d3 | 2017-04-10 09:46:27 -0700 | [diff] [blame] | 72 | return true; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 73 | } else { |
| 74 | loc.gridX = -20; |
| 75 | loc.gridY = 10 * node.index(); |
| 76 | coord = coordFromXY(loc); |
| 77 | } |
| 78 | |
| 79 | node.px = node.x = coord[0]; |
| 80 | node.py = node.y = coord[1]; |
| 81 | |
| 82 | node.fix(true); |
| 83 | return; |
| 84 | } |
| 85 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 86 | // If the device contains explicit LONG/LAT data, use that to position |
| 87 | if (setLongLat(node)) { |
| 88 | // Indicate we want to update cached meta data... |
| 89 | return true; |
| 90 | } |
| 91 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 92 | // if this is a node update (not a node add).. skip randomizer |
| 93 | if (forUpdate) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 98 | // (center of the view) causes them to explode outwards when |
| 99 | // the force layout kicks in. So, we spread them out a bit |
| 100 | // initially, to provide a more serene layout convergence. |
| 101 | // Additionally, if the node is a host, we place it near |
| 102 | // the device it is connected to. |
| 103 | |
| 104 | function rand() { |
| 105 | return { |
| 106 | x: rs.randDim(dim[0]), |
| 107 | y: rs.randDim(dim[1]) |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | function near(node) { |
| 112 | return { |
| 113 | x: node.x + nearDist + rs.spread(nearDist), |
| 114 | y: node.y + nearDist + rs.spread(nearDist) |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | function getDevice(cp) { |
| 119 | return rand(); |
| 120 | } |
| 121 | |
| 122 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 123 | |
| 124 | if (node.class === 'sub-region') { |
| 125 | xy = rand(); |
| 126 | node.x = node.px = xy.x; |
| 127 | node.y = node.py = xy.y; |
| 128 | } |
| 129 | angular.extend(node, xy); |
| 130 | } |
| 131 | |
| 132 | function setLongLat(el) { |
| 133 | var loc = el.get('location'), |
| 134 | coord; |
| 135 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 136 | if (loc && loc.type === 'geo') { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 137 | |
| 138 | if (loc.lat === 0 && loc.lng === 0) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | coord = coordFromLngLat(loc); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 143 | el.fix(true); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 144 | el.x = el.px = coord[0]; |
| 145 | el.y = el.py = coord[1]; |
| 146 | |
| 147 | return true; |
| 148 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 149 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 150 | if (loc && loc.type === 'grid') { |
| 151 | |
| 152 | if (loc.gridX === 0 && loc.gridY === 0) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | coord = coordFromXY(loc); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 157 | el.fix(true); |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 158 | el.x = el.px = coord[0]; |
| 159 | el.y = el.py = coord[1]; |
| 160 | |
| 161 | return true; |
| 162 | } |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | function coordFromLngLat(loc) { |
| 166 | var p = t2mcs.projection(); |
| 167 | return p ? p([loc.lng, loc.lat]) : [0, 0]; |
| 168 | } |
| 169 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 170 | function coordFromXY(loc) { |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 171 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 172 | var bgWidth = t2sls.getWidth() || 100, |
| 173 | bgHeight = t2sls.getHeight() || 100; |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 174 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 175 | var scale = 1000 / bgWidth, |
| 176 | yOffset = (1000 - (bgHeight * scale)) / 2; |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 177 | |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 178 | // 1000 is a hardcoded HTML value of the SVG element (topo2.html) |
| 179 | var x = scale * loc.gridX, |
| 180 | y = (scale * loc.gridY) + yOffset; |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 181 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 182 | return [x, y]; |
| 183 | } |
| 184 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 185 | angular.module('ovTopo2') |
| 186 | .factory('Topo2NodePositionService', |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame] | 187 | ['RandomService', 'Topo2MapConfigService', |
| 188 | 'Topo2SpriteLayerService', 'Topo2BackgroundService', |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 189 | function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 190 | |
| 191 | rs = _rs_; |
| 192 | t2mcs = _t2mcs_; |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 193 | t2sls = _t2sls_; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 194 | t2bgs = _t2bgs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 195 | |
| 196 | return { |
| 197 | positionNode: positionNode, |
| 198 | setLongLat: setLongLat |
| 199 | }; |
| 200 | } |
| 201 | ]); |
| 202 | })(); |