blob: 485d46aa4b3d538ac34f24f1f909abec9f401611 [file] [log] [blame]
Matteo Scandoloba678a92016-06-20 17:16:15 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 6/22/16.
7 */
8
9(function () {
10 'use strict';
11
12 /**
13 * @ngdoc service
14 * @name xos.toscaExporter.serviceGrid
15 **/
16
17 angular.module('xos.serviceGrid')
18 .service('ServiceEncoder', function($q, ArchiveManager, Tenants, Services){
19
20 const serviceTypes = {
21 fabric: 'tosca.nodes.FabricService',
22 onos: 'tosca.nodes.ONOSService',
23 vCPE: 'tosca.nodes.VSGService',
24 vOLT: 'tosca.nodes.VOLTService',
25 vROUTER: 'tosca.nodes.VRouterService',
26 VTN: 'tosca.nodes.VTNService',
27 vTR: 'tosca.nodes.Service'
28 };
29
30 this.formatServiceProperties = (service, toscaSpec) => {
31 const d = $q.defer();
32 const serviceName = `service#${service.name}`;
33 // create the structure to hold the service
34 toscaSpec.topology_template.node_templates[serviceName] = {};
35 toscaSpec.topology_template.node_templates[serviceName].type = serviceTypes[service.kind] || 'tosca.nodes.Service';
36
37 const res = {
38 properties: {
39 kind: service.kind
40 }
41 };
42
43 if(angular.isDefined(service.view_url)){
44 res.properties.view_url = service.view_url;
45 }
46
47 if(angular.isDefined(service.icon_url)){
48 res.properties.icon_url = service.icon_url;
49 }
50
51 if(angular.isDefined(service.private_key_fn)){
52 res.properties.private_key_fn = service.private_key_fn;
53 }
54
55 if(angular.isDefined(service.public_key)){
56 ArchiveManager.addFile(`${service.name}_rsa.pub`, service.public_key);
57 res.properties.public_key = '{ get_artifact: [ SELF, pubkey, LOCAL_FILE] }'
58 res['artifacts'] = {
59 pubkey: `/opt/xos/tosca/${service.name}/${service.name}_rsa.pub`
60 };
61 toscaSpec.topology_template.node_templates[serviceName].artifacts = res.artifacts;
62 }
63
64 toscaSpec.topology_template.node_templates[serviceName].properties = res.properties;
65 d.resolve(toscaSpec);
66 return d.promise;
67 };
68
69 this.getServiceRequirements = (service, toscaSpec) => {
70 const d = $q.defer();
71
72 Tenants.query({subscriber_service: service.id}).$promise
73 .then(tenants => {
74 const services = [];
75 // avoid multiple request for the same service
76 tenants = _.uniqBy(tenants, 'provider_service');
77
78 _.forEach(tenants, t => {
79 services.push(Services.get({id: t.provider_service}).$promise);
80 });
81
82 return $q.all(services)
83 })
84 .then((deps) => {
85 // Get the provider service and create an array of unique names
86 let requirements = _.reduce(deps, (list, d) => list.concat(d.name), []);
87
88 // create a object for requirements, later will parsed in Yaml
89 requirements = _.reduce(requirements, (list, r) => {
90 let name = `${r}_tenant`;
91 let obj = {};
92 obj[name] = {
93 node: `service#${r}`,
94 relationship: 'tosca.relationships.TenantOfService'
95 };
96 return list.concat(obj);
97 }, []);
98
99 if(requirements.length > 0){
100
101 // NOTE set a reference to the requirements in tosca
102 _.forEach(requirements, r => {
103 let reqName = r[Object.keys(r)[0]].node;
104 toscaSpec.topology_template.node_templates[reqName] = {
105 type: 'tosca.nodes.Service',
106 properties: {
107 'no-create': true,
108 'no-delete': true,
109 'no-update': true
110 }
111 };
112 });
113
114 const serviceName = `service#${service.name}`;
115 toscaSpec.topology_template.node_templates[serviceName].requirements = requirements;
116 }
117
118 d.resolve(toscaSpec);
119 });
120
121 return d.promise;
122 };
123 });
124})();
125