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