blob: be523dafe554c4c34dca509e65faf776a5997a88 [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){
Matteo Scandolo219b1a72016-02-09 11:19:22 -080024
25 /**
26 * For each subscriber retrieve devices and append them
27 */
28
Matteo Scandolo7547f042016-02-09 09:13:30 -080029 const deferred = $q.defer();
30
31 let requests = [];
32
33 angular.forEach(res.data, (subscriber) => {
Matteo Scandolo219b1a72016-02-09 11:19:22 -080034 requests.push(SubscriberDevice.query({id: subscriber.id}).$promise);
Matteo Scandolo7547f042016-02-09 09:13:30 -080035 })
36
37 $q.all(requests)
38 .then((list) => {
Matteo Scandolo7547f042016-02-09 09:13:30 -080039 res.data.map((subscriber, i) => {
40 subscriber.devices = list[i];
41 return subscriber;
42 });
Matteo Scandolo219b1a72016-02-09 11:19:22 -080043
Matteo Scandolo7547f042016-02-09 09:13:30 -080044 deferred.resolve(res.data);
45 })
46
47 return deferred.promise;
48 }
49 }
50 }
51 });
52 })
53 .service('SubscriberDevice', function($resource){
54 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -080055 })
Matteo Scandolo071ef462016-01-25 12:00:42 -080056 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -080057
Matteo Scandolof2c99012016-01-25 10:10:38 -080058 // count the mas depth of an object
59 const depthOf = (obj) => {
60 var depth = 0;
61 if (obj.children) {
62 obj.children.forEach(function (d) {
63 var tmpDepth = depthOf(d);
64 if (tmpDepth > depth) {
65 depth = tmpDepth
66 }
67 })
68 }
69 return 1 + depth
70 };
71
Matteo Scandolof16b3792016-01-21 14:23:28 -080072 // find all the relation defined for a given root
73 const findLevelRelation = (tenants, rootId) => {
74 return lodash.filter(tenants, service => {
75 return service.subscriber_service === rootId;
76 });
77 };
78
Matteo Scandoloc9ebd922016-01-28 12:02:57 -080079 const findSpecificInformation = (tenants, rootId) => {
80 var tenants = lodash.filter(tenants, service => {
81 return service.provider_service === rootId && service.subscriber_tenant;
82 });
83
84 var info;
85
86 tenants.forEach((tenant) => {
87 if(tenant.service_specific_attribute){
88 info = JSON.parse(tenant.service_specific_attribute);
89 }
90 });
91
92 return info;
93 };
94
Matteo Scandolof16b3792016-01-21 14:23:28 -080095 // find all the service defined by a given array of relations
96 const findLevelServices = (relations, services) => {
97 const levelServices = [];
98 lodash.forEach(relations, (tenant) => {
99 var service = lodash.find(services, {id: tenant.provider_service});
100 levelServices.push(service);
101 });
102 return levelServices;
103 };
104
105 const buildLevel = (tenants, services, rootService, parentName = null) => {
106
Matteo Scandolof16b3792016-01-21 14:23:28 -0800107 // build an array of unlinked services
108 // these are the services that should still placed in the tree
109 var unlinkedServices = lodash.difference(services, [rootService]);
110
111 // find all relations relative to this rootElement
112 const levelRelation = findLevelRelation(tenants, rootService.id);
113
114 // find all items related to rootElement
115 const levelServices = findLevelServices(levelRelation, services);
116
117 // remove this item from the list (performance
118 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
119
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800120 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
121
122 const tree = {
123 name: rootService.humanReadableName,
124 parent: parentName,
125 type: 'service',
126 service: rootService,
127 children: []
128 };
129
Matteo Scandolof16b3792016-01-21 14:23:28 -0800130 lodash.forEach(levelServices, (service) => {
131 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
132 });
133
Matteo Scandolof2c99012016-01-25 10:10:38 -0800134 // if it is the last element append internet
135 if(tree.children.length === 0){
136 tree.children.push({
137 name: 'Internet',
138 type: 'internet',
139 children: []
140 });
141 }
142
Matteo Scandolof16b3792016-01-21 14:23:28 -0800143 return tree;
144 };
145
Matteo Scandolocb12a1a2016-01-25 14:11:10 -0800146 const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800147
148 // find the root service
149 // it is the one attached to subsriber_root
150 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -0800151 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -0800152 const rootService = lodash.find(services, {id: rootServiceId});
153
154 const serviceTree = buildLevel(tenants, services, rootService);
155
Matteo Scandoloff7df762016-01-22 16:36:34 -0800156 return {
157 name: subscriber.name,
158 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800159 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800160 children: [serviceTree]
161 };
162
Matteo Scandolof16b3792016-01-21 14:23:28 -0800163 };
164
Matteo Scandoloff7df762016-01-22 16:36:34 -0800165 const get = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800166 var deferred = $q.defer();
167 var services, tenants;
168 Services.query().$promise
169 .then((res) => {
170 services = res;
171 return Tenant.query().$promise;
172 })
173 .then((res) => {
174 tenants = res;
Matteo Scandoloff7df762016-01-22 16:36:34 -0800175 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800176 })
177 .catch((e) => {
178 throw new Error(e);
179 });
180
181 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800182 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800183
Matteo Scandolof2c99012016-01-25 10:10:38 -0800184 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800185 return {
186 get: get,
187 buildLevel: buildLevel,
188 buildServiceTree: buildServiceTree,
189 findLevelRelation: findLevelRelation,
190 findLevelServices: findLevelServices,
191 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800192 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800193 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800194 });
195
196}());