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