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