Fixed sla creation calls and cleaned debug messages
[myslice.git] / plugins / sladialog / static / js / sladialog.js
1 /**
2  * MyPlugin:    demonstration plugin
3  * Version:     0.1
4  * Description: Template for writing new plugins and illustrating the different
5  *              possibilities of the plugin API.
6  *              This file is part of the Manifold project 
7  * Requires:    js/plugin.js
8  * URL:         http://www.myslice.info
9  * Author:      Jordan AugĂ© <jordan.auge@lip6.fr>
10  * Copyright:   Copyright 2012-2013 UPMC Sorbonne UniversitĂ©s
11  * License:     GPLv3
12  */
13
14 (function($){
15
16     var SlaDialog = Plugin.extend({
17
18         accepted_slas: {},
19
20         /** XXX to check
21          * @brief Plugin constructor
22          * @param options : an associative array of setting values
23          * @param element : 
24          * @return : a jQuery collection of objects on which the plugin is
25          *     applied, which allows to maintain chainability of calls
26          */
27         init: function(options, element) {
28             // for debugging tools
29             this.classname="SlaDialog";
30             // Call the parent constructor, see FAQ when forgotten
31             this._super(options, element);
32
33             /* Member variables */
34
35             /* Plugin events */
36
37             /* Setup query and record handlers */
38
39             // Explain this will allow query events to be handled
40             // What happens when we don't define some events ?
41             // Some can be less efficient
42             this.listen_query(options.query_uuid);
43             
44             /* GUI setup and event binding */
45             // call function
46             this.button_binding();
47
48         },
49
50         find_row: function(key)
51         {
52             // key in third position, column id = 2
53             var KEY_POS = 2;
54
55             var cols = $.grep(this.table.fnSettings().aoData, function(col) {
56                 return (col._aData[KEY_POS] == key);
57             } );
58
59             if (cols.length == 0)
60                 return null;
61             if (cols.length > 1)
62                 throw "Too many same-key rows in ResourceSelected plugin";
63
64             return cols[0];
65         },
66
67         check_template_status: function() {
68             for (var testbed in this.accepted_slas) {
69                 if (!this.accepted_slas[testbed]) { return false; }
70             }
71
72             return true;
73         },
74
75         /* PLUGIN EVENTS */
76         // on_show like in querytable
77
78
79         /* GUI EVENTS */
80
81         button_binding: function() {
82             var self = this;
83             $(".sla-accept-button").click(function() {
84                 // set SLA as accepted and remove warnings
85                 var id = $(this).closest(".row").attr("id");
86                 self.accepted_slas[id] = true;
87                 var is_ok = self.check_template_status();
88
89                 if (is_ok) {
90                     // remove warnings
91                     // var warnings = manifold.query_store.get_record_state(resource_query.query_uuid, resource_key, STATE_WARNINGS);
92                 }
93             });
94         },
95
96         create_sla: function(record) {
97             var self = this;
98
99             var urns = [];
100
101             if (record.resource.length != 0 && typeof record.resource[0] === "object") {
102
103                 record.resource.forEach(function(r) {
104                     if ($.inArray(r.component_id, record.resource) == -1) { // if not already selected
105                         urns.push(r.component_id);
106                     }
107                 });
108
109                 var data = {
110                     "SLIVER_INFO_AGGREGATE_URN": record.resource[0].component_manager_id,
111                     "SLIVER_INFO_EXPIRATION": record.lease[0].end_time,     // FIXME: only working with leases
112                     "SLIVER_INFO_SLICE_URN": record.slice_urn,
113                     "SLIVER_INFO_CREATOR_URN": record.users[0],
114                     "SLIVER_INFO_URN": urns,
115                     "SLIVER_INFO_SLA_ID": self._getUUID()
116                 };
117
118                 console.log(data);
119
120                 $.post("/sla/agreements/create/", data)
121                     .done(function (response) {
122                         console.log("SLA created");
123                     });
124             }
125         },
126
127         uncheck: function(urn)
128         {
129 //            $('#slamodal').on('hidden.bs.modal', function(e){
130 //                $('#' + (urn).replace(/"/g,'')).click();
131 //                console.log('#' + (data.value).replace(/"/g,''));
132 //            });
133         },
134
135         // a function to bind events here: click change
136         // how to raise manifold events
137         set_state: function(data, username)
138         {
139             
140         },
141
142         post_agreement: function()
143         {
144             console.log(this.options.user);
145         },
146
147         /* GUI MANIPULATION */
148
149         // We advise you to write function to change behaviour of the GUI
150         // Will use naming helpers to access content _inside_ the plugin
151         // always refer to these functions in the remaining of the code
152
153         show_hide_button: function() 
154         {
155             // this.id, this.el, this.cl, this.elts
156             // same output as a jquery selector with some guarantees
157         },
158
159         /* TEMPLATES */
160
161         // see in the html template
162         // How to load a template, use of mustache
163
164         /* QUERY HANDLERS */
165
166         // How to make sure the plugin is not desynchronized
167         // He should manifest its interest in filters, fields or records
168         // functions triggered only if the proper listen is done
169
170         // no prefix
171
172         on_filter_added: function(filter)
173         {
174
175         },
176
177         on_field_state_changed: function(data)
178         {
179             var self = this;
180             // this.set_state(result, this.options.username);
181
182              switch(data.state) {
183                 case STATE_SET:
184                     switch(data.op) {
185                         case STATE_SET_IN_PENDING:
186                             if (typeof(data.value) == 'string') {
187                                 // data.value = urn
188                                 this._supports_sla(data.value)
189                                     .done( function(testbeds) {
190                                         var urn_regexp = /\+(.*?)\+/;
191                                         var urn = urn_regexp.exec(data.value)[1];
192                                         var pos = $.inArray(urn, testbeds);
193                                         if ( pos != -1) {
194                                             var id_ref = testbeds[pos].replace(/\.|:/g, "-");
195                                             $("#" + id_ref).data("urns").push(data.value);
196                                             $("#" + id_ref).show();
197                                             self.accepted_slas[id_ref] = false;
198                                             //$( "#sla_offers_list" ).append(
199                                             //    $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
200                                             //);
201                                         }
202                                     });
203                             }
204                             break;
205                         case STATE_SET_OUT:
206                             // data.value = urn
207                             if (typeof(data.value) == 'string') {
208                                 // data.value = urn
209                                 this._supports_sla(data.value)
210                                     .done( function(testbeds) {
211                                         var urn_regexp = /\+(.*?)\+/;
212                                         var urn = urn_regexp.exec(data.value)[1];
213                                         var pos = $.inArray(urn, testbeds);
214                                         if ( pos != -1) {
215                                             var id_ref = testbeds[pos].replace(/\.|:/g, "-");
216                                             var array = $("#" + id_ref).data("urns");
217                                             array.splice(array.indexOf(data.value), 1);
218
219                                             if ($("#" + id_ref).data("urns").length == 0) {
220                                                 $("#" + id_ref).hide();
221                                                 delete self.accepted_slas[id_ref];
222                                             }
223                                             //$( "#sla_offers_list" ).append(
224                                             //    $("<li>").text("Testbed " + testbeds[pos] + " offers SLA for its resources")
225                                             //);
226                                         }
227                                     });
228                             }
229                             break;
230                     }
231                     break;
232
233                 case STATE_WARNINGS:
234                     // Add resource to SLA
235                     // data.key = urn
236                     // data.value = {1: "SLA acceptance required..."}
237                     // this.change_status(data.key, data.value);
238                     break;
239              }
240         }, 
241
242         // ... be sure to list all events here
243
244         /* RECORD HANDLERS */
245         on_all_new_record: function(record)
246         {
247             //
248         },
249
250         on_new_record: function(record)
251         {
252             this.create_sla(record);
253         },
254
255         /* INTERNAL FUNCTIONS */
256         _dummy: function() {
257             // only convention, not strictly enforced at the moment
258         },
259
260         _supports_sla: function(resource_urn) {
261             return $.ajax("/sla/testbeds/");
262         },
263
264
265
266         _getUUID: function() {
267             return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
268                 var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
269                 return v.toString(16);
270             });
271         },
272
273     });
274
275     /* Plugin registration */
276     $.plugin('SlaDialog', SlaDialog);
277
278     // TODO Here use cases for instanciating plugins in different ways like in the pastie.
279
280 })(jQuery);