blob: 7c96caddf36f6de85c7caa43a5bf315c9b366c96 [file] [log] [blame]
Steven Burrows1c5c8612016-10-05 13:45:13 -05001/*
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 Burrows68d6f952017-03-10 13:53:35 +000026 var rs, t2mcs, t2sls, t2bgs;
Steven Burrows1c5c8612016-10-05 13:45:13 -050027
28 // Internal state;
29 var nearDist = 15;
30
Simon Huntf27a9292017-05-04 17:36:26 -070031 function setElCoord(el, coord) {
32 el.fix(true);
33 el.x = el.px = coord[0];
34 el.y = el.py = coord[1];
35 return true;
36 }
37
Steven Burrows1c5c8612016-10-05 13:45:13 -050038 function positionNode(node, forUpdate) {
Steven Burrows68d6f952017-03-10 13:53:35 +000039
Steven Burrows1c5c8612016-10-05 13:45:13 -050040 var meta = node.get('metaUi'),
41 x = meta && meta.x,
42 y = meta && meta.y,
Simon Hunt2521d5f2017-03-20 18:17:28 -070043 hasMeta = x !== undefined && y !== undefined,
Steven Burrows1c5c8612016-10-05 13:45:13 -050044 dim = [800, 600],
45 xy;
46
Simon Hunt2521d5f2017-03-20 18:17:28 -070047 // If the node has metaUI data attached, it indicates that the user
48 // has dragged the node to a new position on the view; so we should
49 // respect that above any script-configured position...
Simon Huntf27a9292017-05-04 17:36:26 -070050 // (NOTE: This is slightly different to the "classic" topology code)
Steven Burrows68d6f952017-03-10 13:53:35 +000051
Simon Hunt2521d5f2017-03-20 18:17:28 -070052 if (hasMeta) {
53 node.fix(true);
54 node.px = node.x = x;
55 node.py = node.y = y;
56 return;
57 }
58
59 // Otherwise, use a precomputed location for peer regions, or
60 // LONG/LAT (or GRID) locations for regions/devices/hosts
61
62 if (node.nodeType === 'peer-region') {
Steven Burrows68d6f952017-03-10 13:53:35 +000063 if (t2bgs.getBackgroundType() === 'geo') {
Steven Burrowsa85a5d32017-04-10 09:46:27 -070064 setLongLat(node);
65 return true;
Steven Burrows68d6f952017-03-10 13:53:35 +000066 }
67
Simon Huntf27a9292017-05-04 17:36:26 -070068 // assumed to be grid
69 var loc = {
70 longOrX: -20,
71 latOrY: 10 * node.index()
72 };
Steven Burrows68d6f952017-03-10 13:53:35 +000073
Simon Huntf27a9292017-05-04 17:36:26 -070074 setElCoord(node, coordFromXY(loc));
Steven Burrows68d6f952017-03-10 13:53:35 +000075 return;
76 }
77
Steven Burrows1c5c8612016-10-05 13:45:13 -050078 // If the device contains explicit LONG/LAT data, use that to position
79 if (setLongLat(node)) {
80 // Indicate we want to update cached meta data...
81 return true;
82 }
83
Steven Burrows1c5c8612016-10-05 13:45:13 -050084 // if this is a node update (not a node add).. skip randomizer
85 if (forUpdate) {
86 return;
87 }
88
89 // Note: Placing incoming unpinned nodes at exactly the same point
90 // (center of the view) causes them to explode outwards when
91 // the force layout kicks in. So, we spread them out a bit
92 // initially, to provide a more serene layout convergence.
93 // Additionally, if the node is a host, we place it near
94 // the device it is connected to.
95
96 function rand() {
97 return {
98 x: rs.randDim(dim[0]),
99 y: rs.randDim(dim[1])
100 };
101 }
102
103 function near(node) {
104 return {
105 x: node.x + nearDist + rs.spread(nearDist),
106 y: node.y + nearDist + rs.spread(nearDist)
107 };
108 }
109
110 function getDevice(cp) {
111 return rand();
112 }
113
114 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
115
116 if (node.class === 'sub-region') {
117 xy = rand();
118 node.x = node.px = xy.x;
119 node.y = node.py = xy.y;
120 }
121 angular.extend(node, xy);
122 }
123
124 function setLongLat(el) {
Simon Huntf27a9292017-05-04 17:36:26 -0700125 var loc = el.get('location');
Steven Burrows1c5c8612016-10-05 13:45:13 -0500126
Simon Huntf27a9292017-05-04 17:36:26 -0700127 // bail if no location set
128 if (!loc || (loc.latOrY === 0 && loc.longOrX === 0)) {
129 return false;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500130 }
Simon Huntbc30e682017-02-15 18:39:23 -0800131
Simon Huntf27a9292017-05-04 17:36:26 -0700132 if (loc.locType === 'geo') {
133 return setElCoord(el, coordFromLngLat(loc));
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000134 }
Simon Huntf27a9292017-05-04 17:36:26 -0700135 if (loc.locType === 'grid') {
136 return setElCoord(el, coordFromXY(loc));
137 }
138
139 return false;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500140 }
141
142 function coordFromLngLat(loc) {
143 var p = t2mcs.projection();
Simon Huntf27a9292017-05-04 17:36:26 -0700144 return p ? p([loc.longOrX, loc.latOrY]) : [0, 0];
Steven Burrows1c5c8612016-10-05 13:45:13 -0500145 }
146
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000147 function coordFromXY(loc) {
Steven Burrowse8a455a2017-03-16 16:58:59 +0000148 var bgWidth = t2sls.getWidth() || 100,
149 bgHeight = t2sls.getHeight() || 100;
Steven Burrowsbd402842017-03-08 21:30:38 +0000150
Steven Burrowse8a455a2017-03-16 16:58:59 +0000151 var scale = 1000 / bgWidth,
152 yOffset = (1000 - (bgHeight * scale)) / 2;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000153
Steven Burrows11162542017-02-27 21:52:56 +0000154 // 1000 is a hardcoded HTML value of the SVG element (topo2.html)
Simon Huntf27a9292017-05-04 17:36:26 -0700155 var x = scale * loc.longOrX,
156 y = (scale * loc.latOrY) + yOffset;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000157
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000158 return [x, y];
159 }
160
Steven Burrows1c5c8612016-10-05 13:45:13 -0500161 angular.module('ovTopo2')
162 .factory('Topo2NodePositionService',
Simon Hunt2521d5f2017-03-20 18:17:28 -0700163 ['RandomService', 'Topo2MapConfigService',
164 'Topo2SpriteLayerService', 'Topo2BackgroundService',
Steven Burrows68d6f952017-03-10 13:53:35 +0000165 function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500166
167 rs = _rs_;
168 t2mcs = _t2mcs_;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000169 t2sls = _t2sls_;
Steven Burrows68d6f952017-03-10 13:53:35 +0000170 t2bgs = _t2bgs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500171
172 return {
173 positionNode: positionNode,
174 setLongLat: setLongLat
175 };
176 }
177 ]);
178})();