blob: 444f046389aa6687909237fdf6f49763944a2a6c [file] [log] [blame]
Matteo Scandolo0344ef32016-02-22 16:53:22 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
5 .service('ChartData', function($rootScope, $q, lodash, Tenant, Node, serviceTopologyConfig, Ceilometer) {
6 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 = () => {
85 const ip = this.currentServiceChain.children[0].children[0].tenant.wan_container_ip;
86 this.logicTopologyData.children[0].subscriberIP = ip;
87 };
88
89 this.selectSubscriber = (subscriber) => {
90
91 // append the device with to config settings
92 serviceTopologyConfig.elWidths.push(160);
93
94 this.addSubscriber(angular.copy(subscriber));
95
Matteo Scandolo78185102016-02-23 14:03:03 -080096 //clean selected instances
97 this.highlightInstances([]);
98
Matteo Scandolo0344ef32016-02-22 16:53:22 -080099 this.getSubscriberTag();
100 this.getSubscriberIP();
101
102 };
103
104 this.highlightInstances = (instances) => {
105
106 const computeNodes = this.logicTopologyData.children[0].children[0].computeNodes;
107
108 // unselect all
109 computeNodes.map((node) => {
110 node.instances.map((instance) => {
111 instance.selected = false
112 return instance;
113 });
114 });
115
116 lodash.forEach(instances, (instance) => {
117 computeNodes.map((node) => {
118 node.instances.map((d3instance) => {
119 if(d3instance.id === instance.id){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800120 // console.log(d3instance, instance);
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800121 d3instance.selected = true;
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800122 d3instance.stats = instance.stats; //add stats to d3 node
123 d3instance.container = instance.container; // container info to d3 node
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800124 }
125 return d3instance;
126 });
127 });
128 });
129
130 }
131
132 this.getInstanceStatus = (service) => {
133 const deferred = $q.defer();
134
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800135 let p;
136
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800137 // subscriber specific
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800138 if(this.currentSubscriber){
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800139
140 let attr;
141 try {
142 attr = JSON.parse(service.tenant.service_specific_attribute);
143 }
144 catch(e){
145 attr = null;
146 }
147
148 // if no instances are associated to the container
149 if(!attr || !attr.instance_id){
150 let d = $q.defer();
151 d.resolve([]);
152 p = d.promise;
153 }
154 else{
155 let instances = [attr.instance_id];
156 p = Ceilometer.getInstancesStats(instances)
157 .then((instances) => {
158 instances.map(i => {
159 i.container = {
160 name: `vcpe-${this.currentSubscriber.tags.sTag}-${this.currentSubscriber.tags.cTag}`
161 };
162 return i;
163 });
164
165 // TODO fetch container stats
166
167 return instances;
168 });
169 }
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800170 }
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800171 // global scope
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800172 else {
Matteo Scandolo06afdfe2016-02-23 13:47:14 -0800173 const param = {
Matteo Scandolo0344ef32016-02-22 16:53:22 -0800174 'service_vsg': {kind: 'vCPE'},
175 'service_vbng': {kind: 'vBNG'},
176 'service_volt': {kind: 'vOLT'}
177 };
178
179 p = Tenant.queryVsgInstances(param[service.name]).$promise
180 .then((instances) => {
181
182 return Ceilometer.getInstancesStats(instances);
183 });
184 }
185
186 p.then((instances) => {
187 this.highlightInstances(instances);
188 deferred.resolve(instances);
189 })
190 .catch((e) => {
191 deferred.reject(e);
192 });
193
194 return deferred.promise;
195 };
196 })
197})();