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