93e1d481752f7ddb6c211c61e68b79cb2c6b85f9
[vvp/validation-scripts.git] / ice_validator / tests / utils / ports.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 #
39 from tests.structures import Heat
40 from tests.helpers import parameter_type_to_heat_type, prop_iterator
41 from . import nested_dict
42
43
44 def get_aap_exemptions(resource_props):
45     """
46     Gets the list of parameters that the Heat author has exempted from following
47     the naming conventions associated with AAP.
48
49     :param resource_props: dict of properties under the resource ID
50     :return: list of all parameters to exempt or an empty list
51     """
52     metadata = resource_props.get("metadata") or {}
53     return metadata.get("aap_exempt") or []
54
55
56 def check_parameter_format(yaml_file, regx, intext, resource_processor, *properties):
57     """
58     yaml_file: input file to check
59     regx: dictionary containing the regex to use to validate parameter
60     intext: internal or external
61     resource_processor: resource type specific helper, defined in structures.py
62     properties: arg list of property that is being checked
63     """
64
65     invalid_parameters = []
66     heat = Heat(filepath=yaml_file)
67     resource_type = resource_processor.resource_type
68     resources = heat.get_resource_by_type(resource_type)
69     heat_parameters = heat.parameters
70     for rid, resource in resources.items():
71         resource_intext, port_match = resource_processor.get_rid_match_tuple(rid)
72         if not port_match:
73             continue  # port resource ID not formatted correctely
74
75         if (
76             resource_intext != intext
77         ):  # skipping if type (internal/external) doesn't match
78             continue
79
80         for param in prop_iterator(resource, *properties):
81             if (
82                 param
83                 and isinstance(param, dict)
84                 and "get_resource" not in param
85                 and "get_attr" not in param
86             ):
87                 # checking parameter uses get_param
88                 parameter = param.get("get_param")
89                 if not parameter:
90                     msg = (
91                         "Unexpected parameter format for {} {} property {}: {}. "
92                         + "Please consult the heat guidelines documentation for details."
93                     ).format(resource_type, rid, properties, param)
94                     invalid_parameters.append(msg)  # should this be a failure?
95                     continue
96
97                 # getting parameter if the get_param uses list, and getting official HEAT parameter type
98                 parameter_type = parameter_type_to_heat_type(parameter)
99                 if parameter_type == "comma_delimited_list":
100                     parameter = parameter[0]
101                 elif parameter_type != "string":
102                     continue
103
104                 # checking parameter format = parameter type defined in parameters section
105                 heat_parameter_type = nested_dict.get(
106                     heat_parameters, parameter, "type"
107                 )
108                 if not heat_parameter_type or heat_parameter_type != parameter_type:
109                     msg = (
110                         "{} {} parameter {} defined as type {} "
111                         + "is being used as type {} in the heat template"
112                     ).format(
113                         resource_type,
114                         properties,
115                         parameter,
116                         heat_parameter_type,
117                         parameter_type,
118                     )
119                     invalid_parameters.append(msg)  # should this actually be an error?
120                     continue
121
122                 if parameter in get_aap_exemptions(resource):
123                     continue
124
125                 # if parameter type is not in regx dict, then it is not supported by automation
126                 regx_dict = regx[resource_intext].get(parameter_type)
127                 if not regx_dict:
128                     msg = (
129                         "WARNING: {} {} parameter {} defined as type {} "
130                         "is not supported by platform automation. If this VNF is not "
131                         "able to adhere to this requirement, please consult the Heat "
132                         "Orchestration Template guidelines for alternative solutions. "
133                         "If you are using an alternate option and wish to suppress "
134                         "error, then add the parameter to the aap_exempt list "
135                         "under this resources metadata."
136                     ).format(resource_type, properties, parameter, parameter_type)
137                     invalid_parameters.append(msg)
138                     continue
139
140                 # checking if param adheres to guidelines format
141                 regexp = regx[resource_intext][parameter_type]["machine"]
142                 readable_format = regx[resource_intext][parameter_type]["readable"]
143                 match = regexp.match(parameter)
144                 if not match:
145                     msg = (
146                         "{} {} property {} parameter {} does not follow {} format {} "
147                         "which is required by platform automation. If this VNF is not "
148                         "able to adhere to this requirement, please consult the Heat "
149                         "Orchestration Template guidelines for alternative solutions. "
150                         "If you are using an alternate option and wish to suppress "
151                         "error, then add the parameter to the aap_exempt list "
152                         "under this resources metadata."
153                     ).format(
154                         resource_type,
155                         rid,
156                         properties,
157                         parameter,
158                         resource_intext,
159                         readable_format,
160                     )
161                     invalid_parameters.append(msg)
162                     continue
163
164                 # checking that parameter includes correct vm_type/network_role
165                 parameter_checks = regx.get("parameter_to_resource_comparisons", [])
166                 for check in parameter_checks:
167                     resource_match = port_match.group(check)
168                     if (
169                         resource_match
170                         and not parameter.startswith(resource_match)
171                         and parameter.find("_{}_".format(resource_match)) == -1
172                     ):
173                         msg = (
174                             "{0} {1} property {2} parameter "
175                             "{3} {4} does match resource {4} {5}"
176                         ).format(
177                             resource_type,
178                             rid,
179                             properties,
180                             parameter,
181                             check,
182                             resource_match,
183                         )
184                         invalid_parameters.append(msg)
185                         continue
186
187     assert not invalid_parameters, "%s" % "\n".join(invalid_parameters)
188
189
190 def get_list_of_ports_attached_to_nova_server(nova_server):
191     networks_list = nova_server.get("properties", {}).get("networks")
192
193     port_ids = []
194     if networks_list:
195         for network in networks_list:
196             network_prop = network.get("port")
197             if network_prop:
198                 pid = network_prop.get("get_param")
199                 if not pid:
200                     pid = network_prop.get("get_resource")
201                 port_ids.append(pid)
202
203     return port_ids