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)
58 # get the list of valid slice users from the registry and make
59 # they are added to the slice
60 researchers = record.get('researcher', [])
61 for researcher in researchers:
63 person_records = registry.resolve(credential, researcher)
64 for record in person_records:
65 if record.get_type() in ['user']:
66 person_record = record
69 person_dict = person_record.as_dict()
70 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']], ['person_id', 'key_ids'])
72 # Create the person record
74 person_id=api.plshell.AddPerson(api.plauth, person_dict)
76 # The line below enables the user account on the remote aggregate soon after it is created.
77 # without this the user key is not transfered to the slice (as GetSlivers returns key of only enabled users),
78 # which prevents the user from login to the slice. We may do additional checks before enabling the user.
80 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
83 key_ids = persons[0]['key_ids']
85 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'], slicename)
87 # Get this users local keys
88 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
89 keys = [key['key'] for key in keylist]
91 # add keys that arent already there
92 for personkey in person_dict['keys']:
93 if personkey not in keys:
94 key = {'key_type': 'ssh', 'key': personkey}
95 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
97 # find out where this slice is currently running
98 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'], ['hostname'])
99 hostnames = [node['hostname'] for node in nodelist]
101 # remove nodes not in rspec
102 deleted_nodes = list(set(hostnames).difference(nodes))
103 # add nodes from rspec
104 added_nodes = list(set(nodes).difference(hostnames))
106 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
107 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
112 def get_rspec(api, hrn):
116 # Convert HRN to slice name
117 # Get SliceTags for the slice
119 # Construct LinkSpecs from the topo_rspec SliceTags
120 # The first field is the NodeId of the remote node.
121 # So the endpoints are the SliceTag node and the remote node.
124 # - avoid duplicates?
125 # - verify both ends of the link?
128 # Return canned response for now...
130 r.parseFile(SFA_VINI_DEFAULT_RSPEC)
136 def create_slice(api, hrn, xml):
139 rspec = r.toGenDict()
141 # Check request against current allocations
144 nodes = rspec_to_nodeset(rspec)
145 create_slice_vini_aggregate(api, hrn, nodes)
147 # Add VINI-specific topology attributes to slice here
151 def rspec_to_nodeset(rspec):
155 sitespecs = rspec['Rspec'][0]['Capacity'][0]['NetSpec'][0]['SiteSpec']
157 for node in s['NodeSpec']:
158 nodedict[node['name'][0]] = node['hostname'][0]
160 linkspecs = rspec['Rspec'][0]['Request'][0]['NetSpec'][0]['LinkSpec']
162 for e in l['endpoint']:
163 nodes.add(nodedict[e])
173 r.parseFile(sys.argv[1])
174 rspec = r.toGenDict()
175 create_slice(None,'plc',rspec)
177 if __name__ == "__main__":