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
21 from yardstick.common.task_template import TaskTemplate
24 def generate_vnfd(vnf_model, node):
27 :param vnf_model: VNF definition template, e.g. tg_ping_tpl.yaml
28 :param node: node configuration taken from pod.yaml
29 :return: Complete VNF Descriptor that will be taken
30 as input for GenericVNF.__init__
33 rendered_vnfd = TaskTemplate.render(vnf_model, **node)
34 # This is done to get rid of issues with serializing node
36 filled_vnfd = yaml.load(rendered_vnfd)
40 def dict_key_flatten(data):
41 """ Convert nested dict structure to dotted key
42 (e.g. {"a":{"b":1}} -> {"a.b":1}
44 :param data: nested dictionary
45 :return: flat dicrionary
49 if not any((isinstance(v, collections.Iterable) and not isinstance(v, str))
50 for v in data.values()):
53 for key, val in six.iteritems(data):
54 if isinstance(val, collections.Mapping):
55 for n_k, n_v in six.iteritems(val):
56 next_data["%s.%s" % (key, n_k)] = n_v
57 elif isinstance(val, collections.Iterable) and not isinstance(val,
59 for index, item in enumerate(val):
60 next_data["%s%d" % (key, index)] = item
64 return dict_key_flatten(next_data)
67 def get(obj, key, *args):
68 """ Get template key from dictionary, get default value or raise an exception
70 data = dict_key_flatten(obj)
71 return data.get(key, *args)