[VVP] add bug fixes and reserve port updates
[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
69     for k1, v1 in resource["properties"].items():
70         if k1 != 'network':
71             continue
72
73         # get the network id or name
74         network = (
75             v1.get('get_param') or
76             v1.get('get_resource'))
77         if not network:
78             continue
79
80         for v2 in formats:
81             m = v2[3].match(network)
82             if m and m.group(1):
83                 return m.group(1)
84
85     return None
86
87
88 def get_network_type_from_port(resource):
89     '''
90     get the network type from a neutron port resource
91     '''
92     if not isinstance(resource, dict):
93         return None
94     if 'type' not in resource:
95         return None
96     if resource['type'] != 'OS::Neutron::Port':
97         return None
98     if 'properties' not in resource:
99         return None
100
101     formats = [
102               ["network", "string", "internal",
103                re.compile(r'int_(.+?)_net_id')],
104               ["network", "string", "internal",
105                re.compile(r'int_(.+?)_net_name')],
106               ["network", "string", "external",
107                re.compile(r'(.+?)_net_id')],
108               ["network", "string", "external",
109                re.compile(r'(.+?)_net_name')],
110               ]
111
112     for k1, v1 in resource["properties"].items():
113         if k1 != 'network':
114             continue
115         if "get_param" not in v1:
116             continue
117         for v2 in formats:
118             m = v2[3].match(v1["get_param"])
119             if m and m.group(1):
120                 return v2[2]
121
122     return None
123
124
125 def is_valid_ip_address(ip_address, ip_type='ipv4'):
126     '''
127     check if an ip address is valid
128     '''
129     if ip_type == 'ipv4':
130         return is_valid_ipv4_address(ip_address)
131     elif ip_type == 'ipv6':
132         return is_valid_ipv6_address(ip_address)
133     return False
134
135
136 def is_valid_ipv4_address(ip_address):
137     '''
138     check if an ip address of the type ipv4
139     is valid
140     '''
141     try:
142         socket.inet_pton(socket.AF_INET, ip_address)
143     except AttributeError:
144         try:
145             socket.inet_aton(ip_address)
146         except (OSError, socket.error):
147             return False
148         return ip_address.count('.') == 3
149     except (OSError, socket.error):
150         return False
151     return True
152
153
154 def is_valid_ipv6_address(ip_address):
155     '''
156     check if an ip address of the type ipv6
157     is valid
158     '''
159     try:
160         socket.inet_pton(socket.AF_INET6, ip_address)
161     except (OSError, socket.error):
162         return False
163     return True
164
165
166 def property_uses_get_resource(resource, property_name):
167     '''
168     returns true if a port's network property
169     uses the get_resource function
170     '''
171     if not isinstance(resource, dict):
172         return False
173     if 'properties' not in resource:
174         return False
175     for k1, v1 in resource["properties"].items():
176         if k1 != property_name:
177             continue
178         if "get_resource" in v1:
179             return True
180     return False