From: Thierry Parmentelat Date: Fri, 18 Jul 2014 12:15:12 +0000 (+0200) Subject: change designed for illustrative purposes X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=commitdiff_plain;h=refs%2Fheads%2Fvlan_id change designed for illustrative purposes shows how to use tags to implement a simple VLAN ids allocation scheme --- diff --git a/PLC/Methods/AddSlice.py b/PLC/Methods/AddSlice.py index bcae122..d371366 100644 --- a/PLC/Methods/AddSlice.py +++ b/PLC/Methods/AddSlice.py @@ -15,6 +15,17 @@ from PLC.Methods.UpdateSliceTag import UpdateSliceTag can_update = ['name', 'instantiation', 'url', 'description', 'max_nodes'] +# it would be much better to have this configured from the outside +# this is not, however, the subject of the current exercise +# so let's keep it simple for now +VLAN_RANGE = range(128,255) # this would be 128 .. 254 inclusively + +# also this would require the following code added in your Accessors_site.py +#define_accessors(current_module, Slice, 'VlanId', 'vlan_id', +# "slice/general", "the vlan_id allocated to that slice", +# get_roles=all_roles, set_roles=admin_roles) +# see https://svn.planet-lab.org/wiki/TagsAndAccessors for more details + class AddSlice(Method): """ Adds a new slice. Any fields specified in slice_fields are used, @@ -73,6 +84,40 @@ class AddSlice(Method): raise PLCInvalidArgument, "Invalid slice prefix %s in %s"%(login_base,name) site = sites[0] + #################### provisioning vlan_ids + # retrieve all alive slices + alive_slices=Slices(self.api,{},['slice_id','name','slice_tag_ids']) + # build the list of their ids + alive_slices_ids = [ s['slice_id'] for s in alive_slices ] + # retrieve all the 'vlan_id' slice tags applying to these slice_ids + vlan_slice_tags = SliceTags (self.api,{'slice_id':alive_slices_ids,'tagname':'vlan_id'}) + # hash them by the slice_tag_id + vlan_slice_tags_hashed_by_id = { slice_tag['slice_tag_id']:slice_tag for slice_tag in vlan_slice_tags } + # build the set of allocated vlans + allocated_vlan_ids = set() + for alive_slice in alive_slices: + # scan all slice tags attached to that slice + for slice_tag_id in alive_slice['slice_tag_ids']: + # discard the one that are not 'vlan_id' + if slice_tag_id not in vlan_slice_tags_hashed_by_id: continue + # retrive value and convert to int + slice_tag = vlan_slice_tags_hashed_by_id[slice_tag_id] + if slice_tag['tagname']=='vlan_id': + try: + allocated_vlan_ids.add(int(slice_tag['value'])) + except: + import traceback + traceback.print_exc() + pass + # find a free vlan_id + available=set(VLAN_RANGE) - allocated_vlan_ids + try: + assigned=available.pop() + # piggybacking the code below that sets tags; tags value need to be strings.. + tags['vlan_id']=str(assigned) + except: + raise Exception,"Cannot allocate slice %s, ran out of vlan_ids"%name + if 'admin' not in self.caller['roles']: if site['site_id'] not in self.caller['site_ids']: raise PLCPermissionDenied, "Slice prefix %s must match one of your sites' login_base"%login_base