New RSpec format
[sfa.git] / sfa / rspecs / aggregates / vini / utils.py
index c3a057f..34f23e4 100644 (file)
@@ -1,6 +1,10 @@
+from __future__ import with_statement
 import re
 import socket
+from sfa.util.faults import *
 from sfa.rspecs.aggregates.vini.topology import *
+from xmlbuilder import XMLBuilder
+import sys
 
 # Taken from bwlimit.py
 #
@@ -64,14 +68,16 @@ def format_tc_rate(rate):
 
 
 class Node:
-    def __init__(self, node, mbps = 1000):
+    def __init__(self, node, bps = 1000 * 1000000):
         self.id = node['node_id']
+        self.idtag = "n%s" % self.id
         self.hostname = node['hostname']
-        self.shortname = self.hostname.replace('.vini-veritas.net', '')
+        self.name = self.shortname = self.hostname.replace('.vini-veritas.net', '')
         self.site_id = node['site_id']
         self.ipaddr = socket.gethostbyname(self.hostname)
-        self.bps = mbps * 1000000
-        self.links = []
+        self.bps = bps
+        self.links = set()
+        self.sliver = False
 
     def get_link_id(self, remote):
         if self.id < remote.id:
@@ -103,15 +109,22 @@ class Node:
     def get_site(self, sites):
         return sites[self.site_id]
     
-    def init_links(self):
-        self.links = []
-        
-    def add_link(self, remote, bw):
+    def get_topo_rspec(self, link):
+        if link.end1 == self:
+            remote = link.end2
+        elif link.end2 == self:
+            remote = link.end1
+        else:
+            raise Error("Link does not connect to Node")
+            
         my_ip = self.get_virt_ip(remote)
         remote_ip = remote.get_virt_ip(self)
         net = self.get_virt_net(remote)
-        link = remote.id, remote.ipaddr, bw, my_ip, remote_ip, net
-        self.links.append(link)
+        bw = format_tc_rate(link.bps)
+        return (remote.id, remote.ipaddr, bw, my_ip, remote_ip, net)
+        
+    def add_link(self, link):
+        self.links.add(link)
         
     def add_tag(self, sites):
         s = self.get_site(sites)
@@ -122,34 +135,72 @@ class Node:
         else:
             self.tag = None
 
-    # Assumes there is at most one SiteLink between two sites
+    # Assumes there is at most one Link between two sites
     def get_sitelink(self, node, sites):
         site1 = sites[self.site_id]
         site2 = sites[node.site_id]
-        sl = site1.sitelinks.intersection(site2.sitelinks)
+        sl = site1.links.intersection(site2.links)
         if len(sl):
             return sl.pop()
         return None
+
+    def add_sliver(self):
+        self.sliver = True
+
+    def toxml(self, xml, hrn):
+        if not self.tag:
+            return
+        with xml.node(id = self.idtag):
+            with xml.hostname:
+                xml << self.hostname
+            with xml.kbps:
+                xml << str(int(self.bps/1000))
+            if self.sliver:
+                with xml.sliver:
+                    pass
     
 
-class SiteLink:
-    def __init__(self, site1, site2, mbps = 1000):
-        self.site1 = site1
-        self.site2 = site2
-        self.bps = mbps * 1000000
-        
-        site1.add_sitelink(self)
-        site2.add_sitelink(self)
+class Link:
+    def __init__(self, end1, end2, bps = 1000 * 1000000, parent = None):
+        self.end1 = end1
+        self.end2 = end2
+        self.bps = bps
+        self.parent = parent
+        self.children = []
+
+        end1.add_link(self)
+        end2.add_link(self)
         
