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