blob: 88a1b17429308cebc367b73d2064777817f2cba9 [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 Scandolo4889f5a2016-01-25 12:00:42 -080020 .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){
Matteo Scandolo85aad312016-01-21 14:23:28 -080021
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 Scandolo4889f5a2016-01-25 12:00:42 -0800131 const getServiceInterfaces = (serviceId) => {
132 var deferred = $q.defer();
133
134 var _slices;
135
136 Slice.query({service: serviceId}).$promise
137 .then((slices) => {
138 _slices = slices;
139 const promisesArr = slices.reduce((promises, slice) => {
140 promises.push(Instances.query({slice: slice.id}).$promise);
141 return promises;
142 }, []);
143
Matteo Scandolo39f49472016-01-25 13:55:09 -0800144 // TODO add networks
145 // decide how, they should be manually drawn
146 // as they connect more instances without parent dependencies
147
Matteo Scandolo4889f5a2016-01-25 12:00:42 -0800148 return $q.all(promisesArr);
149 })
150 .then((instances) => {
151 console.log(instances);
152 // parse data to build a tree (2 level only)
153 var interfaceTree = [];
154 lodash.forEach(_slices, (slice, i) => {
155 let current = {
156 name: slice.name,
157 slice: slice,
158 type: 'slice',
159 children: instances[i].map((instance) => {
160 return {
161 name: instance.humanReadableName,
162 children: [],
163 type: 'instance',
164 instance: instance
165 };
166
167 })
168 };
169 interfaceTree.push(current);
170 });
171 console.log(interfaceTree)
172 deferred.resolve(interfaceTree);
173 });
174
175 return deferred.promise;
176 };
177
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800178 // export APIs
Matteo Scandolo89276392016-01-22 16:36:34 -0800179 this.get = get;
180 this.buildLevel = buildLevel;
181 this.buildServiceTree = buildServiceTree;
182 this.findLevelRelation = findLevelRelation;
183 this.findLevelServices = findLevelServices;
Matteo Scandolofb46f5b2016-01-25 10:10:38 -0800184 this.depthOf = depthOf;
Matteo Scandolo4889f5a2016-01-25 12:00:42 -0800185 this.getServiceInterfaces = getServiceInterfaces;
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800186 });
187
188}());