[VVP] Adding tests for new reqs from VNFRQTS-630
[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 check_parameter_format(yaml_file, regx, intext, resource_processor, *properties):
45     """
46     yaml_file: input file to check
47     regx: dictionary containing the regex to use to validate parameter
48     intext: internal or external
49     resource_processor: resource type specific helper, defined in structures.py
50     properties: arg list of property that is being checked
51     """
52
53     invalid_parameters = []
54     heat = Heat(filepath=yaml_file)
55     resource_type = resource_processor.resource_type
56     resources = heat.get_resource_by_type(resource_type)
57     heat_parameters = heat.parameters
58     for rid, resource in resources.items():
59         resource_intext, port_match = resource_processor.get_rid_match_tuple(rid)
60         if not port_match:
61             continue  # port resource ID not formatted correctely
62
63         if (
64             resource_intext != intext
65         ):  # skipping if type (internal/external) doesn't match
66             continue
67
68         for param in prop_iterator(resource, *properties):
69             if (
70                 param
71                 and isinstance(param, dict)
72                 and "get_resource" not in param
73                 and "get_attr" not in param
74             ):
75
76                 # checking parameter uses get_param
77                 parameter = param.get("get_param")
78                 if not parameter:
79                     msg = (
80                         "Unexpected parameter format for {} {} property {}: {}. "
81                         + "Please consult the heat guidelines documentation for details."
82                     ).format(resource_type, rid, properties, param)
83                     invalid_parameters.append(msg)  # should this be a failure?
84                     continue
85
86                 # getting parameter if the get_param uses list, and getting official HEAT parameter type
87                 parameter_type = parameter_type_to_heat_type(parameter)
88                 if parameter_type == "comma_delimited_list":
89                     parameter = parameter[0]
90                 elif parameter_type != "string":
91                     continue
92
93                 # checking parameter format = parameter type defined in parameters section
94                 heat_parameter_type = nested_dict.get(heat_parameters, parameter, "type")
95                 if not heat_parameter_type or heat_parameter_type != parameter_type:
96                     msg = (
97                         "{} {} parameter {} defined as type {} "
98                         + "is being used as type {} in the heat template"
99                     ).format(
100                         resource_type, properties, parameter, heat_parameter_type, parameter_type
101                     )
102                     invalid_parameters.append(msg)  # should this actually be an error?
103                     continue
104
105                 # if parameter type is not in regx dict, then it is not supported by automation
106                 regx_dict = regx[resource_intext].get(parameter_type)
107                 if not regx_dict:
108                     msg = (
109                         "WARNING: {} {} parameter {} defined as type {} "
110                         "is not supported by platform automation. If this VNF is not able "
111                         "to adhere to this requirement, please consult the Heat Orchestration "
112                         "Template guidelines for alternative solutions. If already adhering to "
113                         "an alternative provided by the heat guidelines, please disregard this "
114                         "message."
115                     ).format(resource_type, properties, parameter, parameter_type)
116                     invalid_parameters.append(msg)
117                     continue
118
119                 # checking if param adheres to guidelines format
120                 regexp = regx[resource_intext][parameter_type]["machine"]
121                 readable_format = regx[resource_intext][parameter_type]["readable"]
122                 match = regexp.match(parameter)
123                 if not match:
124                     msg = "{} {} property {} parameter {} does not follow {} format {}".format(
125                         resource_type, rid, properties, parameter, resource_intext, readable_format
126                     )
127                     invalid_parameters.append(msg)
128                     continue
129
130                 # checking that parameter includes correct vm_type/network_role
131                 parameter_checks = regx.get("parameter_to_resource_comparisons", [])
132                 for check in parameter_checks:
133                     resource_match = port_match.group(check)
134                     if (
135                         resource_match
136                         and not parameter.startswith(resource_match)
137                         and parameter.find("_{}_".format(resource_match)) == -1
138                     ):
139                         msg = (
140                             "{0} {1} property {2} parameter "
141                             "{3} {4} does match resource {4} {5}"
142                         ).format(resource_type, rid, properties, parameter, check, resource_match)
143                         invalid_parameters.append(msg)
144                         continue
145
146     assert not invalid_parameters, "%s" % "\n".join(invalid_parameters)
147
148
149 def get_list_of_ports_attached_to_nova_server(nova_server):
150     networks_list = nova_server.get("properties", {}).get("networks")
151
152     port_ids = []
153     if networks_list:
154         for network in networks_list:
155             network_prop = network.get("port")
156             if network_prop:
157                 pid = network_prop.get("get_param")
158                 if not pid:
159                     pid = network_prop.get("get_resource")
160                 port_ids.append(pid)
161
162     return port_ids