remove svn keywords one last time and for good (hopefully)
[sfa.git] / sfa / util / specdict.py
1 ##
2 # SpecDict
3 #
4 # SpecDict defines a means for converting a dictionary with plc specific keys
5 # to a dict with rspec specific keys. 
6 #
7 # SpecDict.fields dict defines all the rspec specific attribute and their 
8 # expected type. 
9
10 # SpecDict.plc_fields defines a one to one mapping of plc attribute to rspec 
11 # attribute
12
13 from types import StringTypes, ListType
14
15 class SpecDict(dict):
16     """
17     Base class of SpecDict objects. 
18     """
19     fields = {}
20     plc_fields = {}
21     type = None
22         
23     def __init__(self, spec_dict):
24         # convert plc dict and initialize self
25         sdict = self.plcToSpec(spec_dict)
26         dict.__init__(self, sdict)
27
28
29     def plcToSpec(self, spec_dict):
30         """
31         Defines how to convert a plc dict to rspec dict
32         """
33         spec = {}
34         for field in self.fields:
35             value = ""
36             expected = self.fields[field]
37             if isinstance(expected, StringTypes):
38                 if self.plc_fields.has_key(field):
39                     plc_field = self.plc_fields[field]
40                     if spec_dict.has_key(plc_field):
41                         value = spec_dict[plc_field]
42             elif isinstance(expected, ListType):
43                 expected = expected[0]
44                 if self.plc_fields.has_key(field):
45                     plc_field = self.plc_fields[field]
46                     if spec_dict.has_key(plc_field):
47                         value = [expected(value) for value in spec_dict[plc_field]]
48             spec[field] = value
49         return {self.type: spec}
50
51 #
52 # fields = { geni_field:  type.  Could be class for nested classes, otherwise prob str}
53 # plc_fields = {geni_field : plc_field}
54 #
55  
56 class IfSpecDict(SpecDict):
57     type = 'IfSpec'
58     fields = {'name': '',
59               'addr': '',
60               'type': '',
61               'init_params': '',
62               'min_rate': '',
63               'max_rate': '',
64               'max_kbyte': '',
65               'ip_spoof': ''}
66     plc_fields = {'name': 'is_primary', # XXX needs munging to return name instead of True or False
67                  'addr': 'ip',
68                  'type': 'type'}
69  
70 class LinkSpecDict(SpecDict):
71     type = 'LinkSpec'
72     fields = {'min_alloc': '', 
73               'max_alloc': '', 
74               'type': '', 
75               'start_time': '', 
76               'bw': '', 
77               'duration': '', 
78               'init_params': '',
79               'endpoints': [IfSpecDict]}
80     plc_fields = {'min_alloc': 'min_alloc',
81               'max_alloc': 'max_alloc', 
82               'type': 'type', 
83               'start_time': 'start_time', 
84               'bw': 'bw', 
85               'duration': 'duration', 
86               'init_params': 'init_params',
87               'endpoints': 'endpoints'}
88   
89             
90 class NodeSpecDict(SpecDict):
91     type = 'NodeSpec'
92     fields = {'name': '',
93               'type': '',
94               'init_params': '',
95               'cpu_min': '',
96               'cpu_share': '',
97               'cpu_pct': '',
98               'disk_max': '',
99               'start_time': '',
100               'duration': '',
101               'net_if': [IfSpecDict]}
102
103     plc_fields = {'name': 'hostname',
104                   'net_if': 'interfaces'}  
105
106 class NetSpecDict(SpecDict):
107     type = 'NetSpec'
108     fields = {'name': '',
109               'start_time': '',
110               'duration': '',
111               'nodes': [NodeSpecDict],
112               'links': [LinkSpecDict],
113              }
114     plc_fields = {'name': 'name',
115                   'start_time': 'start_time',
116                   'duration': 'duration',
117                   'nodes': 'nodes',
118                   'links': 'links'}
119
120 class RSpecDict(SpecDict):
121     type = 'RSpec'
122     fields = {'start_time': '',
123               'duration': '',
124               'networks': [NetSpecDict]
125              }
126     plc_fields = {'networks': 'networks',
127                   'start_time': 'start_tim',
128                   'duration': 'duration'
129                  }
130
131 # vim:ts=4:expandtab