2 # Implements support for geni records
4 # TODO: Use existing PLC database methods? or keep this separate?
11 # The GeniRecord class implements a Geni Record. A GeniRecord is a tuple
12 # (Name, GID, Type, Info).
14 # Name specifies the HRN of the object
15 # GID is the GID of the object
16 # Type is user | sa | ma | slice | component
18 # Info is comprised of the following sub-fields
19 # pointer = a pointer to the record in the PL database
20 # pl_info = planetlab-specific info (when talking to client)
21 # geni_info = geni-specific info (when talking to client)
23 # The pointer is interpreted depending on the type of the record. For example,
24 # if the type=="user", then pointer is assumed to be a person_id that indexes
25 # into the persons table.
27 # A given HRN may have more than one record, provided that the records are
28 # of different types. For example, planetlab.us.arizona may have both an SA
29 # and a MA record, but cannot have two SA records.
34 # Create a Geni Record
36 # @param name if !=None, assign the name of the record
37 # @param gid if !=None, assign the gid of the record
38 # @param type one of user | sa | ma | slice | component
39 # @param pointer is a pointer to a PLC record
40 # @param dict if !=None, then fill in this record from the dictionary
42 def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None, string=None):
57 self.set_pointer(pointer)
59 self.load_from_dict(dict)
61 self.load_from_string(string)
64 # Set the name of the record
66 # @param name is a string containing the HRN
68 def set_name(self, name):
73 # Set the GID of the record
75 # @param gid is a GID object or the string representation of a GID object
77 def set_gid(self, gid):
78 if isinstance(gid, str):
81 self.gid = gid.save_to_string(save_parents=True)
85 # Set the type of the record
87 # @param type is a string: user | sa | ma | slice | component
89 def set_type(self, type):
94 # Set the pointer of the record
96 # @param pointer is an integer containing the ID of a PLC record
98 def set_pointer(self, pointer):
99 self.pointer = pointer
103 # Set the PLC info of the record
105 # @param pl_info is a dictionary containing planetlab info
107 def set_pl_info(self, pl_info):
108 self.pl_info = pl_info
112 # Set the geni info the record
114 # @param geni_info is a dictionary containing geni info
116 def set_geni_info(self, geni_info):
117 self.geni_info = geni_info
121 # Return the pl_info of the record, or an empty dictionary if none exists
123 def get_pl_info(self):
130 # Return the geni_info of the record, or an empty dictionary if none exists
132 def get_geni_info(self):
134 return self.geni_info
139 # Return the name (HRN) of the record
145 # Return the type of the record
151 # Return the pointer of the record. The pointer is an integer that may be
152 # used to look up the record in the PLC database. The evaluation of pointer
153 # depends on the type of the record
155 def get_pointer(self):
159 # Return the GID of the record, in the form of a GID object
160 # TODO: not the best name for the function, because we have things called
161 # gidObjects in the Cred
163 def get_gid_object(self):
164 return GID(string=self.gid)
167 # Return a key that uniquely identifies this record among all records in
168 # Geni. This key is used to uniquely identify the record in the Geni
172 return self.name + "#" + self.type
175 # Returns a list of field names in this record. pl_info, geni_info are not
176 # included because they are not part of the record that is stored in the
177 # database, but are rather computed values from other entities
179 def get_field_names(self):
180 return ["name", "gid", "type", "pointer"]
183 # Given a field name ("name", "gid", ...) return the value of that field.
185 # @param name is the name of field to be returned
187 def get_field_value_string(self, fieldname):
188 if fieldname == "key":
191 val = getattr(self, fieldname)
192 if isinstance(val, str):
193 return "'" + str(val) + "'"
198 # Given a list of field names, return a list of values for those fields.
200 # @param fieldnames is a list of field names
202 def get_field_value_strings(self, fieldnames):
204 for fieldname in fieldnames:
205 strs.append(self.get_field_value_string(fieldname))
209 # Return the record in the form of a dictionary
213 names = self.get_field_names()
215 dict[name] = getattr(self, name)
218 dict['pl_info'] = self.pl_info
221 dict['geni_info'] = self.geni_info
226 # Load the record from a dictionary
228 # @param dict dictionary to load record fields from
230 def load_from_dict(self, dict):
231 self.set_name(dict['name'])
232 self.set_gid(dict['gid'])
233 self.set_type(dict['type'])
234 self.set_pointer(dict['pointer'])
235 if "pl_info" in dict:
236 self.set_pl_info(dict["pl_info"])
237 if "geni_info" in dict:
238 self.set_geni_info(dict["geni_info"])
241 # Save the record to a string. The string contains an XML representation of
244 def save_to_string(self):
245 dict = self.as_dict()
246 str = xmlrpclib.dumps((dict,), allow_none=True)
250 # Load the record from a string. The string is assumed to contain an XML
251 # representation of the record.
253 def load_from_string(self, str):
254 dict = xmlrpclib.loads(str)[0][0]
255 self.load_from_dict(dict)
258 # Dump the record to stdout
260 # @param dump_parents if true, then the parents of the GID will be dumped
262 def dump(self, dump_parents=False):
263 print "RECORD", self.name
264 print " hrn:", self.name
265 print " type:", self.type
270 self.get_gid_object().dump(8, dump_parents)
271 print " pointer:", self.pointer
274 geni_info = getattr(self, "geni_info", {})
276 for key in geni_info.keys():
277 print " ", key, ":", geni_info[key]
280 pl_info = getattr(self, "pl_info", {})
282 for key in pl_info.keys():
283 print " ", key, ":", pl_info[key]