1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Generic file to map and build vnf discriptor """
16 from __future__ import absolute_import
18 from functools import reduce
23 from yardstick.common.task_template import finalize_for_yaml
24 from yardstick.common.utils import try_int
25 from yardstick.common.yaml_loader import yaml_load
27 LOG = logging.getLogger(__name__)
30 def render(vnf_model, **kwargs):
31 """Render jinja2 VNF template
32 Do not check for missing arguments
34 :param vnf_model: string that contains template
35 :param kwargs: Dict with template arguments
36 :returns:rendered template str
39 return jinja2.Template(vnf_model, finalize=finalize_for_yaml).render(**kwargs)
42 def generate_vnfd(vnf_model, node):
45 :param vnf_model: VNF definition template, e.g. tg_ping_tpl.yaml
46 :param node: node configuration taken from pod.yaml
47 :return: Complete VNF Descriptor that will be taken
48 as input for GenericVNF.__init__
50 # get is unused as global method inside template
51 # node["get"] = key_flatten_get
52 node["get"] = deepgetitem
53 # Set Node details to default if not defined in pod file
54 # we CANNOT use TaskTemplate.render because it does not allow
55 # for missing variables, we need to allow password for key_filename
57 rendered_vnfd = render(vnf_model, **node)
58 # This is done to get rid of issues with serializing node
60 filled_vnfd = yaml_load(rendered_vnfd)
64 # dict_flatten was causing recursion errors with Jinja2 so we removed and replaced
65 # which this function from stackoverflow that doesn't require generating entire dictionaries
66 # each time we query a key
67 def deepgetitem(obj, item, default=None):
68 """Steps through an item chain to get the ultimate value.
70 If ultimate value or path to value does not exist, does not raise
71 an exception and instead returns `fallback`.
74 https://stackoverflow.com/a/38623359
75 https://stackoverflow.com/users/1820042/donny-winston
77 add try_int to work with sequences
79 >>> d = {'snl_final': {'about': {'_icsd': {'icsd_id': 1, 'fr': [2, 3], '0': 24, 0: 4}}}}
80 >>> deepgetitem(d, 'snl_final.about._icsd.icsd_id')
82 >>> deepgetitem(d, 'snl_final.about._sandbox.sbx_id')
84 >>> deepgetitem(d, 'snl_final.about._icsd.fr.1')
86 >>> deepgetitem(d, 'snl_final.about._icsd.0')
89 def getitem(obj, name):
90 # try string then convert to int
93 except (KeyError, TypeError, IndexError):
97 except (KeyError, TypeError, IndexError):
99 return reduce(getitem, item.split('.'), obj)