[VVP] Removing unnecessary trademark lines
[vvp/validation-scripts.git] / ice_validator / tests / test_contrail_irt_routes.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 contrail interface route table routes
42
43 resources:
44 <resource name>:
45   type: OS::ContrailV2::InterfaceRouteTable
46   depends_on: [resource name of OS::ContrailV2::ServiceInstance]
47   properties:
48     name:
49       str_replace:
50         template: VNF_NAME_interface_route_table
51         params:
52           VNF_NAME: { get_param: vnf_name }
53     interface_route_table_routes:
54       interface_route_table_routes_route: { get_param: fw_oam_route_prefixes }
55     service_instance_refs:
56       - get_resource: <resource name of OS::ContrailV2::ServiceInstance>
57     service_instance_refs_data:
58       - service_instance_refs_data_interface_type:
59         { get_param: oam_interface_type }
60 """
61
62 import re
63
64 import pytest
65
66 from .structures import Heat
67 from .helpers import validates
68
69 VERSION = "1.1.0"
70
71 RE_ROUTE_ROUTE_PARAM = re.compile(
72     r"(?P<vm_type>.+)" r"_(?P<network_role>.+)" r"_route_prefixes" r"$"
73 )
74
75
76 def run_test(heat_template, validate):
77     """call validate for each routes route
78     """
79     heat = Heat(filepath=heat_template)
80     if not heat.resources:
81         pytest.skip("No resources found")
82
83     irts = heat.get_resource_by_type(
84         resource_type="OS::ContrailV2::InterfaceRouteTable"
85     )
86     if not irts:
87         pytest.skip("No Contrail InterfaceRouteTable found")
88
89     skip = True
90     bad = {}
91     for rid, resource in irts.items():
92         routes_route = heat.nested_get(
93             resource,
94             "properties",
95             "interface_route_table_routes",
96             "interface_route_table_routes_route",
97         )
98         if routes_route is None:
99             continue
100         error = validate(heat, routes_route)
101         if error:
102             bad[rid] = error
103             continue
104         skip = False
105     if bad:
106         raise AssertionError(
107             "Bad OS::ContrailV2::InterfaceRouteTable: %s"
108             % (", ".join("%s: %s" % (rid, error) for rid, error in bad.items()))
109         )
110     if skip:
111         pytest.skip("No Contrail routes_route found")
112
113
114 def validate_irt_route_param_format(heat, routes_route):
115     """ensure routes_route has proper format.
116     Returns error message string or None.
117     """
118     param = heat.nested_get(routes_route, "get_param")
119     if param is None:
120         return "missing routes_route get_param"
121     match = RE_ROUTE_ROUTE_PARAM.match(param)
122     if match is None:
123         return 'routes_route get_param "%s" must match "%s"' % (
124             param,
125             RE_ROUTE_ROUTE_PARAM.pattern,
126         )
127     return None
128
129
130 def validate_irt_route_param_type(heat, routes_route):
131     """ensure routes_route has proper type.
132     Returns error message string or None.
133     """
134     param = heat.nested_get(routes_route, "get_param")
135     if param is None:
136         return None
137     if heat.nested_get(heat.parameters, param, "type") != "json":
138         return (
139             'routes_route get_param "%s" '
140             'must have a parameter of type "json"' % param
141         )
142     return None
143
144
145 # pylint: disable=invalid-name
146
147
148 @validates("R-28222")
149 def test_contrail_irt_route_param_format(heat_template):
150     """
151     If a VNF's Heat Orchestration Template
152     ``OS::ContrailV2::InterfaceRouteTable`` resource
153     ``interface_route_table_routes`` property
154     ``interface_route_table_routes_route`` map property parameter name
155     **MUST** follow the format
156     """
157     run_test(heat_template, validate_irt_route_param_format)
158
159
160 @validates("R-19756")
161 def test_contrail_irt_route_param_type(heat_template):
162     """
163     * ``{vm-type}_{network-role}_route_prefixes``
164     **MUST** be defined as type ``json``.
165     """
166     run_test(heat_template, validate_irt_route_param_type)