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