bed3a5afdd44834c084e7158d2996326a0e4be49
[vvp/validation-scripts.git] / ice_validator / tests / utils / network_roles.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 import re
42 import socket
43
44
45 def get_network_role_from_port(resource):
46     '''
47     get the network role from a neutron port resource
48     '''
49     if not isinstance(resource, dict):
50         return None
51     if 'type' not in resource:
52         return None
53     if resource['type'] != 'OS::Neutron::Port':
54         return None
55     if 'properties' not in resource:
56         return None
57
58     formats = [
59               ["network", "string", "internal",
60                re.compile(r'int_(.+?)_net_id')],
61               ["network", "string", "internal",
62                re.compile(r'int_(.+?)_net_name')],
63               ["network", "string", "external",
64                re.compile(r'(.+?)_net_id')],
65               ["network", "string", "external",
66                re.compile(r'(.+?)_net_name')]]
67
68     for k1, v1 in resource["properties"].items():
69         if k1 != 'network':
70             continue
71
72         # get the network id or name
73         network = (
74             v1.get('get_param') or
75             v1.get('get_resource'))
76         if not network:
77             continue
78
79         for v2 in formats:
80             m = v2[3].match(network)
81             if m and m.group(1):
82                 return m.group(1)
83
84     return None
85
86
87 def get_network_roles(resources):
88     network_roles = []
89     for v in resources.values():
90         nr = get_network_role_from_port(v)
91         if nr:
92             network_roles.append(nr)
93
94     return set(network_roles)
95
96
97 def get_network_type_from_port(resource):
98     '''
99     get the network type from a neutron port resource
100     '''
101     if not isinstance(resource, dict):
102         return None
103     if 'type' not in resource:
104         return None
105     if resource['type'] != 'OS::Neutron::Port':
106         return None
107     if 'properties' not in resource:
108         return None
109
110     formats = [
111               ["network", "string", "internal",
112                re.compile(r'int_(.+?)_net_id')],
113               ["network", "string", "internal",
114                re.compile(r'int_(.+?)_net_name')],
115               ["network", "string", "external",
116                re.compile(r'(.+?)_net_id')],
117               ["network", "string", "external",
118                re.compile(r'(.+?)_net_name')]]
119
120     for k1, v1 in resource["properties"].items():
121         if k1 != 'network':
122             continue
123         if "get_param" not in v1:
124             continue
125         for v2 in formats:
126             m = v2[3].match(v1["get_param"])
127             if m and m.group(1):
128                 return v2[2]
129
130     return None
131
132
133 def is_valid_ip_address(ip_address, ip_type='ipv4'):
134     '''
135     check if an ip address is valid
136     '''
137     if ip_type == 'ipv4':
138         return is_valid_ipv4_address(ip_address)
139     elif ip_type == 'ipv6':
140         return is_valid_ipv6_address(ip_address)
141     return False
142
143
144 def is_valid_ipv4_address(ip_address):
145     '''
146     check if an ip address of the type ipv4
147     is valid
148     '''
149     try:
150         socket.inet_pton(socket.AF_INET, ip_address)
151     except AttributeError:
152         try:
153             socket.inet_aton(ip_address)
154         except (OSError, socket.error):
155             return False
156         return ip_address.count('.') == 3
157     except (OSError, socket.error):
158         return False
159     return True
160
161
162 def is_valid_ipv6_address(ip_address):
163     '''
164     check if an ip address of the type ipv6
165     is valid
166     '''
167     try:
168         socket.inet_pton(socket.AF_INET6, ip_address)
169     except (OSError, socket.error):
170         return False
171     return True
172
173
174 def property_uses_get_resource(resource, property_name):
175     '''
176     returns true if a port's network property
177     uses the get_resource function
178     '''
179     if not isinstance(resource, dict):
180         return False
181     if 'properties' not in resource:
182         return False
183     for k1, v1 in resource["properties"].items():
184         if k1 != property_name:
185             continue
186         if "get_resource" in v1:
187             return True
188     return False