Move utils.dict_to_keyval to opendaylight
This seems to be a data transformation specific to this driver, so let's move it there. Change-Id: I3a96fdafbc966732096f4db03cd7f035d169a978
This commit is contained in:
parent
90018e8bee
commit
ac4fa144ad
@ -20,7 +20,6 @@ from six.moves.urllib import parse as urlparse
|
||||
|
||||
from ceilometer.network.statistics import driver
|
||||
from ceilometer.network.statistics.opendaylight import client
|
||||
from ceilometer import utils
|
||||
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
@ -406,14 +405,36 @@ class OpenDayLightDriver(driver.Driver):
|
||||
return _get_int_sample('matchedCount', statistic, resource_id,
|
||||
resource_meta)
|
||||
|
||||
@staticmethod
|
||||
def _iter_flow(extractor, data):
|
||||
def dict_to_keyval(self, value, key_base=None):
|
||||
"""Expand a given dict to its corresponding key-value pairs.
|
||||
|
||||
Generated keys are fully qualified, delimited using dot notation.
|
||||
ie. key = 'key.child_key.grandchild_key[0]'
|
||||
"""
|
||||
val_iter, key_func = None, None
|
||||
if isinstance(value, dict):
|
||||
val_iter = six.iteritems(value)
|
||||
key_func = lambda k: key_base + '.' + k if key_base else k
|
||||
elif isinstance(value, (tuple, list)):
|
||||
val_iter = enumerate(value)
|
||||
key_func = lambda k: key_base + '[%d]' % k
|
||||
|
||||
if val_iter:
|
||||
for k, v in val_iter:
|
||||
key_gen = key_func(k)
|
||||
if isinstance(v, dict) or isinstance(v, (tuple, list)):
|
||||
for key_gen, v in self.dict_to_keyval(v, key_gen):
|
||||
yield key_gen, v
|
||||
else:
|
||||
yield key_gen, v
|
||||
|
||||
def _iter_flow(self, extractor, data):
|
||||
for flow_statistic in data['flow']['flowStatistics']:
|
||||
for statistic in flow_statistic['flowStatistic']:
|
||||
resource_meta = {'flow_id': statistic['flow']['id'],
|
||||
'table_id': statistic['tableId']}
|
||||
for key, value in utils.dict_to_keyval(statistic['flow'],
|
||||
'flow'):
|
||||
for key, value in self.dict_to_keyval(statistic['flow'],
|
||||
'flow'):
|
||||
resource_meta[key.replace('.', '_')] = value
|
||||
yield extractor(statistic,
|
||||
flow_statistic['node']['id'],
|
||||
|
@ -132,6 +132,23 @@ class TestOpenDayLightDriverSpecial(_Base):
|
||||
active_hosts_data = {"hostConfig": []}
|
||||
inactive_hosts_data = {"hostConfig": []}
|
||||
|
||||
def test_dict_to_kv(self):
|
||||
data = {'a': 'A',
|
||||
'b': 'B',
|
||||
'nested': {'a': 'A',
|
||||
'b': 'B',
|
||||
},
|
||||
'nested2': [{'c': 'A'}, {'c': 'B'}]
|
||||
}
|
||||
pairs = list(self.driver.dict_to_keyval(data))
|
||||
self.assertEqual([('a', 'A'),
|
||||
('b', 'B'),
|
||||
('nested.a', 'A'),
|
||||
('nested.b', 'B'),
|
||||
('nested2[0].c', 'A'),
|
||||
('nested2[1].c', 'B')],
|
||||
sorted(pairs, key=lambda x: x[0]))
|
||||
|
||||
def test_not_implemented_meter(self):
|
||||
sample_data = self.driver.get_sample_data('egg',
|
||||
self.fake_odl_url,
|
||||
|
@ -87,23 +87,6 @@ class TestUtils(base.BaseTestCase):
|
||||
def test_decimal_to_dt_with_none_parameter(self):
|
||||
self.assertIsNone(utils.decimal_to_dt(None))
|
||||
|
||||
def test_dict_to_kv(self):
|
||||
data = {'a': 'A',
|
||||
'b': 'B',
|
||||
'nested': {'a': 'A',
|
||||
'b': 'B',
|
||||
},
|
||||
'nested2': [{'c': 'A'}, {'c': 'B'}]
|
||||
}
|
||||
pairs = list(utils.dict_to_keyval(data))
|
||||
self.assertEqual([('a', 'A'),
|
||||
('b', 'B'),
|
||||
('nested.a', 'A'),
|
||||
('nested.b', 'B'),
|
||||
('nested2[0].c', 'A'),
|
||||
('nested2[1].c', 'B')],
|
||||
sorted(pairs, key=lambda x: x[0]))
|
||||
|
||||
def test_hash_of_set(self):
|
||||
x = ['a', 'b']
|
||||
y = ['a', 'b', 'a']
|
||||
|
@ -118,30 +118,6 @@ def decimal_to_dt(dec):
|
||||
return daittyme.replace(microsecond=int(round(micro)))
|
||||
|
||||
|
||||
def dict_to_keyval(value, key_base=None):
|
||||
"""Expand a given dict to its corresponding key-value pairs.
|
||||
|
||||
Generated keys are fully qualified, delimited using dot notation.
|
||||
ie. key = 'key.child_key.grandchild_key[0]'
|
||||
"""
|
||||
val_iter, key_func = None, None
|
||||
if isinstance(value, dict):
|
||||
val_iter = six.iteritems(value)
|
||||
key_func = lambda k: key_base + '.' + k if key_base else k
|
||||
elif isinstance(value, (tuple, list)):
|
||||
val_iter = enumerate(value)
|
||||
key_func = lambda k: key_base + '[%d]' % k
|
||||
|
||||
if val_iter:
|
||||
for k, v in val_iter:
|
||||
key_gen = key_func(k)
|
||||
if isinstance(v, dict) or isinstance(v, (tuple, list)):
|
||||
for key_gen, v in dict_to_keyval(v, key_gen):
|
||||
yield key_gen, v
|
||||
else:
|
||||
yield key_gen, v
|
||||
|
||||
|
||||
def hash_of_set(s):
|
||||
return str(hash(frozenset(s)))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user