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.NetworkEncoder.serviceGrid |
| 33 | **/ |
| 34 | |
| 35 | // TODO write tests |
| 36 | angular.module('xos.serviceGrid') |
| 37 | .service('NetworkEncoder', function($q, Networks, NetworkTemplateEncoder){ |
| 38 | |
| 39 | this.buildTosca = (networks, toscaSpec) => { |
| 40 | |
| 41 | const apiNetworks = angular.copy(networks); |
| 42 | |
| 43 | // store here the promise that will build the dependency structure |
| 44 | let dependency = { |
| 45 | }; |
| 46 | |
| 47 | const d = $q.defer(); |
| 48 | |
| 49 | try { |
| 50 | networks = _.reduce(networks, (obj, n) => { |
| 51 | obj[`network#${n.name}`] = { |
| 52 | type: 'tosca.nodes.network.Network.XOS', |
| 53 | requirements: [] |
| 54 | }; |
| 55 | |
| 56 | // for each network slice, add requirement |
| 57 | if(angular.isDefined(n.slices)) { |
| 58 | _.forEach(n.slices, s => { |
| 59 | let owner = { |
| 60 | owner: { |
| 61 | node: s.name, |
| 62 | relationship: 'tosca.relationships.MemberOfSlice' |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | let conn = { |
| 67 | connection: { |
| 68 | node: s.name, |
| 69 | relationship: 'tosca.relationships.ConnectsToSlice' |
| 70 | } |
| 71 | }; |
| 72 | obj[`network#${n.name}`].requirements.push(owner, conn); |
| 73 | }); |
| 74 | |
| 75 | if(angular.isDefined(n.template)){ |
| 76 | dependency[n.name] = NetworkTemplateEncoder.buildTosca(n.template, toscaSpec); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return obj; |
| 81 | |
| 82 | }, {}); |
| 83 | |
| 84 | // if we have dependency wait for them |
| 85 | if(Object.keys(dependency).length > 0){ |
| 86 | $q.all(dependency) |
| 87 | .then(deps => { |
| 88 | // NOTE how to make this readable?? |
| 89 | if(deps){ |
| 90 | |
| 91 | // for each property in deps |
| 92 | for(let k of Object.keys(deps)){ |
| 93 | let [tosca, template] = deps[k]; |
| 94 | networks[`network#${k}`].requirements.push({ |
| 95 | network_template: { |
| 96 | node: `network_template#${template.name}`, |
| 97 | relationship: 'tosca.relationships.UsesNetworkTemplate' |
| 98 | } |
| 99 | }); |
| 100 | angular.extend(toscaSpec, tosca); |
| 101 | } |
| 102 | } |
| 103 | angular.extend(toscaSpec.topology_template.node_templates, networks); |
| 104 | d.resolve([toscaSpec, apiNetworks]); |
| 105 | }) |
| 106 | .catch(e => { |
| 107 | throw new Error(e); |
| 108 | }); |
| 109 | } |
| 110 | //else resolve directly |
| 111 | else { |
| 112 | angular.extend(toscaSpec.topology_template.node_templates, networks); |
| 113 | d.resolve([toscaSpec, apiNetworks]); |
| 114 | } |
| 115 | } |
| 116 | catch(e){ |
| 117 | d.reject(e); |
| 118 | } |
| 119 | |
| 120 | return d.promise; |
| 121 | }; |
| 122 | |
| 123 | this.getSliceNetworks = (slice, toscaSpec) => { |
| 124 | const d = $q.defer(); |
| 125 | Networks.query({owner: slice.id}).$promise |
| 126 | .then((networks) => { |
| 127 | // check that all the network this slice own are listed in the slice |
| 128 | // does this make sense? |
| 129 | networks = _.filter(networks, n => { |
| 130 | return slice.networks.indexOf(n.id) !== -1; |
| 131 | }); |
| 132 | |
| 133 | // denormalize slice inside network |
| 134 | networks = networks.map(n => { |
| 135 | let idx = n.slices.indexOf(slice.id); |
| 136 | n.slices[idx] = slice; |
| 137 | return n; |
| 138 | }); |
| 139 | |
| 140 | this.buildTosca(networks, toscaSpec) |
| 141 | .then(d.resolve) |
| 142 | .catch(d.reject); |
| 143 | |
| 144 | }); |
| 145 | |
| 146 | return d.promise; |
| 147 | } |
| 148 | }); |
| 149 | })(); |
| 150 | |