blob: 763718f831df2c2ba62ddc0033d8c606e79f6404 [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') {
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 Burrows1c5c8612016-10-05 13:45:13 -050074 // 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 Burrows1c5c8612016-10-05 13:45:13 -050080 // 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 Huntbc30e682017-02-15 18:39:23 -0800124 if (loc && loc.type === 'geo') {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500125
126 if (loc.lat === 0 && loc.lng === 0) {
127 return false;
128 }
129
130 coord = coordFromLngLat(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000131 el.fix(true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500132 el.x = el.px = coord[0];
133 el.y = el.py = coord[1];
134
135 return true;
136 }
Simon Huntbc30e682017-02-15 18:39:23 -0800137
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000138 if (loc && loc.type === 'grid') {
139
140 if (loc.gridX === 0 && loc.gridY === 0) {
141 return false;
142 }
143
144 coord = coordFromXY(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000145 el.fix(true);
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000146 el.x = el.px = coord[0];
147 el.y = el.py = coord[1];
148
149 return true;
150 }
Steven Burrows1c5c8612016-10-05 13:45:13 -0500151 }
152
153 function coordFromLngLat(loc) {
154 var p = t2mcs.projection();
155 return p ? p([loc.lng, loc.lat]) : [0, 0];
156 }
157
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000158 function coordFromXY(loc) {
Steven Burrowsbd402842017-03-08 21:30:38 +0000159
Steven Burrowse8a455a2017-03-16 16:58:59 +0000160 var bgWidth = t2sls.getWidth() || 100,
161 bgHeight = t2sls.getHeight() || 100;
Steven Burrowsbd402842017-03-08 21:30:38 +0000162
Steven Burrowse8a455a2017-03-16 16:58:59 +0000163 var scale = 1000 / bgWidth,
164 yOffset = (1000 - (bgHeight * scale)) / 2;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000165
Steven Burrows11162542017-02-27 21:52:56 +0000166 // 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 Burrowsb43c1a92017-03-07 17:13:28 +0000169
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000170 return [x, y];
171 }
172
Steven Burrows1c5c8612016-10-05 13:45:13 -0500173 angular.module('ovTopo2')
174 .factory('Topo2NodePositionService',
Simon Hunt2521d5f2017-03-20 18:17:28 -0700175 ['RandomService', 'Topo2MapConfigService',
176 'Topo2SpriteLayerService', 'Topo2BackgroundService',
Steven Burrows68d6f952017-03-10 13:53:35 +0000177 function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500178
179 rs = _rs_;
180 t2mcs = _t2mcs_;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000181 t2sls = _t2sls_;
Steven Burrows68d6f952017-03-10 13:53:35 +0000182 t2bgs = _t2bgs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500183
184 return {
185 positionNode: positionNode,
186 setLongLat: setLongLat
187 };
188 }
189 ]);
190})();