1 from django.template import RequestContext
2 from django.shortcuts import render_to_response
4 from manifold.core.query import Query, AnalyzedQuery
5 from manifoldapi.manifoldapi import execute_query
8 from django.views.generic.base import TemplateView
10 from unfold.loginrequired import LoginRequiredView
11 from django.http import HttpResponse
12 from django.shortcuts import render
14 from unfold.page import Page
16 from myslice.configengine import ConfigEngine
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
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
36 from plugins.columns_editor import ColumnsEditor
37 from plugins.sladialog import SlaDialog
38 from plugins.lists.simplelist import SimpleList
40 from myslice.theme import ThemeView
42 class SliceResourceView (LoginRequiredView, ThemeView):
43 template_name = "slice-resource-view.html"
45 def get(self, request, slicename):
47 if request.GET.get('message') :
48 msg = "Slice successfully updated"
53 metadata = page.get_metadata()
54 page.expose_js_metadata()
56 resource_md = metadata.details_by_object('resource')
57 resource_fields = [column['name'] for column in resource_md['column']]
59 user_md = metadata.details_by_object('user')
60 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
62 query_resource_all = Query.get('resource').select(resource_fields)
63 #page.enqueue_query(query_resource_all)
66 #lease_md = metadata.details_by_object('lease')
67 #lease_fields = [column['name'] for column in lease_md['column']]
69 #query_lease_all = Query.get('lease').select(lease_fields)
70 #page.enqueue_query(query_lease_all)
72 slice_md = metadata.details_by_object('slice')
73 slice_fields = [column['name'] for column in slice_md['column']]
74 print "SLICE RES VIEW fields = %s" % slice_fields
75 # TODO The query to run is embedded in the URL
76 # Example: select slice_hrn, resource.urn, lease.resource, lease.start_time, lease.end_time from slice where slice_hrn == "ple.upmc.myslicedemo"
77 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
78 main_query.select(slice_fields)
80 # Columns shown by default in Query_table plugin
81 page.expose_js_var("QUERYTABLE_MAP","{'Resource name': 'hostname', 'Type': 'type', 'Facility': 'facility_name','Testbed': 'testbed_name', 'Status':'boot_state'}")
83 # Columns checked by default in Columns_selector plugin
84 query_default_fields = ['hostname', 'type', 'facility_name', 'testbed_name', 'boot_state']
87 'hostname' : 'Resource name',
89 'facility_name' : 'Facility',
90 'testbed_name' : 'Testbed',
91 'boot_state' : 'Status',
97 # # - The record key is needed otherwise the storage of records
104 # 'resource.hostname', 'resource.type',
105 # # - The facility_name and testbed_name are required for the
106 # # testbeds plugin to properly work.
107 # 'resource.facility_name',
108 # 'resource.testbed_name',
111 # 'lease.start_time',
113 # # - The lease_id is important for NITOS identify already existing
124 # #'application.measurement_point.counter'
126 # for internal use in the querytable plugin;
127 # needs to be a unique column present for each returned record
128 main_query_init_key = 'urn'
129 aq = AnalyzedQuery(main_query, metadata=metadata)
130 page.enqueue_query(main_query, analyzed_query=aq)
131 sq_resource = aq.subquery('resource')
132 sq_lease = aq.subquery('lease')
133 #sq_flowspace = aq.subquery('flowspace')
134 #sq_vms = aq.subquery('vms')
136 # --------------------------------------------------------------------------
138 # resources as a list using datatable plugin
140 list_resources = QueryTable(
142 domid = 'resources-list',
145 query_all = query_resource_all,
146 mapping = QUERYTABLE_MAP,
147 default_fields = query_default_fields,
150 datatables_options = {
151 'iDisplayLength': 25,
152 'bLengthChange' : True,
158 # --------------------------------------------------------------------------
159 # RESERVED RESOURCES LIST
160 # resources as a list using datatable plugin
162 list_reserved_resources = SimpleList(
169 list_reserved_leases = SimpleList(
176 # list_reserved_resources = QueryTable(
178 # domid = 'resources-reserved-list',
179 # title = 'List view',
180 # query = sq_resource,
181 # query_all = sq_resource,
184 # datatables_options = {
185 # 'iDisplayLength': 25,
186 # 'bLengthChange' : True,
187 # 'bAutoWidth' : True,
191 # --------------------------------------------------------------------------
193 # list of fields to be applied on the query
194 # this will add/remove columns in QueryTable plugin
196 filter_column_editor = ColumnsEditor(
199 query_all = query_resource_all,
200 default_fields = query_default_fields,
201 title = "Select Columns",
202 domid = 'select-columns',
205 # --------------------------------------------------------------------------
207 # the resources part is made of a Tabs (Geographic, List),
209 map_resources = GoogleMap(
211 title = 'Geographic view',
212 domid = 'resources-map',
213 # tab's sons preferably turn this off
216 # this key is the one issued by google
217 googlemap_api_key = ConfigEngine().googlemap_api_key(),
218 # the key to use at init-time
219 init_key = main_query_init_key,
222 # To center around Europe : 53,9 / 3
228 # --------------------------------------------------------------------------
229 # LEASES Nitos Scheduler
230 # Display the leases reservation timeslots of the resources
232 resources_as_scheduler2 = Scheduler2(
236 # this is the query at the core of the slice list
238 query_lease = sq_lease,
241 # --------------------------------------------------------------------------
242 # QueryUpdater (Pending Operations)
244 # pending_resources = QueryUpdaterPlugin(
246 # title = 'Pending operations',
247 # query = main_query,
249 # # start turned off, it will open up itself when stuff comes in
252 # outline_complete = True,
253 # username = request.user,
256 # --------------------------------------------------------------------------
258 # testbeds as a list of filters
260 network_md = metadata.details_by_object('network')
261 network_fields = [column['name'] for column in network_md['column']]
263 #query_networks = Query.get('network').select(network_fields)
264 #page.enqueue_query(query_networks)
266 filter_testbeds = TestbedsPlugin(
268 domid = 'testbeds-filter',
269 title = 'Filter by testbeds',
271 #query_networks = query_networks,
272 #init_key = "network_hrn",
274 #datatables_options = {
275 # 'iDisplayLength': 25,
276 # 'bLengthChange' : True,
277 # 'bAutoWidth' : True,
281 filter_status = FilterStatusPlugin(
283 domid = "filter-status",
290 username = request.user,
293 # --------------------------------------------------------------------------
294 # Ofelia OpenFlow Plugin
297 # plugin which display a "gathering resources" message
298 # waiting for all resources to be returned by manifold
299 # univbriswelcome = Univbris(
301 # title = 'univbris_welcome',
302 # domid = 'univbris_welcome',
303 # query = query_resource_all,
306 # univbrisfoamlist = UnivbrisFoam(
308 # title = 'univbris_foam_ports_selection',
309 # domid = 'univbris_foam_ports_selection',
310 # query = query_resource_all,
311 # query_all = query_resource_all,
312 # checkboxes = False,
313 # datatables_options = {
314 # 'iDisplayLength': 10,
315 # 'bLengthChange' : True,
316 # 'bAutoWidth' : True,
320 # #plugin which manages the different flowspaces that the user creates, and also sends flowspaces to manifold
321 # univbrisfvlist = UnivbrisFv(
323 # title = 'univbris_flowspace_selection',
324 # domid = 'univbris_flowspace_selection',
325 # query = sq_flowspace,
326 # query_all = query_resource_all,
327 # datatables_options = {
328 # 'iDisplayLength': 5,
329 # 'bLengthChange' : True,
330 # 'bAutoWidth' : True,
334 # #plugin which allows the definition of a single flowspace
335 # univbrisfvform = UnivbrisFvf(
337 # title = 'univbris_flowspace_form',
338 # domid = 'univbris_flowspace_form',
339 # query = query_resource_all,
341 # datatables_options = {
342 # 'iDisplayLength': 3,
343 # 'bLengthChange' : True,
344 # 'bAutoWidth' : True,
348 # #plugin which allows the definition the match criteria on a single OPTICAL flowspace
349 # univbrisofvform = UnivbrisFvfo(
351 # title = 'univbris_oflowspace_form',
352 # domid = 'univbris_oflowspace_form',
355 # datatables_options = {
356 # 'iDisplayLength': 3,
357 # 'bLengthChange' : True,
358 # 'bAutoWidth' : True,
362 # #plugin which display the gathered topology
363 # univbristopology = UnivbrisTopo(
365 # title = 'univbris_topology',
366 # domid = 'univbris_topology',
367 # query = query_resource_all,
370 # # --------------------------------------------------------------------------
371 # # Ofelia VTAM Plugin
374 # #plugin which display a table where an experimenter will add VMs to according to his needs
375 # # responsible to send the data to Manifold
376 # univbrisvtamplugin = UnivbrisVtamPlugin(
378 # title = 'univbris_vtam',
379 # domid = 'univbris_vtam',
381 # #query = sq_resource,
384 # #plugin which display a form where an experimenter will specify
385 # # in which testbed and which physical server to setup the VM
386 # univbrisvtamform = UnivbrisVtamForm(
388 # title = 'univbris_vtam_form',
389 # domid = 'univbris_vtam_form',
390 # query = query_resource_all,
392 # datatables_options = {
393 # 'iDisplayLength': 3,
394 # 'bLengthChange' : True,
395 # 'bAutoWidth' : True,
399 # # --------------------------------------------------------------------------
400 # # SLA View and accept dialog
402 # sla_dialog = SlaDialog(
404 # title = 'sla dialog',
405 # query = main_query,
407 # # start turned off, it will open up itself when stuff comes in
409 # domid = 'sla_dialog',
410 # outline_complete = True,
411 # username = request.user,
414 ## check user is pi or not
415 platform_query = Query().get('local:platform').select('platform_id','platform','gateway_type','disabled')
416 account_query = Query().get('local:account').select('user_id','platform_id','auth_type','config')
417 platform_details = execute_query(self.request, platform_query)
418 account_details = execute_query(self.request, account_query)
420 # XXX When session has expired, this is None and thus not iterable
421 for platform_detail in platform_details:
422 for account_detail in account_details:
423 if platform_detail['platform_id'] == account_detail['platform_id']:
424 if 'config' in account_detail and account_detail['config'] is not '':
425 account_config = json.loads(account_detail['config'])
426 if 'myslice' in platform_detail['platform']:
427 acc_auth_cred = account_config.get('delegated_authority_credentials','N/A')
429 if acc_auth_cred == {} or acc_auth_cred == 'N/A':
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)
439 template_env['columns_editor'] = filter_column_editor.render(self.request)
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)
445 template_env['map_resources'] = map_resources.render(self.request)
446 template_env['scheduler'] = resources_as_scheduler2.render(self.request)
449 # template_env['welcome'] = univbriswelcome.render(self.request)
450 # template_env['resources'] = univbrisfoamlist.render(self.request)
451 # template_env['flowspaces'] = univbrisfvlist.render(self.request)
452 # template_env['oflowspaces_form'] = univbrisofvform.render(self.request)
453 # template_env['flowspaces_form'] = univbrisfvform.render(self.request)
454 # template_env['topology'] = univbristopology.render(self.request)
455 # template_env['vms_list'] = univbrisvtamplugin.render(self.request)
456 # template_env['vm_form'] = univbrisvtamform.render(self.request)
458 # template_env['pending_resources'] = pending_resources.render(self.request)
459 # template_env['sla_dialog'] = '' # sla_dialog.render(self.request)
460 template_env["theme"] = self.theme
461 template_env["username"] = request.user
462 template_env["pi"] = pi
463 template_env["slice"] = slicename
464 template_env["section"] = "resources"
465 template_env["msg"] = msg
466 template_env.update(page.prelude_env())
468 return render_to_response(self.template, template_env, context_instance=RequestContext(request))