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') { |
| 60 | // TODO: Set coords for geo (lat/long) |
| 61 | } else { |
| 62 | loc.gridX = -20; |
| 63 | loc.gridY = 10 * node.index(); |
| 64 | coord = coordFromXY(loc); |
| 65 | } |
| 66 | |
| 67 | node.px = node.x = coord[0]; |
| 68 | node.py = node.y = coord[1]; |
| 69 | |
| 70 | node.fix(true); |
| 71 | return; |
| 72 | } |
| 73 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 74 | // If the device contains explicit LONG/LAT data, use that to position |
| 75 | if (setLongLat(node)) { |
| 76 | // Indicate we want to update cached meta data... |
| 77 | return true; |
| 78 | } |
| 79 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 80 | // if this is a node update (not a node add).. skip randomizer |
| 81 | if (forUpdate) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 86 | // (center of the view) causes them to explode outwards when |
| 87 | // the force layout kicks in. So, we spread them out a bit |
| 88 | // initially, to provide a more serene layout convergence. |
| 89 | // Additionally, if the node is a host, we place it near |
| 90 | // the device it is connected to. |
| 91 | |
| 92 | function rand() { |
| 93 | return { |
| 94 | x: rs.randDim(dim[0]), |
| 95 | y: rs.randDim(dim[1]) |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | function near(node) { |
| 100 | return { |
| 101 | x: node.x + nearDist + rs.spread(nearDist), |
| 102 | y: node.y + nearDist + rs.spread(nearDist) |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | function getDevice(cp) { |
| 107 | return rand(); |
| 108 | } |
| 109 | |
| 110 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 111 | |
| 112 | if (node.class === 'sub-region') { |
| 113 | xy = rand(); |
| 114 | node.x = node.px = xy.x; |
| 115 | node.y = node.py = xy.y; |
| 116 | } |
| 117 | angular.extend(node, xy); |
| 118 | } |
| 119 | |
| 120 | function setLongLat(el) { |
| 121 | var loc = el.get('location'), |
| 122 | coord; |
| 123 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 124 | if (loc && loc.type === 'geo') { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 125 | |
| 126 | if (loc.lat === 0 && loc.lng === 0) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | coord = coordFromLngLat(loc); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 131 | el.fix(true); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 132 | el.x = el.px = coord[0]; |
| 133 | el.y = el.py = coord[1]; |
| 134 | |
| 135 | return true; |
| 136 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 137 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 138 | if (loc && loc.type === 'grid') { |
| 139 | |
| 140 | if (loc.gridX === 0 && loc.gridY === 0) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | coord = coordFromXY(loc); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 145 | el.fix(true); |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 146 | el.x = el.px = coord[0]; |
| 147 | el.y = el.py = coord[1]; |
| 148 | |
| 149 | return true; |
| 150 | } |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | function coordFromLngLat(loc) { |
| 154 | var p = t2mcs.projection(); |
| 155 | return p ? p([loc.lng, loc.lat]) : [0, 0]; |
| 156 | } |
| 157 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 158 | function coordFromXY(loc) { |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 159 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 160 | var bgWidth = t2sls.getWidth() || 100, |
| 161 | bgHeight = t2sls.getHeight() || 100; |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 162 | |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 163 | var scale = 1000 / bgWidth, |
| 164 | yOffset = (1000 - (bgHeight * scale)) / 2; |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 165 | |
Steven Burrows | 1116254 | 2017-02-27 21:52:56 +0000 | [diff] [blame] | 166 | // 1000 is a hardcoded HTML value of the SVG element (topo2.html) |
| 167 | var x = scale * loc.gridX, |
| 168 | y = (scale * loc.gridY) + yOffset; |
Steven Burrows | b43c1a9 | 2017-03-07 17:13:28 +0000 | [diff] [blame] | 169 | |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 170 | return [x, y]; |
| 171 | } |
| 172 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 173 | angular.module('ovTopo2') |
| 174 | .factory('Topo2NodePositionService', |
Simon Hunt | 2521d5f | 2017-03-20 18:17:28 -0700 | [diff] [blame^] | 175 | ['RandomService', 'Topo2MapConfigService', |
| 176 | 'Topo2SpriteLayerService', 'Topo2BackgroundService', |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 177 | function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 178 | |
| 179 | rs = _rs_; |
| 180 | t2mcs = _t2mcs_; |
Steven Burrows | 3db5ddb | 2017-02-17 15:21:20 +0000 | [diff] [blame] | 181 | t2sls = _t2sls_; |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 182 | t2bgs = _t2bgs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 183 | |
| 184 | return { |
| 185 | positionNode: positionNode, |
| 186 | setLongLat: setLongLat |
| 187 | }; |
| 188 | } |
| 189 | ]); |
| 190 | })(); |