blob: 311ea079226c5d5373dc0a0eb6d904eec48dc71e [file] [log] [blame]
Matteo Scandolo8a64fa42016-01-21 11:21:03 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
5 .service('Services', function($resource){
Matteo Scandolof16b3792016-01-21 14:23:28 -08006 return $resource('/xos/services/:id', {id: '@id'});
7 })
8 .service('Tenant', function($resource){
Matteo Scandolo51031482016-02-17 13:54:11 -08009 return $resource('/xos/tenants', {id: '@id'}, {
10 queryVsgInstances: {
11 method: 'GET',
12 isArray: true,
13 interceptor: {
14 response: (res) => {
15
16 // NOTE
17 // Note that VCPETenant is now VSGTenant.
18
19 let instances = [];
20
21 angular.forEach(res.data, (tenant) => {
22 let info = JSON.parse(tenant.service_specific_attribute);
23 if(info && info.instance_id){
24 instances.push(info.instance_id);
25 }
26 });
27
28 return instances;
29 }
30 }
Matteo Scandolo45fba732016-02-22 14:53:44 -080031 },
32 getSubscriberTag: {
33 method: 'GET',
34 isArray: true,
35 interceptor: {
36 response: (res) => {
37 // NOTE we should receive only one vOLT tenant here
38 return JSON.parse(res.data[0].service_specific_attribute);
39 }
40 }
Matteo Scandolo51031482016-02-17 13:54:11 -080041 }
42 });
43 })
44 .service('Ceilometer', function($http, $q, Instances) {
45
46 /**
47 * Get stats for a single instance
48 */
49 this.getInstanceStats = (instanceUuid) => {
50 let deferred = $q.defer();
51
Matteo Scandolof0d6e692016-02-24 11:14:01 -080052 $http.get('/xoslib/xos-instance-statistics', {params: {'instance-uuid': instanceUuid}})
Matteo Scandolo51031482016-02-17 13:54:11 -080053 .then((res) => {
54 deferred.resolve(res.data);
55 })
56 .catch((e) => {
57 deferred.reject(e);
58 })
59
60 return deferred.promise;
61 };
62
63 /**
64 * Collect stats for an array of instances
65 */
66 this.getInstancesStats = (instances) => {
67 let deferred = $q.defer();
68 let instancePromises = [];
69 let instanceList = [];
70
71 // retrieve instance details
72 instances.forEach((instanceId) => {
73 instancePromises.push(Instances.get({id: instanceId}).$promise);
74 });
75
76 // get all instance data
77 $q.all(instancePromises)
78 .then((_instanceList) => {
79 instanceList = _instanceList;
80 let promises = [];
81 // foreach instance query stats
82 instanceList.forEach((instance) => {
83 promises.push(this.getInstanceStats(instance.instance_uuid));
84 });
85 return $q.all(promises);
86 })
87 .then(stats => {
88 // augment instance with stats information
89 instanceList.map((instance, i) => {
90 instance.stats = stats[i];
91 });
92 deferred.resolve(instanceList);
93 })
94 .catch(deferred.reject);
95
96 return deferred.promise;
97 };
Matteo Scandolof0d6e692016-02-24 11:14:01 -080098
99 this.getContainerStats = (containerName) => {
100 const deferred = $q.defer();
101
102 let res = {};
103
104 $http.get('/xoslib/meterstatistics', {params: {'resource': containerName}})
105 .then((containerStats) => {
106 res.stats = containerStats.data;
107 return $http.get('/xoslib/meterstatistics', {params: {'resource': `${containerName}-eth0`}})
108 })
109 .then((portStats) => {
110 res.port = {
111 eth0: portStats.data
112 };
113 return $http.get('/xoslib/meterstatistics', {params: {'resource': `${containerName}-eth1`}})
114 })
115 .then((portStats) => {
116 res.port.eth1 = portStats.data;
117 deferred.resolve(res);
118 })
119 .catch((e) => {
120 deferred.reject(e);
121 })
122
123 return deferred.promise;
124 }
Matteo Scandolof16b3792016-01-21 14:23:28 -0800125 })
Matteo Scandolo06f45d62016-01-21 15:38:06 -0800126 .service('Slice', function($resource){
127 return $resource('/xos/slices', {id: '@id'});
128 })
129 .service('Instances', function($resource){
Matteo Scandolo51031482016-02-17 13:54:11 -0800130 return $resource('/xos/instances/:id', {id: '@id'});
Matteo Scandolo170d3be2016-02-11 08:58:04 -0800131 })
132 .service('Node', function($resource, $q, Instances){
133 return $resource('/xos/nodes', {id: '@id'}, {
134 queryWithInstances: {
135 method: 'GET',
136 isArray: true,
137 interceptor: {
138 response: function(res){
139
140 // TODO update the API to include instances in nodes
141 // http://stackoverflow.com/questions/14573102/how-do-i-include-related-model-fields-using-django-rest-framework
142
143 const deferred = $q.defer();
144
145 let requests = [];
146
147 angular.forEach(res.data, (node) => {
148 requests.push(Instances.query({node: node.id}).$promise);
149 })
150
151 $q.all(requests)
152 .then((list) => {
153 res.data.map((node, i) => {
154 node.instances = list[i];
155 return node;
156 });
Matteo Scandolo170d3be2016-02-11 08:58:04 -0800157 deferred.resolve(res.data);
158 })
159
160 return deferred.promise;
161 }
162 }
163 }
164 });
Matteo Scandolo06f45d62016-01-21 15:38:06 -0800165 })
Matteo Scandolo7547f042016-02-09 09:13:30 -0800166 .service('Subscribers', function($resource, $q, SubscriberDevice){
Matteo Scandolo45fba732016-02-22 14:53:44 -0800167 return $resource('/xos/subscribers/:id', {id: '@id'}, {
Matteo Scandolo7547f042016-02-09 09:13:30 -0800168 queryWithDevices: {
169 method: 'GET',
170 isArray: true,
171 interceptor: {
172 response: function(res){
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800173
174 /**
175 * For each subscriber retrieve devices and append them
176 */
177
Matteo Scandolo45fba732016-02-22 14:53:44 -0800178 let deferred = $q.defer();
Matteo Scandolo7547f042016-02-09 09:13:30 -0800179
180 let requests = [];
181
182 angular.forEach(res.data, (subscriber) => {
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800183 requests.push(SubscriberDevice.query({id: subscriber.id}).$promise);
Matteo Scandolo7547f042016-02-09 09:13:30 -0800184 })
185
186 $q.all(requests)
187 .then((list) => {
Matteo Scandolo7e67a9a2016-02-16 16:33:26 -0800188
189 // adding devices
190
Matteo Scandolo7547f042016-02-09 09:13:30 -0800191 res.data.map((subscriber, i) => {
192 subscriber.devices = list[i];
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800193 subscriber.type = 'subscriber';
194
195 subscriber.devices.map(d => d.type = 'device')
196
Matteo Scandolo7547f042016-02-09 09:13:30 -0800197 return subscriber;
198 });
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800199
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800200 // faking to have 2 subscriber
Matteo Scandolofd468582016-02-16 16:03:43 -0800201 // res.data.push(angular.copy(res.data[0]));
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800202
Matteo Scandolo7547f042016-02-09 09:13:30 -0800203 deferred.resolve(res.data);
204 })
205
206 return deferred.promise;
207 }
208 }
Matteo Scandolo45fba732016-02-22 14:53:44 -0800209 },
210 getWithDevices: {
211 method: 'GET',
212 isArray: false,
213 interceptor: {
214 response: (res) => {
215 let d = $q.defer();
216
217 SubscriberDevice.query({id: res.data.id}).$promise
218 .then(devices => {
219 devices.map(d => d.type = 'device');
220 res.data.devices = devices;
221 res.data.type = 'subscriber';
Matteo Scandolo45fba732016-02-22 14:53:44 -0800222 d.resolve(res.data);
223 })
224 .catch(err => {
225 d.reject(err);
226 });
227
228 return d.promise;
229 }
230 }
Matteo Scandolo7547f042016-02-09 09:13:30 -0800231 }
232 });
233 })
234 .service('SubscriberDevice', function($resource){
235 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -0800236 })
Matteo Scandolo071ef462016-01-25 12:00:42 -0800237 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -0800238
Matteo Scandolof2c99012016-01-25 10:10:38 -0800239 // count the mas depth of an object
240 const depthOf = (obj) => {
241 var depth = 0;
242 if (obj.children) {
243 obj.children.forEach(function (d) {
244 var tmpDepth = depthOf(d);
245 if (tmpDepth > depth) {
246 depth = tmpDepth
247 }
248 })
249 }
250 return 1 + depth
251 };
252
Matteo Scandolof16b3792016-01-21 14:23:28 -0800253 // find all the relation defined for a given root
254 const findLevelRelation = (tenants, rootId) => {
255 return lodash.filter(tenants, service => {
256 return service.subscriber_service === rootId;
257 });
258 };
259
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800260 const findSpecificInformation = (tenants, rootId) => {
261 var tenants = lodash.filter(tenants, service => {
262 return service.provider_service === rootId && service.subscriber_tenant;
263 });
264
265 var info;
266
267 tenants.forEach((tenant) => {
268 if(tenant.service_specific_attribute){
269 info = JSON.parse(tenant.service_specific_attribute);
270 }
271 });
272
273 return info;
274 };
275
Matteo Scandolof16b3792016-01-21 14:23:28 -0800276 // find all the service defined by a given array of relations
277 const findLevelServices = (relations, services) => {
278 const levelServices = [];
279 lodash.forEach(relations, (tenant) => {
280 var service = lodash.find(services, {id: tenant.provider_service});
281 levelServices.push(service);
282 });
283 return levelServices;
284 };
285
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800286 const buildLevel = (tenants, services, rootService, rootTenant, parentName = null) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800287
Matteo Scandolof16b3792016-01-21 14:23:28 -0800288 // build an array of unlinked services
289 // these are the services that should still placed in the tree
290 var unlinkedServices = lodash.difference(services, [rootService]);
291
292 // find all relations relative to this rootElement
293 const levelRelation = findLevelRelation(tenants, rootService.id);
Matteo Scandolof16b3792016-01-21 14:23:28 -0800294 // find all items related to rootElement
295 const levelServices = findLevelServices(levelRelation, services);
296
297 // remove this item from the list (performance
298 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
299
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800300 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
301
302 const tree = {
303 name: rootService.humanReadableName,
304 parent: parentName,
305 type: 'service',
306 service: rootService,
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800307 tenant: rootTenant,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800308 children: []
309 };
310
Matteo Scandolof16b3792016-01-21 14:23:28 -0800311 lodash.forEach(levelServices, (service) => {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800312 let tenant = lodash.find(tenants, {subscriber_tenant: rootTenant.id, provider_service: service.id});
313 tree.children.push(buildLevel(tenants, unlinkedServices, service, tenant, rootService.humanReadableName));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800314 });
315
Matteo Scandolof2c99012016-01-25 10:10:38 -0800316 // if it is the last element append internet
317 if(tree.children.length === 0){
318 tree.children.push({
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800319 name: 'Router',
320 type: 'router',
Matteo Scandolof2c99012016-01-25 10:10:38 -0800321 children: []
322 });
323 }
324
Matteo Scandolof16b3792016-01-21 14:23:28 -0800325 return tree;
326 };
327
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800328 const buildSubscriberServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800329
330 // find the root service
331 // it is the one attached to subsriber_root
332 // as now we have only one root so this can work
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800333 const rootTenant = lodash.find(tenants, {subscriber_root: subscriber.id});
334 const rootService = lodash.find(services, {id: rootTenant.provider_service});
Matteo Scandolof16b3792016-01-21 14:23:28 -0800335
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800336 const serviceTree = buildLevel(tenants, services, rootService, rootTenant);
Matteo Scandolof16b3792016-01-21 14:23:28 -0800337
Matteo Scandoloff7df762016-01-22 16:36:34 -0800338 return {
339 name: subscriber.name,
340 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800341 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800342 children: [serviceTree]
343 };
344
Matteo Scandolof16b3792016-01-21 14:23:28 -0800345 };
346
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800347 // applying domain knowledge to build the global service tree
348 const buildServiceTree = (services, tenants) => {
349
350 // TODO refactor
351 const buildChild = (services, tenants, currentService) => {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800352
353 const response = {
354 type: 'service',
355 name: currentService.humanReadableName,
356 service: currentService
357 };
358
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800359 let tenant = lodash.find(tenants, {subscriber_service: currentService.id});
360 if(tenant){
361 let next = lodash.find(services, {id: tenant.provider_service});
Matteo Scandolo45fba732016-02-22 14:53:44 -0800362 response.children = [buildChild(services, tenants, next)];
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800363 }
364 else {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800365 response.children = [
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800366 {
367 name: 'Router',
368 type: 'router',
369 children: []
370 }
371 ]
372 }
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800373 delete currentService.id; // conflict with d3
Matteo Scandolo45fba732016-02-22 14:53:44 -0800374 return response;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800375 }
376 let baseService = lodash.find(services, {id: 3});
377
378 const baseData = {
379 name: 'Subscriber',
380 type: 'subscriber',
381 parent: null,
382 children: [buildChild(services, tenants, baseService)]
383 };
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800384 return baseData;
385 };
386
387 const getBySubscriber = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800388 var deferred = $q.defer();
389 var services, tenants;
390 Services.query().$promise
391 .then((res) => {
392 services = res;
393 return Tenant.query().$promise;
394 })
395 .then((res) => {
396 tenants = res;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800397 deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800398 })
399 .catch((e) => {
400 throw new Error(e);
401 });
402
403 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800404 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800405
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800406 const get = () => {
407 var deferred = $q.defer();
408 var services, tenants;
409 Services.query().$promise
410 .then((res) => {
411 services = res;
412 return Tenant.query({kind: 'coarse'}).$promise;
413 })
414 .then((res) => {
415 tenants = res;
416 deferred.resolve(buildServiceTree(services, tenants));
417 })
418 .catch((e) => {
419 throw new Error(e);
420 });
421
422 return deferred.promise;
423 }
424
Matteo Scandolof2c99012016-01-25 10:10:38 -0800425 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800426 return {
427 get: get,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800428 buildServiceTree: buildServiceTree,
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800429 getBySubscriber: getBySubscriber,
430 buildLevel: buildLevel,
431 buildSubscriberServiceTree: buildSubscriberServiceTree,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800432 findLevelRelation: findLevelRelation,
433 findLevelServices: findLevelServices,
434 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800435 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800436 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800437 });
438
439}());