2 # Implements support for geni records
4 # TODO: Use existing PLC database methods? or keep this separate?
8 from types import StringTypes
10 from geni.util.rspec import *
12 # The GeniRecord class implements a Geni Record. A GeniRecord is a tuple
13 # (Name, GID, Type, Info).
15 # Name specifies the HRN of the object
16 # GID is the GID of the object
17 # Type is user | sa | ma | slice | component
19 # Info is comprised of the following sub-fields
20 # pointer = a pointer to the record in the PL database
21 # pl_info = planetlab-specific info (when talking to client)
22 # geni_info = geni-specific info (when talking to client)
24 # The pointer is interpreted depending on the type of the record. For example,
25 # if the type=="user", then pointer is assumed to be a person_id that indexes
26 # into the persons table.
28 # A given HRN may have more than one record, provided that the records are
29 # of different types. For example, planetlab.us.arizona may have both an SA
30 # and a MA record, but cannot have two SA records.
35 # Create a Geni Record
37 # @param name if !=None, assign the name of the record
38 # @param gid if !=None, assign the gid of the record
39 # @param type one of user | sa | ma | slice | component
40 # @param pointer is a pointer to a PLC record
41 # @param dict if !=None, then fill in this record from the dictionary
43 def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None, string=None):
58 self.set_pointer(pointer)
60 self.load_from_dict(dict)
62 self.load_from_string(string)
65 # Set the name of the record
67 # @param name is a string containing the HRN
69 def set_name(self, name):
74 # Set the GID of the record
76 # @param gid is a GID object or the string representation of a GID object
78 def set_gid(self, gid):
79 if isinstance(gid, StringTypes):
82 self.gid = gid.save_to_string(save_parents=True)
86 # Set the type of the record
88 # @param type is a string: user | sa | ma | slice | component
90 def set_type(self, type):
95 # Set the pointer of the record
97 # @param pointer is an integer containing the ID of a PLC record
99 def set_pointer(self, pointer):
100 self.pointer = pointer
104 # Set the PLC info of the record
106 # @param pl_info is a dictionary containing planetlab info
108 def set_pl_info(self, pl_info):
109 if isinstance(pl_info, list):
111 self.pl_info = pl_info
115 # Set the geni info the record
117 # @param geni_info is a dictionary containing geni info
119 def set_geni_info(self, geni_info):
120 if isinstance(geni_info, list):
121 geni_info = geni_info[0]
122 self.geni_info = geni_info
126 # Return the pl_info of the record, or an empty dictionary if none exists
128 def get_pl_info(self):
135 # Return the geni_info of the record, or an empty dictionary if none exists
137 def get_geni_info(self):
139 return self.geni_info
144 # Return the name (HRN) of the record
150 # Return the type of the record
156 # Return the pointer of the record. The pointer is an integer that may be
157 # used to look up the record in the PLC database. The evaluation of pointer
158 # depends on the type of the record
160 def get_pointer(self):
164 # Return the GID of the record, in the form of a GID object
165 # TODO: not the best name for the function, because we have things called
166 # gidObjects in the Cred
168 def get_gid_object(self):
169 return GID(string=self.gid)
172 # Return a key that uniquely identifies this record among all records in
173 # Geni. This key is used to uniquely identify the record in the Geni
177 return self.name + "#" + self.type
180 # Returns a list of field names in this record. pl_info, geni_info are not
181 # included because they are not part of the record that is stored in the
182 # database, but are rather computed values from other entities
184 def get_field_names(self):
185 return ["name", "gid", "type", "pointer"]
188 # Given a field name ("name", "gid", ...) return the value of that field.
190 # @param name is the name of field to be returned
192 def get_field_value_string(self, fieldname):
193 if fieldname == "key":
196 val = getattr(self, fieldname)
197 if isinstance(val, str):
198 return "'" + str(val) + "'"
203 # Given a list of field names, return a list of values for those fields.
205 # @param fieldnames is a list of field names
207 def get_field_value_strings(self, fieldnames):
209 for fieldname in fieldnames:
210 strs.append(self.get_field_value_string(fieldname))
214 # Return the record in the form of a dictionary
218 names = self.get_field_names()
220 dict[name] = getattr(self, name)
223 dict['pl_info'] = self.pl_info
226 dict['geni_info'] = self.geni_info
231 # Load the record from a dictionary
233 # @param dict dictionary to load record fields from
235 def load_from_dict(self, dict):
236 self.set_name(dict['name'])
237 gidstr = dict.get("gid", None)
239 self.set_gid(dict['gid'])
241 self.set_type(dict['type'])
242 self.set_pointer(dict['pointer'])
243 if "pl_info" in dict:
244 self.set_pl_info(dict["pl_info"])
245 if "geni_info" in dict:
246 self.set_geni_info(dict["geni_info"])
249 # Save the record to a string. The string contains an XML representation of
252 def save_to_string(self):
254 dict = self.as_dict()
255 record = RecordSpec()
256 record.parseDict(dict)
258 #str = xmlrpclib.dumps((dict,), allow_none=True)
262 # Load the record from a string. The string is assumed to contain an XML
263 # representation of the record.
265 def load_from_string(self, str):
266 #dict = xmlrpclib.loads(str)[0][0]
268 record = RecordSpec()
269 record.parseString(str)
270 record_dict = record.toDict()
271 geni_dict = record_dict['record']
272 self.load_from_dict(geni_dict)
275 # Dump the record to stdout
277 # @param dump_parents if true, then the parents of the GID will be dumped
279 def dump(self, dump_parents=False):
280 print "RECORD", self.name
281 print " hrn:", self.name
282 print " type:", self.type
287 self.get_gid_object().dump(8, dump_parents)
288 print " pointer:", self.pointer
291 geni_info = getattr(self, "geni_info", {})
293 for key in geni_info.keys():
294 print " ", key, ":", geni_info[key]
297 pl_info = getattr(self, "pl_info", {})
300 for key in pl_info.keys():
301 print " ", key, ":", pl_info[key]
305 info = {'hrn': self.name, 'type': self.type, 'gid': self.gid}
306 info.update(getattr(self, "geni_info", {}))
307 info.update(getattr(self, "pl_info", {}))