blob: 609b27b4cbcfa40d83c63910efca0da4428bdca0 [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 {
14 name: 'WAN',
15 type: 'network',
16 children: [
17 {
18 name: 'Rack',
19 type: 'rack',
20 computeNodes: [],
21 children: [
22 {
23 name: 'LAN',
24 type: 'network',
25 children: [{
26 name: 'Subscriber',
27 type: 'subscriber'
28 }] //subscribers goes here
29 }
30 ]
31 }
32 ]
33 }
34 ]
35 };
36
37 this.getLogicTree = () => {
38 const deferred = $q.defer();
39
40 Node.queryWithInstances().$promise
41 .then((computeNodes) => {
42 this.logicTopologyData.children[0].children[0].computeNodes = computeNodes;
43 // LogicTopologyHelper.updateTree(svg);
44 deferred.resolve(this.logicTopologyData);
45 });
46
47 return deferred.promise;
48 };
49
50 /**
51 * Add Subscriber tag to LAN Network
52 */
53 this.addSubscriberTag = (tags) => {
54 this.logicTopologyData.children[0].children[0].children[0].subscriberTag = {
55 cTag: tags.c_tag,
56 sTag: tags.s_tag
Matteo Scandolo06afdfe2016-02-23 13:47:14 -080057 };
Matteo Scandolo0344ef32016-02-22 16:53:22 -080058 };
59
60 /**
61 * Add Subscribers to the tree
62 */
63 this.addSubscriber = (subscriber) => {
64 subscriber.children = subscriber.devices;
65
66 // add subscriber to data tree
67 this.logicTopologyData.children[0].children[0].children[0].children = [subscriber];
68 return this.logicTopologyData;
69 };
70
71 this.getSubscriberTag = () => {
Matteo Scandolo06afdfe2016-02-23 13:47:14 -080072 const tags = JSON.parse(this.currentServiceChain.children[0].tenant.service_specific_attribute);
73 delete tags.creator_id;
74
75 this.addSubscriberTag(tags);
76 // add tags info to current subscriber
77 this.currentSubscriber.tags = {
78 cTag: tags.c_tag,
79 sTag: tags.s_tag
80 };
Matteo Scandolo0344ef32016-02-22 16:53:22 -080081
82 };
83
84 this.getSubscriberIP = () => {
Matteo Scandolo653c5092016-02-24 11:14:01 -080085 const ip = JSON.parse(this.currentServiceChain.children[0].children[0].tenant.service_specific_attribute).wan_container_ip;
86 // const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip;
Matteo Scandolo0344ef32016-02-22 16:53:22 -080087 this.logicTopologyData.children[0].subscriberIP = ip;
88 };
89
90 this.selectSubscriber = (subscriber) => {
91
92 // append the device with to config settings
93 serviceTopologyConfig.elWidths.push(160);
94
95 this.addSubscriber(angular.copy(subscriber));
96
Matteo Scandolo78185102016-02-23 14:03:03 -080097 //clean selected instances
98 this.highlightInstances([]);
99
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800100 this.getSubscriberTag();
101 this.getSubscriberIP();
102
103 };
104
105 this.highlightInstances = (instances) => {
106
107 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
108
109 // unselect all
110 computeNodes.map((node) => {
111 node.instances.map((instance) => {
112 instance.selected = false
113 return instance;
114 });
115 });
116
117 lodash.forEach(instances, (instance) => {
118 computeNodes.map((node) => {
119 node.instances.map((d3instance) => {
120 if(d3instance.id === instance.id){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800121 // console.log(d3instance, instance);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800122 d3instance.selected = true;
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800123 d3instance.stats = instance.stats; //add stats to d3 node
124 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800125 }
126 return d3instance;
127 });
128 });
129 });
130
131 }
132
133 this.getInstanceStatus = (service) => {
134 const deferred = $q.defer();
135
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800136 let p;
137
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800138 // subscriber specific
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800139 if(this.currentSubscriber){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800140
141 let attr;
142 try {
143 attr = JSON.parse(service.tenant.service_specific_attribute);
144 }
145 catch(e){
146 attr = null;
147 }
148
Matteo Scandolo653c5092016-02-24 11:14:01 -0800149 // if no instances are associated to the subscriber
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800150 if(!attr || !attr.instance_id){
151 let d = $q.defer();
152 d.resolve([]);
153 p = d.promise;
154 }
Matteo Scandolo653c5092016-02-24 11:14:01 -0800155 // if ther is an instance
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800156 else{
Matteo Scandolo653c5092016-02-24 11:14:01 -0800157 let instance = {};
158 p = Instances.get({id: attr.instance_id}).$promise
159 .then(function(_instance){
160 instance = _instance;
161 return Ceilometer.getInstanceStats(instance.instance_uuid);
162 })
163 .then((stats) => {
164 instance.stats = stats;
165 const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`;
166 // append containers
167 instance.container = {
168 name: containerName
169 };
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800170
171 // TODO fetch container stats
Matteo Scandolo653c5092016-02-24 11:14:01 -0800172 return Ceilometer.getContainerStats(containerName);
173 })
174 .then((containerStats) => {
175 instance.container.stats = containerStats.stats;
176 instance.container.port = containerStats.port;
177 return [instance];
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800178 });
179 }
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800180 }
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800181 // global scope
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800182 else {
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800183 const param = {
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800184 'service_vsg': {kind: 'vCPE'},
185 'service_vbng': {kind: 'vBNG'},
186 'service_volt': {kind: 'vOLT'}
187 };
188
189 p = Tenant.queryVsgInstances(param[service.name]).$promise
190 .then((instances) => {
191
192 return Ceilometer.getInstancesStats(instances);
193 });
194 }
195
196 p.then((instances) => {
197 this.highlightInstances(instances);
198 deferred.resolve(instances);
199 })
200 .catch((e) => {
201 deferred.reject(e);
202 });
203
204 return deferred.promise;
205 };
206 })
207})();