[VVP] Added new three new reports
[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", {}).get("resource_def", {}).get("type", None)
72                 if rdt and (rdt.endswith(".yml") or rdt.endswith(".yaml")):
73                     filepath = path.join(dirpath, rdt)
74                     with open(filepath) as fh:
75                         rdt_yml = yaml.load(fh)
76                     nested_files.append(filepath)
77                     nested_files.extend(get_list_of_nested_files(rdt_yml, dirpath))
78         if isinstance(v, dict):
79             nested_files.extend(get_list_of_nested_files(v, dirpath))
80         elif isinstance(v, list):
81             for d in v:
82                 nested_files.extend(get_list_of_nested_files(d, dirpath))
83     return nested_files
84
85
86 def check_for_invalid_nesting(yml, yaml_file, dirpath):
87     """
88     return a list of all nested files
89     """
90     if not hasattr(yml, "items"):
91         return []
92     invalid_nesting = []
93     p = re.compile("^[A-z]*::[A-z]*::[A-z]*$")
94
95     for v in yml.values():
96         if isinstance(v, dict) and "type" in v:
97             t = v["type"]
98             if t.endswith(".yml") or t.endswith(".yaml"):
99                 filepath = path.join(dirpath, t)
100             elif t == "OS::Heat::ResourceGroup":
101                 rd = v["properties"]["resource_def"]
102                 if not isinstance(rd, dict) or "type" not in rd:
103                     invalid_nesting.append(yaml_file)
104                     continue
105                 elif not p.match(rd["type"]) and not (
106                     rd["type"].endswith(".yml") or rd["type"].endswith(".yaml")
107                 ):
108                     filepath = path.join(dirpath, rd["type"])
109                 else:
110                     continue
111             else:
112                 continue
113             try:
114                 with open(filepath) as fh:
115                     yml = yaml.load(fh)
116             except yaml.YAMLError as e:
117                 invalid_nesting.append(filepath)
118                 print(e)  # pylint: disable=superfluous-parens
119             invalid_nesting.extend(check_for_invalid_nesting(yml, filepath, dirpath))
120         if isinstance(v, dict):
121             invalid_nesting.extend(check_for_invalid_nesting(v, yaml_file, dirpath))
122         elif isinstance(v, list):
123             for d in v:
124                 invalid_nesting.extend(check_for_invalid_nesting(d, yaml_file, dirpath))
125     return invalid_nesting