Create a Dragonflow CLI tool for retrieving DB data

This patch introduce a tool to genericly read Dragonflow DB
data and fetch entries values.
Its only a first version, more work is needed to allow this
tool to also read the internal controller cache.

Change-Id: I16d1dd9c8756734c481111cd606ccf9eece0e0c3
This commit is contained in:
Gal Sagie 2015-11-29 10:19:07 +02:00
parent 762420bb59
commit 2b27c85f7f
5 changed files with 120 additions and 1 deletions

View File

@ -101,6 +101,15 @@ class DbApi(object):
:returns: list of values
"""
@abc.abstractmethod
def get_all_keys(self, table):
"""Returns a list of all table entries keys
:param table: table name
:type table: string
:returns: list of keys
"""
@abc.abstractmethod
def wait_for_db_changes(self, callback):
"""Wait for DB changes on caller context, DB should call

100
dragonflow/db/df_db.py Normal file
View File

@ -0,0 +1,100 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
from oslo_config import cfg
from oslo_utils import importutils
from neutron.common import config as common_config
from dragonflow.common import common_params
cfg.CONF.register_opts(common_params.df_opts, 'df')
db_tables = ['lport', 'lswitch', 'lrouter', 'chassis', 'secgroup',
'tunnel_key']
usage_str = "The following commands are supported:\n" \
"1) df_db ls - print all the db tables \n" \
"2) df_db ls <table_name> - print all the keys for specific table \n" \
"3) df_db get <table_name> <key> - print value for specific key"
def print_tables():
print (' ')
print ('DB Tables')
print ('----------')
for table in db_tables:
print table
print(' ')
def print_table(db_driver, table):
keys = db_driver.get_all_keys(table)
print (' ')
print ('Keys for table ' + table)
print ('------------------------------------------------------------')
for key in keys:
print key
print (' ')
def print_key(db_driver, table, key):
value = db_driver.get_key(table, key)
print (' ')
print ('Table = ' + table + ' , Key = ' + key)
print ('------------------------------------------------------------')
print value
print (' ')
def main():
if len(sys.argv) < 2:
print usage_str
return
common_config.init(['--config-file', '/etc/neutron/neutron.conf'])
db_driver_class = importutils.import_class(cfg.CONF.df.nb_db_class)
db_driver = db_driver_class()
db_driver.initialize(db_ip=cfg.CONF.df.remote_db_ip,
db_port=cfg.CONF.df.remote_db_port)
action = sys.argv[1]
if action == 'ls':
if len(sys.argv) == 2:
print_tables()
return
table = sys.argv[2]
if table not in db_tables:
print "<table> must be one of the following:"
print db_tables
return
print_table(db_driver, table)
return
if action == 'get':
table = sys.argv[2]
if len(sys.argv) < 5:
print "must supply a key"
print usage_str
return
key = sys.argv[3]
print_key(db_driver, table, key)
return
print usage_str
if __name__ == "__main__":
main()

View File

@ -57,6 +57,14 @@ class EtcdDbDriver(db_api.DbApi):
res.append(entry.value)
return res
def get_all_keys(self, table):
res = []
directory = self.client.get("/" + table)
for entry in directory.children:
table_name_size = len(table) + 2
res.append(entry.key[table_name_size:])
return res
def _allocate_unique_key(self):
key = '/tunnel_key/key'
prev_value = 0

View File

@ -49,3 +49,4 @@ neutron.ml2.mechanism_drivers =
console_scripts =
neutron-l3-controller-agent = dragonflow.neutron.agent.l3_sdn_agent:main
neutron-l2-controller-agent = dragonflow.neutron.agent.l2.ovs_dragonflow_neutron_agent:main
df-db = dragonflow.db.df_db:main

View File

@ -40,7 +40,8 @@ commands = python setup.py build_sphinx
# H404 multi line docstring should start with a summary
# H405 multi line docstring summary not separated with an empty line
# H904 Wrap long lines in parentheses instead of a backslash
ignore = E125,E126,E128,E129,E265,H301,H305,H307,H402,H404,H405,H904
# H233 Python 3.x incompatible use of print operator
ignore = E125,E126,E128,E129,E265,H301,H305,H307,H402,H404,H405,H904,H233
show-source = true
# TODO(dougw) neutron/tests/unit/vmware exclusion is a temporary services split hack
exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,tools,.ropeproject,rally-scenarios,neutron/tests/unit/vmware*