+        if self.parent:
+            self.parent.children.append(self)
+            
+    def toxml(self, xml):
+        end_ids = "%s %s" % (self.end1.idtag, self.end2.idtag)
+
+        if self.parent:
+            element = xml.vlink(endpoints=end_ids)
+        else:
+            element = xml.link(endpoints=end_ids)
+
+        with element:
+            with xml.description:
+                xml << "%s -- %s" % (self.end1.name, self.end2.name)
+            with xml.kbps:
+                xml << str(int(self.bps/1000))
+            for child in self.children:
+                child.toxml(xml)
         
+
 class Site:
     def __init__(self, site):
         self.id = site['site_id']
+        self.idtag = "s%s" % self.id
         self.node_ids = site['node_ids']
-        self.name = site['abbreviated_name']
+        self.name = site['abbreviated_name'].replace(" ", "_")
         self.tag = site['login_base']
         self.public = site['is_public']
-        self.sitelinks = set()
+        self.enabled = site['enabled']
+        self.links = set()
 
     def get_sitenodes(self, nodes):
         n = []
@@ -157,9 +208,19 @@ class Site:
             n.append(nodes[i])
         return n
     
-    def add_sitelink(self, link):
-        self.sitelinks.add(link)
-    
+    def add_link(self, link):
+        self.links.add(link)
+
+    def toxml(self, xml, hrn, nodes):
+        if not (self.public and self.enabled and self.node_ids):
+            return
+        with xml.site(id = self.idtag):
+            with xml.name:
+                xml << self.name
+                
+            for node in self.get_sitenodes(nodes):
+                node.toxml(xml, hrn)
+   
     
 class Slice:
     def __init__(self, slice):
@@ -310,6 +371,264 @@ class Slicetag:
             api.plshell.DeleteSliceTag(api.plauth, self.id)
 
 
