more documentation
[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 # GeniRecord is a tuple (Name, GID, Type, Info)
12 #    info is comprised of the following sub-fields
13 #        pointer = a pointer to the record in the PL database
14 #        pl_info = planetlab-specific info (when talking to client)
15 #        geni_info = geni-specific info (when talking to client)
16
17 class GeniRecord():
18     def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None):
19         self.dirty = True
20         self.pl_info = None
21         self.geni_info = None
22         if name:
23             self.set_name(name)
24         if gid:
25             self.set_gid(gid)
26         if type:
27             self.set_type(type)
28         if pointer:
29             self.set_pointer(pointer)
30         if dict:
31             self.set_name(dict['name'])
32             self.set_gid(dict['gid'])
33             self.set_type(dict['type'])
34             self.set_pointer(dict['pointer'])
35             if "pl_info" in dict:
36                self.set_pl_info(dict["pl_info"])
37             if "geni_info" in dict:
38                self.set_geni_info(dict["geni_info"])
39
40     def set_name(self, name):
41         self.name = name
42         self.dirty = True
43
44     def set_gid(self, gid):
45         if isinstance(gid, str):
46             self.gid = gid
47         else:
48             self.gid = gid.save_to_string(save_parents=True)
49         self.dirty = True
50
51     def set_type(self, type):
52         self.type = type
53         self.dirty = True
54
55     def set_pointer(self, pointer):
56         self.pointer = pointer
57         self.dirty = True
58
59     def set_pl_info(self, pl_info):
60         self.pl_info = pl_info
61         self.dirty = True
62
63     def set_geni_info(self, geni_info):
64         self.geni_info = geni_info
65         self.dirty = True
66
67     def get_pl_info(self):
68         if self.pl_info:
69             return self.pl_info
70         else:
71             return {}
72
73     def get_geni_info(self):
74         if self.geni_info:
75             return self.geni_info
76         else:
77             return {}
78
79     def get_name(self):
80         return self.name
81
82     def get_type(self):
83         return self.type
84
85     def get_pointer(self):
86         return self.pointer
87
88     # TODO: not the best name for the function, because we have things called gidObjects in the Cred
89     def get_gid_object(self):
90         return GID(string=self.gid)
91
92     def get_key(self):
93         return self.name + "#" + self.type
94
95     def get_field_names(self):
96         return ["name", "gid", "type", "pointer"]
97
98     def get_field_value_string(self, fieldname):
99         if fieldname == "key":
100             val = self.get_key()
101         else:
102             val = getattr(self, fieldname)
103         if isinstance(val, str):
104             return "'" + str(val) + "'"
105         else:
106             return str(val)
107
108     def get_field_value_strings(self, fieldnames):
109         strs = []
110         for fieldname in fieldnames:
111             strs.append(self.get_field_value_string(fieldname))
112         return strs
113
114     def as_dict(self):
115         dict = {}
116         names = self.get_field_names()
117         for name in names:
118             dict[name] = getattr(self, name)
119
120         if self.pl_info:
121             dict['pl_info'] = self.pl_info
122
123         if self.geni_info:
124             dict['geni_info'] = self.geni_info
125
126         return dict
127
128     def dump(self, dump_parents=False):
129         print "RECORD", self.name
130         print "        hrn:", self.name
131         print "       type:", self.type
132         print "        gid:"
133         self.get_gid_object().dump(8, dump_parents)
134         print "    pointer:", self.pointer
135
136         print "  geni_info:"
137         geni_info = getattr(self, "geni_info", {})
138         if geni_info:
139             for key in geni_info.keys():
140                 print "       ", key, ":", geni_info[key]
141
142         print "    pl_info:"
143         pl_info = getattr(self, "pl_info", {})
144         if pl_info:
145             for key in pl_info.keys():
146                 print "       ", key, ":", pl_info[key]
147
148