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