blob: 60a20d25fb5bdc33d2abddfdaf6508de79ab578b [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 Scandolo071ef462016-01-25 12:00:42 -080020 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -080021
Matteo Scandolof2c99012016-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 Scandolof16b3792016-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
Matteo Scandoloc9ebd922016-01-28 12:02:57 -080043 const findSpecificInformation = (tenants, rootId) => {
44 var tenants = lodash.filter(tenants, service => {
45 return service.provider_service === rootId && service.subscriber_tenant;
46 });
47
48 var info;
49
50 tenants.forEach((tenant) => {
51 if(tenant.service_specific_attribute){
52 info = JSON.parse(tenant.service_specific_attribute);
53 }
54 });
55
56 return info;
57 };
58
Matteo Scandolof16b3792016-01-21 14:23:28 -080059 // find all the service defined by a given array of relations
60 const findLevelServices = (relations, services) => {
61 const levelServices = [];
62 lodash.forEach(relations, (tenant) => {
63 var service = lodash.find(services, {id: tenant.provider_service});
64 levelServices.push(service);
65 });
66 return levelServices;
67 };
68
69 const buildLevel = (tenants, services, rootService, parentName = null) => {
70
Matteo Scandolof16b3792016-01-21 14:23:28 -080071 // build an array of unlinked services
72 // these are the services that should still placed in the tree
73 var unlinkedServices = lodash.difference(services, [rootService]);
74
75 // find all relations relative to this rootElement
76 const levelRelation = findLevelRelation(tenants, rootService.id);
77
78 // find all items related to rootElement
79 const levelServices = findLevelServices(levelRelation, services);
80
81 // remove this item from the list (performance
82 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
83
Matteo Scandoloc9ebd922016-01-28 12:02:57 -080084 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
85
86 const tree = {
87 name: rootService.humanReadableName,
88 parent: parentName,
89 type: 'service',
90 service: rootService,
91 children: []
92 };
93
Matteo Scandolof16b3792016-01-21 14:23:28 -080094 lodash.forEach(levelServices, (service) => {
95 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
96 });
97
Matteo Scandolof2c99012016-01-25 10:10:38 -080098 // if it is the last element append internet
99 if(tree.children.length === 0){
100 tree.children.push({
101 name: 'Internet',
102 type: 'internet',
103 children: []
104 });
105 }
106
Matteo Scandolof16b3792016-01-21 14:23:28 -0800107 return tree;
108 };
109
Matteo Scandolocb12a1a2016-01-25 14:11:10 -0800110 const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800111
112 // find the root service
113 // it is the one attached to subsriber_root
114 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -0800115 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -0800116 const rootService = lodash.find(services, {id: rootServiceId});
117
118 const serviceTree = buildLevel(tenants, services, rootService);
119
Matteo Scandoloff7df762016-01-22 16:36:34 -0800120 return {
121 name: subscriber.name,
122 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800123 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800124 children: [serviceTree]
125 };
126
Matteo Scandolof16b3792016-01-21 14:23:28 -0800127 };
128
Matteo Scandoloff7df762016-01-22 16:36:34 -0800129 const get = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800130 var deferred = $q.defer();
131 var services, tenants;
132 Services.query().$promise
133 .then((res) => {
134 services = res;
135 return Tenant.query().$promise;
136 })
137 .then((res) => {
138 tenants = res;
Matteo Scandoloff7df762016-01-22 16:36:34 -0800139 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800140 })
141 .catch((e) => {
142 throw new Error(e);
143 });
144
145 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800146 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800147
Matteo Scandolof2c99012016-01-25 10:10:38 -0800148 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800149 return {
150 get: get,
151 buildLevel: buildLevel,
152 buildServiceTree: buildServiceTree,
153 findLevelRelation: findLevelRelation,
154 findLevelServices: findLevelServices,
155 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800156 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800157 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800158 });
159
160}());