diff options
author | Deepak S <deepak.s@linux.intel.com> | 2017-06-20 14:31:19 -0700 |
---|---|---|
committer | Ross Brattain <ross.b.brattain@intel.com> | 2017-08-08 08:54:23 -0700 |
commit | 5ce3b6f8c8b3217091e51a6041455738603d90b8 (patch) | |
tree | ca34e15a85d69e2b23ce498fead47761624ae42c /yardstick/network_services/vnf_generic/vnfdgen.py | |
parent | 72778951d6b8968f562fb8fefa02a57159ea1b83 (diff) |
NSB update
Refactored main NSB VNF classes accroding to class diagram
https://wiki.opnfv.org/display/yardstick/NSB+class+diagram
All the SampleVNFs have been separated and placed under
the SampleVNF class.
Added AutoConnectSSH to automatically create SSH conneciton on demand.
Added VnfdHelper class to wrap the VNFD dictionary in prepartion for
class-based modeling.
Extracted DpdkVnfSetupEnvHelper for DPDK based VNF setup.
Extracted Stats and other client config to ResourceHelper
Had to replace dict_key_flatten with deepgetitem due to Python 2.7
Jinja2 infinite recursion.
Change-Id: Ia8840e9c44cdbdf39aab6b02e6d2176b31937dc9
Signed-off-by: Deepak S <deepak.s@linux.intel.com>
Signed-off-by: Edward MacGillivray <edward.s.macgillivray@intel.com>
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
Diffstat (limited to 'yardstick/network_services/vnf_generic/vnfdgen.py')
-rw-r--r-- | yardstick/network_services/vnf_generic/vnfdgen.py | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/yardstick/network_services/vnf_generic/vnfdgen.py b/yardstick/network_services/vnf_generic/vnfdgen.py index b56a91915..0120b493e 100644 --- a/yardstick/network_services/vnf_generic/vnfdgen.py +++ b/yardstick/network_services/vnf_generic/vnfdgen.py @@ -14,11 +14,16 @@ """ Generic file to map and build vnf discriptor """ from __future__ import absolute_import -import collections +from functools import reduce import jinja2 +import logging import yaml +from yardstick.common.utils import try_int + +LOG = logging.getLogger(__name__) + def render(vnf_model, **kwargs): """Render jinja2 VNF template @@ -40,7 +45,8 @@ def generate_vnfd(vnf_model, node): as input for GenericVNF.__init__ """ # get is unused as global method inside template - node["get"] = get + # node["get"] = key_flatten_get + node["get"] = deepgetitem # Set Node details to default if not defined in pod file # we CANNOT use TaskTemplate.render because it does not allow # for missing variables, we need to allow password for key_filename @@ -52,36 +58,34 @@ def generate_vnfd(vnf_model, node): return filled_vnfd -def dict_key_flatten(data): - """ Convert nested dict structure to dotted key - (e.g. {"a":{"b":1}} -> {"a.b":1} - - :param data: nested dictionary - :return: flat dicrionary - """ - next_data = {} - - # check for non-string iterables - if not any((isinstance(v, collections.Iterable) and not isinstance(v, str)) - for v in data.values()): - return data +# dict_flatten was causing recursion errors with Jinja2 so we removed and replaced +# which this function from stackoverflow that doesn't require generating entire dictionaries +# each time we query a key +def deepgetitem(obj, item, default=None): + """Steps through an item chain to get the ultimate value. - for key, val in data.items(): - if isinstance(val, collections.Mapping): - for n_k, n_v in val.items(): - next_data["%s.%s" % (key, n_k)] = n_v - elif isinstance(val, collections.Iterable) and not isinstance(val, - str): - for index, item in enumerate(val): - next_data["%s%d" % (key, index)] = item - else: - next_data[key] = val + If ultimate value or path to value does not exist, does not raise + an exception and instead returns `fallback`. - return dict_key_flatten(next_data) + Based on + https://stackoverflow.com/a/38623359 + https://stackoverflow.com/users/1820042/donny-winston + add try_int to work with sequences -def get(obj, key, *args): - """ Get template key from dictionary, get default value or raise an exception + >>> d = {'snl_final': {'about': {'_icsd': {'icsd_id': 1, 'fr': [2, 3]}}}} + >>> deepgetitem(d, 'snl_final.about._icsd.icsd_id') + 1 + >>> deepgetitem(d, 'snl_final.about._sandbox.sbx_id') + >>> + >>> deepgetitem(d, 'snl_final.about._icsd.fr.1') + 3 """ - data = dict_key_flatten(obj) - return data.get(key, *args) + def getitem(obj, name): + # if integer then list index + name = try_int(name) + try: + return obj[name] + except (KeyError, TypeError, IndexError): + return default + return reduce(getitem, item.split('.'), obj) |