+"""
+A topology is a compound object consisting of:
+* a dictionary mapping site IDs to Site objects
+* a dictionary mapping node IDs to Node objects
+* the Site objects are connected via SiteLink objects representing
+  the physical topology and available bandwidth
+* the Node objects are connected via Link objects representing
+  the requested or assigned virtual topology of a slice
+"""
+class Topology:
+    def __init__(self, api):
+        self.api = api
+        self.sites = get_sites(api)
+        self.nodes = get_nodes(api)
+        self.tags = get_slice_tags(api)
+        self.sitelinks = []
+        self.nodelinks = []
+    
+        for (s1, s2) in PhysicalLinks:
+            self.sitelinks.append(Link(self.sites[s1], self.sites[s2]))
+        
+        for id in self.nodes:
+            self.nodes[id].add_tag(self.sites)
+        
+        for t in self.tags:
+            tag = self.tags[t]
+            if tag.tagname == 'topo_rspec':
+                node1 = self.nodes[tag.node_id]
+                l = eval(tag.value)
+                for (id, realip, bw, lvip, rvip, vnet) in l:
+                    allocbps = get_tc_rate(bw)
+                    node1.bps -= allocbps
+                    try:
+                        node2 = self.nodes[id]
+                        if node1.id < node2.id:
+                            sl = node1.get_sitelink(node2, self.sites)
+                            sl.bps -= allocbps
+                    except:
+                        pass
+
+    
+    """ Lookup site based on id or idtag value """
+    def lookupSite(self, id):
+        val = None
+        if isinstance(id, basestring):
+            id = int(id.lstrip('s'))
+        try:
+            val = self.sites[id]
+        except:
+            raise KeyError("site ID %s not found" % id)
+        return val
+    
+    def getSites(self):
+        sites = []
+        for s in self.sites:
+            sites.append(self.sites[s])
+        return sites
+        
+    """ Lookup node based on id or idtag value """
+    def lookupNode(self, id):
+        val = None
+        if isinstance(id, basestring):
+            id = int(id.lstrip('n'))
+        try:
+            val = self.nodes[id]
+        except:
+            raise KeyError("node ID %s not found" % id)
+        return val
+    
+    def getNodes(self):
+        nodes = []
+        for n in self.nodes:
+            nodes.append(self.nodes[n])
+        return nodes
+    
+    def nodesInTopo(self):
+        nodes = []
+        for n in self.nodes:
+            node = self.nodes[n]
+            if node.sliver:
+                nodes.append(node)
+        return nodes
+            
+    def lookupSliceTag(self, id):
+        val = None
+        try:
+            val = self.tags[id]
+        except:
+            raise KeyError("slicetag ID %s not found" % id)
+        return val
+    
+    def getSliceTags(self):
+        tags = []
+        for t in self.tags:
+            tags.append(self.tags[t])
+        return tags
+    
+    def lookupSiteLink(self, node1, node2):
+        site1 = self.sites[node1.site_id]
+        site2 = self.sites[node2.site_id]
+        for link in self.sitelinks:
+            if site1 == link.end1 and site2 == link.end2:
+                return link
+            if site2 == link.end1 and site1 == link.end2:
+                return link
+        return None
+    
+
+    def __add_vlink(self, vlink, slicenodes, parent = None):
+        n1 = n2 = None
+        if 'endpoints' in vlink:
+            end = vlink['endpoints'].split()
+            n1 = self.lookupNode(end[0])
+            n2 = self.lookupNode(end[1])
+        elif parent:
+            """ Try to infer the endpoints """
+            (n1, n2) = self.__infer_endpoints(parent['endpoints'],slicenodes)
+        else:
+            raise Error("no endpoints given")
+
+        #print "Added virtual link: %s -- %s" % (n1.tag, n2.tag)
+        bps = int(vlink['kbps'][0]) * 1000
+        sitelink = self.lookupSiteLink(n1, n2)
+        if not sitelink:
+            raise PermissionError("nodes %s and %s not adjacent" % 
+                                  (n1.idtag, n2.idtag))
+        self.nodelinks.append(Link(n1, n2, bps, sitelink))
+        return
+
+    """ 
+    Infer the endpoints of the virtual link.  If the slice exists on 
+    only a single node at each end of the physical link, we'll assume that
+    the user wants the virtual link to terminate at these nodes.
+    """
+    def __infer_endpoints(self, endpoints, slicenodes):
+        n = []
+        ends = endpoints.split()
+        for end in ends:
+            found = 0
+            site = self.lookupSite(end)
+            for id in site.node_ids:
+                if id in slicenodes:
+                    n.append(slicenodes[id])
+                    found += 1
+            if found != 1:
+                raise Error("could not infer endpoint for site %s" % site.id)
+        #print "Inferred endpoints: %s %s" % (n[0].idtag, n[1].idtag)
+        return n
+        
+    def nodeTopoFromRSpec(self, rspec):
+        if self.nodelinks:
+            raise Error("virtual topology already present")
+            
+        rspecdict = rspec.toDict()
+        nodedict = {}
+        for node in self.getNodes():
+            nodedict[node.idtag] = node
+            
+        slicenodes = {}
+
+        top = rspecdict['rspec']
+        if ('network' in top):
+            sites = top['network'][0]['site']
+            for site in sites:
+                for node in site['node']:
+                    if 'sliver' in node:
+                        n = nodedict[node['id']]
+                        slicenodes[n.id] = n
+                        n.add_sliver()
+            links = top['network'][0]['link']
+            for link in links:
+                if 'vlink' in link:
+                    for vlink in link['vlink']:
+                        self.__add_vlink(vlink, slicenodes, link)
+        elif ('request' in top):
+            for sliver in top['request'][0]['sliver']:
+                n = nodedict[sliver['nodeid']]
+                slicenodes[n.id] = n
+                n.add_sliver()
+            for vlink in top['request'][0]['vlink']:
+                self.__add_vlink(vlink, slicenodes)
+        return
+
+    def nodeTopoFromSliceTags(self, slice):
+        if self.nodelinks:
+            raise Error("virtual topology already present")
+            
+        for node in slice.get_nodes(self.nodes):
+            node.sliver = True
+            linktag = slice.get_tag('topo_rspec', self.tags, node)
+            if linktag:
+                l = eval(linktag.value)
+                for (id, realip, bw, lvip, rvip, vnet) in l:
+                    if node.id < id:
+                        bps = get_tc_rate(bw)
+                        remote = self.lookupNode(id)
+                        sitelink = self.lookupSiteLink(node, remote)
+                        self.nodelinks.append(Link(node,remote,bps,sitelink))
+
+    def updateSliceTags(self, slice):
+        if not self.nodelinks:
+            return
+        slice.update_tag('vini_topo', 'manual', self.tags)
+        slice.assign_egre_key(self.tags)
+        slice.turn_on_netns(self.tags)
+        slice.add_cap_net_admin(self.tags)
+
+        for node in slice.get_nodes(self.nodes):
+            linkdesc = []
+            for link in node.links:
+                linkdesc.append(node.get_topo_rspec(link))
+            if linkdesc:
+                topo_str = "%s" % linkdesc
+                slice.update_tag('topo_rspec', topo_str, self.tags, node)
+
+        # Update slice tags in database
+        for tag in self.getSliceTags():
+            if tag.slice_id == slice.id:
+                if tag.tagname == 'topo_rspec' and not tag.updated:
+                    tag.delete()
+                tag.write(self.api)
+                
+    """
+    Check the requested topology against the available topology and capacity
+    """
+    def verifyNodeTopo(self, hrn, topo):
+        for link in self.nodelinks:
+            if link.bps <= 0:
+                raise GeniInvalidArgument(bw, "BW")
+                
+            n1 = link.end1
+            n2 = link.end2
+            sitelink = self.lookupSiteLink(n1, n2)
+            if not sitelink:
+                raise PermissionError("%s: nodes %s and %s not adjacent" % (hrn, n1.tag, n2.tag))
+            if sitelink.bps < link.bps:
+                raise PermissionError("%s: insufficient capacity between %s and %s" % (hrn, n1.tag, n2.tag))
+                
+    """
+    Produce XML directly from the topology specification.
+    """
+    def toxml(self, hrn = None):
+        xml = XMLBuilder()
+        with xml.rspec(type="VINI"):
+            if hrn:
+                element = xml.network(name="Public VINI", slice=hrn)
+            else:
+                element = xml.network(name="Public VINI")
+                
+            with element:
+                for site in self.getSites():
+                    site.toxml(xml, hrn, self.nodes)
+                for link in self.sitelinks:
+                    link.toxml(xml)
+
+        return str(xml)
+
 """
 Create a dictionary of site objects keyed by site ID
 """
@@ -370,39 +689,3 @@ def free_egre_key(slicetags):
         
     return "%s" % key
    
-"""
-Return the network topology.
-The topology consists of:
-* a dictionary mapping site IDs to Site objects
-* a dictionary mapping node IDs to Node objects
-* the Site objects are connected via SiteLink objects representing
-  the physical topology and available bandwidth
-"""
-def get_topology(api):
-    sites = get_sites(api)
-    nodes = get_nodes(api)
-    tags = get_slice_tags(api)
-    
-    for (s1, s2) in PhysicalLinks:
-        SiteLink(sites[s1], sites[s2])
-        
-    for id in nodes:
-        nodes[id].add_tag(sites)
-        
-    for t in tags:
-        tag = tags[t]
-        if tag.tagname == 'topo_rspec':
-            node1 = nodes[tag.node_id]
-            l = eval(tag.value)
-            for (id, realip, bw, lvip, rvip, vnet) in l:
-                allocbps = get_tc_rate(bw)
-                node1.bps -= allocbps
-                try:
-                    node2 = nodes[id]
-                    if node1.id < node2.id:
-                        sl = node1.get_sitelink(node2, sites)
-                        sl.bps -= allocbps
-                except:
-                    pass
-        
-    return (sites, nodes, tags)