Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | ba678a9 | 2016-06-20 17:16:15 -0700 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | /** |
| 23 | * @ngdoc service |
| 24 | * @name xos.toscaExporter.serviceGrid |
| 25 | **/ |
| 26 | |
| 27 | angular.module('xos.serviceGrid') |
| 28 | .service('ToscaEncoder', function($q, _, ArchiveManager, ServiceEncoder, SlicesEncoder){ |
| 29 | |
| 30 | let toscaSpec = { |
| 31 | tosca_definitions_version: 'tosca_simple_yaml_1_0', |
| 32 | description: '', |
| 33 | imports: [ |
| 34 | 'custom_types/xos.yaml' |
| 35 | ], |
| 36 | topology_template:{ |
| 37 | node_templates: {} |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * @ngdoc method |
| 43 | * @name xos.serviceGrid.ToscaEncoder#$toYml |
| 44 | * @methodOf xos.serviceGrid.ToscaEncoder |
| 45 | * @description |
| 46 | * Convert a Json data structure to Yaml, use https://github.com/nodeca/js-yaml |
| 47 | * @param {Object} item A Json object to be converted |
| 48 | * @returns {string} The Yaml representation of the Json input |
| 49 | **/ |
| 50 | |
| 51 | this.toYml = (item) => { |
| 52 | return jsyaml.dump(item).replace(/'/g, ''); |
| 53 | }; |
| 54 | |
| 55 | this.export = (service) => { |
| 56 | ArchiveManager.download(service.name); |
| 57 | const file = this.toYml(toscaSpec); |
| 58 | return file; |
| 59 | }; |
| 60 | |
| 61 | this.serviceToTosca = service => { |
| 62 | |
| 63 | ArchiveManager.createArchive(); |
| 64 | //clean |
| 65 | toscaSpec.topology_template.node_templates = {}; |
| 66 | |
| 67 | toscaSpec.description = `Just enough Tosca to get the ${service.humanReadableName} service up and running`; |
| 68 | |
| 69 | const d = $q.defer(); |
| 70 | |
| 71 | // build service properties |
| 72 | ServiceEncoder.formatServiceProperties(service, toscaSpec) |
| 73 | .then(spec => { |
| 74 | return SlicesEncoder.getServiceSlices(service, spec); |
| 75 | }) |
| 76 | // add required slices (and it will all the slice requirements) |
| 77 | .then((spec) => { |
| 78 | return ServiceEncoder.getServiceRequirements(service, spec); |
| 79 | }) |
| 80 | // add required services (provider services) |
| 81 | .then((spec) => { |
| 82 | ArchiveManager.addFile(`${service.name}_service.yaml`, this.toYml(spec)); |
| 83 | |
| 84 | this.export(service); |
| 85 | |
| 86 | d.resolve(this.toYml(spec)); |
| 87 | }) |
| 88 | .catch(e => { |
| 89 | d.reject(e); |
| 90 | }); |
| 91 | return d.promise; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | }); |
| 96 | |
| 97 | }()); |