1 from sfa.util.faults import *
2 from sfa.util.misc import *
3 from sfa.util.rspec import Rspec
4 from sfa.server.registry import Registries
5 from sfa.plc.nodes import *
9 SFA_VINI_DEFAULT_RSPEC = '/etc/sfa/vini.rspec'
12 def __init__(self, node):
13 self.id = node['node_id']
14 self.hostname = node['hostname']
15 self.shortname = self.hostname.replace('.vini-veritas.net', '')
16 self.site_id = node['site_id']
17 self.ipaddr = socket.gethostbyname(self.hostname)
19 def get_link_id(self, remote):
20 if self.id < remote.id:
21 link = (self.id<<7) + remote.id
23 link = (remote.id<<7) + self.id
26 def get_iface_id(self, remote):
27 if self.id < remote.id:
33 def get_virt_ip(self, remote):
34 link = self.get_link_id(remote)
35 iface = self.get_iface_id(remote)
37 second = ((link & 0x3f)<<2) + iface
38 return "192.168.%d.%d" % (first, second)
40 def get_virt_net(self, remote):
41 link = self.get_link_id(remote)
43 second = (link & 0x3f)<<2
44 return "192.168.%d.%d/30" % (first, second)
46 def get_site(self, sites):
47 return sites[self.site_id]
49 def adjacent_nodes(self, sites, nodes, node_ids):
50 mysite = self.get_site(sites)
51 adj_ids = mysite.adj_node_ids.intersection(node_ids)
54 adj_nodes.append(nodes[id])
57 def init_rspecs(self):
60 def add_rspec(self, remote):
61 my_ip = self.get_virt_ip(remote)
62 remote_ip = remote.get_virt_ip(self)
63 net = self.get_virt_net(remote)
64 rspec = remote.id, remote.ipaddr, "1Mbit", my_ip, remote_ip, net
65 self.rspecs.append(rspec)
69 def __init__(self, site):
70 self.id = site['site_id']
71 self.node_ids = site['node_ids']
72 self.adj_site_ids = set()
73 self.adj_node_ids = set()
75 def get_sitenodes(self, nodes):
77 for i in self.node_ids:
81 def add_adjacency(self, site):
82 self.adj_site_ids.add(site.id)
83 for n in site.node_ids:
84 self.adj_node_ids.add(n)
88 def __init__(self, slice):
89 self.id = slice['slice_id']
90 self.name = slice['name']
91 self.node_ids = set(slice['node_ids'])
92 self.slice_tag_ids = slice['slice_tag_ids']
94 def get_tag(self, tagname, slicetags, node = None):
95 for i in self.slice_tag_ids:
97 if tag.tagname == tagname:
98 if (not node) or (node.id == tag.node_id):
103 def get_nodes(self, nodes):
105 for id in self.node_ids:
110 # Add a new slice tag
111 def add_tag(self, tagname, value, slicetags, node = None):
112 record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value}
114 record['node_id'] = node.id
116 record['node_id'] = None
117 tag = Slicetag(record)
118 slicetags[tag.id] = tag
119 self.slice_tag_ids.append(tag.id)
124 # Update a slice tag if it exists, else add it
125 def update_tag(self, tagname, value, slicetags, node = None):
126 tag = self.get_tag(tagname, slicetags, node)
127 if tag and tag.value == value:
133 tag = self.add_tag(tagname, value, slicetags, node)
136 def assign_egre_key(self, slicetags):
137 if not self.get_tag('egre_key', slicetags):
139 key = free_egre_key(slicetags)
140 self.update_tag('egre_key', key, slicetags)
142 # Should handle this case...
146 def turn_on_netns(self, slicetags):
147 tag = self.get_tag('netns', slicetags)
148 if (not tag) or (tag.value != '1'):
149 self.update_tag('netns', '1', slicetags)
152 def turn_off_netns(self, slicetags):
153 tag = self.get_tag('netns', slicetags)
154 if tag and (tag.value != '0'):
158 def add_cap_net_admin(self, slicetags):
159 tag = self.get_tag('capabilities', slicetags)
161 caps = tag.value.split(',')
163 if cap == "CAP_NET_ADMIN":
166 newcaps = "CAP_NET_ADMIN," + tag.value
167 self.update_tag('capabilities', newcaps, slicetags)
169 self.add_tag('capabilities', 'CAP_NET_ADMIN', slicetags)
172 def remove_cap_net_admin(self, slicetags):
173 tag = self.get_tag('capabilities', slicetags)
175 caps = tag.value.split(',')
178 if cap != "CAP_NET_ADMIN":
181 value = ','.join(newcaps)
182 self.update_tag('capabilities', value, slicetags)
187 # Update the vsys/setup-link and vsys/setup-nat slice tags.
188 def add_vsys_tags(self, slicetags):
190 for i in self.slice_tag_ids:
192 if tag.tagname == 'vsys':
193 if tag.value == 'setup-link':
195 elif tag.value == 'setup-nat':
198 self.add_tag('vsys', 'setup-link', slicetags)
200 self.add_tag('vsys', 'setup-nat', slicetags)
206 def __init__(self, tag):
207 self.id = tag['slice_tag_id']
209 # Make one up for the time being...
210 self.id = Slicetag.newid
212 self.slice_id = tag['slice_id']
213 self.tagname = tag['tagname']
214 self.value = tag['value']
215 self.node_id = tag['node_id']
220 # Mark a tag as deleted
225 def write(self, slices, nodes, dryrun):
229 UpdateSliceTag(self.id, self.value)
231 AddSliceTag(self.slice_id, self.tagname, self.value, self.node_id)
232 elif self.deleted and int(self.id) > 0:
233 DeleteSliceTag(self.id)
235 slice = slices[self.slice_id].name
237 node = nodes[tag.node_id].hostname
240 self.value = "deleted"
241 elif not self.changed:
242 self.value = "no change"
246 print "[%s] %s: %s (%s, %s)" % (self.id, self.tagname, self.value, slice, node)
248 print "[%s] %s: %s (%s)" % (self.id, self.tagname, self.value, slice)
252 Create a dictionary of site objects keyed by site ID
256 for site in GetSites():
257 t = site['site_id'], Site(site)
263 Create a dictionary of node objects keyed by node ID
267 for node in api.plshell.GetNodes(api.plauth):
268 t = node['node_id'], Node(node)
273 Create a dictionary of slice objects keyed by slice ID
275 def get_slice(api, slicename):
276 slice = api.plshell.GetSlices(api.plauth, [slicename])
278 return Slice(slice[0])
283 Create a dictionary of slicetag objects keyed by slice tag ID
285 def get_slice_tags(api):
287 for tag in api.plshell.GetSliceTags(api.plauth):
288 t = tag['slice_tag_id'], Slicetag(tag)
295 def free_egre_key(slicetags):
299 if tag.tagname == 'egre_key':
300 used.add(int(tag.value))
302 for i in range(1, 256):
307 raise KeyError("No more EGRE keys available")
313 Copied from create_slice_aggregate() in sfa.plc.slices
315 def create_slice_vini_aggregate(api, hrn, nodes):
316 # Get the slice record from geni
318 registries = Registries(api)
319 registry = registries[api.hrn]
320 credential = api.getCredential()
321 records = registry.resolve(credential, hrn)
322 for record in records:
323 if record.get_type() in ['slice']:
324 slice = record.as_dict()
326 raise RecordNotFound(hrn)
328 # Make sure slice exists at plc, if it doesnt add it
329 slicename = hrn_to_pl_slicename(hrn)
330 slices = api.plshell.GetSlices(api.plauth, [slicename], ['node_ids'])
332 parts = slicename.split("_")
333 login_base = parts[0]
334 # if site doesnt exist add it
335 sites = api.plshell.GetSites(api.plauth, [login_base])
337 authority = get_authority(hrn)
338 site_records = registry.resolve(credential, authority)
341 raise RecordNotFound(authority)
342 site_record = site_records[0]
343 site = site_record.as_dict()
347 site_id = api.plshell.AddSite(api.plauth, site)
352 slice_keys = ['name', 'url', 'description']
353 for key in slice_keys:
354 if key in slice and slice[key]:
355 slice_fields[key] = slice[key]
356 api.plshell.AddSlice(api.plauth, slice_fields)
358 slice['node_ids'] = 0
362 # get the list of valid slice users from the registry and make
363 # they are added to the slice
364 researchers = record.get('researcher', [])
365 for researcher in researchers:
367 person_records = registry.resolve(credential, researcher)
368 for record in person_records:
369 if record.get_type() in ['user']:
370 person_record = record
371 if not person_record:
373 person_dict = person_record.as_dict()
374 persons = api.plshell.GetPersons(api.plauth, [person_dict['email']],
375 ['person_id', 'key_ids'])
377 # Create the person record
379 person_id=api.plshell.AddPerson(api.plauth, person_dict)
381 # The line below enables the user account on the remote aggregate
382 # soon after it is created.
383 # without this the user key is not transfered to the slice
384 # (as GetSlivers returns key of only enabled users),
385 # which prevents the user from login to the slice.
386 # We may do additional checks before enabling the user.
388 api.plshell.UpdatePerson(api.plauth, person_id, {'enabled' : True})
391 key_ids = persons[0]['key_ids']
393 api.plshell.AddPersonToSlice(api.plauth, person_dict['email'],
396 # Get this users local keys
397 keylist = api.plshell.GetKeys(api.plauth, key_ids, ['key'])
398 keys = [key['key'] for key in keylist]
400 # add keys that arent already there
401 for personkey in person_dict['keys']:
402 if personkey not in keys:
403 key = {'key_type': 'ssh', 'key': personkey}
404 api.plshell.AddPersonKey(api.plauth, person_dict['email'], key)
406 # find out where this slice is currently running
407 nodelist = api.plshell.GetNodes(api.plauth, slice['node_ids'],
409 hostnames = [node['hostname'] for node in nodelist]
411 # remove nodes not in rspec
412 deleted_nodes = list(set(hostnames).difference(nodes))
413 # add nodes from rspec
414 added_nodes = list(set(nodes).difference(hostnames))
416 api.plshell.AddSliceToNodes(api.plauth, slicename, added_nodes)
417 api.plshell.DeleteSliceFromNodes(api.plauth, slicename, deleted_nodes)
421 def get_rspec(api, hrn):
424 default.parseFile(SFA_VINI_DEFAULT_RSPEC)
427 slicename = hrn_to_pl_slicename(hrn)
428 defaultrspec = default.toGenDict()
429 nodedict = get_nodedict(defaultrspec)
431 # call the default sfa.plc.nodes.get_rspec() method
433 rspec = nodes.get_rspec(hrn)
435 # Grab all the PLC info we'll need at once
436 slice = get_slice(api, slicename)
438 nodes = get_nodes(api)
439 tags = get_slice_tags(api)
441 # Add the node tags from the Capacity statement to Node objects
442 for (k, v) in nodedict.iteritems():
444 if v == nodes[id].hostname:
448 for node in slice.get_nodes(nodes):
449 linktag = slice.get_tag('topo_rspec', tags, node)
451 l = eval(linktag.value)
452 for (id, realip, bw, lvip, rvip, vnet) in l:
453 endpoints.append((node.id, id, bw))
457 for (l, r, bw) in endpoints:
458 if (r, l, bw) in endpoints:
461 edict['endpoint'] = [nodes[l].tag, nodes[r].tag]
463 linkspecs.append(edict)
466 d['Rspec']['Request'][0]['NetSpec'][0]['LinkSpec'] = linkspecs
467 d['Rspec']['Request'][0]['NetSpec'][0]['name'] = hrn
472 # Return canned response for now...
473 rspec = default.toxml()
478 def create_slice(api, hrn, xml):
481 rspec = r.toGenDict()
483 # Check request against current allocations
486 nodes = rspec_to_nodeset(rspec)
487 create_slice_vini_aggregate(api, hrn, nodes)
489 # Add VINI-specific topology attributes to slice here
493 def get_nodedict(rspec):
496 sitespecs = rspec['Rspec'][0]['Capacity'][0]['NetSpec'][0]['SiteSpec']
498 for node in s['NodeSpec']:
499 nodedict[node['name'][0]] = node['hostname'][0]
506 def rspec_to_nodeset(rspec):
509 nodedict = get_nodedict(rspec)
510 linkspecs = rspec['Rspec'][0]['Request'][0]['NetSpec'][0]['LinkSpec']
512 for e in l['endpoint']:
513 nodes.add(nodedict[e])
522 r.parseFile(sys.argv[1])
523 rspec = r.toGenDict()
524 create_slice(None,'plc',rspec)
526 if __name__ == "__main__":