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