blob: 94ff406f3a98b3555cf8ddd638629c2aaa8e0c15 [file] [log] [blame]
Matteo Scandolo38ba3312016-02-09 16:01:49 -08001(function () {
2 'use strict';
3
4 const shapes = {
5 cloud: ' M 79.72 49.60 C 86.00 37.29 98.57 29.01 111.96 26.42 C 124.27 24.11 137.53 26.15 148.18 32.90 C 158.08 38.78 165.39 48.87 167.65 60.20 C 176.20 57.90 185.14 56.01 194.00 57.73 C 206.08 59.59 217.92 66.01 224.37 76.66 C 227.51 81.54 228.85 87.33 229.23 93.06 C 237.59 93.33 246.22 95.10 253.04 100.19 C 256.69 103.13 259.87 107.67 258.91 112.59 C 257.95 118.43 252.78 122.38 247.78 124.82 C 235.27 130.43 220.23 130.09 207.98 123.93 C 199.33 127.88 189.76 129.43 180.30 128.57 C 173.70 139.92 161.70 147.65 148.86 149.93 C 133.10 153.26 116.06 148.15 104.42 137.08 C 92.98 143.04 78.96 143.87 66.97 139.04 C 57.75 135.41 49.70 128.00 46.60 118.43 C 43.87 109.95 45.81 100.29 51.30 93.32 C 57.38 85.18 67.10 80.44 76.99 78.89 C 74.38 69.20 74.87 58.52 79.72 49.60 Z'
6 }
7
Matteo Scandolo14183932016-02-11 08:58:04 -08008 var computeNodeId = 0;
9 var instanceId = 0;
10
Matteo Scandolo38ba3312016-02-09 16:01:49 -080011 angular.module('xos.serviceTopology')
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080012 .service('NodeDrawer', function(d3, serviceTopologyConfig, RackHelper){
Matteo Scandoloba2d63d2016-02-17 13:54:11 -080013
14 var _this = this;
15
Matteo Scandolo38ba3312016-02-09 16:01:49 -080016 this.addNetworks = (nodes) => {
17 nodes.append('path')
18 .attr({
19 d: shapes.cloud,
20 transform: 'translate(-63, -52), scale(0.5)',
21 class: 'cloud'
22 });
23
24 nodes.append('text')
25 .attr({
26 'text-anchor': 'middle'
27 })
28 .text(d => d.name)
Matteo Scandolodffc1382016-02-22 14:53:44 -080029
30 nodes.each(function(n){
31 let currentNode = d3.select(this);
32 // cicle trouch node to add Tags and Public IP
33 if(n.name === 'LAN' && angular.isDefined(n.subscriberTag)){
34 currentNode.append('text')
35 .attr({
36 'text-anchor': 'middle',
37 y: 40
38 })
39 .text(() => `C-Tag: ${n.subscriberTag.cTag}`);
40
41 currentNode.append('text')
42 .attr({
43 'text-anchor': 'middle',
44 y: 60
45 })
46 .text(() => `S-Tag: ${n.subscriberTag.sTag}`);
47 }
48 });
Matteo Scandolo38ba3312016-02-09 16:01:49 -080049 }
50
51 this.addRack = (nodes) => {
Matteo Scandolo14183932016-02-11 08:58:04 -080052
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080053 // loop because of D3
54 // rack will be only one
55 nodes.each(d => {
56 let [w, h] = RackHelper.getRackSize(d.computeNodes);
Matteo Scandolob785b572016-02-16 11:50:51 -080057
58 let rack = nodes
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080059 .append('g');
60
61 rack
62 .attr({
63 transform: `translate(0,0)`
64 })
65 .transition()
66 .duration(serviceTopologyConfig.duration)
67 .attr({
Matteo Scandolob785b572016-02-16 11:50:51 -080068 transform: d => `translate(${- (w / 2)}, ${- (h / 2)})`
69 });
70
71 rack
72 .append('rect')
73 .attr({
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080074 width: 0,
75 height: 0
76 })
77 .transition()
78 .duration(serviceTopologyConfig.duration)
79 .attr({
Matteo Scandolob785b572016-02-16 11:50:51 -080080 width: w,
81 height: h
82 });
83
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080084 rack.append('text')
Matteo Scandolob785b572016-02-16 11:50:51 -080085 .attr({
86 'text-anchor': 'middle',
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080087 y: - 10,
88 x: w / 2,
89 opacity: 0
Matteo Scandolob785b572016-02-16 11:50:51 -080090 })
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080091 .text(d => d.name)
92 .transition()
93 .duration(serviceTopologyConfig.duration)
94 .attr({
95 opacity: 1
96 })
Matteo Scandolob785b572016-02-16 11:50:51 -080097
Matteo Scandolo7fd4d042016-02-16 14:44:51 -080098 this.drawComputeNodes(rack, d.computeNodes);
99
Matteo Scandolo14183932016-02-11 08:58:04 -0800100 });
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800101
Matteo Scandolo14183932016-02-11 08:58:04 -0800102 };
103
104 this.drawComputeNodes = (container, nodes) => {
Matteo Scandolob785b572016-02-16 11:50:51 -0800105
Matteo Scandolo14183932016-02-11 08:58:04 -0800106 let elements = container.selectAll('.compute-nodes')
107 .data(nodes, d => {
108 if(!angular.isString(d.d3Id)){
109 d.d3Id = `compute-node-${++computeNodeId}`;
110 }
111 return d.d3Id;
112 });
113
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800114 let {width, height} = container.node().getBoundingClientRect();
115
116 var nodeContainer = elements.enter().append('g');
117
118 nodeContainer
Matteo Scandolo14183932016-02-11 08:58:04 -0800119 .attr({
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800120 transform: `translate(${width / 2}, ${ height / 2})`,
Matteo Scandolob785b572016-02-16 11:50:51 -0800121 class: 'compute-node',
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800122 })
123 .transition()
124 .duration(serviceTopologyConfig.duration)
125 .attr({
Matteo Scandolob785b572016-02-16 11:50:51 -0800126 transform: (d) => `translate(${RackHelper.getComputeNodePosition(nodes, d.d3Id.replace('compute-node-', '') - 1)})`
Matteo Scandolo14183932016-02-11 08:58:04 -0800127 });
128
129 nodeContainer.append('rect')
Matteo Scandolob785b572016-02-16 11:50:51 -0800130 .attr({
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800131 width: 0,
132 height: 0
133 })
134 .transition()
135 .duration(serviceTopologyConfig.duration)
136 .attr({
Matteo Scandolob785b572016-02-16 11:50:51 -0800137 width: d => RackHelper.getComputeNodeSize(d.instances)[0],
138 height: d => RackHelper.getComputeNodeSize(d.instances)[1],
139 });
140
141 nodeContainer.append('text')
142 .attr({
143 'text-anchor': 'start',
144 y: 15, //FIXME
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800145 x: 10, //FIXME
146 opacity: 0
Matteo Scandolob785b572016-02-16 11:50:51 -0800147 })
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800148 .text(d => d.humanReadableName.split('.')[0])
149 .transition()
150 .duration(serviceTopologyConfig.duration)
151 .attr({
152 opacity: 1
153 })
Matteo Scandolo14183932016-02-11 08:58:04 -0800154
155 // if there are Compute Nodes
156 if(nodeContainer.length > 0){
Matteo Scandoloba2d63d2016-02-17 13:54:11 -0800157 // draw instances for each compute node
158 nodeContainer.each(function(a){
159 _this.drawInstances(d3.select(this), a.instances);
160 })
Matteo Scandolo14183932016-02-11 08:58:04 -0800161 }
162
163 };
164
Matteo Scandolo0cfd5a22016-02-16 13:29:26 -0800165 // NOTE Stripping unuseful names to shorten labels.
166 // This is not elegant
167 const formatInstanceName = (name) => {
168 return name
169 .replace('app_', '')
170 .replace('service_', '')
171 .replace('ovs_', '')
172 .replace('mysite_', '')
173 .replace('_instance', '');
174 };
175
Matteo Scandolo14183932016-02-11 08:58:04 -0800176 this.drawInstances = (container, instances) => {
Matteo Scandoloba2d63d2016-02-17 13:54:11 -0800177
Matteo Scandoloc303fd02016-02-17 15:11:33 -0800178 // TODO check for stats field in instance and draw popup
179
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800180 let {width, height} = container.node().getBoundingClientRect();
181
Matteo Scandolo14183932016-02-11 08:58:04 -0800182 let elements = container.selectAll('.instances')
183 .data(instances, d => angular.isString(d.d3Id) ? d.d3Id : d.d3Id = `instance-${++instanceId}`)
184
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800185 var instanceContainer = elements.enter().append('g');
186
187 instanceContainer
Matteo Scandolo14183932016-02-11 08:58:04 -0800188 .attr({
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800189 transform: `translate(${width / 2}, ${ height / 2})`,
Matteo Scandolo4aae3aa2016-02-16 16:33:26 -0800190 class: d => `instance ${d.selected ? 'active' : ''}`,
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800191 })
192 .transition()
193 .duration(serviceTopologyConfig.duration)
194 .attr({
Matteo Scandoloba2d63d2016-02-17 13:54:11 -0800195 transform: (d, i) => `translate(${RackHelper.getInstancePosition(i)})`
Matteo Scandolo14183932016-02-11 08:58:04 -0800196 });
197
198 instanceContainer.append('rect')
Matteo Scandolob785b572016-02-16 11:50:51 -0800199 .attr({
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800200 width: 0,
201 height: 0
202 })
203 .transition()
204 .duration(serviceTopologyConfig.duration)
205 .attr({
Matteo Scandolob785b572016-02-16 11:50:51 -0800206 width: serviceTopologyConfig.instance.width,
207 height: serviceTopologyConfig.instance.height
208 });
209
210 instanceContainer.append('text')
211 .attr({
Matteo Scandolo0cfd5a22016-02-16 13:29:26 -0800212 'text-anchor': 'middle',
213 y: 23, //FIXME
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800214 x: 40, //FIXME
215 opacity: 0
Matteo Scandolob785b572016-02-16 11:50:51 -0800216 })
Matteo Scandolo7fd4d042016-02-16 14:44:51 -0800217 .text(d => formatInstanceName(d.name))
218 .transition()
219 .duration(serviceTopologyConfig.duration)
220 .attr({
221 opacity: 1
222 });
Matteo Scandolo0cfd5a22016-02-16 13:29:26 -0800223
224 instanceContainer
225 .on('click', d => {
226 console.log(d);
227 });
Matteo Scandolo14183932016-02-11 08:58:04 -0800228 };
Matteo Scandolo38ba3312016-02-09 16:01:49 -0800229
230 this.addPhisical = (nodes) => {
231 nodes.append('rect')
232 .attr(serviceTopologyConfig.square);
233
234 nodes.append('text')
235 .attr({
236 'text-anchor': 'middle',
237 y: serviceTopologyConfig.square.y - 10
238 })
239 .text(d => d.name);
240 }
241
242 this.addDevice = (nodes) => {
243 nodes.append('circle')
244 .attr(serviceTopologyConfig.circle);
245
246 nodes.append('text')
247 .attr({
248 'text-anchor': 'end',
249 x: - serviceTopologyConfig.circle.r - 10,
250 y: serviceTopologyConfig.circle.r / 2
251 })
252 .text(d => d.name);
253 }
254 });
255})();