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 *
20 GENI_TABLE_PREFIX = "sfa$"
22 def __init__(self, create=False, hrn="unspecified.default.registry", cninfo=None):
26 # pgsql doesn't like table names with "." in them, to replace it with "$"
27 self.tablename = GeniTable.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 date_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, \
53 last_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP);"
54 template = "CREATE INDEX %s_%s_idx ON %s (%s);"
55 indexes = [template % ( self.tablename, field, self.tablename, field) \
56 for field in ['key', 'hrn', 'type','pointer']]
57 # IF EXISTS doenst exist in postgres < 8.2
59 self.cnx.query('DROP TABLE IF EXISTS ' + self.tablename)
60 except ProgrammingError:
62 self.cnx.query('DROP TABLE ' + self.tablename)
63 except ProgrammingError:
66 self.cnx.query(querystr)
70 def remove(self, record):
71 query_str = "DELETE FROM " + self.tablename + " WHERE key = '" + record.get_key() + "'"
72 self.cnx.query(query_str)
74 def insert(self, record):
75 dont_insert = ['date_created', 'last_updated']
76 fields = [field for field in record.keys() if field not in dont_insert]
77 fieldnames = ["key"] + fields
78 fieldvals = record.get_field_value_strings(fieldnames)
79 query_str = "INSERT INTO " + self.tablename + \
80 "(" + ",".join(fieldnames) + ") " + \
81 "VALUES(" + ",".join(fieldvals) + ")"
83 self.cnx.query(query_str)
85 def update(self, record):
86 names = record.get_field_names()
89 val = record.get_field_value_string(name)
90 pairs.append(name + " = " + val)
91 update = ", ".join(pairs)
93 query_str = "UPDATE " + self.tablename+ " SET " + update + " WHERE key = '" + record.get_key() + "'"
95 self.cnx.query(query_str)
97 def find_dict(self, type, value, searchfield):
98 query_str = "SELECT * FROM " + self.tablename + " WHERE " + searchfield + " = '" + str(value) + "'"
99 dict_list = self.cnx.query(query_str).dictresult()
100 result_dict_list = []
101 for dict in dict_list:
102 if (type=="*") or (dict['type'] == type):
103 result_dict_list.append(dict)
104 return result_dict_list
106 def find(self, type, value, searchfield):
107 result_dict_list = self.find_dict(type, value, searchfield)
109 for result in result_dict_list:
110 if result['type'] in ['authority']:
111 result_rec_list.append(AuthorityRecord(dict=result))
112 elif result['type'] in ['node']:
113 result_rec_list.append(NodeRecord(dict=result))
114 elif result['type'] in ['slice']:
115 result_rec_list.append(SliceRecord(dict=result))
116 elif result['type'] in ['user']:
117 result_rec_list.append(UserRecord(dict=result))
119 result_rec_list.append(GeniRecord(dict=result))
120 return result_rec_list
122 def resolve_dict(self, type, hrn):
123 return self.find_dict(type, hrn, "hrn")
125 def resolve(self, type, hrn):
126 return self.find(type, hrn, "hrn")
129 query_str = "SELECT * FROM " + self.tablename
130 result_dict_list = self.cnx.query(query_str).dictresult()
131 return result_dict_list
134 result_dict_list = self.list_dict()
136 for dict in result_dict_list:
137 result_rec_list.append(GeniRecord(dict=dict).as_dict())
138 return result_rec_list
141 def geni_records_purge(cninfo):
143 cnx = DB(cninfo['dbname'], cninfo['address'],
144 port=cninfo['port'], user=cninfo['user'], passwd=cninfo['password'])
145 tableList = cnx.get_tables()
146 for table in tableList:
147 if table.startswith(GeniTable.GENI_TABLE_PREFIX) or \
148 table.startswith('public.' + GeniTable.GENI_TABLE_PREFIX) or \
149 table.startswith('public."' + GeniTable.GENI_TABLE_PREFIX):
150 report.trace("dropping table " + table)
151 cnx.query("DROP TABLE " + table)