blob: d6d2fa8cdceb8a042749e21001ff8452064b4f1a [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 Scandoloff7df762016-01-22 16:36:34 -080017 .service('Subscribers', function($resource){
18 return $resource('/xos/subscribers', {id: '@id'});
19 })
Matteo Scandolof16b3792016-01-21 14:23:28 -080020 .service('ServiceRelation', function($q, _, lodash, Services, Tenant){
21
22 // find all the relation defined for a given root
23 const findLevelRelation = (tenants, rootId) => {
24 return lodash.filter(tenants, service => {
25 return service.subscriber_service === rootId;
26 });
27 };
28
29 // find all the service defined by a given array of relations
30 const findLevelServices = (relations, services) => {
31 const levelServices = [];
32 lodash.forEach(relations, (tenant) => {
33 var service = lodash.find(services, {id: tenant.provider_service});
34 levelServices.push(service);
35 });
36 return levelServices;
37 };
38
39 const buildLevel = (tenants, services, rootService, parentName = null) => {
40
41 const tree = {
42 name: rootService.humanReadableName,
43 parent: parentName,
Matteo Scandoloe89935b2016-01-22 13:17:33 -080044 service: rootService,
Matteo Scandolof16b3792016-01-21 14:23:28 -080045 children: []
46 };
47
48 // build an array of unlinked services
49 // these are the services that should still placed in the tree
50 var unlinkedServices = lodash.difference(services, [rootService]);
51
52 // find all relations relative to this rootElement
53 const levelRelation = findLevelRelation(tenants, rootService.id);
54
55 // find all items related to rootElement
56 const levelServices = findLevelServices(levelRelation, services);
57
58 // remove this item from the list (performance
59 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
60
61 lodash.forEach(levelServices, (service) => {
62 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
63 });
64
65 return tree;
66 };
67
Matteo Scandoloff7df762016-01-22 16:36:34 -080068 const buildServiceTree = (services, tenants, subscriber = {id:1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -080069
70 // find the root service
71 // it is the one attached to subsriber_root
72 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -080073 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -080074 const rootService = lodash.find(services, {id: rootServiceId});
75
76 const serviceTree = buildLevel(tenants, services, rootService);
77
Matteo Scandoloff7df762016-01-22 16:36:34 -080078 return {
79 name: subscriber.name,
80 parent: null,
81 children: [serviceTree]
82 };
83
84 //return serviceTree;
Matteo Scandolof16b3792016-01-21 14:23:28 -080085 };
86
Matteo Scandoloff7df762016-01-22 16:36:34 -080087 const get = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -080088 var deferred = $q.defer();
89 var services, tenants;
90 Services.query().$promise
91 .then((res) => {
92 services = res;
93 return Tenant.query().$promise;
94 })
95 .then((res) => {
96 tenants = res;
Matteo Scandoloff7df762016-01-22 16:36:34 -080097 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -080098 })
99 .catch((e) => {
100 throw new Error(e);
101 });
102
103 return deferred.promise;
104 }
Matteo Scandoloff7df762016-01-22 16:36:34 -0800105
106 this.get = get;
107 this.buildLevel = buildLevel;
108 this.buildServiceTree = buildServiceTree;
109 this.findLevelRelation = findLevelRelation;
110 this.findLevelServices = findLevelServices;
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800111 });
112
113}());