added start_time,duration to NetSpec.plc_fields
[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                     
50              
51             spec[field] = value
52         return {self.type: spec}
53     
54 class IfSpecDict(SpecDict):
55     type = 'IfSpec'
56     fields = {'name': '',
57               'addr': '',
58               'type': '',
59               'init_params': '',
60               'min_rate': '',
61               'max_rate': '',
62               'max_kbyte': '',
63               'ip_spoof': ''}
64     plc_fields = {'name': 'is_primary',
65                  'addr': 'ip',
66                  'type': 'type'}
67             
68 class NodeSpecDict(SpecDict):
69     type = 'NodeSpec'
70     fields = {'name': '',
71               'type': '',
72               'init_params': '',
73               'cpu_min': '',
74               'cpu_share': '',
75               'cpu_pct': '',
76               'disk_max': '',
77               'start_time': '',
78               'duration': '',
79               'net_if': [IfSpecDict]}
80
81     plc_fields = {'name': 'hostname',
82                   'net_if': 'interfaces'}  
83
84 class NetSpecDict(SpecDict):
85     type = 'NetSpec'
86     fields = {'name': '',
87               'start_time': '',
88               'duration': '',
89               'nodes': [NodeSpecDict],
90              }
91     plc_fields = {'name': 'name',
92                   'start_time': 'start_time',
93                   'duration': 'duration',
94                   'nodes': 'nodes'}
95
96 class RspecDict(SpecDict):
97     type = 'RSpec'
98     fields = {'start_time': '',
99               'duration': '',
100               'networks': [NetSpecDict]
101              }
102     plc_fields = {'networks': 'networks'}
103
104 # vim:ts=4:expandtab