blob: 522a12601e57abcb68541c408e712ce752c51063 [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';
Matteo Scandolo45fba732016-02-22 14:53:44 -0800195 d.resolve(res.data);
196 })
197 .catch(err => {
198 d.reject(err);
199 });
200
201 return d.promise;
202 }
203 }
Matteo Scandolo7547f042016-02-09 09:13:30 -0800204 }
205 });
206 })
207 .service('SubscriberDevice', function($resource){
208 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -0800209 })
Matteo Scandolo071ef462016-01-25 12:00:42 -0800210 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -0800211
Matteo Scandolof2c99012016-01-25 10:10:38 -0800212 // count the mas depth of an object
213 const depthOf = (obj) => {
214 var depth = 0;
215 if (obj.children) {
216 obj.children.forEach(function (d) {
217 var tmpDepth = depthOf(d);
218 if (tmpDepth > depth) {
219 depth = tmpDepth
220 }
221 })
222 }
223 return 1 + depth
224 };
225
Matteo Scandolof16b3792016-01-21 14:23:28 -0800226 // find all the relation defined for a given root
227 const findLevelRelation = (tenants, rootId) => {
228 return lodash.filter(tenants, service => {
229 return service.subscriber_service === rootId;
230 });
231 };
232
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800233 const findSpecificInformation = (tenants, rootId) => {
234 var tenants = lodash.filter(tenants, service => {
235 return service.provider_service === rootId && service.subscriber_tenant;
236 });
237
238 var info;
239
240 tenants.forEach((tenant) => {
241 if(tenant.service_specific_attribute){
242 info = JSON.parse(tenant.service_specific_attribute);
243 }
244 });
245
246 return info;
247 };
248
Matteo Scandolof16b3792016-01-21 14:23:28 -0800249 // find all the service defined by a given array of relations
250 const findLevelServices = (relations, services) => {
251 const levelServices = [];
252 lodash.forEach(relations, (tenant) => {
253 var service = lodash.find(services, {id: tenant.provider_service});
254 levelServices.push(service);
255 });
256 return levelServices;
257 };
258
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800259 const buildLevel = (tenants, services, rootService, rootTenant, parentName = null) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800260
Matteo Scandolof16b3792016-01-21 14:23:28 -0800261 // build an array of unlinked services
262 // these are the services that should still placed in the tree
263 var unlinkedServices = lodash.difference(services, [rootService]);
264
265 // find all relations relative to this rootElement
266 const levelRelation = findLevelRelation(tenants, rootService.id);
267
268 // find all items related to rootElement
269 const levelServices = findLevelServices(levelRelation, services);
270
271 // remove this item from the list (performance
272 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
273
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800274 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
275
276 const tree = {
277 name: rootService.humanReadableName,
278 parent: parentName,
279 type: 'service',
280 service: rootService,
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800281 tenant: rootTenant,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800282 children: []
283 };
284
Matteo Scandolof16b3792016-01-21 14:23:28 -0800285 lodash.forEach(levelServices, (service) => {
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800286 let tenant = lodash.find(tenants, {subscriber_tenant: rootTenant.id, provider_service: service.id});
287 tree.children.push(buildLevel(tenants, unlinkedServices, service, tenant, rootService.humanReadableName));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800288 });
289
Matteo Scandolof2c99012016-01-25 10:10:38 -0800290 // if it is the last element append internet
291 if(tree.children.length === 0){
292 tree.children.push({
Matteo Scandolo9fe01af2016-02-09 16:01:49 -0800293 name: 'Router',
294 type: 'router',
Matteo Scandolof2c99012016-01-25 10:10:38 -0800295 children: []
296 });
297 }
298
Matteo Scandolof16b3792016-01-21 14:23:28 -0800299 return tree;
300 };
301
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800302 const buildSubscriberServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800303
304 // find the root service
305 // it is the one attached to subsriber_root
306 // as now we have only one root so this can work
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800307 const rootTenant = lodash.find(tenants, {subscriber_root: subscriber.id});
308 const rootService = lodash.find(services, {id: rootTenant.provider_service});
Matteo Scandolof16b3792016-01-21 14:23:28 -0800309
Matteo Scandolo012dddb2016-02-22 16:53:22 -0800310 const serviceTree = buildLevel(tenants, services, rootService, rootTenant);
Matteo Scandolof16b3792016-01-21 14:23:28 -0800311
Matteo Scandoloff7df762016-01-22 16:36:34 -0800312 return {
313 name: subscriber.name,
314 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800315 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800316 children: [serviceTree]
317 };
318
Matteo Scandolof16b3792016-01-21 14:23:28 -0800319 };
320
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800321 // applying domain knowledge to build the global service tree
322 const buildServiceTree = (services, tenants) => {
323
324 // TODO refactor
325 const buildChild = (services, tenants, currentService) => {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800326
327 const response = {
328 type: 'service',
329 name: currentService.humanReadableName,
330 service: currentService
331 };
332
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800333 let tenant = lodash.find(tenants, {subscriber_service: currentService.id});
334 if(tenant){
335 let next = lodash.find(services, {id: tenant.provider_service});
Matteo Scandolo45fba732016-02-22 14:53:44 -0800336 response.children = [buildChild(services, tenants, next)];
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800337 }
338 else {
Matteo Scandolo45fba732016-02-22 14:53:44 -0800339 response.children = [
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800340 {
341 name: 'Router',
342 type: 'router',
343 children: []
344 }
345 ]
346 }
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800347 delete currentService.id; // conflict with d3
Matteo Scandolo45fba732016-02-22 14:53:44 -0800348 return response;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800349 }
350 let baseService = lodash.find(services, {id: 3});
351
352 const baseData = {
353 name: 'Subscriber',
354 type: 'subscriber',
355 parent: null,
356 children: [buildChild(services, tenants, baseService)]
357 };
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800358 return baseData;
359 };
360
361 const getBySubscriber = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800362 var deferred = $q.defer();
363 var services, tenants;
364 Services.query().$promise
365 .then((res) => {
366 services = res;
367 return Tenant.query().$promise;
368 })
369 .then((res) => {
370 tenants = res;
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800371 deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800372 })
373 .catch((e) => {
374 throw new Error(e);
375 });
376
377 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800378 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800379
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800380 const get = () => {
381 var deferred = $q.defer();
382 var services, tenants;
383 Services.query().$promise
384 .then((res) => {
385 services = res;
386 return Tenant.query({kind: 'coarse'}).$promise;
387 })
388 .then((res) => {
389 tenants = res;
390 deferred.resolve(buildServiceTree(services, tenants));
391 })
392 .catch((e) => {
393 throw new Error(e);
394 });
395
396 return deferred.promise;
397 }
398
Matteo Scandolof2c99012016-01-25 10:10:38 -0800399 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800400 return {
401 get: get,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800402 buildServiceTree: buildServiceTree,
Matteo Scandolo77d8fa02016-02-16 17:43:00 -0800403 getBySubscriber: getBySubscriber,
404 buildLevel: buildLevel,
405 buildSubscriberServiceTree: buildSubscriberServiceTree,
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800406 findLevelRelation: findLevelRelation,
407 findLevelServices: findLevelServices,
408 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800409 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800410 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800411 });
412
413}());