Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 1 | ############################################################################## |
| 2 | # Copyright 2018 EuropeanSoftwareMarketingLtd. |
| 3 | # =================================================================== |
| 4 | # Licensed under the ApacheLicense, Version2.0 (the"License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # software distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and limitations under |
| 12 | # the License |
| 13 | ############################################################################## |
| 14 | # vnftest comment: this is a modified copy of |
| 15 | # yardstick/common/template_format.py |
| 16 | |
| 17 | from __future__ import absolute_import |
| 18 | |
| 19 | import yaml |
| 20 | from oslo_serialization import jsonutils |
| 21 | |
| 22 | if hasattr(yaml, 'CSafeLoader'): |
| 23 | # make a dynamic subclass so we don't override global yaml Loader |
| 24 | yaml_loader = type('HeatYamlLoader', (yaml.CSafeLoader,), {}) |
| 25 | else: |
| 26 | yaml_loader = type('HeatYamlLoader', (yaml.SafeLoader,), {}) |
| 27 | |
| 28 | if hasattr(yaml, 'CSafeDumper'): |
| 29 | yaml_dumper = yaml.CSafeDumper |
| 30 | else: |
| 31 | yaml_dumper = yaml.SafeDumper |
| 32 | |
| 33 | |
| 34 | # This breaks NetworkServiceTestCase yaml loading, because we need to conversion to |
| 35 | # native Python str() objects because we use use Trex and Trex is has broken unicode handling |
| 36 | def _construct_yaml_str(self, node): |
| 37 | # Override the default string handling function |
| 38 | # to always return unicode objects |
| 39 | return self.construct_scalar(node) |
| 40 | |
| 41 | yaml_loader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str) |
| 42 | # Unquoted dates like 2013-05-23 in yaml files get loaded as objects of type |
| 43 | # datetime.data which causes problems in API layer when being processed by |
| 44 | # openstack.common.jsonutils. Therefore, make unicode string out of timestamps |
| 45 | # until jsonutils can handle dates. |
| 46 | yaml_loader.add_constructor(u'tag:yaml.org,2002:timestamp', |
| 47 | _construct_yaml_str) |
| 48 | |
| 49 | |
| 50 | def parse(tmpl_str): |
| 51 | """Takes a string and returns a dict containing the parsed structure. |
| 52 | |
| 53 | This includes determination of whether the string is using the |
| 54 | JSON or YAML format. |
| 55 | """ |
| 56 | if tmpl_str.startswith('{'): |
| 57 | tpl = jsonutils.loads(tmpl_str) |
| 58 | else: |
| 59 | try: |
| 60 | # we already use SafeLoader when constructing special Heat YAML loader class |
| 61 | tpl = yaml.load(tmpl_str, Loader=yaml_loader) |
| 62 | except yaml.YAMLError as yea: |
| 63 | raise ValueError(yea) |
| 64 | else: |
| 65 | if tpl is None: |
| 66 | tpl = {} |
| 67 | # Looking for supported version keys in the loaded template |
| 68 | if not ('HeatTemplateFormatVersion' in tpl or |
| 69 | 'heat_template_version' in tpl or |
| 70 | 'AWSTemplateFormatVersion' in tpl): |
| 71 | raise ValueError("Template format version not found.") |
| 72 | return tpl |