blob: a39a1a6b894b57a8e6fea7314c12bf877f501269 [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){
9 return $resource('/xos/tenants');
10 })
Matteo Scandolo06f45d62016-01-21 15:38:06 -080011 .service('Slice', function($resource){
12 return $resource('/xos/slices', {id: '@id'});
13 })
14 .service('Instances', function($resource){
15 return $resource('/xos/instances', {id: '@id'});
16 })
Matteo Scandolof16b3792016-01-21 14:23:28 -080017 .service('ServiceRelation', function($q, _, lodash, Services, Tenant){
18
19 // find all the relation defined for a given root
20 const findLevelRelation = (tenants, rootId) => {
21 return lodash.filter(tenants, service => {
22 return service.subscriber_service === rootId;
23 });
24 };
25
26 // find all the service defined by a given array of relations
27 const findLevelServices = (relations, services) => {
28 const levelServices = [];
29 lodash.forEach(relations, (tenant) => {
30 var service = lodash.find(services, {id: tenant.provider_service});
31 levelServices.push(service);
32 });
33 return levelServices;
34 };
35
36 const buildLevel = (tenants, services, rootService, parentName = null) => {
37
Matteo Scandoloe89935b2016-01-22 13:17:33 -080038 console.log(rootService);
39
Matteo Scandolof16b3792016-01-21 14:23:28 -080040 const tree = {
41 name: rootService.humanReadableName,
42 parent: parentName,
Matteo Scandoloe89935b2016-01-22 13:17:33 -080043 service: rootService,
Matteo Scandolof16b3792016-01-21 14:23:28 -080044 children: []
45 };
46
47 // build an array of unlinked services
48 // these are the services that should still placed in the tree
49 var unlinkedServices = lodash.difference(services, [rootService]);
50
51 // find all relations relative to this rootElement
52 const levelRelation = findLevelRelation(tenants, rootService.id);
53
54 // find all items related to rootElement
55 const levelServices = findLevelServices(levelRelation, services);
56
57 // remove this item from the list (performance
58 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
59
60 lodash.forEach(levelServices, (service) => {
61 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
62 });
63
64 return tree;
65 };
66
67 const buildServiceTree = (services, tenants) => {
68
69 // find the root service
70 // it is the one attached to subsriber_root
71 // as now we have only one root so this can work
72 const rootServiceId = lodash.find(tenants, {subscriber_root: 1}).provider_service;
73 const rootService = lodash.find(services, {id: rootServiceId});
74
75 const serviceTree = buildLevel(tenants, services, rootService);
76
77 return serviceTree;
78 };
79
80 this.get = () => {
81 var deferred = $q.defer();
82 var services, tenants;
83 Services.query().$promise
84 .then((res) => {
85 services = res;
86 return Tenant.query().$promise;
87 })
88 .then((res) => {
89 tenants = res;
90 deferred.resolve(buildServiceTree(services, tenants));
91 })
92 .catch((e) => {
93 throw new Error(e);
94 });
95
96 return deferred.promise;
97 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080098 });
99
100}());