blob: 86d1faa55cd193d9f2aa36b64e75bb62b11b3df0 [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
31 function positionNode(node, forUpdate) {
Steven Burrows68d6f952017-03-10 13:53:35 +000032
Steven Burrows1c5c8612016-10-05 13:45:13 -050033 var meta = node.get('metaUi'),
34 x = meta && meta.x,
35 y = meta && meta.y,
Simon Hunt2521d5f2017-03-20 18:17:28 -070036 hasMeta = x !== undefined && y !== undefined,
Steven Burrows1c5c8612016-10-05 13:45:13 -050037 dim = [800, 600],
38 xy;
39
Simon Hunt2521d5f2017-03-20 18:17:28 -070040 // 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 Burrows68d6f952017-03-10 13:53:35 +000044
Simon Hunt2521d5f2017-03-20 18:17:28 -070045 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 Burrows68d6f952017-03-10 13:53:35 +000056 var coord = [0, 0],
57 loc = {};
58
59 if (t2bgs.getBackgroundType() === 'geo') {
Steven Burrowsa85a5d32017-04-10 09:46:27 -070060 setLongLat(node);
61 return true;
Steven Burrows68d6f952017-03-10 13:53:35 +000062 } else {
63 loc.gridX = -20;
64 loc.gridY = 10 * node.index();
65 coord = coordFromXY(loc);
66 }
67
68 node.px = node.x = coord[0];
69 node.py = node.y = coord[1];
70
71 node.fix(true);
72 return;
73 }
74
Steven Burrows1c5c8612016-10-05 13:45:13 -050075 // If the device contains explicit LONG/LAT data, use that to position
76 if (setLongLat(node)) {
77 // Indicate we want to update cached meta data...
78 return true;
79 }
80
Steven Burrows1c5c8612016-10-05 13:45:13 -050081 // if this is a node update (not a node add).. skip randomizer
82 if (forUpdate) {
83 return;
84 }
85
86 // Note: Placing incoming unpinned nodes at exactly the same point
87 // (center of the view) causes them to explode outwards when
88 // the force layout kicks in. So, we spread them out a bit
89 // initially, to provide a more serene layout convergence.
90 // Additionally, if the node is a host, we place it near
91 // the device it is connected to.
92
93 function rand() {
94 return {
95 x: rs.randDim(dim[0]),
96 y: rs.randDim(dim[1])
97 };
98 }
99
100 function near(node) {
101 return {
102 x: node.x + nearDist + rs.spread(nearDist),
103 y: node.y + nearDist + rs.spread(nearDist)
104 };
105 }
106
107 function getDevice(cp) {
108 return rand();
109 }
110
111 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
112
113 if (node.class === 'sub-region') {
114 xy = rand();
115 node.x = node.px = xy.x;
116 node.y = node.py = xy.y;
117 }
118 angular.extend(node, xy);
119 }
120
121 function setLongLat(el) {
122 var loc = el.get('location'),
123 coord;
124
Simon Huntbc30e682017-02-15 18:39:23 -0800125 if (loc && loc.type === 'geo') {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500126
127 if (loc.lat === 0 && loc.lng === 0) {
128 return false;
129 }
130
131 coord = coordFromLngLat(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000132 el.fix(true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500133 el.x = el.px = coord[0];
134 el.y = el.py = coord[1];
135
136 return true;
137 }
Simon Huntbc30e682017-02-15 18:39:23 -0800138
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000139 if (loc && loc.type === 'grid') {
140
141 if (loc.gridX === 0 && loc.gridY === 0) {
142 return false;
143 }
144
145 coord = coordFromXY(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000146 el.fix(true);
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000147 el.x = el.px = coord[0];
148 el.y = el.py = coord[1];
149
150 return true;
151 }
Steven Burrows1c5c8612016-10-05 13:45:13 -0500152 }
153
154 function coordFromLngLat(loc) {
155 var p = t2mcs.projection();
156 return p ? p([loc.lng, loc.lat]) : [0, 0];
157 }
158
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000159 function coordFromXY(loc) {
Steven Burrowsbd402842017-03-08 21:30:38 +0000160
Steven Burrowse8a455a2017-03-16 16:58:59 +0000161 var bgWidth = t2sls.getWidth() || 100,
162 bgHeight = t2sls.getHeight() || 100;
Steven Burrowsbd402842017-03-08 21:30:38 +0000163
Steven Burrowse8a455a2017-03-16 16:58:59 +0000164 var scale = 1000 / bgWidth,
165 yOffset = (1000 - (bgHeight * scale)) / 2;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000166
Steven Burrows11162542017-02-27 21:52:56 +0000167 // 1000 is a hardcoded HTML value of the SVG element (topo2.html)
168 var x = scale * loc.gridX,
169 y = (scale * loc.gridY) + yOffset;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000170
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000171 return [x, y];
172 }
173
Steven Burrows1c5c8612016-10-05 13:45:13 -0500174 angular.module('ovTopo2')
175 .factory('Topo2NodePositionService',
Simon Hunt2521d5f2017-03-20 18:17:28 -0700176 ['RandomService', 'Topo2MapConfigService',
177 'Topo2SpriteLayerService', 'Topo2BackgroundService',
Steven Burrows68d6f952017-03-10 13:53:35 +0000178 function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500179
180 rs = _rs_;
181 t2mcs = _t2mcs_;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000182 t2sls = _t2sls_;
Steven Burrows68d6f952017-03-10 13:53:35 +0000183 t2bgs = _t2bgs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500184
185 return {
186 positionNode: positionNode,
187 setLongLat: setLongLat
188 };
189 }
190 ]);
191})();