blob: d41acfe33d31b7e26767fd02451fe62b5d185d9a [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,
36 dim = [800, 600],
37 xy;
38
Steven Burrows68d6f952017-03-10 13:53:35 +000039 if (node.nodeType === 'peer-region') {
40
41 var coord = [0, 0],
42 loc = {};
43
44 if (t2bgs.getBackgroundType() === 'geo') {
45 // TODO: Set coords for geo (lat/long)
46 } else {
47 loc.gridX = -20;
48 loc.gridY = 10 * node.index();
49 coord = coordFromXY(loc);
50 }
51
52 node.px = node.x = coord[0];
53 node.py = node.y = coord[1];
54
55 node.fix(true);
56 return;
57 }
58
Steven Burrows1c5c8612016-10-05 13:45:13 -050059 // If the device contains explicit LONG/LAT data, use that to position
60 if (setLongLat(node)) {
61 // Indicate we want to update cached meta data...
62 return true;
63 }
64
65 // else if we have [x,y] cached in meta data, use that...
66 if (x !== undefined && y !== undefined) {
Steven Burrowsb11a8b82017-03-10 16:00:31 +000067 node.fix(true);
Steven Burrows1c5c8612016-10-05 13:45:13 -050068 node.px = node.x = x;
69 node.py = node.y = y;
70 return;
71 }
72
73 // if this is a node update (not a node add).. skip randomizer
74 if (forUpdate) {
75 return;
76 }
77
78 // Note: Placing incoming unpinned nodes at exactly the same point
79 // (center of the view) causes them to explode outwards when
80 // the force layout kicks in. So, we spread them out a bit
81 // initially, to provide a more serene layout convergence.
82 // Additionally, if the node is a host, we place it near
83 // the device it is connected to.
84
85 function rand() {
86 return {
87 x: rs.randDim(dim[0]),
88 y: rs.randDim(dim[1])
89 };
90 }
91
92 function near(node) {
93 return {
94 x: node.x + nearDist + rs.spread(nearDist),
95 y: node.y + nearDist + rs.spread(nearDist)
96 };
97 }
98
99 function getDevice(cp) {
100 return rand();
101 }
102
103 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
104
105 if (node.class === 'sub-region') {
106 xy = rand();
107 node.x = node.px = xy.x;
108 node.y = node.py = xy.y;
109 }
110 angular.extend(node, xy);
111 }
112
113 function setLongLat(el) {
114 var loc = el.get('location'),
115 coord;
116
Simon Huntbc30e682017-02-15 18:39:23 -0800117 if (loc && loc.type === 'geo') {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500118
119 if (loc.lat === 0 && loc.lng === 0) {
120 return false;
121 }
122
123 coord = coordFromLngLat(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000124 el.fix(true);
Steven Burrows1c5c8612016-10-05 13:45:13 -0500125 el.x = el.px = coord[0];
126 el.y = el.py = coord[1];
127
128 return true;
129 }
Simon Huntbc30e682017-02-15 18:39:23 -0800130
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000131 if (loc && loc.type === 'grid') {
132
133 if (loc.gridX === 0 && loc.gridY === 0) {
134 return false;
135 }
136
137 coord = coordFromXY(loc);
Steven Burrowsb11a8b82017-03-10 16:00:31 +0000138 el.fix(true);
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000139 el.x = el.px = coord[0];
140 el.y = el.py = coord[1];
141
142 return true;
143 }
Steven Burrows1c5c8612016-10-05 13:45:13 -0500144 }
145
146 function coordFromLngLat(loc) {
147 var p = t2mcs.projection();
148 return p ? p([loc.lng, loc.lat]) : [0, 0];
149 }
150
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000151 function coordFromXY(loc) {
Steven Burrowsbd402842017-03-08 21:30:38 +0000152
Steven Burrowse8a455a2017-03-16 16:58:59 +0000153 var bgWidth = t2sls.getWidth() || 100,
154 bgHeight = t2sls.getHeight() || 100;
Steven Burrowsbd402842017-03-08 21:30:38 +0000155
Steven Burrowse8a455a2017-03-16 16:58:59 +0000156 var scale = 1000 / bgWidth,
157 yOffset = (1000 - (bgHeight * scale)) / 2;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000158
Steven Burrows11162542017-02-27 21:52:56 +0000159 // 1000 is a hardcoded HTML value of the SVG element (topo2.html)
160 var x = scale * loc.gridX,
161 y = (scale * loc.gridY) + yOffset;
Steven Burrowsb43c1a92017-03-07 17:13:28 +0000162
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000163 return [x, y];
164 }
165
Steven Burrows1c5c8612016-10-05 13:45:13 -0500166 angular.module('ovTopo2')
167 .factory('Topo2NodePositionService',
Steven Burrows68d6f952017-03-10 13:53:35 +0000168 ['RandomService', 'Topo2MapConfigService', 'Topo2SpriteLayerService', 'Topo2BackgroundService',
169 function (_rs_, _t2mcs_, _t2sls_, _t2bgs_) {
Steven Burrows1c5c8612016-10-05 13:45:13 -0500170
171 rs = _rs_;
172 t2mcs = _t2mcs_;
Steven Burrows3db5ddb2017-02-17 15:21:20 +0000173 t2sls = _t2sls_;
Steven Burrows68d6f952017-03-10 13:53:35 +0000174 t2bgs = _t2bgs_;
Steven Burrows1c5c8612016-10-05 13:45:13 -0500175
176 return {
177 positionNode: positionNode,
178 setLongLat: setLongLat
179 };
180 }
181 ]);
182})();