[VVP] updating OS::Neutron::Port parameter tests
[vvp/validation-scripts.git] / ice_validator / tests / test_neutron_port_fixed_ips_subnet.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
40 """
41 resources:
42 {vm-type}_{vm-type_index}_{network-role}_port_{port-index}:
43   type: OS::Neutron::Port
44   properties:
45     network: { get_param: ...}
46     fixed_ips: [ { "ipaddress": { get_param: ... } } ]
47     binding:vnic_type: direct           #only SR-IOV ports, not OVS ports
48     value_specs: {
49       vlan_filter: { get_param: ... },  #all NC ports
50       public_vlans: { get_param: ... }, #all NC ports
51       private_vlans: { get_param: ... },#all NC ports
52       guest_vlans: { get_param: ... },  #SR-IOV Trunk Port only
53       vlan_mirror: { get_param: ... },  #SRIOV Trunk Port
54                                         # Receiving Mirrored Traffic only
55      ATT_FABRIC_CONFIGURATION_REQUIRED: true #all NC ports
56     }
57   metadata:
58     port_type: SR-IOV_Trunk             #SR-IOV Trunk Port
59     port_type: SR-IOV_Non_Trunk         #SR-IOV Non Trunk Port
60     port_type: OVS                      #OVS Port
61     port_type: SR-IOV_Mirrored_Trunk    #SR-IOV Trunk Port
62                                         # Receiving Mirrored Traffic
63 """
64 import re
65
66 from tests.utils.network_roles import get_network_type_from_port
67 from tests.parametrizers import get_nested_files
68
69 from .structures import Heat
70 from .helpers import validates, load_yaml, get_base_template_from_yaml_files
71 from .utils.ports import check_ip_format
72
73 VERSION = "1.3.0"
74
75 RE_EXTERNAL_PARAM_SUBNET = re.compile(  # match pattern
76     r"(?P<network_role>.+[^(v6)])(_v6)?_subnet_id$"
77 )
78
79 RE_INTERNAL_PARAM_SUBNET = re.compile(  # match pattern
80     r"int_(?P<network_role>.+[^(v6)])(_v6)?_subnet_id$"
81 )
82
83 fip_regx_dict = {
84     "external": {
85         "string": {
86             "readable": "{network-role}_subnet_id or {network-role}_v6_subnet_id",
87             "machine": RE_EXTERNAL_PARAM_SUBNET,
88         }
89     },
90     "internal": {
91         "string": {
92             "readable": "int_{network-role}_subnet_id or int_{network-role}_v6_subnet_id",
93             "machine": RE_INTERNAL_PARAM_SUBNET,
94         }
95     },
96     "parameter_to_resource_comparisons": ["network_role"],
97 }
98
99
100 @validates("R-38236", "R-84123", "R-76160")
101 def test_internal_subnet_format(yaml_file):
102     check_ip_format(yaml_file, fip_regx_dict, "internal", "fixed_ips", "subnet")
103
104
105 @validates("R-38236", "R-62802", "R-15287")
106 def test_external_subnet_format(yaml_file):
107     check_ip_format(yaml_file, fip_regx_dict, "external", "fixed_ips", "subnet")
108
109
110 @validates("R-84123", "R-76160")
111 def test_neutron_port_internal_fixed_ips_subnet_in_base(yaml_files):
112     """
113     Only check parent incremental modules, because nested file parameter
114     name may have been changed.
115
116     When
117
118       * the VNF's Heat Orchestration Template's
119         resource ``OS::Neutron::Port`` in an Incremental Module is attaching
120         to an internal network
121         that is created in the Base Module, AND
122       * an IPv4 address is being cloud assigned by OpenStack's DHCP Service AND
123       * the internal network IPv4 subnet is to be specified
124         using the property ``fixed_ips`` map property ``subnet``/``subnet_id``,
125
126     the parameter **MUST** follow the naming convention
127
128       * ``int_{network-role}_subnet_id``
129     an IPv6 address is being cloud assigned by OpenStack's DHCP Service AND
130       * ``int_{network-role}_v6_subnet_id``
131
132     Note that the parameter MUST be defined as an output parameter in
133     the base module.
134     """
135
136     base_path = get_base_template_from_yaml_files(yaml_files)
137     base_heat = load_yaml(base_path)
138     base_outputs = base_heat.get("outputs") or {}
139     nested_template_paths = get_nested_files(yaml_files)
140     errors = []
141
142     for yaml_file in yaml_files:
143         if yaml_file == base_path or yaml_file in nested_template_paths:
144             continue  # Only applies to incremental modules
145         heat = Heat(filepath=yaml_file)
146         internal_ports = {
147             r_id: p
148             for r_id, p in heat.neutron_port_resources.items()
149             if get_network_type_from_port(p) == "internal"
150         }
151         for r_id, port in internal_ports.items():
152             props = port.get("properties") or {}
153             fip_list = props.get("fixed_ips") or []
154             if not isinstance(fip_list, list):
155                 continue
156             for ip in fip_list:
157                 subnet = ip.get("subnet")
158                 if not subnet:
159                     continue
160
161                 if "get_param" not in subnet:
162                     continue
163                 param = subnet.get("get_param")
164                 if param not in base_outputs:
165                     errors.append(
166                         (
167                             "Internal fixed_ips/subnet parameter {} is attached to port {}, but the subnet parameter "
168                             "is not defined as an output in the base module ({})."
169                         ).format(param, r_id, base_path)
170                     )
171
172     assert not errors, " ".join(errors)