blob: e7c6e3feab1775d20b82fb05c44f89da25f79659 [file] [log] [blame]
Matteo Scandolobe9b13d2016-01-21 11:21:03 -08001(function () {
2 'use strict';
3
4 angular.module('xos.serviceTopology')
5 .service('Services', function($resource){
Matteo Scandolo85aad312016-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 Scandolo68236262016-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 Scandolo89276392016-01-22 16:36:34 -080017 .service('Subscribers', function($resource){
18 return $resource('/xos/subscribers', {id: '@id'});
19 })
Matteo Scandolo85aad312016-01-21 14:23:28 -080020 .service('ServiceRelation', function($q, _, lodash, Services, Tenant){
21
Matteo Scandolofb46f5b2016-01-25 10:10:38 -080022 // count the mas depth of an object
23 const depthOf = (obj) => {
24 var depth = 0;
25 if (obj.children) {
26 obj.children.forEach(function (d) {
27 var tmpDepth = depthOf(d);
28 if (tmpDepth > depth) {
29 depth = tmpDepth
30 }
31 })
32 }
33 return 1 + depth
34 };
35
Matteo Scandolo85aad312016-01-21 14:23:28 -080036 // find all the relation defined for a given root
37 const findLevelRelation = (tenants, rootId) => {
38 return lodash.filter(tenants, service => {
39 return service.subscriber_service === rootId;
40 });
41 };
42
43 // find all the service defined by a given array of relations
44 const findLevelServices = (relations, services) => {
45 const levelServices = [];
46 lodash.forEach(relations, (tenant) => {
47 var service = lodash.find(services, {id: tenant.provider_service});
48 levelServices.push(service);
49 });
50 return levelServices;
51 };
52
53 const buildLevel = (tenants, services, rootService, parentName = null) => {
54
55 const tree = {
56 name: rootService.humanReadableName,
57 parent: parentName,
Matteo Scandolofb46f5b2016-01-25 10:10:38 -080058 type: 'service',
Matteo Scandolo148f22b2016-01-22 13:17:33 -080059 service: rootService,
Matteo Scandolo85aad312016-01-21 14:23:28 -080060 children: []
61 };
62
63 // build an array of unlinked services
64 // these are the services that should still placed in the tree
65 var unlinkedServices = lodash.difference(services, [rootService]);
66
67 // find all relations relative to this rootElement
68 const levelRelation = findLevelRelation(tenants, rootService.id);
69
70 // find all items related to rootElement
71 const levelServices = findLevelServices(levelRelation, services);
72
73 // remove this item from the list (performance
74 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
75
76 lodash.forEach(levelServices, (service) => {
77 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
78 });
79
Matteo Scandolofb46f5b2016-01-25 10:10:38 -080080 // if it is the last element append internet
81 if(tree.children.length === 0){
82 tree.children.push({
83 name: 'Internet',
84 type: 'internet',
85 children: []
86 });
87 }
88
Matteo Scandolo85aad312016-01-21 14:23:28 -080089 return tree;
90 };
91
Matteo Scandolo89276392016-01-22 16:36:34 -080092 const buildServiceTree = (services, tenants, subscriber = {id:1, name: 'fakeSubs'}) => {
Matteo Scandolo85aad312016-01-21 14:23:28 -080093
94 // find the root service
95 // it is the one attached to subsriber_root
96 // as now we have only one root so this can work
Matteo Scandolo89276392016-01-22 16:36:34 -080097 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolo85aad312016-01-21 14:23:28 -080098 const rootService = lodash.find(services, {id: rootServiceId});
99
100 const serviceTree = buildLevel(tenants, services, rootService);
101
Matteo Scandolo89276392016-01-22 16:36:34 -0800102 return {
103 name: subscriber.name,
104 parent: null,
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800105 type: 'subscriber',
Matteo Scandolo89276392016-01-22 16:36:34 -0800106 children: [serviceTree]
107 };
108
109 //return serviceTree;
Matteo Scandolo85aad312016-01-21 14:23:28 -0800110 };
111
Matteo Scandolo89276392016-01-22 16:36:34 -0800112 const get = (subscriber) => {
Matteo Scandolo85aad312016-01-21 14:23:28 -0800113 var deferred = $q.defer();
114 var services, tenants;
115 Services.query().$promise
116 .then((res) => {
117 services = res;
118 return Tenant.query().$promise;
119 })
120 .then((res) => {
121 tenants = res;
Matteo Scandolo89276392016-01-22 16:36:34 -0800122 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolo85aad312016-01-21 14:23:28 -0800123 })
124 .catch((e) => {
125 throw new Error(e);
126 });
127
128 return deferred.promise;
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800129 };
Matteo Scandolo89276392016-01-22 16:36:34 -0800130
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800131 // export APIs
Matteo Scandolo89276392016-01-22 16:36:34 -0800132 this.get = get;
133 this.buildLevel = buildLevel;
134 this.buildServiceTree = buildServiceTree;
135 this.findLevelRelation = findLevelRelation;
136 this.findLevelServices = findLevelServices;
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800137 this.depthOf = depthOf;
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800138 });
139
140}());