# $Id$ # $URL$ """ VINI/Trellis NodeManager plugin. Create virtual links from the topo_rspec slice attribute. """ import logger import subprocess import sioc import re dryrun=0 setup_link_cmd="/usr/share/vini/setup-egre-link" teardown_link_cmd="/usr/share/vini/teardown-egre-link" ifaces = {} def run(cmd): if dryrun: logger.log(cmd) return -1 else: return subprocess.call(cmd, shell=True); """ Check for existence of interface dx """ def virtual_link(key, nodeid): name = "d%sx%s" % (key, nodeid) # logger.log("Looking for iface %s" % name) if name in ifaces: return True else: return False """ Create a "virtual link" for slice between here and nodeid. The key is used to create the EGRE tunnel. """ def setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr): logger.log("Set up virtual link to node %d" % nodeid) if myid < nodeid: virtip = "10.%d.%d.2" % (myid, nodeid) else: virtip = "10.%d.%d.3" % (nodeid, myid) run(setup_link_cmd + " %s %s %s %s %s %s" % (slice, nodeid, ipaddr, key, rate, virtip)) return """ Tear down the "virtual link" for slice between here and nodeid. """ def teardown_virtual_link(slice, key, nodeid): logger.log("Tear down virtual link to node %s" % nodeid) run(teardown_link_cmd + " %s %s %s" % (slice, nodeid, key)) return """ Clean up old virtual links (e.g., to nodes that have been deleted from the slice). """ def clean_up_old_virtual_links(slice, key, nodelist): pattern = "d%sx(.*)" % key for iface in ifaces: m = re.match(pattern, iface) if m: node = int(m.group(1)) if not node in nodelist: logger.log("%s" % nodelist) teardown_virtual_link(slice, key, node) """ Not the safest thing to do, probably should use pickle() or something. """ def convert_topospec_to_list(rspec): return eval(rspec) """ Update virtual links for the slice """ def update(slice, myid, topospec, key): topolist = convert_topospec_to_list(topospec) nodelist=[] for (nodeid,ipaddr,rate) in topolist: nodelist.append(nodeid) if not virtual_link(key, nodeid): setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr) else: logger.log("Virtual link to node %s exists" % nodeid) clean_up_old_virtual_links(slice, key, nodelist) def start(options, config): pass """ Update the virtual links for a sliver if it has a 'netns' attribute, an 'egre_key' attribute, and a 'topo_rspec' attribute. """ def GetSlivers(data): global ifaces ifaces = sioc.gifconf() for sliver in data['slivers']: attrs = {} for attribute in sliver['attributes']: attrs[attribute['name']] = attribute['value'] if 'netns' in attrs and 'egre_key' in attrs and 'topo_rspec' in attrs: if attrs['netns'] > 0: logger.log("Update topology for slice %s" % sliver['name']) update(sliver['name'], data['node_id'], attrs['topo_rspec'], attrs['egre_key'])