Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .service('Services', function($resource){ |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 6 | return $resource('/xos/services/:id', {id: '@id'}); |
| 7 | }) |
| 8 | .service('Tenant', function($resource){ |
| 9 | return $resource('/xos/tenants'); |
| 10 | }) |
Matteo Scandolo | 06f45d6 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 11 | .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 Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 17 | .service('Subscribers', function($resource){ |
| 18 | return $resource('/xos/subscribers', {id: '@id'}); |
| 19 | }) |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 20 | .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){ |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 21 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 22 | // 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 Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 36 | // 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 Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 58 | type: 'service', |
Matteo Scandolo | e89935b | 2016-01-22 13:17:33 -0800 | [diff] [blame] | 59 | service: rootService, |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 60 | 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 Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 80 | // 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 Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 89 | return tree; |
| 90 | }; |
| 91 | |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 92 | const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 93 | |
| 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 Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 97 | const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service; |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 98 | const rootService = lodash.find(services, {id: rootServiceId}); |
| 99 | |
| 100 | const serviceTree = buildLevel(tenants, services, rootService); |
| 101 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 102 | return { |
| 103 | name: subscriber.name, |
| 104 | parent: null, |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 105 | type: 'subscriber', |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 106 | children: [serviceTree] |
| 107 | }; |
| 108 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 111 | const get = (subscriber) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 112 | var deferred = $q.defer(); |
| 113 | var services, tenants; |
| 114 | Services.query().$promise |
| 115 | .then((res) => { |
| 116 | services = res; |
| 117 | return Tenant.query().$promise; |
| 118 | }) |
| 119 | .then((res) => { |
| 120 | tenants = res; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 121 | deferred.resolve(buildServiceTree(services, tenants, subscriber)); |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 122 | }) |
| 123 | .catch((e) => { |
| 124 | throw new Error(e); |
| 125 | }); |
| 126 | |
| 127 | return deferred.promise; |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 128 | }; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 129 | |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 130 | const buildServiceInterfacesTree = (slices, instances) => { |
| 131 | var interfaceTree = []; |
| 132 | lodash.forEach(slices, (slice, i) => { |
| 133 | let current = { |
| 134 | name: slice.name, |
| 135 | slice: slice, |
| 136 | type: 'slice', |
| 137 | children: instances[i].map((instance) => { |
| 138 | return { |
| 139 | name: instance.humanReadableName, |
| 140 | children: [], |
| 141 | type: 'instance', |
| 142 | instance: instance |
| 143 | }; |
| 144 | |
| 145 | }) |
| 146 | }; |
| 147 | interfaceTree.push(current); |
| 148 | }); |
| 149 | return interfaceTree; |
| 150 | }; |
| 151 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 152 | const getServiceInterfaces = (serviceId) => { |
| 153 | var deferred = $q.defer(); |
| 154 | |
| 155 | var _slices; |
| 156 | |
| 157 | Slice.query({service: serviceId}).$promise |
| 158 | .then((slices) => { |
| 159 | _slices = slices; |
| 160 | const promisesArr = slices.reduce((promises, slice) => { |
| 161 | promises.push(Instances.query({slice: slice.id}).$promise); |
| 162 | return promises; |
| 163 | }, []); |
| 164 | |
Matteo Scandolo | 9ef3c84 | 2016-01-25 13:55:09 -0800 | [diff] [blame] | 165 | // TODO add networks |
| 166 | // decide how, they should be manually drawn |
| 167 | // as they connect more instances without parent dependencies |
| 168 | |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 169 | return $q.all(promisesArr); |
| 170 | }) |
| 171 | .then((instances) => { |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 172 | deferred.resolve(buildServiceInterfacesTree(_slices, instances)); |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 173 | }); |
| 174 | |
| 175 | return deferred.promise; |
| 176 | }; |
| 177 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 178 | // export APIs |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 179 | return { |
| 180 | get: get, |
| 181 | buildLevel: buildLevel, |
| 182 | buildServiceTree: buildServiceTree, |
| 183 | findLevelRelation: findLevelRelation, |
| 184 | findLevelServices: findLevelServices, |
| 185 | depthOf: depthOf, |
| 186 | getServiceInterfaces: getServiceInterfaces, |
| 187 | buildServiceInterfacesTree: buildServiceInterfacesTree |
| 188 | } |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 189 | }); |
| 190 | |
| 191 | }()); |