blob: 2bfc77a99fa9d473b3f6d053ce7fc62ffa81ca73 [file] [log] [blame]
Matteo Scandolo012dddb2016-02-22 16:53:22 -08001(function () {
2 'use strict';
3
Matteo Scandolo04564952016-02-24 11:22:48 -08004 angular.module('xos.diagnostic')
Matteo Scandolofe307b12016-05-17 14:29:01 -07005 .service('ChartData', function($rootScope, $q, _, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) {
Matteo Scandolo012dddb2016-02-22 16:53:22 -08006 this.currentSubscriber = null;
7 this.currentServiceChain = null;
8
9 this.logicTopologyData = {
10 name: 'Router',
11 type: 'router',
12 children: [
13 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080014 name: 'WAN-Side',
15 subtitle: 'Virtual Network',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080016 type: 'network',
17 children: [
18 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080019 name: 'Compute Servers',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080020 type: 'rack',
21 computeNodes: [],
22 children: [
23 {
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080024 name: 'LAN-Side',
Matteo Scandolo79108192016-03-08 09:33:26 -080025 subtitle: 'Virtual Network',
Matteo Scandolo012dddb2016-02-22 16:53:22 -080026 type: 'network',
27 children: [{
28 name: 'Subscriber',
29 type: 'subscriber'
30 }] //subscribers goes here
31 }
32 ]
33 }
34 ]
35 }
36 ]
37 };
38
39 this.getLogicTree = () => {
40 const deferred = $q.defer();
41
42 Node.queryWithInstances().$promise
43 .then((computeNodes) => {
44 this.logicTopologyData.children[0].children[0].computeNodes = computeNodes;
45 // LogicTopologyHelper.updateTree(svg);
46 deferred.resolve(this.logicTopologyData);
47 });
48
49 return deferred.promise;
50 };
51
52 /**
53 * Add Subscriber tag to LAN Network
54 */
55 this.addSubscriberTag = (tags) => {
56 this.logicTopologyData.children[0].children[0].children[0].subscriberTag = {
Matteo Scandolod4ea8772016-03-01 15:20:29 -080057 cTag: tags.cTag,
58 sTag: tags.sTag
Matteo Scandolo26d17e12016-02-23 13:47:14 -080059 };
Matteo Scandolo012dddb2016-02-22 16:53:22 -080060 };
61
62 /**
63 * Add Subscribers to the tree
64 */
65 this.addSubscriber = (subscriber) => {
66 subscriber.children = subscriber.devices;
67
68 // add subscriber to data tree
69 this.logicTopologyData.children[0].children[0].children[0].children = [subscriber];
70 return this.logicTopologyData;
71 };
72
Matteo Scandolo3a176a22016-03-07 16:42:03 -080073 /**
74 * Remove a subscriber from the tree
75 */
76
77 this.removeSubscriber = () => {
78 this.logicTopologyData.children[0].children[0].children[0].children[0].humanReadableName = 'Subscriber';
79 this.currentSubscriber = null;
80 if(serviceTopologyConfig.elWidths[serviceTopologyConfig.elWidths.length - 1] === 160){
81 serviceTopologyConfig.elWidths.pop();
82 }
83
Matteo Scandolof81130f2016-03-11 11:16:58 -080084 //remove tags and ip
85 delete this.logicTopologyData.children[0].children[0].children[0].subscriberTag;
86 delete this.logicTopologyData.children[0].subscriberIP;
87
Matteo Scandolo3a176a22016-03-07 16:42:03 -080088 this.highlightInstances([]);
89 delete this.logicTopologyData.children[0].children[0].children[0].children[0].children;
90 }
91
Matteo Scandolod4ea8772016-03-01 15:20:29 -080092 this.getSubscriberTag = (subscriber) => {
93 const tags = {
Matteo Scandoloe64dcc02016-08-02 11:53:22 -070094 cTag: subscriber.related.c_tag,
95 sTag: subscriber.related.s_tag
Matteo Scandolod4ea8772016-03-01 15:20:29 -080096 };
Matteo Scandolo26d17e12016-02-23 13:47:14 -080097
98 this.addSubscriberTag(tags);
99 // add tags info to current subscriber
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800100 this.currentSubscriber.tags = tags;
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800101
102 };
103
Matteo Scandolo6c6e5942016-03-02 10:59:46 -0800104 this.getSubscriberIP = (subscriber) => {
Matteo Scandoloe64dcc02016-08-02 11:53:22 -0700105 this.logicTopologyData.children[0].subscriberIP = subscriber.related.wan_container_ip;
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800106 };
107
108 this.selectSubscriber = (subscriber) => {
Matteo Scandolof79f0b32016-09-30 10:23:10 -0700109
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800110 // append the device with to config settings
111 serviceTopologyConfig.elWidths.push(160);
112
113 this.addSubscriber(angular.copy(subscriber));
114
Matteo Scandolo6aa165f2016-02-23 14:03:03 -0800115 //clean selected instances
116 this.highlightInstances([]);
117
Matteo Scandolod4ea8772016-03-01 15:20:29 -0800118 this.getSubscriberTag(subscriber);
Matteo Scandolo6c6e5942016-03-02 10:59:46 -0800119 this.getSubscriberIP(subscriber);
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800120
121 };
122
123 this.highlightInstances = (instances) => {
124
125 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
126
127 // unselect all
128 computeNodes.map((node) => {
129 node.instances.map((instance) => {
130 instance.selected = false
131 return instance;
132 });
133 });
134
Matteo Scandolofe307b12016-05-17 14:29:01 -0700135 _.forEach(instances, (instance) => {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800136 computeNodes.map((node) => {
137 node.instances.map((d3instance) => {
138 if(d3instance.id === instance.id){
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800139 // console.log(d3instance, instance);
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800140 d3instance.selected = true;
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800141 d3instance.stats = instance.stats; //add stats to d3 node
142 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800143 }
144 return d3instance;
145 });
146 });
147 });
148
149 }
150
151 this.getInstanceStatus = (service) => {
152 const deferred = $q.defer();
153
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800154 let p;
155
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800156 // subscriber specific
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800157 if(this.currentSubscriber){
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800158
159 let attr;
160 try {
161 attr = JSON.parse(service.tenant.service_specific_attribute);
162 }
163 catch(e){
164 attr = null;
165 }
166
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800167 // if no instances are associated to the subscriber
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800168 if(!attr || !attr.instance_id){
169 let d = $q.defer();
170 d.resolve([]);
171 p = d.promise;
172 }
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800173 // if ther is an instance
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800174 else{
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800175 let instance = {};
176 p = Instances.get({id: attr.instance_id}).$promise
177 .then(function(_instance){
178 instance = _instance;
179 return Ceilometer.getInstanceStats(instance.instance_uuid);
180 })
181 .then((stats) => {
182 instance.stats = stats;
183 const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`;
184 // append containers
185 instance.container = {
186 name: containerName
187 };
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800188
189 // TODO fetch container stats
Matteo Scandolof0d6e692016-02-24 11:14:01 -0800190 return Ceilometer.getContainerStats(containerName);
191 })
192 .then((containerStats) => {
193 instance.container.stats = containerStats.stats;
194 instance.container.port = containerStats.port;
195 return [instance];
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800196 });
197 }
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800198 }
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800199 // global scope
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800200 else {
Matteo Scandolo26d17e12016-02-23 13:47:14 -0800201 const param = {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800202 'service_vsg': {kind: 'vCPE'},
203 'service_vbng': {kind: 'vBNG'},
204 'service_volt': {kind: 'vOLT'}
205 };
206
207 p = Tenant.queryVsgInstances(param[service.name]).$promise
208 .then((instances) => {
Matteo Scandolofe307b12016-05-17 14:29:01 -0700209 return Ceilometer.getInstancesStats(_.uniq(instances));
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800210 });
211 }
212
213 p.then((instances) => {
214 this.highlightInstances(instances);
215 deferred.resolve(instances);
216 })
217 .catch((e) => {
218 deferred.reject(e);
219 });
220
221 return deferred.promise;
222 };
223 })
224})();