2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
43 {vm-type}_{vm-type_index}_{network-role}_port_{port-index}:
44 type: OS::Neutron::Port
46 network: { get_param: ...}
47 fixed_ips: [ { "ipaddress": { get_param: ... } } ]
48 binding:vnic_type: direct #only SR-IOV ports, not OVS ports
50 vlan_filter: { get_param: ... }, #all NC ports
51 public_vlans: { get_param: ... }, #all NC ports
52 private_vlans: { get_param: ... },#all NC ports
53 guest_vlans: { get_param: ... }, #SR-IOV Trunk Port only
54 vlan_mirror: { get_param: ... }, #SRIOV Trunk Port
55 # Receiving Mirrored Traffic only
56 ATT_FABRIC_CONFIGURATION_REQUIRED: true #all NC ports
59 port_type: SR-IOV_Trunk #SR-IOV Trunk Port
60 port_type: SR-IOV_Non_Trunk #SR-IOV Non Trunk Port
61 port_type: OVS #OVS Port
62 port_type: SR-IOV_Mirrored_Trunk #SR-IOV Trunk Port
63 # Receiving Mirrored Traffic
71 from .structures import Heat
72 from .helpers import validates
76 RE_BASE = re.compile(r"(^base$)|(^base_)|(_base_)|(_base$)") # search pattern
77 RE_NEUTRON_PORT_RID = re.compile( # match pattern
79 r"_(?P<vm_type_index>\d+)"
80 r"_(?P<network_role>.+)"
82 r"(?P<port_index>\d+)"
85 RE_INTERNAL_NETWORK_PARAM = re.compile( # match pattern
86 r"int_(?P<network_role>.+)_net_(?P<value_type>id|name)$"
88 RE_INTERNAL_NETWORK_RID = re.compile( # match pattern
89 r"int_(?P<network_role>.+)_network$"
93 def get_base_template_filepath(yaml_files):
94 """Return first filepath to match RE_BASE
96 for filepath in yaml_files:
97 basename, __ = os.path.splitext(os.path.basename(filepath))
98 if RE_BASE.search(basename) and basename.find("volume") == -1:
103 def get_internal_network(yaml_files):
104 """Return the base template's Heat istance.
106 base_template_filepath = get_base_template_filepath(yaml_files)
107 if base_template_filepath is None:
108 pytest.skip("No base template found")
109 base_template = Heat(filepath=base_template_filepath)
110 for r in base_template.resources.values():
111 # if base_template.nested_get(r, 'type') == 'OS::Neutron::Net':
117 def get_neutron_ports(heat):
118 """Return dict of resource_id: resource, whose type is
123 for rid, resource in heat.resources.items()
124 if heat.nested_get(resource, "type") == "OS::Neutron::Port"
128 # pylint: disable=invalid-name
131 @validates("R-86182", "R-22688")
132 def test_neutron_port_internal_network(yaml_files):
134 When the VNF's Heat Orchestration Template's Resource
135 ``OS::Neutron::Port`` is attaching to an internal network,
136 and the internal network is created in a
137 different Heat Orchestration Template than the ``OS::Neutron::Port``,
138 the ``network`` parameter name **MUST**
140 * follow the naming convention ``int_{network-role}_net_id``
142 network UUID value is used to reference the network
143 * follow the naming convention ``int_{network-role}_net_name`` if the
144 OpenStack network name in is used to reference the network.
146 where ``{network-role}`` is the network-role of the internal network and
147 a ``get_param`` **MUST** be used as the intrinsic function.
149 In Requirement R-86182, the internal network is created in the VNF's
150 Base Module (Heat Orchestration Template) and the parameter name is
151 declared in the Base Module's ``outputs`` section.
152 When the parameter's value uses a "get_param" function, its name
153 must end in "_name", and when it uses a "get_resource" function,
154 its name must end in "_id".
156 The output parameter name will be declared as a parameter in the
157 ``parameters`` section of the incremental module.
159 internal_network = get_internal_network(yaml_files)
160 if not internal_network:
161 pytest.skip("internal_network template not found")
163 if not internal_network.outputs:
164 pytest.skip('internal_network template has no "outputs"')
166 for filepath in yaml_files:
167 if filepath != internal_network.filepath:
168 validate_neutron_port(filepath, internal_network)
171 def validate_neutron_port(filepath, internal_network):
172 """validate the neutron port
174 heat = Heat(filepath=filepath)
175 if not heat.resources:
177 neutron_ports = get_neutron_ports(heat)
178 if not neutron_ports:
181 for rid, resource in neutron_ports.items():
182 if not heat.parameters:
183 bad[rid] = 'missing "parameters"'
185 network = heat.nested_get(resource, "properties", "network", "get_param")
187 bad[rid] = 'missing "network.get_param"'
189 if not network.startswith("int_"):
190 continue # not an internal network port
191 error = validate_param(heat, network, internal_network)
196 "Bad OS::Neutron::Port: %s"
197 % (", ".join("%s: %s" % (rid, error) for rid, error in bad.items()))
201 def validate_param(heat, network, internal_network):
202 """Ensure network (the parameter name) is defined in the base
203 template, and has the correct value function. Ensure its
204 network-role is found in the base template in some
205 OS::Neutron::Net resource.
206 Return error message string, or None if no no errors.
208 match = RE_INTERNAL_NETWORK_PARAM.match(network)
210 return 'network.get_param "%s" does not match "%s"' % (
212 RE_INTERNAL_NETWORK_PARAM.pattern,
214 if heat.nested_get(heat.parameters, network) is None:
215 return "missing parameters.%s" % network
216 output = heat.nested_get(internal_network.outputs, network)
218 return 'network.get_param "%s"' " not found in base template outputs" % network
219 param_dict = match.groupdict()
220 expect = {"name": "get_param", "id": "get_resource"}[param_dict["value_type"]]
221 value = heat.nested_get(output, "value")
222 if heat.nested_get(value, expect) is None:
224 'network.get_param "%s" implies its base template'
225 ' output value function should be "%s" dict not "%s"'
226 % (network, expect, value)
228 network_role = param_dict["network_role"]
229 for rid, resource in internal_network.resources.items():
231 heat.nested_get(resource, "type") == "OS::Neutron::Net"
232 or heat.nested_get(resource, "type") == "OS::ContrailV2::VirtualNetwork"
234 match = RE_INTERNAL_NETWORK_RID.match(rid)
235 if match and match.groupdict()["network_role"] == network_role:
238 "OS::Neutron::Net with network-role"
239 ' "%s" not found in base template."' % network_role