blob: 877b4855665653a09ebc1120c518a24caa0d2f2e [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 Scandoloc49ff702016-02-17 15:11:33 -080052 $http.get('/xoslib/meterstatistics', {params:{resource: 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 Scandolof16b3792016-01-21 14:23:28 -080098 })
Matteo Scandolo06f45d62016-01-21 15:38:06 -080099 .service('Slice', function($resource){
100 return $resource('/xos/slices', {id: '@id'});
101 })
102 .service('Instances', function($resource){
Matteo Scandolo51031482016-02-17 13:54:11 -0800103 return $resource('/xos/instances/:id', {id: '@id'});
Matteo Scandolo170d3be2016-02-11 08:58:04 -0800104 })
105 .service('Node', function($resource, $q, Instances){
106 return $resource('/xos/nodes', {id: '@id'}, {
107 queryWithInstances: {
108 method: 'GET',
109 isArray: true,
110 interceptor: {
111 response: function(res){
112
113 // TODO update the API to include instances in nodes
114 // http://stackoverflow.com/questions/14573102/how-do-i-include-related-model-fields-using-django-rest-framework
115
116 const deferred = $q.defer();
117
118 let requests = [];
119
120 angular.forEach(res.data, (node) => {
121 requests.push(Instances.query({node: node.id}).$promise);
122 })
123
124 $q.all(requests)
125 .then((list) => {
126 res.data.map((node, i) => {
127 node.instances = list[i];
128 return node;
129 });
Matteo Scandolo170d3be2016-02-11 08:58:04 -0800130 deferred.resolve(res.data);
131 })
132
133 return deferred.promise;
134 }
135 }
136 }
137 });
Matteo Scandolo06f45d62016-01-21 15:38:06 -0800138 })
Matteo Scandolo7547f042016-02-09 09:13:30 -0800139 .service('Subscribers', function($resource, $q, SubscriberDevice){
Matteo Scandolo45fba732016-02-22 14:53:44 -0800140 return $resource('/xos/subscribers/:id', {id: '@id'}, {
Matteo Scandolo7547f042016-02-09 09:13:30 -0800141 queryWithDevices: {
142 method: 'GET',
143 isArray: true,
144 interceptor: {
145 response: function(res){
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800146
147 /**
148 * For each subscriber retrieve devices and append them
149 */
150
Matteo Scandolo45fba732016-02-22 14:53:44 -0800151 let deferred = $q.defer();
Matteo Scandolo7547f042016-02-09 09:13:30 -0800152
153 let requests = [];
154
155 angular.forEach(res.data, (subscriber) => {
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800156 requests.push(SubscriberDevice.query({id: subscriber.id}).$promise);
Matteo Scandolo7547f042016-02-09 09:13:30 -0800157 })
158
159 $q.all(requests)
160 .then((list) => {
Matteo Scandolo7e67a9a2016-02-16 16:33:26 -0800161
162 // adding devices
163
Matteo Scandolo7547f042016-02-09 09:13:30 -0800164 res.data.map((subscriber, i) => {
165 subscriber.devices = list[i];
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800166 subscriber.type = 'subscriber';
167
168 subscriber.devices.map(d => d.type = 'device')
169
Matteo Scandolo7547f042016-02-09 09:13:30 -0800170 return subscriber;
171 });
Matteo Scandolo219b1a72016-02-09 11:19:22 -0800172
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800173 // faking to have 2 subscriber
Matteo Scandolofd468582016-02-16 16:03:43 -0800174 // res.data.push(angular.copy(res.data[0]));
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800175
Matteo Scandolo7547f042016-02-09 09:13:30 -0800176 deferred.resolve(res.data);
177 })
178
179 return deferred.promise;
180 }
181 }
Matteo Scandolo45fba732016-02-22 14:53:44 -0800182 },
183 getWithDevices: {
184 method: 'GET',
185 isArray: false,
186 interceptor: {
187 response: (res) => {
188 let d = $q.defer();
189
190 SubscriberDevice.query({id: res.data.id}).$promise
191 .then(devices => {
192 devices.map(d => d.type = 'device');
193 res.data.devices = devices;
194 res.data.type = 'subscriber';
195 console.log(res.data);
196 d.resolve(res.data);
197 })
198 .catch(err => {
199 d.reject(err);
200 });
201
202 return d.promise;
203 }
204 }
Matteo Scandolo7547f042016-02-09 09:13:30 -0800205 }
206 });
207 })
208 .service('SubscriberDevice', function($resource){
209 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -0800210 })
Matteo Scandolo071ef462016-01-25 12:00:42 -0800211 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -0800212
Matteo Scandolof2c99012016-01-25 10:10:38 -0800213 // count the mas depth of an object
214 const depthOf = (obj) => {
215 var depth = 0;
216 if (obj.children) {
217 obj.children.forEach(function (d) {
218 var tmpDepth = depthOf(d);
219 if (tmpDepth > depth) {
220 depth = tmpDepth
221 }
222 })
223 }
224 return 1 + depth
225 };
226
Matteo Scandolof16b3792016-01-21 14:23:28 -0800227 // find all the relation defined for a given root
228 const findLevelRelation = (tenants, rootId) => {
229 return lodash.filter(tenants, service => {
230 return service.subscriber_service === rootId;
231 });
232 };
233
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800234 const findSpecificInformation = (tenants, rootId) => {
235 var tenants = lodash.filter(tenants, service => {
236 return service.provider_service === rootId && service.subscriber_tenant;
237 });
238
239 var info;
240
241 tenants.forEach((tenant) => {
242 if(tenant.service_specific_attribute){
243 info = JSON.parse(tenant.service_specific_attribute);
244 }
245 });
246
247 return info;
248 };
249
Matteo Scandolof16b3792016-01-21 14:23:28 -0800250 // find all the service defined by a given array of relations
251 const findLevelServices = (relations, services) => {
252 const levelServices = [];
253 lodash.forEach(relations, (tenant) => {
254 var service = lodash.find(services, {id: tenant.provider_service});
255 levelServices.push(service);
256 });
257 return levelServices;
258 };
259
260 const buildLevel = (tenants, services, rootService, parentName = null) => {
261
Matteo Scandolof16b3792016-01-21 14:23:28 -0800262 // build an array of unlinked services
263 // these are the services that should still placed in the tree
264 var unlinkedServices = lodash.difference(services, [rootService]);
265
266 // find all relations relative to this rootElement
267 const levelRelation = findLevelRelation(tenants, rootService.id);
268
269 // find all items related to rootElement
270 const levelServices = findLevelServices(levelRelation, services);
271
272 // remove this item from the list (performance
273 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
274
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800275 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
276
277 const tree = {
278 name: rootService.humanReadableName,
279 parent: parentName,
280 type: 'service',
281 service: rootService,
282 children: []
283 };
284
Matteo Scandolof16b3792016-01-21 14:23:28 -0800285 lodash.forEach(levelServices, (service) => {
286 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
287 });
288
Matteo Scandolof2c99012016-01-25 10:10:38 -0800289 // if it is the last element append internet
290 if(tree.children.length === 0){
291 tree.children.push({
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800292 name: 'Router',
293 type: 'router',
Matteo Scandolof2c99012016-01-25 10:10:38 -0800294 children: []
295 });
296 }
297
Matteo Scandolof16b3792016-01-21 14:23:28 -0800298 return tree;
299 };
300
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800301 const buildSubscriberServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800302
303 // find the root service
304 // it is the one attached to subsriber_root
305 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -0800306 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -0800307 const rootService = lodash.find(services, {id: rootServiceId});
308
309 const serviceTree = buildLevel(tenants, services, rootService);
310
Matteo Scandoloff7df762016-01-22 16:36:34 -0800311 return {
312 name: subscriber.name,
313 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800314 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800315 children: [serviceTree]
316 };
317
Matteo Scandolof16b3792016-01-21 14:23:28 -0800318 };
319
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800320 // applying domain knowledge to build the global service tree
321 const buildServiceTree = (services, tenants) => {
322
323 // TODO refactor
324 const buildChild = (services, tenants, currentService) => {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800325
326 const response = {
327 type: 'service',
328 name: currentService.humanReadableName,
329 service: currentService
330 };
331
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800332 let tenant = lodash.find(tenants, {subscriber_service: currentService.id});
333 if(tenant){
334 let next = lodash.find(services, {id: tenant.provider_service});
Matteo Scandolo45fba732016-02-22 14:53:44 -0800335 response.children = [buildChild(services, tenants, next)];
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800336 }
337 else {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800338 response.children = [
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800339 {
340 name: 'Router',
341 type: 'router',
342 children: []
343 }
344 ]
345 }
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800346 delete currentService.id; // conflict with d3
Matteo Scandolo45fba732016-02-22 14:53:44 -0800347 return response;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800348 }
349 let baseService = lodash.find(services, {id: 3});
350
351 const baseData = {
352 name: 'Subscriber',
353 type: 'subscriber',
354 parent: null,
355 children: [buildChild(services, tenants, baseService)]
356 };
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800357 return baseData;
358 };
359
360 const getBySubscriber = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800361 var deferred = $q.defer();
362 var services, tenants;
363 Services.query().$promise
364 .then((res) => {
365 services = res;
366 return Tenant.query().$promise;
367 })
368 .then((res) => {
369 tenants = res;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800370 deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800371 })
372 .catch((e) => {
373 throw new Error(e);
374 });
375
376 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800377 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800378
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800379 const get = () => {
380 var deferred = $q.defer();
381 var services, tenants;
382 Services.query().$promise
383 .then((res) => {
384 services = res;
385 return Tenant.query({kind: 'coarse'}).$promise;
386 })
387 .then((res) => {
388 tenants = res;
389 deferred.resolve(buildServiceTree(services, tenants));
390 })
391 .catch((e) => {
392 throw new Error(e);
393 });
394
395 return deferred.promise;
396 }
397
Matteo Scandolof2c99012016-01-25 10:10:38 -0800398 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800399 return {
400 get: get,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800401 buildServiceTree: buildServiceTree,
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800402 getBySubscriber: getBySubscriber,
403 buildLevel: buildLevel,
404 buildSubscriberServiceTree: buildSubscriberServiceTree,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800405 findLevelRelation: findLevelRelation,
406 findLevelServices: findLevelServices,
407 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800408 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800409 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800410 });
411
412}());