1 from sfa.util.faults import *
2 from sfa.util.rspec import Rspec
3 from sfa.server.registry import Registries
7 SFA_VINI_DEFAULT_RSPEC = '/etc/sfa/vini.rspec'
10 Copied from create_slice_aggregate() in sfa.plc.slices
12 def create_slice_vini_aggregate(api, hrn, nodes):
13 # Get the slice record from geni
15 registries = Registries(api)
16 registry = registries[api.hrn]
17 credential = api.getCredential()
18 records = registry.resolve(credential, hrn)
19 for record in records:
20 if record.get_type() in ['slice']:
21 slice = record.as_dict()
23 raise RecordNotFound(hrn)
25 # Make sure slice exists at plc, if it doesnt add it
26 slicename = hrn_to_pl_slicename(hrn)
27 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
29 parts = slicename.split("_")
31 # if site doesnt exist add it
32 sites = api.plshell.GetSites(api.plauth, [login_base])
34 authority = get_authority(hrn)
35 site_records = registry.resolve(credential, authority)
38 raise RecordNotFound(authority)
39 site_record = site_records[0]
40 site = site_record.as_dict()
44 site_id = api.plshell.AddSite(api.plauth, site)
49 slice_keys = ['name', 'url', 'description']
50 for key in slice_keys:
51 if key in slice and slice[key]:
52 slice_fields[key] = slice[key]
53 api.plshell.AddSlice(api.plauth, slice_fields)
59 # get the list of valid slice users from the registry and make
60 # they are added to the slice
61 researchers = record.get('researcher', [])
62 for researcher in researchers:
64 person_records = registry.resolve(credential, researcher)
65 for record in person_records:
66 if record.get_type() in ['user']:
67 person_record = record
70 person_dict = person_record.as_dict()
71 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
72 ['person_id', 'key_ids'])
74 # Create the person record
76 person_id=api.plshell.AddPerson(api.plauth, person_dict)
78 # The line below enables the user account on the remote aggregate
79 # soon after it is created.
80 # without this the user key is not transfered to the slice
81 # (as GetSlivers returns key of only enabled users),
82 # which prevents the user from login to the slice.
83 # We may do additional checks before enabling the user.
85 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
88 key_ids = persons[0]['key_ids']
90 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
93 # Get this users local keys
94 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
95 keys = [key['key'] for key in keylist]
97 # add keys that arent already there
98 for personkey in person_dict['keys']:
99 if personkey not in keys:
100 key = {'key_type': 'ssh', 'key': personkey}
101 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
103 # find out where this slice is currently running
104 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
106 hostnames = [node['hostname'] for node in nodelist]
108 # remove nodes not in rspec
109 deleted_nodes = list(set(hostnames).difference(nodes))
110 # add nodes from rspec
111 added_nodes = list(set(nodes).difference(hostnames))
113 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
114 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
118 def get_rspec(api, hrn):
122 # Convert HRN to slice name
123 # Get SliceTags for the slice
125 # Construct LinkSpecs from the topo_rspec SliceTags
126 # The first field is the NodeId of the remote node.
127 # So the endpoints are the SliceTag node and the remote node.
130 # - avoid duplicates?
131 # - verify both ends of the link?
134 # Return canned response for now...
136 r.parseFile(SFA_VINI_DEFAULT_RSPEC)
142 def create_slice(api, hrn, xml):
145 rspec = r.toGenDict()
147 # Check request against current allocations
150 nodes = rspec_to_nodeset(rspec)
151 create_slice_vini_aggregate(api, hrn, nodes)
153 # Add VINI-specific topology attributes to slice here
157 def rspec_to_nodeset(rspec):
161 sitespecs = rspec['Rspec'][0]['Capacity'][0]['NetSpec'][0]['SiteSpec']
163 for node in s['NodeSpec']:
164 nodedict[node['name'][0]] = node['hostname'][0]
166 linkspecs = rspec['Rspec'][0]['Request'][0]['NetSpec'][0]['LinkSpec']
168 for e in l['endpoint']:
169 nodes.add(nodedict[e])
179 r.parseFile(sys.argv[1])
180 rspec = r.toGenDict()
181 create_slice(None,'plc',rspec)
183 if __name__ == "__main__":