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