-# record.py
-#
-# implements support for geni records
+##
+# Implements support for geni records
#
# TODO: Use existing PLC database methods? or keep this separate?
+##
-from pg import DB
-
-GENI_TABLE_PREFIX = "geni$"
+import report
+from gid import *
-# Record is a tuple (Name, GID, Type, Info)
-# info is implemented as a pointer to a PLC record
+##
+# GeniRecord is a tuple (Name, GID, Type, Info)
+# info is comprised of the following sub-fields
+# pointer = a pointer to the record in the PL database
+# pl_info = planetlab-specific info (when talking to client)
+# geni_info = geni-specific info (when talking to client)
class GeniRecord():
def __init__(self, name=None, gid=None, type=None, pointer=None, dict=None):
- self.dirty=True
+ self.dirty = True
+ self.pl_info = None
+ self.geni_info = None
if name:
self.set_name(name)
if gid:
self.set_gid(dict['gid'])
self.set_type(dict['type'])
self.set_pointer(dict['pointer'])
+ if "pl_info" in dict:
+ self.set_pl_info(dict["pl_info"])
+ if "geni_info" in dict:
+ self.set_geni_info(dict["geni_info"])
def set_name(self, name):
self.name = name
self.dirty = True
def set_gid(self, gid):
- self.gid = gid
+ if isinstance(gid, str):
+ self.gid = gid
+ else:
+ self.gid = gid.save_to_string(save_parents=True)
self.dirty = True
def set_type(self, type):
self.pointer = pointer
self.dirty = True
+ def set_pl_info(self, pl_info):
+ self.pl_info = pl_info
+ self.dirty = True
+
+ def set_geni_info(self, geni_info):
+ self.geni_info = geni_info
+ self.dirty = True
+
+ def get_pl_info(self):
+ if self.pl_info:
+ return self.pl_info
+ else:
+ return {}
+
+ def get_geni_info(self):
+ if self.geni_info:
+ return self.geni_info
+ else:
+ return {}
+
+ def get_name(self):
+ return self.name
+
+ def get_type(self):
+ return self.type
+
+ def get_pointer(self):
+ return self.pointer
+
+ # TODO: not the best name for the function, because we have things called gidObjects in the Cred
+ def get_gid_object(self):
+ return GID(string=self.gid)
+
def get_key(self):
return self.name + "#" + self.type
dict = {}
names = self.get_field_names()
for name in names:
- dict[name] = self.getattr(name)
+ dict[name] = getattr(self, name)
+
+ if self.pl_info:
+ dict['pl_info'] = self.pl_info
+
+ if self.geni_info:
+ dict['geni_info'] = self.geni_info
+
return dict
-# GeniTable
-#
-# Represents a single table on a registry for a single authority.
-
-class GeniTable():
- def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
- global GENI_TABLE_PREFIX
-
- self.hrn = hrn
-
- # pgsql doesn't like table names with "." in them, to replace it with "$"
- self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
-
- # establish a connection to the pgsql server
- self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
-
- # if asked to create the table, then create it
- if create:
- self.create()
-
- def create(self):
- querystr = "CREATE TABLE " + self.tablename + " ( \
- key text, \
- name text, \
- gid text, \
- type text, \
- pointer integer);"
-
- self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
- self.cnx.query(querystr)
-
- def insert(self, record):
- fieldnames = ["key"] + record.get_field_names()
- fieldvals = record.get_field_value_strings(fieldnames)
- query_str = "INSERT INTO " + self.tablename + \
- "(" + ",".join(fieldnames) + ") " + \
- "VALUES(" + ",".join(fieldvals) + ")"
- #print query_str
- self.cnx.query(query_str)
-
- def update(self, record):
- names = record.get_field_names()
- pairs = []
- for name in names:
- val = record.get_field_value_string(name)
- pairs.append(name + " = " + val)
- update = ", ".join(pairs)
-
- query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
- #print query_str
- self.cnx.query(query_str)
-
- def resolve_raw(self, type, hrn):
- query_str = "SELECT * FROM " + self.tablename + " WHERE name = '" + hrn + "'"
- dict_list = self.cnx.query(query_str).dictresult()
- result_dict_list = []
- for dict in dict_list:
- if (type=="*") or (dict['type'] == type):
- result_dict_list.append(dict)
- return result_dict_list
-
- def resolve(self, type, hrn):
- result_dict_list = self.resolve_raw(type, hrn)
- result_rec_list = []
- for dict in result_dict_list:
- result_rec_list.append(GeniRecord(dict=dict))
- return result_rec_list
-
-def set_geni_table_prefix(x):
- global GENI_TABLE_PREFIX
-
- GENI_TABLE_PREFIX = x
-
-def geni_records_purge(cninfo):
- global GENI_TABLE_PREFIX
-
- cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
- tableList = cnx.get_tables()
- for table in tableList:
- if table.startswith(GENI_TABLE_PREFIX):
- cnx.query("DROP TABLE " + table)
+ def dump(self, dump_parents=False):
+ print "RECORD", self.name
+ print " hrn:", self.name
+ print " type:", self.type
+ print " gid:"
+ self.get_gid_object().dump(8, dump_parents)
+ print " pointer:", self.pointer
+
+ print " geni_info:"
+ geni_info = getattr(self, "geni_info", {})
+ if geni_info:
+ for key in geni_info.keys():
+ print " ", key, ":", geni_info[key]
+
+ print " pl_info:"
+ pl_info = getattr(self, "pl_info", {})
+ if pl_info:
+ for key in pl_info.keys():
+ print " ", key, ":", pl_info[key]
+
+