Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[myslice.git] / portal / sliceresourceview.py
1 from django.template                    import RequestContext
2 from django.shortcuts                   import render_to_response
3
4 from manifold.core.query                import Query, AnalyzedQuery
5 from manifoldapi.manifoldapi            import execute_query
6 import json
7
8 from django.views.generic.base          import TemplateView
9
10 from unfold.loginrequired               import LoginRequiredView
11 from django.http import HttpResponse
12 from django.shortcuts import render
13
14 from unfold.page                        import Page
15
16 from myslice.configengine               import ConfigEngine
17
18 from plugins.apply                      import ApplyPlugin
19 from plugins.querytable                 import QueryTable
20 from plugins.googlemap                  import GoogleMap
21 # from plugins.queryupdater               import QueryUpdaterPlugin
22 from plugins.filter_status              import FilterStatusPlugin
23 from plugins.testbeds                   import TestbedsPlugin
24 from plugins.scheduler2                 import Scheduler2
25
26 # Bristol plugin
27 from plugins.univbris                   import Univbris
28 from plugins.univbrisfoam               import UnivbrisFoam
29 from plugins.univbrisfv                 import UnivbrisFv
30 from plugins.univbrisfvf                import UnivbrisFvf
31 from plugins.univbrisfvfo               import UnivbrisFvfo
32 from plugins.univbristopo               import UnivbrisTopo
33 from plugins.univbrisvtam                   import UnivbrisVtam as UnivbrisVtamPlugin
34 from plugins.univbrisvtamform           import UnivbrisVtamForm
35
36 from plugins.columns_editor             import ColumnsEditor
37 from plugins.sladialog                  import SlaDialog
38 from plugins.lists.simplelist           import SimpleList
39
40 from myslice.theme import ThemeView
41
42 class SliceResourceView (LoginRequiredView, ThemeView):
43     template_name = "slice-resource-view.html"
44     
45     def get(self, request, slicename):
46
47         if request.GET.get('message') : 
48             msg = "Slice successfully updated"
49         else :
50             msg = None
51
52         page = Page(request)
53         metadata = page.get_metadata()
54         page.expose_js_metadata()
55
56         resource_md = metadata.details_by_object('resource')
57         resource_fields = [column['name'] for column in resource_md['column']]
58
59         user_md = metadata.details_by_object('user')
60         user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
61
62         # TODO The query to run is embedded in the URL
63         # Example: select slice_hrn, resource.urn, lease.resource, lease.start_time, lease.end_time from slice where slice_hrn == "ple.upmc.myslicedemo"
64         main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
65         main_query.select(
66                 # SLICE
67                 'slice_hrn',
68                 # - The record key is needed otherwise the storage of records
69                 #   bugs !
70                 'slice_urn',
71                 # RESOURCES
72                 'resource',
73                 'lease',
74                 'resource.urn',
75                 'resource.hostname', 'resource.type',
76                 # - The facility_name and testbed_name are required for the
77                 #   testbeds plugin to properly work.
78                 'resource.facility_name', 
79                 'resource.testbed_name',
80                 # LEASES
81                 'lease.resource',
82                 'lease.start_time',
83                 'lease.end_time',
84                 # - The lease_id is important for NITOS identify already existing
85                 #   leases
86                 'lease.lease_id', 
87
88                 # FLOWSPACE
89                 #'flowspace',               
90                 # VMS
91                 #'vms',
92
93
94                 #'user.user_hrn',
95                 #'application.measurement_point.counter'
96         )
97         # for internal use in the querytable plugin;
98         # needs to be a unique column present for each returned record
99         main_query_init_key = 'urn'
100         aq = AnalyzedQuery(main_query, metadata=metadata)
101         page.enqueue_query(main_query, analyzed_query=aq)
102         sq_resource    = aq.subquery('resource')
103         sq_lease       = aq.subquery('lease')
104         #sq_flowspace   = aq.subquery('flowspace')
105         #sq_vms         = aq.subquery('vms')
106
107         query_resource_all = Query.get('resource').select(resource_fields)
108         page.enqueue_query(query_resource_all)
109
110         # leases query
111         #lease_md = metadata.details_by_object('lease')
112         #lease_fields = [column['name'] for column in lease_md['column']]
113
114         #query_lease_all = Query.get('lease').select(lease_fields)
115         #page.enqueue_query(query_lease_all)
116
117         # --------------------------------------------------------------------------
118         # ALL RESOURCES LIST
119         # resources as a list using datatable plugin
120  
121         list_resources = QueryTable(
122             page       = page,
123             domid      = 'resources-list',
124             title      = 'List view',
125             query      = sq_resource,
126             query_all  = query_resource_all,
127             init_key   = "urn",
128             checkboxes = True,
129             datatables_options = {
130                 'iDisplayLength': 25,
131                 'bLengthChange' : True,
132                 'bAutoWidth'    : True,
133                 },
134         )
135
136
137         # --------------------------------------------------------------------------
138         # RESERVED RESOURCES LIST
139         # resources as a list using datatable plugin
140  
141         list_reserved_resources = SimpleList(
142             title = None,
143             page  = page,
144             key   = 'urn',
145             query = sq_resource,
146         )
147
148         list_reserved_leases = SimpleList(
149             title = None,
150             page  = page,
151             key   = 'resource',
152             query = sq_lease,
153         )
154
155 #        list_reserved_resources = QueryTable(
156 #            page       = page,
157 #            domid      = 'resources-reserved-list',
158 #            title      = 'List view',
159 #            query      = sq_resource,
160 #            query_all  = sq_resource,
161 #            init_key   = "urn",
162 #            checkboxes = True,
163 #            datatables_options = {
164 #                'iDisplayLength': 25,
165 #                'bLengthChange' : True,
166 #                'bAutoWidth'    : True,
167 #                },
168 #        )
169
170         # --------------------------------------------------------------------------
171         # COLUMNS EDITOR
172         # list of fields to be applied on the query 
173         # this will add/remove columns in QueryTable plugin
174  
175         filter_column_editor = ColumnsEditor(
176             page  = page,
177             query = sq_resource, 
178             query_all = query_resource_all,
179             title = "Select Columns",
180             domid = 'select-columns',
181         )
182
183         # --------------------------------------------------------------------------
184         # RESOURCES MAP
185         # the resources part is made of a Tabs (Geographic, List), 
186
187         map_resources  = GoogleMap(
188             page       = page,
189             title      = 'Geographic view',
190             domid      = 'resources-map',
191             # tab's sons preferably turn this off
192             togglable  = False,
193             query      = sq_resource,
194             # this key is the one issued by google
195             googlemap_api_key = ConfigEngine().googlemap_api_key(),
196             # the key to use at init-time
197             init_key   = main_query_init_key,
198             checkboxes = True,
199             
200             # To center around Europe : 53,9 / 3
201             latitude   = 53.,
202             longitude  = 9.,
203             zoom       = 3,
204         )
205
206         # --------------------------------------------------------------------------
207         # LEASES Nitos Scheduler
208         # Display the leases reservation timeslots of the resources
209
210         resources_as_scheduler2 = Scheduler2( 
211             page       = page,
212             domid      = 'scheduler',
213             title      = 'Scheduler',
214             # this is the query at the core of the slice list
215             query = sq_resource,
216             query_lease = sq_lease,
217         )
218
219         # --------------------------------------------------------------------------
220         # QueryUpdater (Pending Operations)
221  
222 #         pending_resources = QueryUpdaterPlugin(
223 #             page                = page,
224 #             title               = 'Pending operations',
225 #             query               = main_query,
226 #             togglable           = False,
227 #             # start turned off, it will open up itself when stuff comes in
228 #             toggled             = False,
229 #             domid               = 'pending',
230 #             outline_complete    = True,
231 #             username            = request.user,
232 #         )
233
234         # --------------------------------------------------------------------------
235         # NETWORKS
236         # testbeds as a list of filters 
237
238         network_md = metadata.details_by_object('network')
239         network_fields = [column['name'] for column in network_md['column']]
240
241         #query_networks = Query.get('network').select(network_fields)
242         #page.enqueue_query(query_networks)
243
244         filter_testbeds = TestbedsPlugin(
245             page            = page,
246             domid           = 'testbeds-filter',
247             title           = 'Filter by testbeds',
248             query           = sq_resource,
249             #query_networks  = query_networks,
250             #init_key        = "network_hrn",
251             #checkboxes      = True,
252             #datatables_options = {
253             #    'iDisplayLength': 25,
254             #    'bLengthChange' : True,
255             #    'bAutoWidth'    : True,
256             #    },
257         )
258
259         filter_status = FilterStatusPlugin(
260             page            = page,
261             domid           = "filter-status",
262             query           = sq_resource,
263         )
264         apply = ApplyPlugin(
265             page            = page,
266             domid           = "apply",
267             query           = main_query,
268             username            = request.user,
269         )
270             
271         # --------------------------------------------------------------------------
272         # Ofelia OpenFlow Plugin 
273         # Bristol plugin
274
275         # plugin which display a "gathering resources" message 
276         # waiting for all resources to be returned by manifold
277        # univbriswelcome = Univbris(
278        #     page  = page,
279        #     title = 'univbris_welcome',
280        #     domid = 'univbris_welcome',
281        #     query = query_resource_all,
282        # )
283
284        # univbrisfoamlist = UnivbrisFoam(
285        #     page  = page,
286        #     title = 'univbris_foam_ports_selection',
287        #     domid = 'univbris_foam_ports_selection',
288        #     query = query_resource_all,
289        #     query_all = query_resource_all,
290        #     checkboxes = False,
291        #     datatables_options = {
292        #         'iDisplayLength': 10,
293        #         'bLengthChange' : True,
294        #         'bAutoWidth'    : True,
295        #         },
296        # )
297
298        # #plugin which manages the different flowspaces that the user creates, and also sends flowspaces to manifold
299        # univbrisfvlist = UnivbrisFv(
300        #         page  = page,
301        #         title = 'univbris_flowspace_selection',
302        #         domid = 'univbris_flowspace_selection',
303        #         query = sq_flowspace,
304        #         query_all = query_resource_all,
305        #         datatables_options = {
306        #             'iDisplayLength': 5,
307        #             'bLengthChange' : True,
308        #             'bAutoWidth'    : True,
309        #             },
310        # )
311
312        # #plugin which allows the definition of a single flowspace
313        # univbrisfvform = UnivbrisFvf(
314        #         page  = page,
315        #         title = 'univbris_flowspace_form',
316        #         domid = 'univbris_flowspace_form',
317        #         query = query_resource_all,
318        #         query_all = None,
319        #         datatables_options = {
320        #             'iDisplayLength': 3,
321        #             'bLengthChange' : True,
322        #             'bAutoWidth'    : True,
323        #             },
324        # )
325
326            # #plugin which allows the definition the match criteria on a single OPTICAL flowspace
327        # univbrisofvform = UnivbrisFvfo(
328        #     page  = page,
329        #     title = 'univbris_oflowspace_form',
330        #     domid = 'univbris_oflowspace_form',
331            #     query = None,
332        #     query_all = None,
333        #     datatables_options = { 
334        #         'iDisplayLength': 3,
335        #         'bLengthChange' : True,
336        #         'bAutoWidth'    : True,
337        #         },
338        # )
339
340        # #plugin which display the gathered topology
341        # univbristopology = UnivbrisTopo(
342        #     page  = page,
343        #     title = 'univbris_topology',
344        #     domid = 'univbris_topology',
345        #     query = query_resource_all,
346        # )
347
348        # # --------------------------------------------------------------------------
349        # # Ofelia VTAM Plugin 
350        # # Bristol Plugin
351
352        # #plugin which display a table where an experimenter will add VMs to according to his needs
353        # # responsible to send the data to Manifold
354        # univbrisvtamplugin = UnivbrisVtamPlugin(
355        #     page  = page,
356        #     title = 'univbris_vtam',
357        #     domid = 'univbris_vtam',
358        #     query = sq_vms,
359        #     #query = sq_resource,
360        # )
361
362        # #plugin which display a form where an experimenter will specify 
363        # # in which testbed and which physical server to setup the VM
364        # univbrisvtamform = UnivbrisVtamForm(
365        #     page  = page,
366        #     title = 'univbris_vtam_form',
367        #     domid = 'univbris_vtam_form',
368            #     query =  query_resource_all,
369        #     query_all = None,
370        #     datatables_options = { 
371        #         'iDisplayLength': 3,
372        #         'bLengthChange' : True,
373        #         'bAutoWidth'    : True,
374        #         },
375        # )
376
377        # # --------------------------------------------------------------------------
378        # # SLA View and accept dialog
379        # 
380        # sla_dialog = SlaDialog(
381        #     page                = page,
382        #     title               = 'sla dialog',
383        #     query               = main_query,
384        #     togglable           = False,
385        #     # start turned off, it will open up itself when stuff comes in
386        #     toggled             = True,
387        #     domid               = 'sla_dialog',
388        #     outline_complete    = True,
389        #     username            = request.user,
390        # )
391        # 
392         ## check user is pi or not
393         platform_query  = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
394         account_query  = Query().get('local:account').select('user_id','platform_id','auth_type','config')
395         platform_details = execute_query(self.request, platform_query)
396         account_details = execute_query(self.request, account_query)
397
398         # XXX When session has expired, this is None and thus not iterable
399         for platform_detail in platform_details:
400             for account_detail in account_details:
401                 if platform_detail['platform_id'] == account_detail['platform_id']:
402                     if 'config' in account_detail and account_detail['config'] is not '':
403                         account_config = json.loads(account_detail['config'])
404                         if 'myslice' in platform_detail['platform']:
405                             acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
406         # assigning values
407         if acc_auth_cred == {} or acc_auth_cred == 'N/A':
408             pi = "is_not_pi"
409         else:
410             pi = "is_pi"
411         
412         template_env = {}
413         template_env['list_resources'] = list_resources.render(self.request)
414         template_env['list_reserved_resources'] = list_reserved_resources.render(self.request)
415         template_env['list_reserved_leases'] = list_reserved_leases.render(self.request)
416
417         template_env['columns_editor'] = filter_column_editor.render(self.request)
418
419         template_env['filter_testbeds'] = filter_testbeds.render(self.request)
420         template_env['filter_status'] = filter_status.render(self.request)
421         template_env['apply'] = apply.render(self.request)
422
423         template_env['map_resources'] = map_resources.render(self.request)
424         template_env['scheduler'] = resources_as_scheduler2.render(self.request)
425
426         # Bristol plugin
427        # template_env['welcome'] = univbriswelcome.render(self.request)
428        # template_env['resources'] = univbrisfoamlist.render(self.request)
429        # template_env['flowspaces'] = univbrisfvlist.render(self.request)
430        # template_env['oflowspaces_form'] = univbrisofvform.render(self.request)
431        # template_env['flowspaces_form'] = univbrisfvform.render(self.request)
432        # template_env['topology'] = univbristopology.render(self.request)
433        # template_env['vms_list'] = univbrisvtamplugin.render(self.request)
434        # template_env['vm_form'] = univbrisvtamform.render(self.request)
435
436 #        template_env['pending_resources'] = pending_resources.render(self.request)
437        # template_env['sla_dialog'] = '' # sla_dialog.render(self.request)
438         template_env["theme"] = self.theme
439         template_env["username"] = request.user
440         template_env["pi"] = pi
441         template_env["slice"] = slicename
442         template_env["section"] = "resources"
443         template_env["msg"] = msg
444         template_env.update(page.prelude_env())
445
446         return render_to_response(self.template, template_env, context_instance=RequestContext(request))