blob: 19825a1f8a5ffafa5aca1f0feb473967b7bdf24a [file] [log] [blame]
Matteo Scandolo0344ef32016-02-22 16:53:22 -08001(function () {
2 'use strict';
3
Matteo Scandolo4b3d8722016-02-24 11:22:48 -08004 angular.module('xos.diagnostic')
Matteo Scandolo653c5092016-02-24 11:14:01 -08005 .service('ChartData', function($rootScope, $q, lodash, Tenant, Node, serviceTopologyConfig, Ceilometer, Instances) {
Matteo Scandolo0344ef32016-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 Scandolobd663742016-03-07 16:07:13 -080014 name: 'WAN-Side',
15 subtitle: 'Virtual Network',
Matteo Scandolo0344ef32016-02-22 16:53:22 -080016 type: 'network',
17 children: [
18 {
Matteo Scandolobd663742016-03-07 16:07:13 -080019 name: 'Compute Servers',
Matteo Scandolo0344ef32016-02-22 16:53:22 -080020 type: 'rack',
21 computeNodes: [],
22 children: [
23 {
Matteo Scandolobd663742016-03-07 16:07:13 -080024 name: 'LAN-Side',
Matteo Scandolod54a7fd2016-03-08 09:33:26 -080025 subtitle: 'Virtual Network',
Matteo Scandolo0344ef32016-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 Scandoloa03110c2016-03-01 15:20:29 -080057 cTag: tags.cTag,
58 sTag: tags.sTag
Matteo Scandolo06afdfe2016-02-23 13:47:14 -080059 };
Matteo Scandolo0344ef32016-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 Scandolod1b3bf52016-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
84 this.highlightInstances([]);
85 delete this.logicTopologyData.children[0].children[0].children[0].children[0].children;
86 }
87
Matteo Scandoloa03110c2016-03-01 15:20:29 -080088 this.getSubscriberTag = (subscriber) => {
89 const tags = {
90 cTag: subscriber.c_tag,
91 sTag: subscriber.s_tag
92 };
Matteo Scandolo06afdfe2016-02-23 13:47:14 -080093
94 this.addSubscriberTag(tags);
95 // add tags info to current subscriber
Matteo Scandoloa03110c2016-03-01 15:20:29 -080096 this.currentSubscriber.tags = tags;
Matteo Scandolo0344ef32016-02-22 16:53:22 -080097
98 };
99
Matteo Scandolo5fc82c32016-03-02 10:59:46 -0800100 this.getSubscriberIP = (subscriber) => {
101 // const ip = JSON.parse(this.currentServiceChain.children[0].children[0].tenant.service_specific_attribute).wan_container_ip;
Matteo Scandolo653c5092016-02-24 11:14:01 -0800102 // const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip;
Matteo Scandolo5fc82c32016-03-02 10:59:46 -0800103 this.logicTopologyData.children[0].subscriberIP = subscriber.wan_container_ip;
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800104 };
105
106 this.selectSubscriber = (subscriber) => {
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800107 // append the device with to config settings
108 serviceTopologyConfig.elWidths.push(160);
109
110 this.addSubscriber(angular.copy(subscriber));
111
Matteo Scandolo78185102016-02-23 14:03:03 -0800112 //clean selected instances
113 this.highlightInstances([]);
114
Matteo Scandoloa03110c2016-03-01 15:20:29 -0800115 this.getSubscriberTag(subscriber);
Matteo Scandolo5fc82c32016-03-02 10:59:46 -0800116 this.getSubscriberIP(subscriber);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800117
118 };
119
120 this.highlightInstances = (instances) => {
121
122 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
123
124 // unselect all
125 computeNodes.map((node) => {
126 node.instances.map((instance) => {
127 instance.selected = false
128 return instance;
129 });
130 });
131
132 lodash.forEach(instances, (instance) => {
133 computeNodes.map((node) => {
134 node.instances.map((d3instance) => {
135 if(d3instance.id === instance.id){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800136 // console.log(d3instance, instance);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800137 d3instance.selected = true;
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800138 d3instance.stats = instance.stats; //add stats to d3 node
139 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800140 }
141 return d3instance;
142 });
143 });
144 });
145
146 }
147
148 this.getInstanceStatus = (service) => {
149 const deferred = $q.defer();
150
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800151 let p;
152
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800153 // subscriber specific
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800154 if(this.currentSubscriber){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800155
156 let attr;
157 try {
158 attr = JSON.parse(service.tenant.service_specific_attribute);
159 }
160 catch(e){
161 attr = null;
162 }
163
Matteo Scandolo653c5092016-02-24 11:14:01 -0800164 // if no instances are associated to the subscriber
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800165 if(!attr || !attr.instance_id){
166 let d = $q.defer();
167 d.resolve([]);
168 p = d.promise;
169 }
Matteo Scandolo653c5092016-02-24 11:14:01 -0800170 // if ther is an instance
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800171 else{
Matteo Scandolo653c5092016-02-24 11:14:01 -0800172 let instance = {};
173 p = Instances.get({id: attr.instance_id}).$promise
174 .then(function(_instance){
175 instance = _instance;
176 return Ceilometer.getInstanceStats(instance.instance_uuid);
177 })
178 .then((stats) => {
179 instance.stats = stats;
180 const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`;
181 // append containers
182 instance.container = {
183 name: containerName
184 };
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800185
186 // TODO fetch container stats
Matteo Scandolo653c5092016-02-24 11:14:01 -0800187 return Ceilometer.getContainerStats(containerName);
188 })
189 .then((containerStats) => {
190 instance.container.stats = containerStats.stats;
191 instance.container.port = containerStats.port;
192 return [instance];
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800193 });
194 }
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800195 }
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800196 // global scope
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800197 else {
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800198 const param = {
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800199 'service_vsg': {kind: 'vCPE'},
200 'service_vbng': {kind: 'vBNG'},
201 'service_volt': {kind: 'vOLT'}
202 };
203
204 p = Tenant.queryVsgInstances(param[service.name]).$promise
205 .then((instances) => {
206
207 return Ceilometer.getInstancesStats(instances);
208 });
209 }
210
211 p.then((instances) => {
212 this.highlightInstances(instances);
213 deferred.resolve(instances);
214 })
215 .catch((e) => {
216 deferred.reject(e);
217 });
218
219 return deferred.promise;
220 };
221 })
222})();