[VVP] create new validation scripts
[vvp/validation-scripts.git] / ice_validator / tests / utils / nested_files.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 """nested files
42 """
43
44 from os import path
45 import re
46 import yaml
47
48 VERSION = '1.0.2'
49
50
51 def get_list_of_nested_files(yml, dirpath):
52     '''
53     return a list of all nested files
54     '''
55
56     if not hasattr(yml, 'items'):
57         return []
58
59     nested_files = []
60
61     for v in yml.values():
62         if isinstance(v, dict) and "type" in v:
63             t = v["type"]
64             if t.endswith(".yml") or t.endswith(".yaml"):
65                 filepath = path.join(dirpath, t)
66                 with open(filepath) as fh:
67                     t_yml = yaml.load(fh)
68                 nested_files.append(filepath)
69                 nested_files.extend(get_list_of_nested_files(t_yml, dirpath))
70             elif t == "OS::Heat::ResourceGroup":
71                 rdt = (v.get("properties", {})
72                         .get("resource_def", {})
73                         .get("type", None))
74                 if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
75                     filepath = path.join(dirpath, rdt)
76                     with open(filepath) as fh:
77                         rdt_yml = yaml.load(fh)
78                     nested_files.append(filepath)
79                     nested_files.extend(
80                         get_list_of_nested_files(rdt_yml, dirpath))
81         if isinstance(v, dict):
82             nested_files.extend(
83                 get_list_of_nested_files(v, dirpath))
84         elif isinstance(v, list):
85             for d in v:
86                 nested_files.extend(
87                     get_list_of_nested_files(d, dirpath))
88     return nested_files
89
90
91 def check_for_invalid_nesting(yml, yaml_file, dirpath):
92     '''
93     return a list of all nested files
94     '''
95     if not hasattr(yml, 'items'):
96         return []
97     invalid_nesting = []
98     p = re.compile('^[A-z]*::[A-z]*::[A-z]*$')
99
100     for v in yml.values():
101         if isinstance(v, dict) and "type" in v:
102             t = v["type"]
103             if t.endswith(".yml") or t.endswith(".yaml"):
104                 filepath = path.join(dirpath, t)
105             elif t == "OS::Heat::ResourceGroup":
106                 rd = v["properties"]["resource_def"]
107                 if not isinstance(rd, dict) or "type" not in rd:
108                     invalid_nesting.append(yaml_file)
109                     continue
110                 elif not p.match(rd["type"]) and not (
111                         rd["type"].endswith(".yml")
112                         or rd["type"].endswith(".yaml")):
113                     filepath = path.join(dirpath, rd["type"])
114                 else:
115                     continue
116             else:
117                 continue
118             try:
119                 with open(filepath) as fh:
120                     yml = yaml.load(fh)
121             except yaml.YAMLError as e:
122                 invalid_nesting.append(filepath)
123                 print(e)    # pylint: disable=superfluous-parens
124             invalid_nesting.extend(check_for_invalid_nesting(
125                     yml,
126                     filepath,
127                     dirpath))
128         if isinstance(v, dict):
129             invalid_nesting.extend(check_for_invalid_nesting(
130                     v,
131                     yaml_file,
132                     dirpath))
133         elif isinstance(v, list):
134             for d in v:
135                 invalid_nesting.extend(check_for_invalid_nesting(
136                         d,
137                         yaml_file,
138                         dirpath))
139     return invalid_nesting