updates
[sfa.git] / geni / 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
16 class SpecDict(dict):
17     """
18     Base class of SpecDict objects. 
19     """
20     fields = {}
21     plc_fields = {}
22     type = None
23         
24     def __init__(self, spec_dict):
25         # convert plc dict and initialize self
26         sdict = self.plcToSpec(spec_dict)
27         dict.__init__(self, sdict)
28
29
30     def plcToSpec(self, spec_dict):
31         """
32         Defines how to convert a plc dict to rspec dict
33         """
34         spec = {}
35         for field in self.fields:
36             value = ""
37             expected = self.fields[field]
38             if isinstance(expected, StringTypes):
39                 if self.plc_fields.has_key(field):
40                     plc_field = self.plc_fields[field]
41                     print self.type, spec_dict
42                     if spec_dict.has_key(plc_field):
43                         value = spec_dict[plc_field]
44             elif isinstance(expected, ListType):
45                 expected = expected[0]
46                 if self.plc_fields.has_key(field):
47                     plc_field = self.plc_fields[field]
48                     if spec_dict.has_key(plc_field):
49                         value = [expected(value) for value in spec_dict[plc_field]]
50                     
51              
52             spec[field] = value
53         return {self.type: spec}
54     
55 class IfSpecDict(SpecDict):
56     type = 'IfSpec'
57     fields = {'name': '',
58               'addr': '',
59               'type': '',
60               'init_params': '',
61               'min_rate': '',
62               'max_rate': '',
63               'max_kbyte': '',
64               'ip_spoof': ''}
65     plc_fields = {'name': 'is_primary',
66                  'addr': 'ip',
67                  'type': 'type'}
68             
69 class NodeSpecDict(SpecDict):
70     type = 'NodeSpec'
71     fields = {'name': '',
72               'type': '',
73               'init_params': '',
74               'cpu_min': '',
75               'cpu_share': '',
76               'cpu_pct': '',
77               'disk_max': '',
78               'start_time': '',
79               'duration': '',
80               'net_if': [IfSpecDict]}
81
82     plc_fields = {'name': 'hostname',
83                   'net_if': 'interfaces'}  
84
85 class NetSpecDict(SpecDict):
86     type = 'NetSpec'
87     fields = {'name': '',
88               'start_time': '',
89               'duration': '',
90               'nodes': [NodeSpecDict],
91              }
92     plc_fields = {'name': 'name',
93                   'start_time': 'start_time',
94                   'duration': 'duration',
95                   'nodes': 'nodes'}
96
97 class RspecDict(SpecDict):
98     type = 'RSpec'
99     fields = {'start_time': '',
100               'duration': '',
101               'networks': [NetSpecDict]
102              }
103     plc_fields = {'networks': 'networks'}
104
105 # vim:ts=4:expandtab