3076219c5eb0bc9e9dbdb1b2e1c0b950eb6dbb11
[sfa.git] / sfa / rspecs / aggregates / vini / rspec.py
1 from sfa.util.rspec import Rspec
2 from sfa.rspecs.aggregates.vini.utils import *
3 import sys
4
5 SFA_VINI_DEFAULT_RSPEC = '/etc/sfa/vini.rspec'
6
7 class ViniRspec(Rspec):
8     def __init__(self):
9         Rspec.__init__(self)
10         self.parseFile(SFA_VINI_DEFAULT_RSPEC)
11         
12     def updateCapacity(self, sites, nodes):
13         d = self.toDict()
14         sitespecs = []
15         sitelinkspecs = []
16         for s in sites:
17             site = sites[s]
18             if not site.public:
19                 continue
20             sdict = {}
21             nodespecs = []
22             for node in site.get_sitenodes(nodes):
23                 if not node.tag:
24                     continue
25                 ndict = {}
26                 ndict['hostname'] = [node.hostname]
27                 ndict['name'] = node.tag
28                 ndict['bw'] = ['999Mbit']
29                 nodespecs.append(ndict)
30             sdict['NodeSpec'] = nodespecs
31             sdict['name'] = site.name
32             sitespecs.append(sdict)
33             
34             for sl in site.sitelinks:
35                 if sl.site1 == site:
36                     sldict = {}
37                     sldict['endpoint'] = [sl.site1.name, sl.site2.name]
38                     sldict['bw'] = [str(sl.availMbps) + "Mbit"]
39                     sitelinkspecs.append(sldict)
40                     
41         d['Rspec']['Capacity'][0]['NetSpec'][0]['SiteSpec'] = sitespecs
42         d['Rspec']['Capacity'][0]['NetSpec'][0]['SiteLinkSpec'] = sitelinkspecs
43         self.parseDict(d)
44
45
46     def updateRequest(self, slice, nodes, tags):
47         endpoints = []
48         for node in slice.get_nodes(nodes):
49             linktag = slice.get_tag('topo_rspec', tags, node)
50             if linktag:
51                 l = eval(linktag.value)
52                 for (id, realip, bw, lvip, rvip, vnet) in l:
53                     endpoints.append((node.id, id, bw))
54             
55         if endpoints:
56             linkspecs = []
57             for (l, r, bw) in endpoints:
58                 if (r, l, bw) in endpoints:
59                     if l < r:
60                         edict = {}
61                         edict['endpoint'] = [nodes[l].tag, nodes[r].tag]
62                         edict['bw'] = [bw]
63                         linkspecs.append(edict)
64
65             d = self.toDict()
66             d['Rspec']['Request'][0]['NetSpec'][0]['LinkSpec'] = linkspecs
67             d['Rspec']['Request'][0]['NetSpec'][0]['name'] = slice.hrn
68             self.parseDict(d)