blob: 87033446305d86a42ae0a734a77a98aa4d2045d1 [file] [log] [blame]
Matteo Scandolo219b1a72016-02-09 11:19:22 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
Matteo Scandolo9fe01af2016-02-09 16:01:49 -08005 .service('LogicTopologyHelper', function($window, $log, lodash, serviceTopologyConfig, NodeDrawer){
Matteo Scandoloaf286372016-02-09 14:46:14 -08006
7 var diagonal, nodes, links, i = 0, svgWidth, svgHeight, layout;
8
9 const baseData = {
10 name: 'Router',
11 type: 'router',
12 children: [
13 {
14 name: 'WAN',
15 type: 'network',
16 children: [
17 {
18 name: 'Rack',
19 type: 'rack',
Matteo Scandolo35d53c82016-02-16 14:44:51 -080020 computeNodes: [],
Matteo Scandoloaf286372016-02-09 14:46:14 -080021 children: [
22 {
23 name: 'LAN',
24 type: 'network',
Matteo Scandolo7e67a9a2016-02-16 16:33:26 -080025 children: [{
26 name: 'Subscriber',
27 type: 'subscriber'
28 }] //subscribers goes here
Matteo Scandoloaf286372016-02-09 14:46:14 -080029 }
30 ]
31 }
32 ]
33 }
34 ]
35 };
36
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080037 /**
Matteo Scandolocc0db942016-02-11 17:37:08 -080038 * Calculate the horizontal position for each element.
39 * subsrcribers, devices and routers have the same fixed width 20
40 * network have a fixed width 104
41 * rack have a fixed width 105
42 * build and array of 6 elements representing the position of each element in the svg
43 * to equally space them
44 */
45
46 this.computeElementPosition = (svgWidth) => {
47
48 let xPos = [];
49
50 let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0);
51
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080052 let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2);
Matteo Scandolocc0db942016-02-11 17:37:08 -080053
54 let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1);
55
56 lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => {
57
58 // get half of the previous elements width
59 let previousElWidth = 0;
60 if(i !== 0){
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080061 previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0);
Matteo Scandolocc0db942016-02-11 17:37:08 -080062 }
63
64 let elPos =
65 serviceTopologyConfig.widthMargin // right margin
66 + (step * i) // space between elements
67 + (el / 2) // this el width
68 + previousElWidth; // previous elements width
Matteo Scandolo77d8fa02016-02-16 17:43:00 -080069
Matteo Scandolocc0db942016-02-11 17:37:08 -080070 xPos.push(svgWidth - elPos);
71 })
72
73 return xPos
74 };
75
76 /**
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080077 * from a nested data structure,
78 * create nodes and links for a D3 Tree Layout
79 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080080 const computeLayout = (data) => {
81 let nodes = layout.nodes(data);
82
83 // Normalize for fixed-depth.
Matteo Scandolocc0db942016-02-11 17:37:08 -080084 nodes.forEach((d) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -080085 // position the child node horizontally
Matteo Scandolocc0db942016-02-11 17:37:08 -080086 d.y = this.computeElementPosition(svgWidth)[d.depth];
Matteo Scandoloaf286372016-02-09 14:46:14 -080087 });
88
89 let links = layout.links(nodes);
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080090
Matteo Scandoloaf286372016-02-09 14:46:14 -080091 return [nodes, links];
92 };
93
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080094 /**
95 * Draw the containing group for any node or update the existing one
96 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080097 const drawNodes = (svg, nodes) => {
98 // Update the nodes…
99 var node = svg.selectAll('g.node')
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800100 .data(nodes, d => {
101 if(!angular.isString(d.d3Id)){
102 d.d3Id = `tree-${++i}`;
103 }
104 return d.d3Id;
105 });
Matteo Scandoloaf286372016-02-09 14:46:14 -0800106
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800107 // Enter any new nodes
Matteo Scandoloaf286372016-02-09 14:46:14 -0800108 var nodeEnter = node.enter().append('g')
109 .attr({
110 class: d => `node ${d.type}`,
111 transform: `translate(${svgWidth / 2}, ${svgHeight / 2})`
112 });
113
Matteo Scandolofd468582016-02-16 16:03:43 -0800114 // create Nodes
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800115 NodeDrawer.addNetworks(node.filter('.network'));
116 NodeDrawer.addRack(node.filter('.rack'));
117 NodeDrawer.addPhisical(node.filter('.router'));
118 NodeDrawer.addPhisical(node.filter('.subscriber'));
119 NodeDrawer.addDevice(node.filter('.device'));
Matteo Scandoloaf286372016-02-09 14:46:14 -0800120
Matteo Scandolofd468582016-02-16 16:03:43 -0800121 //update nodes
122 // TODO if data change, only update them
123 // NodeDrawer.updateRack(node.filter('.rack'));
124
Matteo Scandoloaf286372016-02-09 14:46:14 -0800125 // Transition nodes to their new position.
126 var nodeUpdate = node.transition()
127 .duration(serviceTopologyConfig.duration)
128 .attr({
129 'transform': d => `translate(${d.y},${d.x})`
130 });
131
132 // TODO handle node remove
133 };
134
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800135 /**
136 * Handle links in the tree layout
137 */
Matteo Scandoloaf286372016-02-09 14:46:14 -0800138 const drawLinks = (svg, links) => {
139
140 diagonal = d3.svg.diagonal()
141 .projection(d => [d.y, d.x]);
142
143 // Update the links…
144 var link = svg.selectAll('path.link')
145 .data(links, d => {
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800146 return d.target.d3Id
Matteo Scandoloaf286372016-02-09 14:46:14 -0800147 });
148
149 // Enter any new links at the parent's previous position.
150 link.enter().insert('path', 'g')
151 .attr('class', d => `link ${d.target.type}`)
152 .attr('d', function(d) {
153 var o = {x: svgHeight / 2, y: svgWidth / 2};
154 return diagonal({source: o, target: o});
155 });
156
157 // Transition links to their new position.
158 link.transition()
159 .duration(serviceTopologyConfig.duration)
160 .attr('d', diagonal);
161 };
162
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800163 /**
164 * Calculate the svg size and setup tree layout
165 */
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800166 this.setupTree = (svg) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800167
168
169 svgWidth = svg.node().getBoundingClientRect().width;
170 svgHeight = svg.node().getBoundingClientRect().height;
171
172 const width = svgWidth - (serviceTopologyConfig.widthMargin * 2);
173 const height = svgHeight - (serviceTopologyConfig.heightMargin * 2);
174
175 layout = d3.layout.tree()
176 .size([height, width]);
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800177 };
Matteo Scandoloaf286372016-02-09 14:46:14 -0800178
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800179 /**
180 * Update the tree layout
181 */
182
183 this.updateTree = (svg) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800184 // Compute the new tree layout.
185 [nodes, links] = computeLayout(baseData);
186
187 drawNodes(svg, nodes);
188 drawLinks(svg, links);
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800189 }
Matteo Scandoloaf286372016-02-09 14:46:14 -0800190
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800191 /**
192 * Add Subscribers to the tree
193 */
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800194 this.addSubscribers = (subscribers) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800195
Matteo Scandoloaf286372016-02-09 14:46:14 -0800196 subscribers.map((subscriber) => {
197 subscriber.children = subscriber.devices;
198 });
199
200 // add subscriber to data tree
201 baseData.children[0].children[0].children[0].children = subscribers;
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800202
203 return baseData;
204 };
Matteo Scandoloaf286372016-02-09 14:46:14 -0800205
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800206 /**
207 * Add compute nodes to the rack element
208 */
209
210 this.addComputeNodes = (computeNodes) => {
211 baseData.children[0].children[0].computeNodes = computeNodes;
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800212 };
213
214 this.getInstanceStatus = (instanceId) => {
215
216 const computeNodes = baseData.children[0].children[0].computeNodes;
217
Matteo Scandolofd468582016-02-16 16:03:43 -0800218 // unselect all
219 computeNodes.map((node) => {
220 node.instances.map((instance) => {
221 instance.selected = false
222 return instance;
223 });
224 });
225
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800226 let targetInstance = computeNodes.reduce((selected, node) => {
227 let found = lodash.find(node.instances, {id: instanceId});
228
229 if(found){
230 return found;
231 }
232 }, null);
233
234 // object are passed by reference,
235 // updating this update the instance in the tree
Matteo Scandolofd468582016-02-16 16:03:43 -0800236 if(targetInstance){
237 targetInstance.selected = true;
238 }
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800239
Matteo Scandoloaf286372016-02-09 14:46:14 -0800240 }
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800241 });
242
243}());