blob: 669d02808c4ad48219f07fdd97d5972ed44da31f [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 Scandolo7547f042016-02-09 09:13:30 -080017 .service('Subscribers', function($resource, $q, SubscriberDevice){
18 return $resource('/xos/subscribers', {id: '@id'}, {
19 queryWithDevices: {
20 method: 'GET',
21 isArray: true,
22 interceptor: {
23 response: function(res){
24 const deferred = $q.defer();
25
26 let requests = [];
27
28 angular.forEach(res.data, (subscriber) => {
29 requests.push(SubscriberDevice.query({id: subscriber.id}));
30 })
31
32 $q.all(requests)
33 .then((list) => {
34 console.log(list);
35 res.data.map((subscriber, i) => {
36 subscriber.devices = list[i];
37 return subscriber;
38 });
39 deferred.resolve(res.data);
40 })
41
42 return deferred.promise;
43 }
44 }
45 }
46 });
47 })
48 .service('SubscriberDevice', function($resource){
49 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -080050 })
Matteo Scandolo071ef462016-01-25 12:00:42 -080051 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -080052
Matteo Scandolof2c99012016-01-25 10:10:38 -080053 // count the mas depth of an object
54 const depthOf = (obj) => {
55 var depth = 0;
56 if (obj.children) {
57 obj.children.forEach(function (d) {
58 var tmpDepth = depthOf(d);
59 if (tmpDepth > depth) {
60 depth = tmpDepth
61 }
62 })
63 }
64 return 1 + depth
65 };
66
Matteo Scandolof16b3792016-01-21 14:23:28 -080067 // find all the relation defined for a given root
68 const findLevelRelation = (tenants, rootId) => {
69 return lodash.filter(tenants, service => {
70 return service.subscriber_service === rootId;
71 });
72 };
73
Matteo Scandoloc9ebd922016-01-28 12:02:57 -080074 const findSpecificInformation = (tenants, rootId) => {
75 var tenants = lodash.filter(tenants, service => {
76 return service.provider_service === rootId && service.subscriber_tenant;
77 });
78
79 var info;
80
81 tenants.forEach((tenant) => {
82 if(tenant.service_specific_attribute){
83 info = JSON.parse(tenant.service_specific_attribute);
84 }
85 });
86
87 return info;
88 };
89
Matteo Scandolof16b3792016-01-21 14:23:28 -080090 // find all the service defined by a given array of relations
91 const findLevelServices = (relations, services) => {
92 const levelServices = [];
93 lodash.forEach(relations, (tenant) => {
94 var service = lodash.find(services, {id: tenant.provider_service});
95 levelServices.push(service);
96 });
97 return levelServices;
98 };
99
100 const buildLevel = (tenants, services, rootService, parentName = null) => {
101
Matteo Scandolof16b3792016-01-21 14:23:28 -0800102 // build an array of unlinked services
103 // these are the services that should still placed in the tree
104 var unlinkedServices = lodash.difference(services, [rootService]);
105
106 // find all relations relative to this rootElement
107 const levelRelation = findLevelRelation(tenants, rootService.id);
108
109 // find all items related to rootElement
110 const levelServices = findLevelServices(levelRelation, services);
111
112 // remove this item from the list (performance
113 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
114
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800115 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
116
117 const tree = {
118 name: rootService.humanReadableName,
119 parent: parentName,
120 type: 'service',
121 service: rootService,
122 children: []
123 };
124
Matteo Scandolof16b3792016-01-21 14:23:28 -0800125 lodash.forEach(levelServices, (service) => {
126 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
127 });
128
Matteo Scandolof2c99012016-01-25 10:10:38 -0800129 // if it is the last element append internet
130 if(tree.children.length === 0){
131 tree.children.push({
132 name: 'Internet',
133 type: 'internet',
134 children: []
135 });
136 }
137
Matteo Scandolof16b3792016-01-21 14:23:28 -0800138 return tree;
139 };
140
Matteo Scandolocb12a1a2016-01-25 14:11:10 -0800141 const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800142
143 // find the root service
144 // it is the one attached to subsriber_root
145 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -0800146 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -0800147 const rootService = lodash.find(services, {id: rootServiceId});
148
149 const serviceTree = buildLevel(tenants, services, rootService);
150
Matteo Scandoloff7df762016-01-22 16:36:34 -0800151 return {
152 name: subscriber.name,
153 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800154 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800155 children: [serviceTree]
156 };
157
Matteo Scandolof16b3792016-01-21 14:23:28 -0800158 };
159
Matteo Scandoloff7df762016-01-22 16:36:34 -0800160 const get = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800161 var deferred = $q.defer();
162 var services, tenants;
163 Services.query().$promise
164 .then((res) => {
165 services = res;
166 return Tenant.query().$promise;
167 })
168 .then((res) => {
169 tenants = res;
Matteo Scandoloff7df762016-01-22 16:36:34 -0800170 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800171 })
172 .catch((e) => {
173 throw new Error(e);
174 });
175
176 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800177 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800178
Matteo Scandolof2c99012016-01-25 10:10:38 -0800179 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800180 return {
181 get: get,
182 buildLevel: buildLevel,
183 buildServiceTree: buildServiceTree,
184 findLevelRelation: findLevelRelation,
185 findLevelServices: findLevelServices,
186 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800187 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800188 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800189 });
190
191}());