blob: d2b48ea814f3594c88982ab62b822007ddf81459 [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',
25 children: [] //subscribers goes here
26 }
27 ]
28 }
29 ]
30 }
31 ]
32 };
33
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080034 /**
Matteo Scandolocc0db942016-02-11 17:37:08 -080035 * Calculate the horizontal position for each element.
36 * subsrcribers, devices and routers have the same fixed width 20
37 * network have a fixed width 104
38 * rack have a fixed width 105
39 * build and array of 6 elements representing the position of each element in the svg
40 * to equally space them
41 */
42
43 this.computeElementPosition = (svgWidth) => {
44
45 let xPos = [];
46
47 let totalElWidth = lodash.reduce(serviceTopologyConfig.elWidths, (el, val) => val + el, 0);
48
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080049 let remainingSpace = svgWidth - totalElWidth - (serviceTopologyConfig.widthMargin * 2);
Matteo Scandolocc0db942016-02-11 17:37:08 -080050
51 let step = remainingSpace / (serviceTopologyConfig.elWidths.length - 1);
52
53 lodash.forEach(serviceTopologyConfig.elWidths, (el, i) => {
54
55 // get half of the previous elements width
56 let previousElWidth = 0;
57 if(i !== 0){
Matteo Scandolobec0a6c2016-02-11 17:58:18 -080058 previousElWidth = lodash.reduce(serviceTopologyConfig.elWidths.slice(0, i), (el, val) => val + el, 0);
Matteo Scandolocc0db942016-02-11 17:37:08 -080059 }
60
61 let elPos =
62 serviceTopologyConfig.widthMargin // right margin
63 + (step * i) // space between elements
64 + (el / 2) // this el width
65 + previousElWidth; // previous elements width
66
67 xPos.push(svgWidth - elPos);
68 })
69
70 return xPos
71 };
72
73 /**
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080074 * from a nested data structure,
75 * create nodes and links for a D3 Tree Layout
76 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080077 const computeLayout = (data) => {
78 let nodes = layout.nodes(data);
79
80 // Normalize for fixed-depth.
Matteo Scandolocc0db942016-02-11 17:37:08 -080081 nodes.forEach((d) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -080082 // position the child node horizontally
Matteo Scandolocc0db942016-02-11 17:37:08 -080083 d.y = this.computeElementPosition(svgWidth)[d.depth];
Matteo Scandoloaf286372016-02-09 14:46:14 -080084 });
85
86 let links = layout.links(nodes);
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080087
Matteo Scandoloaf286372016-02-09 14:46:14 -080088 return [nodes, links];
89 };
90
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080091 /**
92 * Draw the containing group for any node or update the existing one
93 */
Matteo Scandoloaf286372016-02-09 14:46:14 -080094 const drawNodes = (svg, nodes) => {
95 // Update the nodes…
96 var node = svg.selectAll('g.node')
Matteo Scandolo9fe01af2016-02-09 16:01:49 -080097 .data(nodes, d => {
98 if(!angular.isString(d.d3Id)){
99 d.d3Id = `tree-${++i}`;
100 }
101 return d.d3Id;
102 });
Matteo Scandoloaf286372016-02-09 14:46:14 -0800103
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800104 // Enter any new nodes
Matteo Scandoloaf286372016-02-09 14:46:14 -0800105 var nodeEnter = node.enter().append('g')
106 .attr({
107 class: d => `node ${d.type}`,
108 transform: `translate(${svgWidth / 2}, ${svgHeight / 2})`
109 });
110
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800111 NodeDrawer.addNetworks(node.filter('.network'));
112 NodeDrawer.addRack(node.filter('.rack'));
113 NodeDrawer.addPhisical(node.filter('.router'));
114 NodeDrawer.addPhisical(node.filter('.subscriber'));
115 NodeDrawer.addDevice(node.filter('.device'));
Matteo Scandoloaf286372016-02-09 14:46:14 -0800116
117 // Transition nodes to their new position.
118 var nodeUpdate = node.transition()
119 .duration(serviceTopologyConfig.duration)
120 .attr({
121 'transform': d => `translate(${d.y},${d.x})`
122 });
123
124 // TODO handle node remove
125 };
126
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800127 /**
128 * Handle links in the tree layout
129 */
Matteo Scandoloaf286372016-02-09 14:46:14 -0800130 const drawLinks = (svg, links) => {
131
132 diagonal = d3.svg.diagonal()
133 .projection(d => [d.y, d.x]);
134
135 // Update the links…
136 var link = svg.selectAll('path.link')
137 .data(links, d => {
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800138 return d.target.d3Id
Matteo Scandoloaf286372016-02-09 14:46:14 -0800139 });
140
141 // Enter any new links at the parent's previous position.
142 link.enter().insert('path', 'g')
143 .attr('class', d => `link ${d.target.type}`)
144 .attr('d', function(d) {
145 var o = {x: svgHeight / 2, y: svgWidth / 2};
146 return diagonal({source: o, target: o});
147 });
148
149 // Transition links to their new position.
150 link.transition()
151 .duration(serviceTopologyConfig.duration)
152 .attr('d', diagonal);
153 };
154
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800155 /**
156 * Calculate the svg size and setup tree layout
157 */
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800158 this.setupTree = (svg) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800159
160
161 svgWidth = svg.node().getBoundingClientRect().width;
162 svgHeight = svg.node().getBoundingClientRect().height;
163
164 const width = svgWidth - (serviceTopologyConfig.widthMargin * 2);
165 const height = svgHeight - (serviceTopologyConfig.heightMargin * 2);
166
167 layout = d3.layout.tree()
168 .size([height, width]);
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800169 };
Matteo Scandoloaf286372016-02-09 14:46:14 -0800170
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800171 /**
172 * Update the tree layout
173 */
174
175 this.updateTree = (svg) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800176 // Compute the new tree layout.
177 [nodes, links] = computeLayout(baseData);
178
179 drawNodes(svg, nodes);
180 drawLinks(svg, links);
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800181 }
Matteo Scandoloaf286372016-02-09 14:46:14 -0800182
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800183 /**
184 * Add Subscribers to the tree
185 */
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800186 this.addSubscribers = (subscribers) => {
Matteo Scandoloaf286372016-02-09 14:46:14 -0800187
Matteo Scandoloaf286372016-02-09 14:46:14 -0800188 subscribers.map((subscriber) => {
189 subscriber.children = subscriber.devices;
190 });
191
192 // add subscriber to data tree
193 baseData.children[0].children[0].children[0].children = subscribers;
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800194
195 return baseData;
196 };
Matteo Scandoloaf286372016-02-09 14:46:14 -0800197
Matteo Scandolo35d53c82016-02-16 14:44:51 -0800198 /**
199 * Add compute nodes to the rack element
200 */
201
202 this.addComputeNodes = (computeNodes) => {
203 baseData.children[0].children[0].computeNodes = computeNodes;
Matteo Scandolo79de20a2016-02-16 15:06:11 -0800204 };
205
206 this.getInstanceStatus = (instanceId) => {
207
208 const computeNodes = baseData.children[0].children[0].computeNodes;
209
210 let targetInstance = computeNodes.reduce((selected, node) => {
211 let found = lodash.find(node.instances, {id: instanceId});
212
213 if(found){
214 return found;
215 }
216 }, null);
217
218 // object are passed by reference,
219 // updating this update the instance in the tree
220 targetInstance.selected = true;
221
Matteo Scandoloaf286372016-02-09 14:46:14 -0800222 }
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800223 });
224
225}());