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