Update for PL4.3
[nodemanager-topo.git] / create-topo-attributes.py
index c446b94..b9536eb 100755 (executable)
@@ -2,7 +2,7 @@
 # $URL$
 
 """
-Scan the VINI Central database and create topology "rspec" attributes for
+Scan the VINI Central database and create topology "rspec" tags for
 slices that have an EGRE key.  This script to be run from a cron job.
 """
 
@@ -13,7 +13,6 @@ from topology import links
 
 def get_adjacency_matrix(links):
     topo = {}
-    idx = 1
     for (a, b) in links:
         aNodes = get_sitenodes(a)
         bNodes = get_sitenodes(b)
@@ -23,9 +22,8 @@ def get_adjacency_matrix(links):
                     topo[nodeA] = {}
                 if nodeB not in topo:
                     topo[nodeB] = {}
-                topo[nodeA][nodeB] = idx
-                topo[nodeB][nodeA] = idx
-        idx += 1
+                topo[nodeA][nodeB] = 1
+                topo[nodeB][nodeA] = 1
     return topo
                     
             
@@ -50,13 +48,35 @@ def get_sitenodes(siteid):
 
 """
 Find the IP address assigned to a virtual interface in the topology
-(for creating /etc/hosts)
+(for creating /etc/hosts).
+Each virtual link is on a /30 subnet.
 """
-def get_virt_ip(myid, linkid):
-    return "192.168.%d.%d" % (linkid, myid)
+def get_linkid(myid, remoteid):
+    if myid < remoteid:
+        linkid = (myid<<7) + remoteid
+    else:
+        linkid = (remoteid<<7) + myid
+    return linkid
 
-def get_virt_net(linkid):
-    return "192.168.%d.0/24" % linkid
+def get_nodeid(myid, remoteid):
+    if myid < remoteid:
+        nodeid = 1
+    else:
+        nodeid = 2
+    return nodeid
+
+def get_virt_ip(myid, remoteid):
+    linkid = get_linkid(myid, remoteid)
+    nodeid = get_nodeid(myid, remoteid)
+    first = linkid >> 6
+    second = ((linkid & 0x3f)<<2) + nodeid
+    return "192.168.%d.%d" % (first, second)
+
+def get_virt_net(myid, remoteid):
+    linkid = get_linkid(myid, remoteid)
+    first = linkid >> 6
+    second = (linkid & 0x3f)<<2
+    return "192.168.%d.%d/30" % (first, second)
 
 """
 Create a dictionary of site records keyed by site ID
@@ -90,13 +110,13 @@ nodes = get_nodes()
 adj_matrix = get_adjacency_matrix(links)
 
 for slice in GetSlices():
-    # Create dictionary of the slice's attribute
+    # Create dictionary of the slice's tag
     attrs ={}
     topo_attr = {}
-    for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
-        attrs[attribute['name']] = attribute['slice_attribute_id']
-        if attribute['name'] == 'topo_rspec' and attribute['node_id']:
-            topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
+    for tag in GetSliceTags(slice['slice_tag_ids']):
+        attrs[tag['tagname']] = tag['slice_tag_id']
+        if tag['tagname'] == 'topo_rspec' and tag['node_id']:
+            topo_attr[tag['node_id']] = tag['slice_tag_id']
             
     if dryrun and slice['name'] == 'pl_trellis':
         attrs['egre_key'] = 101
@@ -116,10 +136,9 @@ for slice in GetSlices():
                 if b in slicenodes:
                     if a not in topo:
                         topo[a] = []
-                    link_id = adj_matrix[a][b]
-                    my_ip = get_virt_ip(a, link_id)
-                    remote_ip = get_virt_ip(b, link_id)
-                    net = get_virt_net(link_id)
+                    my_ip = get_virt_ip(a, b)
+                    remote_ip = get_virt_ip(b, a)
+                    net = get_virt_net(a, b)
                     link = b, get_ipaddr(b), "1Mbit", my_ip, remote_ip, net
                     topo[a].append(link)
                     shortname = nodes[a]['hostname'].replace('.vini-veritas.net', '')
@@ -130,19 +149,19 @@ for slice in GetSlices():
             if dryrun:
                 print node, topo_str
             elif node in topo_attr:
-                UpdateSliceAttribute(topo_attr[node], topo_str)
+                UpdateSliceTag(topo_attr[node], topo_str)
                 del topo_attr[node]
             else:
                 id = slice['slice_id']
-                AddSliceAttribute(id, 'topo_rspec', topo_str, node)
+                AddSliceTag(id, 'topo_rspec', topo_str, node)
 
         if dryrun:
             print hosts
         elif 'hosts' in attrs:
-            UpdateSliceAttribute(attrs['hosts'], hosts)
+            UpdateSliceTag(attrs['hosts'], hosts)
         else:
             id = slice['slice_id']
-            AddSliceAttribute(id, 'hosts', hosts)
+            AddSliceTag(id, 'hosts', hosts)
     else:
         if dryrun:
             print "No EGRE key for %s" % slice['name']
@@ -151,6 +170,6 @@ for slice in GetSlices():
     
     if not dryrun:
         for node in topo_attr:
-            DeleteSliceAttribute(topo_attr[node])
+            DeleteSliceTag(topo_attr[node])