2d6d7acfda7fceafe43861f80fc9831160098d10
[sfa.git] / util / record.py
1 ##
2 # Implements support for geni records
3 #
4 # TODO: Use existing PLC database methods? or keep this separate?
5 ##
6
7 import report
8 from gid import *
9
10 ##
11 # The GeniRecord class implements a Geni Record. A GeniRecord is a tuple
12 # (Name, GID, Type, Info).
13 #
14 # Name specifies the HRN of the object
15 # GID is the GID of the object
16 # Type is user | sa | ma | slice | component
17 #
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)
22 #
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.
26 #
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.
30
31 class GeniRecord():
32
33     ##
34     # Create a Geni Record
35     #
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
41
42     def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None):
43         self.dirty = True
44         self.pl_info = None
45         self.geni_info = None
46         if name:
47             self.set_name(name)
48         if gid:
49             self.set_gid(gid)
50         if type:
51             self.set_type(type)
52         if pointer:
53             self.set_pointer(pointer)
54         if dict:
55             self.set_name(dict['name'])
56             self.set_gid(dict['gid'])
57             self.set_type(dict['type'])
58             self.set_pointer(dict['pointer'])
59             if "pl_info" in dict:
60                self.set_pl_info(dict["pl_info"])
61             if "geni_info" in dict:
62                self.set_geni_info(dict["geni_info"])
63
64     ##
65     # Set the name of the record
66     #
67     # @param name is a string containing the HRN
68
69     def set_name(self, name):
70         self.name = name
71         self.dirty = True
72
73     ##
74     # Set the GID of the record
75     #
76     # @param gid is a GID object or the string representation of a GID object
77
78     def set_gid(self, gid):
79         if isinstance(gid, str):
80             self.gid = gid
81         else:
82             self.gid = gid.save_to_string(save_parents=True)
83         self.dirty = True
84
85     ##
86     # Set the type of the record
87     #
88     # @param type is a string: user | sa | ma | slice | component
89
90     def set_type(self, type):
91         self.type = type
92         self.dirty = True
93
94     ##
95     # Set the pointer of the record
96     #
97     # @param pointer is an integer containing the ID of a PLC record
98
99     def set_pointer(self, pointer):
100         self.pointer = pointer
101         self.dirty = True
102
103     ##
104     # Set the PLC info of the record
105     #
106     # @param pl_info is a dictionary containing planetlab info
107
108     def set_pl_info(self, pl_info):
109         self.pl_info = pl_info
110         self.dirty = True
111
112     ##
113     # Set the geni info the record
114     #
115     # @param geni_info is a dictionary containing geni info
116
117     def set_geni_info(self, geni_info):
118         self.geni_info = geni_info
119         self.dirty = True
120
121     ##
122     # Return the pl_info of the record, or an empty dictionary if none exists
123
124     def get_pl_info(self):
125         if self.pl_info:
126             return self.pl_info
127         else:
128             return {}
129
130     ##
131     # Return the geni_info of the record, or an empty dictionary if none exists
132
133     def get_geni_info(self):
134         if self.geni_info:
135             return self.geni_info
136         else:
137             return {}
138
139     ##
140     # Return the name (HRN) of the record
141
142     def get_name(self):
143         return self.name
144
145     ##
146     # Return the type of the record
147
148     def get_type(self):
149         return self.type
150
151     ##
152     # Return the pointer of the record. The pointer is an integer that may be
153     # used to look up the record in the PLC database. The evaluation of pointer
154     # depends on the type of the record
155
156     def get_pointer(self):
157         return self.pointer
158
159     ##
160     # Return the GID of the record, in the form of a GID object
161     # TODO: not the best name for the function, because we have things called
162     # gidObjects in the Cred
163
164     def get_gid_object(self):
165         return GID(string=self.gid)
166
167     ##
168     # Return a key that uniquely identifies this record among all records in
169     # Geni. This key is used to uniquely identify the record in the Geni
170     # database.
171
172     def get_key(self):
173         return self.name + "#" + self.type
174
175     ##
176     # Returns a list of field names in this record. pl_info, geni_info are not
177     # included because they are not part of the record that is stored in the
178     # database, but are rather computed values from other entities
179
180     def get_field_names(self):
181         return ["name", "gid", "type", "pointer"]
182
183     ##
184     # Given a field name ("name", "gid", ...) return the value of that field.
185     #
186     # @param name is the name of field to be returned
187
188     def get_field_value_string(self, fieldname):
189         if fieldname == "key":
190             val = self.get_key()
191         else:
192             val = getattr(self, fieldname)
193         if isinstance(val, str):
194             return "'" + str(val) + "'"
195         else:
196             return str(val)
197
198     ##
199     # Given a list of field names, return a list of values for those fields.
200     #
201     # @param fieldnames is a list of field names
202
203     def get_field_value_strings(self, fieldnames):
204         strs = []
205         for fieldname in fieldnames:
206             strs.append(self.get_field_value_string(fieldname))
207         return strs
208
209     ##
210     # Return the record in the form of a dictionary
211
212     def as_dict(self):
213         dict = {}
214         names = self.get_field_names()
215         for name in names:
216             dict[name] = getattr(self, name)
217
218         if self.pl_info:
219             dict['pl_info'] = self.pl_info
220
221         if self.geni_info:
222             dict['geni_info'] = self.geni_info
223
224         return dict
225
226     ##
227     # Dump the record to stdout
228     #
229     # @param dump_parents if true, then the parents of the GID will be dumped
230
231     def dump(self, dump_parents=False):
232         print "RECORD", self.name
233         print "        hrn:", self.name
234         print "       type:", self.type
235         print "        gid:"
236         self.get_gid_object().dump(8, dump_parents)
237         print "    pointer:", self.pointer
238
239         print "  geni_info:"
240         geni_info = getattr(self, "geni_info", {})
241         if geni_info:
242             for key in geni_info.keys():
243                 print "       ", key, ":", geni_info[key]
244
245         print "    pl_info:"
246         pl_info = getattr(self, "pl_info", {})
247         if pl_info:
248             for key in pl_info.keys():
249                 print "       ", key, ":", pl_info[key]
250
251