1 from __future__ import print_function
3 from django.template import RequestContext
4 from django.shortcuts import render_to_response
6 from manifold.core.query import Query, AnalyzedQuery
7 from manifoldapi.manifoldapi import execute_query
10 from django.views.generic.base import TemplateView
12 from unfold.loginrequired import LoginRequiredView
13 from django.http import HttpResponse
14 from django.shortcuts import render
16 from unfold.page import Page
18 from myslice.configengine import ConfigEngine
20 from plugins.apply import ApplyPlugin
21 from plugins.querytable import QueryTable
22 from plugins.googlemap import GoogleMap
23 # from plugins.queryupdater import QueryUpdaterPlugin
24 from plugins.filter_status import FilterStatusPlugin
25 from plugins.testbeds import TestbedsPlugin
26 from plugins.scheduler2 import Scheduler2
29 from plugins.univbris import Univbris
30 from plugins.univbrisfoam import UnivbrisFoam
31 from plugins.univbrisfv import UnivbrisFv
32 from plugins.univbrisfvf import UnivbrisFvf
33 from plugins.univbrisfvfo import UnivbrisFvfo
34 from plugins.univbristopo import UnivbrisTopo
35 from plugins.univbrisvtam import UnivbrisVtam as UnivbrisVtamPlugin
36 from plugins.univbrisvtamform import UnivbrisVtamForm
38 from plugins.columns_editor import ColumnsEditor
39 from plugins.sladialog import SlaDialog
40 from plugins.lists.simplelist import SimpleList
42 from myslice.theme import ThemeView
44 class SliceResourceView (LoginRequiredView, ThemeView):
45 template_name = "slice-resource-view.html"
47 def get(self, request, slicename):
49 if request.GET.get('message') :
50 msg = "Slice successfully updated"
55 metadata = page.get_metadata()
56 page.expose_js_metadata()
58 resource_md = metadata.details_by_object('resource')
59 resource_fields = [column['name'] for column in resource_md['column']]
61 user_md = metadata.details_by_object('user')
62 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
64 query_resource_all = Query.get('resource').select(resource_fields)
65 #page.enqueue_query(query_resource_all)
68 #lease_md = metadata.details_by_object('lease')
69 #lease_fields = [column['name'] for column in lease_md['column']]
71 #query_lease_all = Query.get('lease').select(lease_fields)
72 #page.enqueue_query(query_lease_all)
74 slice_md = metadata.details_by_object('slice')
75 slice_fields = [column['name'] for column in slice_md['column']]
76 print("SLICE RES VIEW fields = %s" % 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)
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'}")
85 # Columns checked by default in Columns_selector plugin
86 query_default_fields = ['hostname', 'type', 'facility_name', 'testbed_name', 'boot_state']
89 'hostname' : 'Resource name',
91 'facility_name' : 'Facility',
92 'testbed_name' : 'Testbed',
93 'boot_state' : 'Status',
99 # # - The record key is needed otherwise the storage of records
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',
113 # 'lease.start_time',
115 # # - The lease_id is important for NITOS identify already existing
126 # #'application.measurement_point.counter'
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')
138 # --------------------------------------------------------------------------
140 # resources as a list using datatable plugin
142 list_resources = QueryTable(
144 domid = 'resources-list',
147 query_all = query_resource_all,
148 mapping = QUERYTABLE_MAP,
149 default_fields = query_default_fields,
152 datatables_options = {
153 'iDisplayLength': 25,
154 'bLengthChange' : True,
160 # --------------------------------------------------------------------------
161 # RESERVED RESOURCES LIST
162 # resources as a list using datatable plugin
164 list_reserved_resources = SimpleList(
171 list_reserved_leases = SimpleList(
178 # list_reserved_resources = QueryTable(
180 # domid = 'resources-reserved-list',
181 # title = 'List view',
182 # query = sq_resource,
183 # query_all = sq_resource,
186 # datatables_options = {
187 # 'iDisplayLength': 25,
188 # 'bLengthChange' : True,
189 # 'bAutoWidth' : True,
193 # --------------------------------------------------------------------------
195 # list of fields to be applied on the query
196 # this will add/remove columns in QueryTable plugin
198 filter_column_editor = ColumnsEditor(
201 query_all = query_resource_all,
202 default_fields = query_default_fields,
203 title = "Select Columns",
204 domid = 'select-columns',
207 # --------------------------------------------------------------------------
209 # the resources part is made of a Tabs (Geographic, List),
211 map_resources = GoogleMap(
213 title = 'Geographic view',
214 domid = 'resources-map',
215 # tab's sons preferably turn this off
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,
224 # To center around Europe : 53,9 / 3
230 # --------------------------------------------------------------------------
231 # LEASES Nitos Scheduler
232 # Display the leases reservation timeslots of the resources
234 resources_as_scheduler2 = Scheduler2(
238 # this is the query at the core of the slice list
240 query_lease = sq_lease,
243 # --------------------------------------------------------------------------
244 # QueryUpdater (Pending Operations)
246 # pending_resources = QueryUpdaterPlugin(
248 # title = 'Pending operations',
249 # query = main_query,
251 # # start turned off, it will open up itself when stuff comes in
254 # outline_complete = True,
255 # username = request.user,
258 # --------------------------------------------------------------------------
260 # testbeds as a list of filters
262 network_md = metadata.details_by_object('network')
263 network_fields = [column['name'] for column in network_md['column']]
265 #query_networks = Query.get('network').select(network_fields)
266 #page.enqueue_query(query_networks)
268 filter_testbeds = TestbedsPlugin(
270 domid = 'testbeds-filter',
271 title = 'Filter by testbeds',
273 #query_networks = query_networks,
274 #init_key = "network_hrn",
276 #datatables_options = {
277 # 'iDisplayLength': 25,
278 # 'bLengthChange' : True,
279 # 'bAutoWidth' : True,
283 filter_status = FilterStatusPlugin(
285 domid = "filter-status",
292 username = request.user,
295 # --------------------------------------------------------------------------
296 # Ofelia OpenFlow Plugin
299 # plugin which display a "gathering resources" message
300 # waiting for all resources to be returned by manifold
301 # univbriswelcome = Univbris(
303 # title = 'univbris_welcome',
304 # domid = 'univbris_welcome',
305 # query = query_resource_all,
308 # univbrisfoamlist = UnivbrisFoam(
310 # title = 'univbris_foam_ports_selection',
311 # domid = 'univbris_foam_ports_selection',
312 # query = query_resource_all,
313 # query_all = query_resource_all,
314 # checkboxes = False,
315 # datatables_options = {
316 # 'iDisplayLength': 10,
317 # 'bLengthChange' : True,
318 # 'bAutoWidth' : True,
322 # #plugin which manages the different flowspaces that the user creates, and also sends flowspaces to manifold
323 # univbrisfvlist = UnivbrisFv(
325 # title = 'univbris_flowspace_selection',
326 # domid = 'univbris_flowspace_selection',
327 # query = sq_flowspace,
328 # query_all = query_resource_all,
329 # datatables_options = {
330 # 'iDisplayLength': 5,
331 # 'bLengthChange' : True,
332 # 'bAutoWidth' : True,
336 # #plugin which allows the definition of a single flowspace
337 # univbrisfvform = UnivbrisFvf(
339 # title = 'univbris_flowspace_form',
340 # domid = 'univbris_flowspace_form',
341 # query = query_resource_all,
343 # datatables_options = {
344 # 'iDisplayLength': 3,
345 # 'bLengthChange' : True,
346 # 'bAutoWidth' : True,
350 # #plugin which allows the definition the match criteria on a single OPTICAL flowspace
351 # univbrisofvform = UnivbrisFvfo(
353 # title = 'univbris_oflowspace_form',
354 # domid = 'univbris_oflowspace_form',
357 # datatables_options = {
358 # 'iDisplayLength': 3,
359 # 'bLengthChange' : True,
360 # 'bAutoWidth' : True,
364 # #plugin which display the gathered topology
365 # univbristopology = UnivbrisTopo(
367 # title = 'univbris_topology',
368 # domid = 'univbris_topology',
369 # query = query_resource_all,
372 # # --------------------------------------------------------------------------
373 # # Ofelia VTAM Plugin
376 # #plugin which display a table where an experimenter will add VMs to according to his needs
377 # # responsible to send the data to Manifold
378 # univbrisvtamplugin = UnivbrisVtamPlugin(
380 # title = 'univbris_vtam',
381 # domid = 'univbris_vtam',
383 # #query = sq_resource,
386 # #plugin which display a form where an experimenter will specify
387 # # in which testbed and which physical server to setup the VM
388 # univbrisvtamform = UnivbrisVtamForm(
390 # title = 'univbris_vtam_form',
391 # domid = 'univbris_vtam_form',
392 # query = query_resource_all,
394 # datatables_options = {
395 # 'iDisplayLength': 3,
396 # 'bLengthChange' : True,
397 # 'bAutoWidth' : True,
401 # # --------------------------------------------------------------------------
402 # # SLA View and accept dialog
404 # sla_dialog = SlaDialog(
406 # title = 'sla dialog',
407 # query = main_query,
409 # # start turned off, it will open up itself when stuff comes in
411 # domid = 'sla_dialog',
412 # outline_complete = True,
413 # username = request.user,
416 ## check user is pi or not
417 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
418 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
419 platform_details = execute_query(self.request, platform_query)
420 account_details = execute_query(self.request, account_query)
422 # XXX When session has expired, this is None and thus not iterable
423 for platform_detail in platform_details:
424 for account_detail in account_details:
425 if platform_detail['platform_id'] == account_detail['platform_id']:
426 if 'config' in account_detail and account_detail['config'] is not '':
427 account_config = json.loads(account_detail['config'])
428 if 'myslice' in platform_detail['platform']:
429 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
431 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
437 template_env['list_resources'] = list_resources.render(self.request)
438 template_env['list_reserved_resources'] = list_reserved_resources.render(self.request)
439 template_env['list_reserved_leases'] = list_reserved_leases.render(self.request)
441 template_env['columns_editor'] = filter_column_editor.render(self.request)
443 template_env['filter_testbeds'] = filter_testbeds.render(self.request)
444 template_env['filter_status'] = filter_status.render(self.request)
445 template_env['apply'] = apply.render(self.request)
447 template_env['map_resources'] = map_resources.render(self.request)
448 template_env['scheduler'] = resources_as_scheduler2.render(self.request)
451 # template_env['welcome'] = univbriswelcome.render(self.request)
452 # template_env['resources'] = univbrisfoamlist.render(self.request)
453 # template_env['flowspaces'] = univbrisfvlist.render(self.request)
454 # template_env['oflowspaces_form'] = univbrisofvform.render(self.request)
455 # template_env['flowspaces_form'] = univbrisfvform.render(self.request)
456 # template_env['topology'] = univbristopology.render(self.request)
457 # template_env['vms_list'] = univbrisvtamplugin.render(self.request)
458 # template_env['vm_form'] = univbrisvtamform.render(self.request)
460 # template_env['pending_resources'] = pending_resources.render(self.request)
461 # template_env['sla_dialog'] = '' # sla_dialog.render(self.request)
462 template_env["theme"] = self.theme
463 template_env["username"] = request.user
464 template_env["pi"] = pi
465 template_env["slice"] = slicename
466 template_env["section"] = "resources"
467 template_env["msg"] = msg
468 template_env.update(page.prelude_env())
470 return render_to_response(self.template, template_env, context_instance=RequestContext(request))