1 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 from attrdict import AttrDict
21 def config_load(file_name, from_cfg=None):
22 """Load a yaml file into a config dict, merge with from_cfg if not None
23 The config file content taking precedence in case of duplicate
26 with open(file_name) as fileobj:
27 cfg = AttrDict(yaml.safe_load(fileobj))
29 raise Exception("Configuration file at '{}' was not found. Please use correct path "
30 "and verify it is visible to container if you run nfvbench in container."
34 _validate_config(cfg, from_cfg)
40 def config_loads(cfg_text, from_cfg=None):
41 """Same as config_load but load from a string
44 cfg = AttrDict(yaml.load(cfg_text))
49 _validate_config(cfg, from_cfg)
54 def _get_err_config(subset, superset):
56 for k, v in subset.items():
59 elif v is not None and superset[k] is not None:
60 if not isinstance(v, type(superset[k])):
63 if isinstance(v, dict):
64 res = _get_err_config(v, superset[k])
66 result.update({k: res})
71 def _validate_config(subset, superset):
72 err_cfg = _get_err_config(subset, superset)
74 err_msg = 'Unknown options found in config file/string: ' + str(err_cfg)
76 raise Exception(err_msg)