<s> '''<STR_LIT>''' <EOL> import logging <EOL> from django . views . generic import simple <EOL> from django . http import HttpResponseRedirect , HttpResponseNotAllowed <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . db import transaction <EOL> from django . db . models import Q <EOL> from django . db . models . loading import cache <EOL> from django . conf import settings <EOL> from django import forms <EOL> from expedient . common . messaging . models import DatedMessage <EOL> from expedient . common . utils . views import generic_crud <EOL> from expedient . common . xmlrpc_serverproxy . forms import PasswordXMLRPCServerProxyForm <EOL> from expedient . common . permissions . decorators import require_objs_permissions_for_view <EOL> from expedient . common . permissions . utils import get_queryset_from_class , get_user_from_req , get_queryset <EOL> from expedient . clearinghouse . slice . models import Slice <EOL> from expedient . clearinghouse . resources . models import Resource <EOL> from expedient . clearinghouse . aggregate . models import Aggregate <EOL> from models import OpenFlowAggregate , OpenFlowSliceInfo , OpenFlowConnection <EOL> from models import NonOpenFlowConnection <EOL> from forms import OpenFlowAggregateForm , OpenFlowSliceInfoForm , OpenFlowSliceControllerForm <EOL> from forms import OpenFlowStaticConnectionForm , OpenFlowConnectionSelectionForm <EOL> from forms import NonOpenFlowStaticConnectionForm <EOL> from expedient . common . permissions . shortcuts import give_permission_to , must_have_permission <EOL> from openflow . plugin . vlans import * <EOL> from openflow . plugin . vlan . manager import VlanManager <EOL> from openflow . plugin . vlan . utils import create_slice_with_vlan_range <EOL> from openflow . plugin . models import OpenFlowAggregate , OpenFlowSwitch , OpenFlowInterface , OpenFlowInterfaceSliver , FlowSpaceRule , OpenFlowConnection , NonOpenFlowConnection <EOL> from expedient_geni . planetlab . models import PlanetLabNode , PlanetLabSliver , PlanetLabAggregate <EOL> from expedient . common . utils . plugins . resources . node import Node <EOL> from expedient . common . utils . plugins . resources . link import Link <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> MAX_ALLOWED_VLAN_RANGE = <NUM_LIT:20> <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset_from_class ( Aggregate ) , <EOL> methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def aggregate_create ( request ) : <EOL> return aggregate_crud ( request ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( OpenFlowAggregate , "<STR_LIT>" ) , <EOL> methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def aggregate_edit ( request , agg_id ) : <EOL> return aggregate_crud ( request , agg_id = agg_id ) <EOL> def aggregate_crud ( request , agg_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if agg_id != None : <EOL> aggregate = get_object_or_404 ( OpenFlowAggregate , pk = agg_id ) <EOL> client = aggregate . client <EOL> else : <EOL> aggregate = None <EOL> client = None <EOL> if request . method == "<STR_LIT:GET>" : <EOL> agg_form = OpenFlowAggregateForm ( instance = aggregate ) <EOL> client_form = PasswordXMLRPCServerProxyForm ( instance = client ) <EOL> elif request . method == "<STR_LIT:POST>" : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> agg_form = OpenFlowAggregateForm ( <EOL> data = request . POST , instance = aggregate ) <EOL> client_form = PasswordXMLRPCServerProxyForm ( <EOL> data = request . POST , instance = client ) <EOL> logger . debug ( "<STR_LIT>" ) <EOL> if client_form . is_valid ( ) and agg_form . is_valid ( ) : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> logger . debug ( "<STR_LIT>" % request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> client = client_form . save ( ) <EOL> aggregate = agg_form . save ( commit = False ) <EOL> aggregate . client = client <EOL> aggregate . save ( ) <EOL> agg_form . save_m2m ( ) <EOL> try : <EOL> info = aggregate . client . proxy . get_am_info ( ) <EOL> aggregate . vlan_auto_assignment = info [ "<STR_LIT>" ] <EOL> aggregate . flowspace_auto_approval = info [ "<STR_LIT>" ] <EOL> except Exception as e : <EOL> pass <EOL> try : <EOL> err = '<STR_LIT:U+0020>' <EOL> aggregate . client . proxy . checkFlowVisor ( ) <EOL> aggregate . setup_new_aggregate ( request . build_absolute_uri ( "<STR_LIT:/>" ) ) <EOL> except Exception as e : <EOL> err = str ( e ) <EOL> if err is not '<STR_LIT:U+0020>' : <EOL> if "<STR_LIT>" in err : <EOL> msg_type = DatedMessage . TYPE_WARNING <EOL> if agg_id : <EOL> flowvisor_msg = "<STR_LIT>" <EOL> else : <EOL> flowvisor_msg = "<STR_LIT>" <EOL> else : <EOL> flowvisor_msg = err <EOL> msg_type = DatedMessage . TYPE_ERROR <EOL> DatedMessage . objects . post_message_to_user ( <EOL> flowvisor_msg , user = request . user , msg_type = msg_type , <EOL> ) <EOL> return HttpResponseRedirect ( "<STR_LIT:/>" ) <EOL> aggregate . save ( ) <EOL> give_permission_to ( <EOL> "<STR_LIT>" , <EOL> aggregate , <EOL> request . user , <EOL> can_delegate = True <EOL> ) <EOL> give_permission_to ( <EOL> "<STR_LIT>" , <EOL> aggregate , <EOL> request . user , <EOL> can_delegate = True <EOL> ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( aggregate . name , err ) , <EOL> user = request . user , msg_type = DatedMessage . TYPE_SUCCESS , <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ aggregate . id ] ) ) <EOL> logger . debug ( "<STR_LIT>" ) <EOL> else : <EOL> return HttpResponseNotAllowed ( "<STR_LIT:GET>" , "<STR_LIT:POST>" ) <EOL> available = aggregate . check_status ( ) if agg_id else False <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : agg_form , <EOL> "<STR_LIT>" : client_form , <EOL> "<STR_LIT>" : not agg_id , <EOL> "<STR_LIT>" : aggregate , <EOL> "<STR_LIT>" : available , <EOL> "<STR_LIT>" : ( <EOL> ( '<STR_LIT>' , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % ( "<STR_LIT>" if agg_id else "<STR_LIT>" ) , <EOL> request . path ) , <EOL> ) <EOL> } , <EOL> ) <EOL> def aggregate_add_links ( request , agg_id ) : <EOL> """<STR_LIT>""" <EOL> aggregate = get_object_or_404 ( OpenFlowAggregate , id = agg_id ) <EOL> return handle_add_links ( <EOL> request , aggregate , <EOL> extra_context = { <EOL> "<STR_LIT>" : ( <EOL> ( '<STR_LIT>' , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" , args = [ agg_id ] ) ) , <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" , args = [ agg_id ] ) ) , <EOL> ) , <EOL> } <EOL> ) <EOL> def handle_add_links ( request , aggregate , <EOL> template = "<STR_LIT>" , <EOL> extra_context = { } ) : <EOL> """<STR_LIT>""" <EOL> must_have_permission ( <EOL> request . user , aggregate , "<STR_LIT>" ) <EOL> of_iface_filter = ( <EOL> ( Q ( src_iface__aggregate__id = aggregate . id ) <EOL> & ~ Q ( dst_iface__aggregate__id = aggregate . id ) ) <EOL> | <EOL> ( Q ( dst_iface__aggregate__id = aggregate . id ) <EOL> & ~ Q ( src_iface__aggregate__id = aggregate . id ) ) <EOL> ) <EOL> of_cnxn_qs = OpenFlowConnection . objects . filter ( <EOL> of_iface_filter ) . filter ( <EOL> src_iface__available = True , dst_iface__available = True ) <EOL> non_of_cnxn_qs = NonOpenFlowConnection . objects . filter ( <EOL> of_iface__aggregate__id = aggregate . id , <EOL> of_iface__available = True , <EOL> ) <EOL> types = [ ] <EOL> for app , model_name in getattr ( settings , "<STR_LIT>" , [ ] ) : <EOL> app_label = app . rpartition ( "<STR_LIT:.>" ) [ <NUM_LIT:2> ] <EOL> model = cache . get_model ( app_label , model_name ) <EOL> if not model : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( model_name , app ) ) <EOL> types . append ( model ) <EOL> resource_qs = Resource . objects . filter_for_classes ( types ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> new_cnxn_form = OpenFlowStaticConnectionForm ( <EOL> aggregate , request . POST ) <EOL> existing_links_form = OpenFlowConnectionSelectionForm ( <EOL> of_cnxn_qs , non_of_cnxn_qs , request . POST ) <EOL> new_other_cnxn_form = NonOpenFlowStaticConnectionForm ( <EOL> aggregate , resource_qs , request . POST ) <EOL> if "<STR_LIT>" in request . POST : <EOL> if new_cnxn_form . is_valid ( ) : <EOL> cnxns = new_cnxn_form . save_connections ( ) <EOL> if not cnxns [ <NUM_LIT:0> ] and not cnxns [ <NUM_LIT:1> ] : <EOL> msg = "<STR_LIT>" <EOL> elif not cnxns [ <NUM_LIT:0> ] or not cnxns [ <NUM_LIT:1> ] : <EOL> msg = "<STR_LIT>" <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( msg , aggregate . name ) , <EOL> user = request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> return HttpResponseRedirect ( request . path ) <EOL> elif "<STR_LIT>" in request . POST : <EOL> if new_other_cnxn_form . is_valid ( ) : <EOL> cnxn = new_other_cnxn_form . save ( ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % cnxn , <EOL> user = request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> return HttpResponseRedirect ( request . path ) <EOL> else : <EOL> if existing_links_form . is_valid ( ) : <EOL> cnxns = list ( existing_links_form . cleaned_data [ "<STR_LIT>" ] ) <EOL> cnxns . extend ( <EOL> list ( existing_links_form . cleaned_data [ "<STR_LIT>" ] ) ) <EOL> for c in cnxns : <EOL> c . delete ( ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( <EOL> len ( cnxns ) , aggregate . name ) , <EOL> user = request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> return HttpResponseRedirect ( request . path ) <EOL> else : <EOL> new_cnxn_form = OpenFlowStaticConnectionForm ( aggregate ) <EOL> new_other_cnxn_form = NonOpenFlowStaticConnectionForm ( <EOL> aggregate , resource_qs ) <EOL> if of_cnxn_qs . count ( ) + non_of_cnxn_qs . count ( ) : <EOL> existing_links_form = OpenFlowConnectionSelectionForm ( <EOL> of_cnxn_qs , non_of_cnxn_qs ) . as_table ( ) <EOL> else : <EOL> existing_links_form = None <EOL> ctx = { <EOL> "<STR_LIT>" : existing_links_form , <EOL> "<STR_LIT>" : new_cnxn_form . as_table ( ) , <EOL> "<STR_LIT>" : new_other_cnxn_form . as_table ( ) , <EOL> "<STR_LIT>" : aggregate , <EOL> } <EOL> ctx . update ( ** extra_context ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = template , <EOL> extra_context = ctx , <EOL> ) <EOL> def aggregate_add_to_slice ( request , agg_id , slice_id ) : <EOL> """<STR_LIT>""" <EOL> aggregate = get_object_or_404 ( OpenFlowAggregate , id = agg_id ) <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> return handle_add_to_slice ( request , aggregate , slice ) <EOL> def handle_add_to_slice ( request , aggregate , slice ) : <EOL> """<STR_LIT>""" <EOL> must_have_permission ( request . user , aggregate , "<STR_LIT>" ) <EOL> must_have_permission ( slice . project , aggregate , "<STR_LIT>" ) <EOL> next = request . GET . get ( "<STR_LIT>" , None ) <EOL> give_permission_to ( "<STR_LIT>" , aggregate , slice ) <EOL> return HttpResponseRedirect ( next if next else reverse ( "<STR_LIT>" , args = [ slice . id ] ) ) <EOL> def add_controller_to_slice ( request , agg_id , slice_id ) : <EOL> """<STR_LIT>""" <EOL> aggregate = get_object_or_404 ( OpenFlowAggregate , id = agg_id ) <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> must_have_permission ( request . user , aggregate , "<STR_LIT>" ) <EOL> must_have_permission ( slice . project , aggregate , "<STR_LIT>" ) <EOL> next = request . GET . get ( "<STR_LIT>" , None ) <EOL> try : <EOL> info = OpenFlowSliceInfo . objects . get ( slice = slice ) <EOL> except OpenFlowSliceInfo . DoesNotExist : <EOL> info = None <EOL> id = info . id if info else None <EOL> creating = info != None <EOL> def pre_save ( instance , created ) : <EOL> instance . slice = slice <EOL> def post_save ( instance , created ) : <EOL> give_permission_to ( "<STR_LIT>" , aggregate , slice ) <EOL> if not created : <EOL> try : <EOL> for agg in Aggregate . objects . all ( ) : <EOL> agg = agg . as_leaf_class ( ) <EOL> if agg . leaf_name == "<STR_LIT>" : <EOL> agg . change_slice_controller ( slice ) <EOL> except Exception , e : <EOL> error_msg = str ( e ) <EOL> print e <EOL> return generic_crud ( <EOL> request , id , OpenFlowSliceInfo , <EOL> form_class = OpenFlowSliceControllerForm , <EOL> extra_form_params = { "<STR_LIT>" : slice } , <EOL> template = "<STR_LIT>" , <EOL> redirect = lambda instance : next if next else reverse ( <EOL> "<STR_LIT>" , args = [ slice . id ] ) , <EOL> extra_context = { "<STR_LIT>" : creating , <EOL> "<STR_LIT>" : slice , <EOL> "<STR_LIT>" : aggregate } , <EOL> template_object_name = "<STR_LIT:info>" , <EOL> pre_save = pre_save , <EOL> post_save = post_save , <EOL> success_msg = lambda instance : "<STR_LIT>" % ( slice . name ) , <EOL> ) <EOL> def remove_controller_from_slice ( request , agg_id , slice_id ) : <EOL> """<STR_LIT>""" <EOL> aggregate = get_object_or_404 ( OpenFlowAggregate , id = agg_id ) <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> must_have_permission ( request . user , aggregate , "<STR_LIT>" ) <EOL> must_have_permission ( slice . project , aggregate , "<STR_LIT>" ) <EOL> error = "<STR_LIT>" <EOL> try : <EOL> OpenFlowSliceInfo . objects . get ( slice = slice ) . delete ( ) <EOL> if slice . started : <EOL> slice . stop ( request . user ) <EOL> except OpenFlowSliceInfo . DoesNotExist : <EOL> error = "<STR_LIT>" <EOL> except Exception as e : <EOL> error = "<STR_LIT>" % ( str ( slice . id ) . str ( e ) ) <EOL> print error <EOL> DatedMessage . objects . post_message_to_user ( <EOL> error , request . user , msg_type = DatedMessage . TYPE_ERROR , <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , <EOL> args = [ slice_id ] ) ) <EOL> def book_openflow ( request , slice_id ) : <EOL> """<STR_LIT>""" <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> enable_simple_mode = not check_existing_flowspace_in_slice ( slice ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> _update_openflow_resources ( request , slice ) <EOL> _update_planetlab_resources ( request , slice ) <EOL> slice . modified = True <EOL> slice . save ( ) <EOL> free_vlan = '<STR_LIT:None>' <EOL> alertMessage = '<STR_LIT>' <EOL> request . method <EOL> fsmode = request . POST [ '<STR_LIT>' ] <EOL> vlan_range = request . POST . get ( '<STR_LIT>' ) <EOL> if not vlan_range : <EOL> vlan_range = <NUM_LIT:1> <EOL> if fsmode == '<STR_LIT>' and enable_simple_mode : <EOL> try : <EOL> manager = VlanManager ( ) <EOL> free_vlan = manager . process ( slice , int ( vlan_range ) ) <EOL> except Exception as e : <EOL> fsmode = '<STR_LIT>' <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR , <EOL> ) <EOL> alertMessage = "<STR_LIT>" <EOL> return flowspace ( request , slice_id , fsmode , free_vlan , alertMessage ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , <EOL> args = [ slice_id ] ) ) <EOL> else : <EOL> ui_extra_context = { <EOL> "<STR_LIT>" : slice , <EOL> "<STR_LIT>" : enable_simple_mode , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % slice . project . name , reverse ( "<STR_LIT>" , args = [ slice . project . id ] ) ) , <EOL> ( "<STR_LIT>" % slice . name , reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) , <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) , <EOL> ) <EOL> } <EOL> from expedient . clearinghouse . urls import TOPOLOGY_GENERATOR <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = "<STR_LIT>" , <EOL> extra_context = dict ( TOPOLOGY_GENERATOR . load_ui_data ( slice ) . items ( ) + ui_extra_context . items ( ) ) , <EOL> ) <EOL> def slice_save_start ( request , slice_id ) : <EOL> """<STR_LIT>""" <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> slice . modified = True <EOL> slice . save ( ) <EOL> exp = "<STR_LIT>" <EOL> try : <EOL> if slice . started : <EOL> slice . start ( request . user ) <EOL> except Exception as e : <EOL> exp = str ( e ) <EOL> finally : <EOL> if exp : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( slice . name , exp ) , <EOL> request . user , msg_type = DatedMessage . TYPE_WARNING , <EOL> ) <EOL> else : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % slice . name , <EOL> request . user , msg_type = DatedMessage . TYPE_SUCCESS , <EOL> ) <EOL> def flowspace ( request , slice_id , fsmode = '<STR_LIT>' , free_vlan = None , alertMessage = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> class SliverMultipleChoiceField ( forms . ModelMultipleChoiceField ) : <EOL> def label_from_instance ( self , obj ) : <EOL> return "<STR_LIT:%s>" % obj . resource . as_leaf_class ( ) <EOL> def widget_attrs ( self , widget ) : <EOL> return { "<STR_LIT:class>" : "<STR_LIT>" } <EOL> def formfield_callback ( f ) : <EOL> if f . name == "<STR_LIT>" : <EOL> return SliverMultipleChoiceField ( <EOL> queryset = OpenFlowInterfaceSliver . objects . filter ( slice = slice ) , <EOL> initial = OpenFlowInterfaceSliver . objects . filter ( slice = slice ) ) <EOL> else : <EOL> return f . formfield ( ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> continue_to_start_slice = False <EOL> flowspace_form_number = <NUM_LIT:0> <EOL> if fsmode == '<STR_LIT>' : <EOL> flowspace_form_number = <NUM_LIT:1> <EOL> FSFormSet = forms . models . modelformset_factory ( <EOL> model = FlowSpaceRule , <EOL> formfield_callback = formfield_callback , <EOL> can_delete = True , <EOL> extra = flowspace_form_number , <EOL> ) <EOL> formset = FSFormSet ( <EOL> queryset = FlowSpaceRule . objects . filter ( <EOL> slivers__slice = slice ) . distinct ( ) , <EOL> ) <EOL> if formset . is_valid ( ) : <EOL> formset . save ( ) <EOL> if fsmode == '<STR_LIT>' : <EOL> formset = FSFormSet ( <EOL> request . POST , <EOL> queryset = FlowSpaceRule . objects . filter ( <EOL> slivers__slice = slice ) . distinct ( ) , <EOL> ) <EOL> if formset . is_valid ( ) : <EOL> formset . save ( ) <EOL> continue_to_start_slice = True <EOL> elif fsmode == '<STR_LIT>' : <EOL> try : <EOL> create_slice_with_vlan_range ( slice , free_vlan ) <EOL> continue_to_start_slice = True <EOL> except : <EOL> pass <EOL> if continue_to_start_slice : <EOL> slice_save_start ( request , slice_id ) <EOL> if fsmode == '<STR_LIT>' : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( request . path ) <EOL> elif request . method == "<STR_LIT:GET>" : <EOL> flowspace_form_contents = FlowSpaceRule . objects . filter ( slivers__slice = slice ) . distinct ( ) <EOL> flowspace_form_number = <NUM_LIT:1> <EOL> if "<STR_LIT>" in request . META : <EOL> if reverse ( "<STR_LIT>" , args = [ slice_id ] ) in request . META [ '<STR_LIT>' ] : <EOL> if not flowspace_form_contents : <EOL> flowspace_form_number = <NUM_LIT:1> <EOL> else : <EOL> flowspace_form_number = <NUM_LIT:0> <EOL> FSFormSet = forms . models . modelformset_factory ( <EOL> model = FlowSpaceRule , <EOL> formfield_callback = formfield_callback , <EOL> can_delete = True , <EOL> extra = flowspace_form_number , <EOL> ) <EOL> formset = FSFormSet ( <EOL> queryset = flowspace_form_contents , <EOL> ) <EOL> else : <EOL> return HttpResponseNotAllowed ( "<STR_LIT:GET>" , "<STR_LIT:POST>" ) <EOL> done = PlanetLabSliver . objects . filter ( slice = slice ) . count ( ) == <NUM_LIT:0> <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : slice , <EOL> "<STR_LIT>" : formset , <EOL> "<STR_LIT>" : alertMessage , <EOL> "<STR_LIT>" : done , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % slice . project . name , reverse ( "<STR_LIT>" , args = [ slice . project . id ] ) ) , <EOL> ( "<STR_LIT>" % slice . name , reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) , <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) , <EOL> ) , <EOL> } , <EOL> ) <EOL> def save_flowspace ( request , slice_id ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> class SliverMultipleChoiceField ( forms . ModelMultipleChoiceField ) : <EOL> def label_from_instance ( self , obj ) : <EOL> return "<STR_LIT:%s>" % obj . resource . as_leaf_class ( ) <EOL> def widget_attrs ( self , widget ) : <EOL> return { "<STR_LIT:class>" : "<STR_LIT>" } <EOL> def formfield_callback ( f ) : <EOL> if f . name == "<STR_LIT>" : <EOL> return SliverMultipleChoiceField ( <EOL> queryset = OpenFlowInterfaceSliver . objects . filter ( slice = slice ) ) <EOL> else : <EOL> return f . formfield ( ) <EOL> continue_to_start_slice = False <EOL> FSFormSet = forms . models . modelformset_factory ( <EOL> model = FlowSpaceRule , <EOL> formfield_callback = formfield_callback , <EOL> can_delete = True , <EOL> extra = <NUM_LIT:0> , <EOL> ) <EOL> formset = FSFormSet ( <EOL> request . POST , <EOL> queryset = FlowSpaceRule . objects . filter ( <EOL> slivers__slice = slice ) . distinct ( ) , <EOL> ) <EOL> if formset . is_valid ( ) : <EOL> formset . save ( ) <EOL> continue_to_start_slice = True <EOL> if continue_to_start_slice : <EOL> slice_save_start ( request , slice_id ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) <EOL> else : <EOL> return HttpResponseNotAllowed ( "<STR_LIT:POST>" ) <EOL> '''<STR_LIT>''' <EOL> def _update_openflow_resources ( request , slice ) : <EOL> """<STR_LIT>""" <EOL> iface_ids = map ( int , request . POST . getlist ( "<STR_LIT>" ) ) <EOL> ifaces = OpenFlowInterface . objects . filter ( id__in = iface_ids ) <EOL> for iface in ifaces : <EOL> sliver , created = OpenFlowInterfaceSliver . objects . get_or_create ( <EOL> slice = slice , resource = iface ) <EOL> if created : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( <EOL> iface , slice . name ) , <EOL> request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> to_del = OpenFlowInterfaceSliver . objects . exclude ( <EOL> resource__id__in = iface_ids ) . filter ( slice = slice ) <EOL> to_del . delete ( ) <EOL> def _update_planetlab_resources ( request , slice ) : <EOL> """<STR_LIT>""" <EOL> node_ids = map ( int , request . POST . getlist ( "<STR_LIT>" ) ) <EOL> nodes = PlanetLabNode . objects . filter ( id__in = node_ids ) <EOL> for node in nodes : <EOL> sliver , created = PlanetLabSliver . objects . get_or_create ( <EOL> slice = slice , resource = node ) <EOL> if created : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( <EOL> node , slice . name ) , <EOL> request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> to_del = PlanetLabSliver . objects . exclude ( <EOL> resource__id__in = node_ids ) . filter ( slice = slice ) <EOL> to_del . delete ( ) <EOL> def get_checked_ids ( slice ) : <EOL> checked_ids = [ ] <EOL> try : <EOL> checked_ids = list ( OpenFlowInterface . objects . filter ( <EOL> slice_set = slice ) . values_list ( "<STR_LIT:id>" , flat = True ) ) <EOL> checked_ids . extend ( PlanetLabNode . objects . filter ( <EOL> slice_set = slice ) . values_list ( "<STR_LIT:id>" , flat = True ) ) <EOL> except : <EOL> pass <EOL> return checked_ids <EOL> def get_controller_url ( slice ) : <EOL> try : <EOL> controller_url = OpenFlowSliceInfo . objects . get ( slice = slice ) . controller_url <EOL> except : <EOL> controller_url = "<STR_LIT>" <EOL> return controller_url <EOL> def get_openflow_aggregates ( slice ) : <EOL> aggs_filter = ( Q ( leaf_name = OpenFlowAggregate . __name__ . lower ( ) ) | <EOL> Q ( leaf_name = GCFOpenFlowAggregate . __name__ . lower ( ) ) ) <EOL> return slice . aggregates . filter ( aggs_filter ) <EOL> def get_planetlab_aggregates ( slice ) : <EOL> return slice . aggregates . filter ( leaf_name = PlanetLabAggregate . __name__ . lower ( ) ) <EOL> def get_gfs ( slice ) : <EOL> gfs_list = [ ] <EOL> try : <EOL> of_aggs = get_openflow_aggregates ( slice ) <EOL> for of_agg in of_aggs : <EOL> if of_agg . available : <EOL> gfs = of_agg . as_leaf_class ( ) . get_granted_flowspace ( of_agg . as_leaf_class ( ) . _get_slice_uuid ( slice ) ) <EOL> if not gfs : <EOL> gfs = of_agg . as_leaf_class ( ) . get_granted_flowspace ( of_agg . as_leaf_class ( ) . _get_slice_id ( slice ) ) <EOL> gfs_list . append ( [ of_agg . id , gfs ] ) <EOL> except : <EOL> pass <EOL> return gfs_list <EOL> def get_node_description ( node ) : <EOL> description = "<STR_LIT>" + node . name + "<STR_LIT>" <EOL> if len ( node . connection ) : <EOL> description += "<STR_LIT>" <EOL> for connection in node . connection : <EOL> description += "<STR_LIT>" + connection [ '<STR_LIT>' ] + "<STR_LIT>" + connection [ '<STR_LIT>' ] + "<STR_LIT>" + connection [ '<STR_LIT>' ] + "<STR_LIT>" ; <EOL> return description <EOL> def get_nodes_links ( slice ) : <EOL> nodes = [ ] <EOL> links = [ ] <EOL> id_to_idx = { } <EOL> agg_ids = [ ] <EOL> of_aggs = get_openflow_aggregates ( slice ) <EOL> pl_aggs = get_planetlab_aggregates ( slice ) <EOL> try : <EOL> image_url = reverse ( '<STR_LIT>' , args = ( "<STR_LIT>" , ) ) <EOL> except : <EOL> image_url = '<STR_LIT>' <EOL> for i , agg in enumerate ( of_aggs ) : <EOL> agg_ids . append ( agg . pk ) <EOL> switches = OpenFlowSwitch . objects . filter ( <EOL> aggregate__pk = agg . pk , <EOL> available = True , <EOL> ) <EOL> for s in switches : <EOL> id_to_idx [ s . id ] = len ( nodes ) <EOL> nodes . append ( Node ( name = s . name , value = s . id , description = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> image = image_url , aggregate = agg , <EOL> connection = [ ] <EOL> ) <EOL> ) <EOL> try : <EOL> image_url = reverse ( '<STR_LIT>' , args = ( "<STR_LIT>" , ) ) <EOL> except : <EOL> image_url = '<STR_LIT>' <EOL> for i , agg in enumerate ( pl_aggs ) : <EOL> agg_ids . append ( agg . pk ) <EOL> pl_nodes = PlanetLabNode . objects . filter ( <EOL> aggregate__pk = agg . pk , <EOL> available = True , <EOL> ) <EOL> for n in pl_nodes : <EOL> id_to_idx [ n . id ] = len ( nodes ) <EOL> nodes . append ( Node ( name = s . name , value = s . id , description = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> image = image_url , aggregate = agg <EOL> ) <EOL> ) <EOL> of_cnxn_qs = OpenFlowConnection . objects . filter ( <EOL> src_iface__aggregate__id__in = agg_ids , <EOL> src_iface__available = True , <EOL> dst_iface__aggregate__id__in = agg_ids , <EOL> dst_iface__available = True , <EOL> ) <EOL> non_of_cnxn_qs = NonOpenFlowConnection . objects . filter ( <EOL> of_iface__aggregate__id__in = agg_ids , <EOL> resource__id__in = id_to_idx . keys ( ) , <EOL> of_iface__available = True , <EOL> resource__available = True , <EOL> ) <EOL> sliceUUID = Slice . objects . get ( id = slice . id ) . uuid <EOL> for node in nodes : <EOL> try : <EOL> for cnxn in of_cnxn_qs : <EOL> cnx_exists = False <EOL> if node . value == cnxn . src_iface . switch . id : <EOL> for old_cnx in node . connection : <EOL> if ( old_cnx [ "<STR_LIT>" ] == str ( cnxn . dst_iface . switch . datapath_id ) and old_cnx [ "<STR_LIT>" ] == str ( cnxn . dst_iface . port_num ) ) : <EOL> cnx_exists = True <EOL> break <EOL> if not cnx_exists : <EOL> node . connection . append ( dict ( <EOL> src_port = str ( cnxn . src_iface . port_num ) , <EOL> target_port = str ( cnxn . dst_iface . port_num ) , <EOL> target_datapath = str ( cnxn . dst_iface . switch . datapath_id ) ) ) <EOL> elif node . value == cnxn . dst_iface . switch . id : <EOL> for old_cnx in node . connection : <EOL> if ( old_cnx [ "<STR_LIT>" ] == str ( cnxn . src_iface . switch . datapath_id ) and old_cnx [ "<STR_LIT>" ] == str ( cnxn . src_iface . port_num ) ) : <EOL> cnx_exists = True <EOL> break <EOL> if not cnx_exists : <EOL> node . connection . append ( dict ( <EOL> target_port = str ( cnxn . src_iface . port_num ) , <EOL> src_port = str ( cnxn . dst_iface . port_num ) , <EOL> target_datapath = str ( cnxn . src_iface . switch . datapath_id ) ) ) <EOL> node . description = get_node_description ( node ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> for cnxn in of_cnxn_qs : <EOL> try : <EOL> links . append ( Link ( source = str ( cnxn . src_iface . switch . id ) , target = str ( cnxn . dst_iface . switch . id ) , <EOL> value = "<STR_LIT>" % ( cnxn . src_iface . id , cnxn . dst_iface . id ) <EOL> ) <EOL> ) <EOL> except : <EOL> pass <EOL> for cnxn in non_of_cnxn_qs : <EOL> links . append ( Link ( source = str ( cnxn . of_iface . switch . id ) , target = str ( cnxn . resource . id ) , <EOL> value = "<STR_LIT>" % ( cnxn . of_iface . id , cnxn . resource . id ) <EOL> ) <EOL> ) <EOL> links . append ( Link ( source = str ( cnxn . resource . id ) , target = str ( cnxn . of_iface . switch . id ) , <EOL> value = "<STR_LIT>" % ( cnxn . resource . id , cnxn . of_iface . id ) <EOL> ) <EOL> ) <EOL> return [ nodes , links ] <EOL> def get_tree_ports ( of_aggs , pl_aggs ) : <EOL> """<STR_LIT>""" <EOL> tree = set ( ) <EOL> clusters = { } <EOL> of_agg_ids = of_aggs . values_list ( "<STR_LIT:id>" , flat = True ) <EOL> pl_agg_ids = pl_aggs . values_list ( "<STR_LIT:id>" , flat = True ) <EOL> of_cnxn_qs = OpenFlowConnection . objects . filter ( <EOL> src_iface__aggregate__id__in = of_agg_ids , <EOL> src_iface__available = True , <EOL> dst_iface__aggregate__id__in = of_agg_ids , <EOL> dst_iface__available = True , <EOL> ) <EOL> for cnxn in of_cnxn_qs : <EOL> a = cnxn . src_iface . switch . id <EOL> b = cnxn . dst_iface . switch . id <EOL> if a in clusters and b in clusters and clusters [ a ] == clusters [ b ] : <EOL> continue <EOL> tree . add ( cnxn . src_iface . id ) <EOL> tree . add ( cnxn . dst_iface . id ) <EOL> if a not in clusters : <EOL> clusters [ a ] = set ( [ a ] ) <EOL> if b not in clusters : <EOL> clusters [ b ] = set ( [ b ] ) <EOL> merged_cluster = clusters [ a ] | clusters [ b ] <EOL> for x in merged_cluster : <EOL> clusters [ x ] = merged_cluster <EOL> non_of_cnxn_qs = NonOpenFlowConnection . objects . filter ( <EOL> of_iface__aggregate__id__in = of_agg_ids , <EOL> of_iface__available = True , <EOL> resource__aggregate__id__in = pl_agg_ids , <EOL> resource__available = True , <EOL> ) <EOL> iface_ids = list ( non_of_cnxn_qs . values_list ( "<STR_LIT>" , flat = True ) ) <EOL> tree . update ( iface_ids ) <EOL> tree . update ( <EOL> PlanetLabNode . objects . filter ( <EOL> aggregate__id__in = pl_agg_ids , <EOL> available = True , <EOL> ) . values_list ( "<STR_LIT:id>" , flat = True ) , <EOL> ) <EOL> return list ( tree ) <EOL> def get_ui_data ( slice ) : <EOL> """<STR_LIT>""" <EOL> ui_context = dict ( ) <EOL> try : <EOL> ui_context [ '<STR_LIT>' ] = get_checked_ids ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = get_controller_url ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = { } <EOL> all_flowspaces = FlowSpaceRule . objects . filter ( slivers__slice = slice ) . distinct ( ) . order_by ( '<STR_LIT:id>' ) <EOL> for aggregate in slice . aggregates . all ( ) : <EOL> if aggregate . id not in ui_context [ '<STR_LIT>' ] : <EOL> ui_context [ '<STR_LIT>' ] [ aggregate . id ] = [ ] <EOL> for flowspace in all_flowspaces : <EOL> ui_context [ '<STR_LIT>' ] [ aggregate . id ] += FlowSpaceRule . objects . filter ( id = flowspace . id , slivers__resource__aggregate__id = aggregate . id ) . distinct ( ) . order_by ( "<STR_LIT:id>" ) <EOL> ui_context [ '<STR_LIT>' ] = get_planetlab_aggregates ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = get_openflow_aggregates ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = get_gfs ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = OpenFlowSwitch <EOL> ui_context [ '<STR_LIT>' ] = PlanetLabNode <EOL> ui_context [ '<STR_LIT>' ] = get_tree_ports ( ui_context [ '<STR_LIT>' ] , ui_context [ '<STR_LIT>' ] ) <EOL> ui_context [ '<STR_LIT>' ] , ui_context [ '<STR_LIT>' ] = get_nodes_links ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = range ( <NUM_LIT:1> , ( MAX_ALLOWED_VLAN_RANGE + <NUM_LIT:1> ) ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> return ui_context </s>
