Extend specdict for linkspec class.
[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                     if spec_dict.has_key(plc_field):
42                         value = spec_dict[plc_field]
43             elif isinstance(expected, ListType):
44                 expected = expected[0]
45                 if self.plc_fields.has_key(field):
46                     plc_field = self.plc_fields[field]
47                     if spec_dict.has_key(plc_field):
48                         value = [expected(value) for value in spec_dict[plc_field]]
49             spec[field] = value
50         return {self.type: spec}
51     
52 class IfSpecDict(SpecDict):
53     type = 'IfSpec'
54     fields = {'name': '',
55               'addr': '',
56               'type': '',
57               'init_params': '',
58               'min_rate': '',
59               'max_rate': '',
60               'max_kbyte': '',
61               'ip_spoof': ''}
62     plc_fields = {'name': 'is_primary',
63                  'addr': 'ip',
64                  'type': 'type'}
65  
66 class LinkSpecDict(SpecDict):
67     type = 'IfSpec'
68     fields = {'name': '',
69               'addr': '',
70               'type': '',
71               'init_params': '',
72               'min_rate': '',
73               'max_rate': '',
74               'max_kbyte': ''}
75     plc_fields = {}
76                  
77             
78 class NodeSpecDict(SpecDict):
79     type = 'NodeSpec'
80     fields = {'name': '',
81               'type': '',
82               'init_params': '',
83               'cpu_min': '',
84               'cpu_share': '',
85               'cpu_pct': '',
86               'disk_max': '',
87               'start_time': '',
88               'duration': '',
89               'net_if': [IfSpecDict]}
90
91     plc_fields = {'name': 'hostname',
92                   'net_if': 'interfaces'}  
93
94 class NetSpecDict(SpecDict):
95     type = 'NetSpec'
96     fields = {'name': '',
97               'start_time': '',
98               'duration': '',
99               'nodes': [NodeSpecDict],
100              }
101     plc_fields = {'name': 'name',
102                   'start_time': 'start_time',
103                   'duration': 'duration',
104                   'nodes': 'nodes'}
105
106 class RspecDict(SpecDict):
107     type = 'RSpec'
108     fields = {'start_time': '',
109               'duration': '',
110               'networks': [NetSpecDict]
111              }
112     plc_fields = {'networks': 'networks',
113                   'start_time': 'start_tim',
114                   'duration': 'duration'
115                  }
116
117 # vim:ts=4:expandtab