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 |
| 26 | var rs, t2mcs; |
| 27 | |
| 28 | // Internal state; |
| 29 | var nearDist = 15; |
| 30 | |
| 31 | function positionNode(node, forUpdate) { |
| 32 | var meta = node.get('metaUi'), |
| 33 | x = meta && meta.x, |
| 34 | y = meta && meta.y, |
| 35 | dim = [800, 600], |
| 36 | xy; |
| 37 | |
| 38 | // If the device contains explicit LONG/LAT data, use that to position |
| 39 | if (setLongLat(node)) { |
| 40 | // Indicate we want to update cached meta data... |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // else if we have [x,y] cached in meta data, use that... |
| 45 | if (x !== undefined && y !== undefined) { |
| 46 | node.fixed = true; |
| 47 | node.px = node.x = x; |
| 48 | node.py = node.y = y; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // if this is a node update (not a node add).. skip randomizer |
| 53 | if (forUpdate) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 58 | // (center of the view) causes them to explode outwards when |
| 59 | // the force layout kicks in. So, we spread them out a bit |
| 60 | // initially, to provide a more serene layout convergence. |
| 61 | // Additionally, if the node is a host, we place it near |
| 62 | // the device it is connected to. |
| 63 | |
| 64 | function rand() { |
| 65 | return { |
| 66 | x: rs.randDim(dim[0]), |
| 67 | y: rs.randDim(dim[1]) |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | function near(node) { |
| 72 | return { |
| 73 | x: node.x + nearDist + rs.spread(nearDist), |
| 74 | y: node.y + nearDist + rs.spread(nearDist) |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | function getDevice(cp) { |
| 79 | return rand(); |
| 80 | } |
| 81 | |
| 82 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 83 | |
| 84 | if (node.class === 'sub-region') { |
| 85 | xy = rand(); |
| 86 | node.x = node.px = xy.x; |
| 87 | node.y = node.py = xy.y; |
| 88 | } |
| 89 | angular.extend(node, xy); |
| 90 | } |
| 91 | |
| 92 | function setLongLat(el) { |
| 93 | var loc = el.get('location'), |
| 94 | coord; |
| 95 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame^] | 96 | if (loc && loc.type === 'geo') { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 97 | |
| 98 | if (loc.lat === 0 && loc.lng === 0) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | coord = coordFromLngLat(loc); |
| 103 | el.fixed = true; |
| 104 | el.x = el.px = coord[0]; |
| 105 | el.y = el.py = coord[1]; |
| 106 | |
| 107 | return true; |
| 108 | } |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame^] | 109 | |
| 110 | // TODO: handle case where loc.type === 'grid' |
| 111 | // implying loc.gridX and loc.gridY hold values |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | function coordFromLngLat(loc) { |
| 115 | var p = t2mcs.projection(); |
| 116 | return p ? p([loc.lng, loc.lat]) : [0, 0]; |
| 117 | } |
| 118 | |
| 119 | angular.module('ovTopo2') |
| 120 | .factory('Topo2NodePositionService', |
Simon Hunt | 1bee529 | 2016-10-14 11:02:33 -0700 | [diff] [blame] | 121 | ['RandomService', 'Topo2MapConfigService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 122 | function (_rs_, _t2mcs_) { |
| 123 | |
| 124 | rs = _rs_; |
| 125 | t2mcs = _t2mcs_; |
| 126 | |
| 127 | return { |
| 128 | positionNode: positionNode, |
| 129 | setLongLat: setLongLat |
| 130 | }; |
| 131 | } |
| 132 | ]); |
| 133 | })(); |