blob: beeb4fe7efe23e4639003cd56689847088e1bc96 [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
44 // faking to have 2 subscriber
45 res.data.push(res.data[0]);
Matteo Scandolo7547f042016-02-09 09:13:30 -080046 deferred.resolve(res.data);
47 })
48
49 return deferred.promise;
50 }
51 }
52 }
53 });
54 })
55 .service('SubscriberDevice', function($resource){
56 return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'});
Matteo Scandoloff7df762016-01-22 16:36:34 -080057 })
Matteo Scandolo071ef462016-01-25 12:00:42 -080058 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolof16b3792016-01-21 14:23:28 -080059
Matteo Scandolof2c99012016-01-25 10:10:38 -080060 // count the mas depth of an object
61 const depthOf = (obj) => {
62 var depth = 0;
63 if (obj.children) {
64 obj.children.forEach(function (d) {
65 var tmpDepth = depthOf(d);
66 if (tmpDepth > depth) {
67 depth = tmpDepth
68 }
69 })
70 }
71 return 1 + depth
72 };
73
Matteo Scandolof16b3792016-01-21 14:23:28 -080074 // find all the relation defined for a given root
75 const findLevelRelation = (tenants, rootId) => {
76 return lodash.filter(tenants, service => {
77 return service.subscriber_service === rootId;
78 });
79 };
80
Matteo Scandoloc9ebd922016-01-28 12:02:57 -080081 const findSpecificInformation = (tenants, rootId) => {
82 var tenants = lodash.filter(tenants, service => {
83 return service.provider_service === rootId && service.subscriber_tenant;
84 });
85
86 var info;
87
88 tenants.forEach((tenant) => {
89 if(tenant.service_specific_attribute){
90 info = JSON.parse(tenant.service_specific_attribute);
91 }
92 });
93
94 return info;
95 };
96
Matteo Scandolof16b3792016-01-21 14:23:28 -080097 // find all the service defined by a given array of relations
98 const findLevelServices = (relations, services) => {
99 const levelServices = [];
100 lodash.forEach(relations, (tenant) => {
101 var service = lodash.find(services, {id: tenant.provider_service});
102 levelServices.push(service);
103 });
104 return levelServices;
105 };
106
107 const buildLevel = (tenants, services, rootService, parentName = null) => {
108
Matteo Scandolof16b3792016-01-21 14:23:28 -0800109 // build an array of unlinked services
110 // these are the services that should still placed in the tree
111 var unlinkedServices = lodash.difference(services, [rootService]);
112
113 // find all relations relative to this rootElement
114 const levelRelation = findLevelRelation(tenants, rootService.id);
115
116 // find all items related to rootElement
117 const levelServices = findLevelServices(levelRelation, services);
118
119 // remove this item from the list (performance
120 unlinkedServices = lodash.difference(unlinkedServices, levelServices);
121
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800122 rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id);
123
124 const tree = {
125 name: rootService.humanReadableName,
126 parent: parentName,
127 type: 'service',
128 service: rootService,
129 children: []
130 };
131
Matteo Scandolof16b3792016-01-21 14:23:28 -0800132 lodash.forEach(levelServices, (service) => {
133 tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName));
134 });
135
Matteo Scandolof2c99012016-01-25 10:10:38 -0800136 // if it is the last element append internet
137 if(tree.children.length === 0){
138 tree.children.push({
139 name: 'Internet',
140 type: 'internet',
141 children: []
142 });
143 }
144
Matteo Scandolof16b3792016-01-21 14:23:28 -0800145 return tree;
146 };
147
Matteo Scandolocb12a1a2016-01-25 14:11:10 -0800148 const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800149
150 // find the root service
151 // it is the one attached to subsriber_root
152 // as now we have only one root so this can work
Matteo Scandoloff7df762016-01-22 16:36:34 -0800153 const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service;
Matteo Scandolof16b3792016-01-21 14:23:28 -0800154 const rootService = lodash.find(services, {id: rootServiceId});
155
156 const serviceTree = buildLevel(tenants, services, rootService);
157
Matteo Scandoloff7df762016-01-22 16:36:34 -0800158 return {
159 name: subscriber.name,
160 parent: null,
Matteo Scandolof2c99012016-01-25 10:10:38 -0800161 type: 'subscriber',
Matteo Scandoloff7df762016-01-22 16:36:34 -0800162 children: [serviceTree]
163 };
164
Matteo Scandolof16b3792016-01-21 14:23:28 -0800165 };
166
Matteo Scandoloff7df762016-01-22 16:36:34 -0800167 const get = (subscriber) => {
Matteo Scandolof16b3792016-01-21 14:23:28 -0800168 var deferred = $q.defer();
169 var services, tenants;
170 Services.query().$promise
171 .then((res) => {
172 services = res;
173 return Tenant.query().$promise;
174 })
175 .then((res) => {
176 tenants = res;
Matteo Scandoloff7df762016-01-22 16:36:34 -0800177 deferred.resolve(buildServiceTree(services, tenants, subscriber));
Matteo Scandolof16b3792016-01-21 14:23:28 -0800178 })
179 .catch((e) => {
180 throw new Error(e);
181 });
182
183 return deferred.promise;
Matteo Scandolof2c99012016-01-25 10:10:38 -0800184 };
Matteo Scandoloff7df762016-01-22 16:36:34 -0800185
Matteo Scandolof2c99012016-01-25 10:10:38 -0800186 // export APIs
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800187 return {
188 get: get,
189 buildLevel: buildLevel,
190 buildServiceTree: buildServiceTree,
191 findLevelRelation: findLevelRelation,
192 findLevelServices: findLevelServices,
193 depthOf: depthOf,
Matteo Scandoloc9ebd922016-01-28 12:02:57 -0800194 findSpecificInformation: findSpecificInformation
Matteo Scandolo5bf04572016-01-25 17:36:08 -0800195 }
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800196 });
197
198}());