blob: 54a27481ecdff46244172f7c2cd470a2c715ed8f [file] [log] [blame]
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001/*
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 Layout Module.
19 Module that contains the d3.force.layout logic
20 */
21
22(function () {
23 'use strict';
24
25 var randomService;
26 var fn;
27
28 //internal state;
29 var defaultLinkType = 'direct',
30 nearDist = 15;
31
32 function positionNode(node, forUpdate) {
33
34 var meta = node.metaUi,
35 x = meta && meta.x,
36 y = meta && meta.y,
37 dim = [800, 600],
38 xy;
39
40 // if the device contains explicit LONG/LAT data, use that to position
41 if (setLongLat(node)) {
42 //indicate we want to update cached meta data...
43 return true;
44 }
45
46 // else if we have [x,y] cached in meta data, use that...
47 if (x !== undefined && y !== undefined) {
48 node.fixed = true;
49 node.px = node.x = x;
50 node.py = node.y = y;
51 return;
52 }
53
54 // if this is a node update (not a node add).. skip randomizer
55 if (forUpdate) {
56 return;
57 }
58
59 // Note: Placing incoming unpinned nodes at exactly the same point
60 // (center of the view) causes them to explode outwards when
61 // the force layout kicks in. So, we spread them out a bit
62 // initially, to provide a more serene layout convergence.
63 // Additionally, if the node is a host, we place it near
64 // the device it is connected to.
65
66 function rand() {
67 return {
68 x: randomService.randDim(dim[0]),
69 y: randomService.randDim(dim[1])
70 };
71 }
72
73 function near(node) {
74 return {
75 x: node.x + nearDist + randomService.spread(nearDist),
76 y: node.y + nearDist + randomService.spread(nearDist)
77 };
78 }
79
80 function getDevice(cp) {
81 // console.log(cp);
82 // var d = lu[cp.device];
83 // return d || rand();
84 return rand();
85 }
86
87 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
88 angular.extend(node, xy);
89 }
90
91 function setLongLat(node) {
92 var loc = node.location,
93 coord;
94
95 if (loc && loc.type === 'lnglat') {
96 coord = [0, 0];
97 node.fixed = true;
98 node.px = node.x = coord[0];
99 node.py = node.y = coord[1];
100 return true;
101 }
102 }
103
104 angular.module('ovTopo2')
105 .factory('Topo2NodeModel',
106 ['Topo2Model', 'FnService', 'RandomService',
107 function (Model, _fn_, _RandomService_) {
108
109 randomService = _RandomService_;
110 fn = _fn_;
111
112 return Model.extend({
113 initialize: function () {
114 this.node = this.createNode();
115 },
116 svgClassName: function () {
117 return fn.classNames('node', this.nodeType, this.get('type'), {
118 online: this.get('online')
119 });
120 },
121 createNode: function () {
122
123 var node = angular.extend({}, this.attributes);
124
125 // Augment as needed...
126 node.class = this.nodeType;
127 node.svgClass = this.svgClassName();
128 positionNode(node);
129 return node;
130 }
131 });
132 }]
133 );
134})();