Progressive loading per platform for SFA AM Queries (slice, resource, lease)
[unfold.git] / portal / static / js / myslice.js
1 /*
2  * MySlice Class
3  */
4
5 function list() {
6         this.elements = [];
7 }
8
9 list.prototype.add = function(element) {
10         if (!this.has(element)) {
11                 this.elements.push(element);
12         }
13 };
14     
15 list.prototype.del = function(element) {
16         if (this.has(element)) {
17                 this.elements.splice(index, 1);
18         }
19 };
20     
21 list.prototype.has = function(element) {
22         index = $.inArray( element, this.elements );
23         if (index != -1) {
24                 return true;
25         }
26         return false;
27 };
28     
29 list.prototype.count = function() {
30     return this.elements.length;
31 };
32
33
34 /*
35  * resources, users, leases
36  */
37
38 function resources() {
39         this.pending = {
40                 toremove: new list(),
41                 toadd: new list(),
42         };
43 };
44
45 function leases() {
46         this.pending = {
47                 toremove: new list(),
48                 toadd: new list(),
49         };
50 };
51
52 function users() {
53         this.pending = {
54                 toremove: new list(),
55                 toadd: new list(),
56         };
57 };
58
59 /*
60  * Slice
61  */
62 function slice(name) {
63         this.name = name;
64         this.resources = new resources();
65         this.users = new users();
66         this.leases = new leases();
67         
68 };
69
70
71 /*
72  * User
73  */
74 function user(user) {
75         this.user = user;
76         this.testbeds = new list();
77         this.slices = new list();
78         
79         for (i = 0; i < this.user.slices.length; i++) {
80                 this.slices[this.user.slices[i]] = new slice(this.user.slices[i]);
81         }
82 };
83
84 user.prototype.slice = function(name) {
85         return this.slices[name];
86 };
87
88 user.prototype.list = function() {
89     for (s in this.slices) {
90     for (o in s) {
91       if (typeof o != 'function') {
92         console.log(o);
93       } else {
94         console.log("w "+o);
95       }
96       }
97     }
98 };
99
100 /*
101  * MySlice
102  */
103 var myslice = {
104         user: {},
105         
106         user: function() {
107                 if ($.isEmptyObject(this.user)) {
108                         //this.login(function() { return this.user; });
109             if(localStorage.getItem('user')!='undefined'){
110                             this.user = JSON.parse(localStorage.getItem('user'));
111             }else{
112                 return false;
113             }
114                 }
115                 return this.user;
116         },
117     loadSlices: function(slices) {
118         if (typeof(slices) == "undefined"){
119
120             if(myslice.user != null && typeof(myslice.user.slices) != "undefined" && myslice.user.slices.length>0){
121                 slices = myslice.user.slices
122             }
123         }
124         // myslice.user is in LocalStorage
125         if(typeof(slices) != "undefined"){
126             /*
127                 This allows progressive loading per AM platform
128                 Launch queries to get the resources and leases in Manifold Cache
129                 XXX platform:object
130                 TODO support cache for prefixed objects
131                 XXX Disabled until it's supported on Manifold side
132             */
133             /*
134             $.post("/rest/platform/", function( data ) {
135                 $.each(data, function(index, p) {
136                     $.post("/rest/"+p.platform+":resource/", function( data ) {
137                     });
138                     $.post("/rest/"+p.platform+":lease/", function( data ) {
139                     });
140                     $.each( slices, function(i, val) {
141                         // Launch a Query for each slice to get it in Manifold Cache
142                         $.post("/rest/"+p.platform+":slice/", { 'filters': { 'slice_hrn' : val } }, function(data) {
143                         });
144                     });
145
146                 });
147             });
148             */
149         }
150
151     },
152         login: function(fn) {
153         user = localStorage.getItem('user');
154         if($.isEmptyObject(user)){
155             // REGISTRY ONLY TO BE REMOVED WITH MANIFOLD-V2
156                     $.post("/rest/myslice:user/",{'filters':{'user_hrn':'$user_hrn'}}, function( data ) {
157                             //myslice.user = new user(data[0]);
158                             localStorage.setItem('user', JSON.stringify(data[0]));
159                 myslice.loadSlices(data[0].slices);
160                     });
161         }
162         },
163     getSlices: function(name) {
164         
165     },
166     
167     refreshUser: function() {
168         
169     },
170     
171     apply: function() {
172
173         //$('div#loading').show();
174         //this.pending = [];
175         //this.save();
176         //setTimeout(function(){
177                 //$('div#loading').hide();
178                 //window.location.href = '/resources/' + this.slice + '?message=true';
179                 //},6000);
180         
181          
182
183          $.post("/rest/slice/", { 'fields': ['resource','slice_hrn'], 'filters': { 'slice_hrn' : this.slice  } }, function(data) {
184                  console.log(data);
185                  resources = [];
186                  reserved = [];
187                  update = [];
188                  if ('resource' in data[0]) {
189                          $.each(data[0].resource, function(idx, r) {
190                                  resources.push(r.urn);
191                          });
192                  }
193                  //myslice.pending
194                  console.log(myslice.pending);
195                  console.log(resources);
196                  $.each(resources.concat(myslice.pending), function(idx, v) {
197                          update.push( v );
198                  });
199                  console.log(update);
200                  $.post("/update/slice/", { 'filters': { 'slice_hrn' : myslice.slice  }, 'params' : update }, function(data) {
201                          console.log(data);
202                  });
203          });
204         console.log(this.slice);
205     }
206     
207 };
208
209
210 /* MySlice Init */
211
212 // var Reflector = function(obj) {
213   // this.getProperties = function() {
214     // var properties = [];
215     // for (var prop in obj) {
216       // if (typeof obj[prop] != 'function') {
217         // properties.push(prop);
218         // console.log("prop: " + prop);
219       // } else {
220         // console.log("func: " + prop);
221       // }
222     // }
223     // return properties;
224   // };
225 // };
226 // var reflector = new Reflector(myslice.slices[0].resources.pending);
227 // reflector.getProperties();
228
229
230 $(document).ready(function() {
231         //console.log(myslice.user().slices);
232         
233         // $.post("/rest/user/",{'filters':{'user_hrn':'$user_hrn'}}, function(data) {
234                 // myslice.user = new user(data[0]);
235                 // console.log(myslice.user.slices);
236                 // myslice.user.list();
237 //        
238         // }).fail(function() {
239                 // throw "error retreiving user data";
240         // });
241         // Put the object into storage
242         //localStorage.setItem('testObject', JSON.stringify(testObject));
243
244         // Retrieve the object from storage
245         //myslice.load();
246
247 });
248
249 /* EXEMPLES */
250 // add a resource to pending
251
252 //myslice.user.slice('ple.upmc.myslicedemo').resources.pending.add(resource);
253