Minor bug fixes
[nodemanager-topo.git] / topo.py
1 # $Id$
2 # $URL$
3
4 """ 
5 VINI/Trellis NodeManager plugin.
6 Create virtual links from the topo_rspec slice attribute. 
7 """
8
9 import logger
10 import subprocess
11 import sioc
12 import re
13
14 dryrun=0
15 setup_link_cmd="/usr/share/vini/setup-egre-link"
16 teardown_link_cmd="/usr/share/vini/teardown-egre-link"
17 ifaces = {}
18
19 def run(cmd):
20     if dryrun:
21         logger.log(cmd)
22         return -1
23     else:
24         return subprocess.call(cmd, shell=True);
25
26
27 """
28 Check for existence of interface d<key>x<nodeid>
29 """
30 def virtual_link(key, nodeid):
31     name = "d%sx%s" % (key, nodeid)
32     # logger.log("Looking for iface %s" % name)
33     if name in ifaces:
34         return True
35     else:
36         return False
37
38
39 """
40 Create a "virtual link" for slice between here and nodeid.
41 The key is used to create the EGRE tunnel.
42 """
43 def setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr):
44     logger.log("Set up virtual link to node %d" % nodeid)
45     if myid < nodeid:
46         virtip = "10.%d.%d.2" % (myid, nodeid)
47     else:
48         virtip = "10.%d.%d.3" % (nodeid, myid)
49         
50     run(setup_link_cmd + " %s %s %s %s %s %s" % (slice, nodeid, ipaddr, 
51                                                  key, rate, virtip))
52     return
53
54
55 """
56 Tear down the "virtual link" for slice between here and nodeid.
57 """
58 def teardown_virtual_link(slice, key, nodeid):
59     logger.log("Tear down virtual link to node %s" % nodeid)
60     run(teardown_link_cmd + " %s %s %s" % (slice, nodeid, key))
61     return
62
63
64 """
65 Clean up old virtual links (e.g., to nodes that have been deleted 
66 from the slice).
67 """
68 def clean_up_old_virtual_links(slice, key, nodelist):
69     pattern = "d%sx(.*)" % key
70     for iface in ifaces:
71         m = re.match(pattern, iface)
72         if m:
73             node = int(m.group(1))
74             if not node in nodelist:
75                 logger.log("%s" % nodelist)
76                 teardown_virtual_link(slice, key, node)
77
78
79 """
80 Not the safest thing to do, probably should use pickle() or something.
81 """
82 def convert_topospec_to_list(rspec):
83     return eval(rspec)
84
85
86 """
87 Update virtual links for the slice
88 """
89 def update(slice, myid, topospec, key):
90     topolist = convert_topospec_to_list(topospec)
91     nodelist=[]
92     for (nodeid,ipaddr,rate) in topolist:
93         nodelist.append(nodeid)
94         if not virtual_link(key, nodeid):
95             setup_virtual_link(slice, key, rate, myid, nodeid, ipaddr)
96         else:
97             logger.log("Virtual link to node %s exists" % nodeid)
98
99     clean_up_old_virtual_links(slice, key, nodelist)
100
101
102 def start(options, config):
103     pass
104
105
106 """
107 Update the virtual links for a sliver if it has a 'netns' attribute,
108 an 'egre_key' attribute, and a 'topo_rspec' attribute.
109 """
110 def GetSlivers(data):
111     global ifaces
112     ifaces = sioc.gifconf()
113
114     for sliver in data['slivers']:
115         attrs = {}
116         for attribute in sliver['attributes']:
117             attrs[attribute['name']] = attribute['value']
118         if 'netns' in attrs and 'egre_key' in attrs and 'topo_rspec' in attrs:
119             if attrs['netns'] > 0:
120                 logger.log("Update topology for slice %s" % sliver['name'])
121                 update(sliver['name'], data['node_id'], 
122                        attrs['topo_rspec'], attrs['egre_key'])
123