Matteo Scandolo | ba678a9 | 2016-06-20 17:16:15 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** |
| 5 | * @ngdoc service |
| 6 | * @name xos.toscaExporter.serviceGrid |
| 7 | **/ |
| 8 | |
| 9 | angular.module('xos.serviceGrid') |
| 10 | .service('ToscaEncoder', function($q, _, ArchiveManager, ServiceEncoder, SlicesEncoder){ |
| 11 | |
| 12 | let toscaSpec = { |
| 13 | tosca_definitions_version: 'tosca_simple_yaml_1_0', |
| 14 | description: '', |
| 15 | imports: [ |
| 16 | 'custom_types/xos.yaml' |
| 17 | ], |
| 18 | topology_template:{ |
| 19 | node_templates: {} |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * @ngdoc method |
| 25 | * @name xos.serviceGrid.ToscaEncoder#$toYml |
| 26 | * @methodOf xos.serviceGrid.ToscaEncoder |
| 27 | * @description |
| 28 | * Convert a Json data structure to Yaml, use https://github.com/nodeca/js-yaml |
| 29 | * @param {Object} item A Json object to be converted |
| 30 | * @returns {string} The Yaml representation of the Json input |
| 31 | **/ |
| 32 | |
| 33 | this.toYml = (item) => { |
| 34 | return jsyaml.dump(item).replace(/'/g, ''); |
| 35 | }; |
| 36 | |
| 37 | this.export = (service) => { |
| 38 | ArchiveManager.download(service.name); |
| 39 | const file = this.toYml(toscaSpec); |
| 40 | return file; |
| 41 | }; |
| 42 | |
| 43 | this.serviceToTosca = service => { |
| 44 | |
| 45 | ArchiveManager.createArchive(); |
| 46 | //clean |
| 47 | toscaSpec.topology_template.node_templates = {}; |
| 48 | |
| 49 | toscaSpec.description = `Just enough Tosca to get the ${service.humanReadableName} service up and running`; |
| 50 | |
| 51 | const d = $q.defer(); |
| 52 | |
| 53 | // build service properties |
| 54 | ServiceEncoder.formatServiceProperties(service, toscaSpec) |
| 55 | .then(spec => { |
| 56 | return SlicesEncoder.getServiceSlices(service, spec); |
| 57 | }) |
| 58 | // add required slices (and it will all the slice requirements) |
| 59 | .then((spec) => { |
| 60 | return ServiceEncoder.getServiceRequirements(service, spec); |
| 61 | }) |
| 62 | // add required services (provider services) |
| 63 | .then((spec) => { |
| 64 | ArchiveManager.addFile(`${service.name}_service.yaml`, this.toYml(spec)); |
| 65 | |
| 66 | this.export(service); |
| 67 | |
| 68 | d.resolve(this.toYml(spec)); |
| 69 | }) |
| 70 | .catch(e => { |
| 71 | d.reject(e); |
| 72 | }); |
| 73 | return d.promise; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | }); |
| 78 | |
| 79 | }()); |