a07225bd6137fcd6b0a4ca8f6cc35541d9b064f4
[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 import re
40
41 from tests.utils.network_roles import get_network_type_from_port
42 from tests.parametrizers import get_nested_files
43
44 from .structures import Heat
45 from .helpers import validates, load_yaml, get_base_template_from_yaml_files
46 from .utils.ports import check_parameter_format
47 from tests.structures import NeutronPortProcessor
48
49 VERSION = "1.3.0"
50
51 RE_EXTERNAL_PARAM_SUBNET = re.compile(  # match pattern
52     r"(?P<network_role>.+?)(_v6)?_subnet_id$"
53 )
54
55 RE_INTERNAL_PARAM_SUBNET = re.compile(  # match pattern
56     r"int_(?P<network_role>.+?)(_v6)?_subnet_id$"
57 )
58
59 fip_regx_dict = {
60     "external": {
61         "string": {
62             "readable": "{network-role}_subnet_id or {network-role}_v6_subnet_id",
63             "machine": RE_EXTERNAL_PARAM_SUBNET,
64         }
65     },
66     "internal": {
67         "string": {
68             "readable": "int_{network-role}_subnet_id or int_{network-role}_v6_subnet_id",
69             "machine": RE_INTERNAL_PARAM_SUBNET,
70         }
71     },
72     "parameter_to_resource_comparisons": ["network_role"],
73 }
74
75
76 @validates("R-38236", "R-84123", "R-76160")
77 def test_internal_subnet_format(yaml_file):
78     check_parameter_format(yaml_file, fip_regx_dict, "internal", NeutronPortProcessor, "fixed_ips", "subnet")
79
80
81 @validates("R-38236", "R-62802", "R-15287")
82 def test_external_subnet_format(yaml_file):
83     check_parameter_format(yaml_file, fip_regx_dict, "external", NeutronPortProcessor, "fixed_ips", "subnet")
84
85
86 @validates("R-84123", "R-76160")
87 def test_neutron_port_internal_fixed_ips_subnet_in_base(yaml_files):
88     """
89     Only check parent incremental modules, because nested file parameter
90     name may have been changed.
91
92     When
93
94       * the VNF's Heat Orchestration Template's
95         resource ``OS::Neutron::Port`` in an Incremental Module is attaching
96         to an internal network
97         that is created in the Base Module, AND
98       * an IPv4 address is being cloud assigned by OpenStack's DHCP Service AND
99       * the internal network IPv4 subnet is to be specified
100         using the property ``fixed_ips`` map property ``subnet``/``subnet_id``,
101
102     the parameter **MUST** follow the naming convention
103
104       * ``int_{network-role}_subnet_id``
105     an IPv6 address is being cloud assigned by OpenStack's DHCP Service AND
106       * ``int_{network-role}_v6_subnet_id``
107
108     Note that the parameter MUST be defined as an output parameter in
109     the base module.
110     """
111
112     base_path = get_base_template_from_yaml_files(yaml_files)
113     base_heat = load_yaml(base_path)
114     base_outputs = base_heat.get("outputs") or {}
115     nested_template_paths = get_nested_files(yaml_files)
116     errors = []
117
118     for yaml_file in yaml_files:
119         if yaml_file == base_path or yaml_file in nested_template_paths:
120             continue  # Only applies to incremental modules
121         heat = Heat(filepath=yaml_file)
122         internal_ports = {
123             r_id: p
124             for r_id, p in heat.neutron_port_resources.items()
125             if get_network_type_from_port(p) == "internal"
126         }
127         for r_id, port in internal_ports.items():
128             props = port.get("properties") or {}
129             fip_list = props.get("fixed_ips") or []
130             if not isinstance(fip_list, list):
131                 continue
132             for ip in fip_list:
133                 subnet = ip.get("subnet")
134                 if not subnet:
135                     continue
136
137                 if "get_param" not in subnet:
138                     continue
139                 param = subnet.get("get_param")
140                 if param not in base_outputs:
141                     errors.append(
142                         (
143                             "Internal fixed_ips/subnet parameter {} is attached to port {}, but the subnet parameter "
144                             "is not defined as an output in the base module ({})."
145                         ).format(param, r_id, base_path)
146                     )
147
148     assert not errors, " ".join(errors)