[VVP] create new validation scripts
[vvp/validation-scripts.git] / ice_validator / tests / structures.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 #
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 '''structures
42 '''
43
44 import os
45
46 import yaml
47
48 from .utils import nested_dict
49
50 VERSION = '1.4.0'
51
52
53 class Heat(object):
54     """A Heat template.
55     filepath - absolute path to template file.
56     envpath - absolute path to environmnt file.
57     """
58     def __init__(self, filepath=None, envpath=None):
59         self.filepath = None
60         self.basename = None
61         self.dirname = None
62         self.yml = None
63         self.heat_template_version = None
64         self.description = None
65         self.parameter_groups = None
66         self.parameters = None
67         self.resources = None
68         self.outputs = None
69         self.conditions = None
70         if filepath:
71             self.load(filepath)
72         self.env = None
73         if envpath:
74             self.load_env(envpath)
75
76     def load(self, filepath):
77         """Load the Heat template given a filepath.
78         """
79         self.filepath = filepath
80         self.basename = os.path.basename(self.filepath)
81         self.dirname = os.path.dirname(self.filepath)
82         with open(self.filepath) as fi:
83             self.yml = yaml.load(fi)
84         self.heat_template_version = self.yml.get('heat_template_version',
85                                                   None)
86         self.description = self.yml.get('description', '')
87         self.parameter_groups = self.yml.get('parameter_groups', {})
88         self.parameters = self.yml.get('parameters', {})
89         self.resources = self.yml.get('resources', {})
90         self.outputs = self.yml.get('outputs', {})
91         self.conditions = self.yml.get('conditions', {})
92
93     def load_env(self, envpath):
94         """Load the Environment template given a envpath.
95         """
96         self.env = Env(filepath=envpath)
97
98     @staticmethod
99     def nested_get(dic, *keys):
100         """make utils.nested_dict.get available as a class method.
101         """
102         return nested_dict.get(dic, *keys)
103
104
105 class Env(Heat):
106     """An Environment file
107     """
108     pass
109
110
111 class Resource(object):
112     """A Resource
113     """
114     def __init__(self, resource_id=None, resource=None):
115         self.resource_id = resource_id or ''
116         self.resource = resource or {}
117
118     @staticmethod
119     def get_index_var(resource):
120         """Return the index_var for this resource.
121         """
122         index_var = nested_dict.get(
123                 resource,
124                 'properties',
125                 'index_var') or 'index'
126         return index_var