3 # implements support for geni records stored in db tables
5 # TODO: Use existing PLC database methods? or keep this separate?
12 from pg import DB, ProgrammingError
14 from sfa.trust.gid import *
15 from sfa.util.record import *
16 from sfa.util.debug import *
18 GENI_TABLE_PREFIX = "geni$"
21 def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
22 global GENI_TABLE_PREFIX
26 # pgsql doesn't like table names with "." in them, to replace it with "$"
27 self.tablename = GENI_TABLE_PREFIX + self.hrn.replace(".", "$")
29 # establish a connection to the pgsql server
30 self.cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
32 # if asked to create the table, then create it
37 tableList = self.cnx.get_tables()
38 if 'public.' + self.tablename in tableList:
40 if 'public."' + self.tablename + '"' in tableList:
46 querystr = "CREATE TABLE " + self.tablename + " ( \
52 template = "CREATE INDEX %s_%s_idx ON %s (%s);"
53 indexes = [template % ( self.tablename, field, self.tablename, field) \
54 for field in ['name', 'type' ]]
55 # IF EXISTS doenst exist in postgres < 8.2
57 self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
58 except ProgrammingError:
60 self.cnx.query('DROP TABLE ' + self.tablename)
61 except ProgrammingError:
64 self.cnx.query(querystr)
68 def remove(self, record):
69 query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'"
70 self.cnx.query(query_str)
72 def insert(self, record):
73 fieldnames = ["key"] + record.get_field_names()
74 fieldvals = record.get_field_value_strings(fieldnames)
75 query_str = "INSERT INTO " + self.tablename + \
76 "(" + ",".join(fieldnames) + ") " + \
77 "VALUES(" + ",".join(fieldvals) + ")"
79 self.cnx.query(query_str)
81 def update(self, record):
82 names = record.get_field_names()
85 val = record.get_field_value_string(name)
86 pairs.append(name + " = " + val)
87 update = ", ".join(pairs)
89 query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
91 self.cnx.query(query_str)
93 def find_dict(self, type, value, searchfield):
94 query_str = "SELECT * FROM " + self.tablename + " WHERE " + searchfield + " = '" + str(value) + "'"
95 dict_list = self.cnx.query(query_str).dictresult()
97 for dict in dict_list:
98 if (type=="*") or (dict['type'] == type):
99 dict['hrn'] = dict['name']
100 result_dict_list.append(dict)
101 return result_dict_list
103 def find(self, type, value, searchfield):
104 result_dict_list = self.find_dict(type, value, searchfield)
106 for result in result_dict_list:
107 if result['type'] in ['authority']:
108 result_rec_list.append(AuthorityRecord(dict=result))
109 elif result['type'] in ['node']:
110 result_rec_list.append(NodeRecord(dict=result))
111 elif result['type'] in ['slice']:
112 result_rec_list.append(SliceRecord(dict=result))
113 elif result['type'] in ['user']:
114 result_rec_list.append(UserRecord(dict=result))
116 result_rec_list.append(GeniRecord(dict=result))
117 return result_rec_list
119 def resolve_dict(self, type, hrn):
120 return self.find_dict(type, hrn, "name")
122 def resolve(self, type, hrn):
123 return self.find(type, hrn, "name")
126 query_str = "SELECT * FROM " + self.tablename
127 result_dict_list = self.cnx.query(query_str).dictresult()
128 return result_dict_list
131 result_dict_list = self.list_dict()
133 for dict in result_dict_list:
134 result_rec_list.append(GeniRecord(dict=dict).as_dict())
135 return result_rec_list
137 def set_geni_table_prefix(x):
138 global GENI_TABLE_PREFIX
140 GENI_TABLE_PREFIX = x
142 def geni_records_purge(cninfo):
143 global GENI_TABLE_PREFIX
145 cnx = DB(cninfo['dbname'], cninfo['address'], port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
146 tableList = cnx.get_tables()
147 for table in tableList:
148 if table.startswith(GENI_TABLE_PREFIX) or \
149 table.startswith('public.' + GENI_TABLE_PREFIX) or \
150 table.startswith('public."' + GENI_TABLE_PREFIX):
151 report.trace("dropping table " + table)
152 cnx.query("DROP TABLE " + table)