4 # SpecDict defines a means for converting a dictionary with plc specific keys
5 # to a dict with rspec specific keys.
7 # SpecDict.fields dict defines all the rspec specific attribute and their
10 # SpecDict.plc_fields defines a one to one mapping of plc attribute to rspec
13 from types import StringTypes, ListType
17 Base class of SpecDict objects.
23 def __init__(self, spec_dict):
24 # convert plc dict and initialize self
25 sdict = self.plcToSpec(spec_dict)
26 dict.__init__(self, sdict)
29 def plcToSpec(self, spec_dict):
31 Defines how to convert a plc dict to rspec dict
34 for field in self.fields:
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]]
49 return {self.type: spec}
52 # fields = { geni_field: type. Could be class for nested classes, otherwise prob str}
53 # plc_fields = {geni_field : plc_field}
56 class IfSpecDict(SpecDict):
66 plc_fields = {'name': 'is_primary', # XXX needs munging to return name instead of True or False
70 class LinkSpecDict(SpecDict):
72 fields = {'min_alloc': '',
79 'endpoints': [IfSpecDict]}
80 plc_fields = {'min_alloc': 'min_alloc',
81 'max_alloc': 'max_alloc',
83 'start_time': 'start_time',
85 'duration': 'duration',
86 'init_params': 'init_params',
87 'endpoints': 'endpoints'}
90 class NodeSpecDict(SpecDict):
101 'net_if': [IfSpecDict]}
103 plc_fields = {'name': 'hostname',
104 'net_if': 'interfaces'}
106 class NetSpecDict(SpecDict):
108 fields = {'name': '',
111 'nodes': [NodeSpecDict],
112 'links': [LinkSpecDict],
114 plc_fields = {'name': 'name',
115 'start_time': 'start_time',
116 'duration': 'duration',
120 class RSpecDict(SpecDict):
122 fields = {'start_time': '',
124 'networks': [NetSpecDict]
126 plc_fields = {'networks': 'networks',
127 'start_time': 'start_tim',
128 'duration': 'duration'