/
db.py
156 lines (126 loc) · 4.95 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
"""
Service monitor DB to store VM, SI information
"""
import inspect
from cfgm_common import jsonutils as json
from cfgm_common.vnc_cassandra import VncCassandraClient
from sandesh_common.vns.constants import SVC_MONITOR_KEYSPACE_NAME
class ServiceMonitorDB(VncCassandraClient):
_KEYSPACE = SVC_MONITOR_KEYSPACE_NAME
_SVC_SI_CF = 'service_instance_table'
_LB_CF = 'pool_table'
def __init__(self, args, logger):
self._logger = logger
if args.reset_config:
reset_config = [self._KEYSPACE]
else:
reset_config = []
self._keyspaces = {
self._KEYSPACE: [
(self._SVC_SI_CF, None),
(self._LB_CF, None)
]
}
super(ServiceMonitorDB, self).__init__(args.cassandra_server_list,
None,
self._keyspaces,
self._logger.log,
reset_config=reset_config)
self._svc_si_cf = self._cf_dict[self._SVC_SI_CF]
self._lb_cf = self._cf_dict[self._LB_CF]
# db CRUD
def _db_get(self, table, key):
try:
entry = table.get(key)
except Exception:
self._logger.log("DB: %s %s get failed" %
(inspect.stack()[1][3], key))
return None
return entry
def _db_insert(self, table, key, entry):
try:
table.insert(key, entry)
except Exception:
self._logger.log("DB: %s %s insert failed" %
(inspect.stack()[1][3], key))
return False
return True
def _db_remove(self, table, key, columns=None):
try:
if columns:
table.remove(key, columns=columns)
else:
table.remove(key)
except Exception:
self._logger.log("DB: %s %s remove failed" %
(inspect.stack()[1][3], key))
return False
return True
def _db_list(self, table):
try:
entries = list(table.get_range())
except Exception:
self._logger.log("DB: %s list failed" %
(inspect.stack()[1][3]))
return None
return entries
def get_vm_db_prefix(self, inst_count):
return('vm' + str(inst_count) + '-')
def remove_vm_info(self, si_fq_str, vm_uuid):
si_info = self.service_instance_get(si_fq_str)
if not si_info:
return
prefix = None
for key, item in si_info.items():
if item == vm_uuid:
prefix = key.split('-')[0]
break
if not prefix:
return
vm_column_list = []
for key in si_info.keys():
if key.startswith(prefix):
vm_column_list.append(key)
self.service_instance_remove(si_fq_str, vm_column_list)
# service instance CRUD
def service_instance_get(self, si_fq_str):
return self._db_get(self._svc_si_cf, si_fq_str)
def service_instance_insert(self, si_fq_str, entry):
return self._db_insert(self._svc_si_cf, si_fq_str, entry)
def service_instance_remove(self, si_fq_str, columns=None):
return self._db_remove(self._svc_si_cf, si_fq_str, columns)
def service_instance_list(self):
return self._db_list(self._svc_si_cf)
def pool_config_get(self, pool_id):
json_str = self._db_get(self._lb_cf, pool_id)
if json_str and 'config_info' in json_str:
return json.loads(json_str['config_info'])
else:
return None
def pool_driver_info_get(self, pool_id):
json_str = self._db_get(self._lb_cf, pool_id)
if json_str and 'driver_info' in json_str:
return json.loads(json_str['driver_info'])
else:
return None
def pool_config_insert(self, pool_id, pool_obj):
entry = json.dumps(pool_obj)
return self._db_insert(self._lb_cf, pool_id, {'config_info': entry})
def pool_driver_info_insert(self, pool_id, pool_obj):
entry = json.dumps(pool_obj)
return self._db_insert(self._lb_cf, pool_id, {'driver_info': entry})
def pool_remove(self, pool_id, columns=None):
return self._db_remove(self._lb_cf, pool_id, columns)
def pool_list(self):
ret_list = []
for each_entry_id, each_entry_data in self._db_list(self._lb_cf) or []:
config_info_obj_dict = json.loads(each_entry_data['config_info'])
driver_info_obj_dict = None
if 'driver_info' in each_entry_data:
driver_info_obj_dict = json.loads(each_entry_data['driver_info'])
ret_list.append((each_entry_id, config_info_obj_dict, driver_info_obj_dict))
return ret_list