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