blob: f39a1e25ca6bdf84d053a455b8751bcd6d05f227 [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 = {
Matteo Scandoloa03110c2016-03-01 15:20:29 -080055 cTag: tags.cTag,
56 sTag: tags.sTag
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
Matteo Scandoloa03110c2016-03-01 15:20:29 -080071 this.getSubscriberTag = (subscriber) => {
72 const tags = {
73 cTag: subscriber.c_tag,
74 sTag: subscriber.s_tag
75 };
Matteo Scandolo06afdfe2016-02-23 13:47:14 -080076
77 this.addSubscriberTag(tags);
78 // add tags info to current subscriber
Matteo Scandoloa03110c2016-03-01 15:20:29 -080079 this.currentSubscriber.tags = tags;
Matteo Scandolo0344ef32016-02-22 16:53:22 -080080
81 };
82
Matteo Scandolo5fc82c32016-03-02 10:59:46 -080083 this.getSubscriberIP = (subscriber) => {
84 // const ip = JSON.parse(this.currentServiceChain.children[0].children[0].tenant.service_specific_attribute).wan_container_ip;
Matteo Scandolo653c5092016-02-24 11:14:01 -080085 // const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip;
Matteo Scandolo5fc82c32016-03-02 10:59:46 -080086 this.logicTopologyData.children[0].subscriberIP = subscriber.wan_container_ip;
Matteo Scandolo0344ef32016-02-22 16:53:22 -080087 };
88
89 this.selectSubscriber = (subscriber) => {
Matteo Scandolo0344ef32016-02-22 16:53:22 -080090 // append the device with to config settings
91 serviceTopologyConfig.elWidths.push(160);
92
93 this.addSubscriber(angular.copy(subscriber));
94
Matteo Scandolo78185102016-02-23 14:03:03 -080095 //clean selected instances
96 this.highlightInstances([]);
97
Matteo Scandoloa03110c2016-03-01 15:20:29 -080098 this.getSubscriberTag(subscriber);
Matteo Scandolo5fc82c32016-03-02 10:59:46 -080099 this.getSubscriberIP(subscriber);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800100
101 };
102
103 this.highlightInstances = (instances) => {
104
105 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
106
107 // unselect all
108 computeNodes.map((node) => {
109 node.instances.map((instance) => {
110 instance.selected = false
111 return instance;
112 });
113 });
114
115 lodash.forEach(instances, (instance) => {
116 computeNodes.map((node) => {
117 node.instances.map((d3instance) => {
118 if(d3instance.id === instance.id){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800119 // console.log(d3instance, instance);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800120 d3instance.selected = true;
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800121 d3instance.stats = instance.stats; //add stats to d3 node
122 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800123 }
124 return d3instance;
125 });
126 });
127 });
128
129 }
130
131 this.getInstanceStatus = (service) => {
132 const deferred = $q.defer();
133
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800134 let p;
135
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800136 // subscriber specific
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800137 if(this.currentSubscriber){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800138
139 let attr;
140 try {
141 attr = JSON.parse(service.tenant.service_specific_attribute);
142 }
143 catch(e){
144 attr = null;
145 }
146
Matteo Scandolo653c5092016-02-24 11:14:01 -0800147 // if no instances are associated to the subscriber
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800148 if(!attr || !attr.instance_id){
149 let d = $q.defer();
150 d.resolve([]);
151 p = d.promise;
152 }
Matteo Scandolo653c5092016-02-24 11:14:01 -0800153 // if ther is an instance
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800154 else{
Matteo Scandolo653c5092016-02-24 11:14:01 -0800155 let instance = {};
156 p = Instances.get({id: attr.instance_id}).$promise
157 .then(function(_instance){
158 instance = _instance;
159 return Ceilometer.getInstanceStats(instance.instance_uuid);
160 })
161 .then((stats) => {
162 instance.stats = stats;
163 const containerName = `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`;
164 // append containers
165 instance.container = {
166 name: containerName
167 };
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800168
169 // TODO fetch container stats
Matteo Scandolo653c5092016-02-24 11:14:01 -0800170 return Ceilometer.getContainerStats(containerName);
171 })
172 .then((containerStats) => {
173 instance.container.stats = containerStats.stats;
174 instance.container.port = containerStats.port;
175 return [instance];
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800176 });
177 }
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800178 }
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800179 // global scope
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800180 else {
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800181 const param = {
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800182 'service_vsg': {kind: 'vCPE'},
183 'service_vbng': {kind: 'vBNG'},
184 'service_volt': {kind: 'vOLT'}
185 };
186
187 p = Tenant.queryVsgInstances(param[service.name]).$promise
188 .then((instances) => {
189
190 return Ceilometer.getInstancesStats(instances);
191 });
192 }
193
194 p.then((instances) => {
195 this.highlightInstances(instances);
196 deferred.resolve(instances);
197 })
198 .catch((e) => {
199 deferred.reject(e);
200 });
201
202 return deferred.promise;
203 };
204 })
205})();